Skip to content

Commit

Permalink
some fixes in the server
Browse files Browse the repository at this point in the history
  • Loading branch information
voith committed Aug 9, 2018
1 parent 736000c commit 5c2b5de
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 4 deletions.
52 changes: 52 additions & 0 deletions eth_tester_rpc/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import random

import click


from .server import get_application
from .utils.compat_threading import (
make_server,
spawn,
sleep,
)


@click.command()
@click.option(
'--host',
'-h',
default='localhost',
)
@click.option(
'--port',
'-p',
default=8545,
type=int,
)
def runserver(host, port):
application = get_application()

print(application.rpc_methods.web3_clientVersion(None))

print("\nListening on %s:%s" % (host, port))

server = make_server(
host,
port,
application,
)

spawn(server.serve_forever)

try:
while True:
sleep(random.random())
except KeyboardInterrupt:
try:
server.stop()
except AttributeError:
server.shutdown()


if __name__ == "__main__":
runserver()
10 changes: 8 additions & 2 deletions eth_tester_rpc/rpc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import operator
import random
import sys
from functools import (
partial,
)

from eth_tester import (
EthereumTester,
Expand Down Expand Up @@ -33,7 +36,7 @@ def not_implemented(*args, **kwargs):


@curry
def call_eth_tester(fn_name, eth_tester, fn_args, fn_kwargs=None):
def call_eth_tester(fn_name, eth_tester, *fn_args, **fn_kwargs):
if fn_kwargs is None:
fn_kwargs = {}
return getattr(eth_tester, fn_name)(*fn_args, **fn_kwargs)
Expand Down Expand Up @@ -362,6 +365,9 @@ def __getattr__(self, item):
namespace, _, endpoint = item.partition('_')
try:
delegator = self.api_endpoints[namespace][endpoint]
return curry(delegator)(self.client)
try:
return lambda *args, **kwargs: delegator(self.client, *args, **kwargs)
except NotImplementedError:
return None
except KeyError:
return super().__getattribute__(item)
96 changes: 94 additions & 2 deletions eth_tester_rpc/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,97 @@
from jsonrpc import JSONRPCResponseManager, dispatcher
import json

from jsonrpc import (
JSONRPCResponseManager,
dispatcher,
)
from werkzeug.wrappers import (
Request,
Response,
)

from .rpc import RPCMethods
from .utils.compat_threading import threading


RESPONSE_HEADERS = {
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
"Access-Control-Allow-Origin": "*",
}


def get_application():
pass
# When calling server.register_function, first wrap the function in a mutex.
# This has the effect of serializing all RPC calls. Although we use a
# multithreaded HTTP server, the EVM itself is not thread-safe, so we must take
# care when interacting with it.
rpc_methods = RPCMethods()
evm_lock = threading.Lock()

def with_lock(rpc_fn):
# @functools.wraps(rpc_fn)
def inner(*args, **kwargs):
evm_lock.acquire()
try:
return rpc_fn(*args, **kwargs)
finally:
evm_lock.release()

return inner

def add_method_with_lock(rpc_fn, *args, **kwargs):
rpc_fn_with_lock = with_lock(rpc_fn)
return dispatcher.add_method(rpc_fn_with_lock, *args, **kwargs)

add_method_with_lock(rpc_methods.eth_coinbase, 'eth_coinbase')
add_method_with_lock(rpc_methods.eth_accounts, 'eth_accounts')
add_method_with_lock(rpc_methods.eth_gasPrice, 'eth_gasPrice')
add_method_with_lock(rpc_methods.eth_blockNumber, 'eth_blockNumber')
add_method_with_lock(rpc_methods.eth_estimateGas, 'eth_estimateGas')
add_method_with_lock(rpc_methods.eth_call, 'eth_call')
add_method_with_lock(rpc_methods.eth_sendTransaction, 'eth_sendTransaction')
add_method_with_lock(rpc_methods.eth_sendRawTransaction, 'eth_sendRawTransaction')
add_method_with_lock(rpc_methods.eth_getCompilers, 'eth_getCompilers')
add_method_with_lock(rpc_methods.eth_compileSolidity, 'eth_compileSolidity')
add_method_with_lock(rpc_methods.eth_getCode, 'eth_getCode')
add_method_with_lock(rpc_methods.eth_getBalance, 'eth_getBalance')
add_method_with_lock(rpc_methods.eth_getTransactionCount, 'eth_getTransactionCount')
add_method_with_lock(rpc_methods.eth_getTransactionByHash, 'eth_getTransactionByHash')
add_method_with_lock(rpc_methods.eth_getTransactionReceipt, 'eth_getTransactionReceipt')
add_method_with_lock(rpc_methods.eth_getBlockByHash, 'eth_getBlockByHash')
add_method_with_lock(rpc_methods.eth_getBlockByNumber, 'eth_getBlockByNumber')
add_method_with_lock(rpc_methods.eth_newBlockFilter, 'eth_newBlockFilter')
add_method_with_lock(rpc_methods.eth_newPendingTransactionFilter,
'eth_newPendingTransactionFilter')
add_method_with_lock(rpc_methods.eth_newFilter, 'eth_newFilter')
add_method_with_lock(rpc_methods.eth_getFilterChanges, 'eth_getFilterChanges')
add_method_with_lock(rpc_methods.eth_getFilterLogs, 'eth_getFilterLogs')
add_method_with_lock(rpc_methods.eth_uninstallFilter, 'eth_uninstallFilter')
add_method_with_lock(rpc_methods.eth_protocolVersion, 'eth_protocolVersion')
add_method_with_lock(rpc_methods.eth_syncing, 'eth_syncing')
add_method_with_lock(rpc_methods.eth_mining, 'eth_mining')
add_method_with_lock(rpc_methods.web3_sha3, 'web3_sha3')
add_method_with_lock(rpc_methods.web3_clientVersion, 'web3_clientVersion')
add_method_with_lock(rpc_methods.net_version, 'net_version')
add_method_with_lock(rpc_methods.net_listening, 'net_listening')
add_method_with_lock(rpc_methods.net_peerCount, 'net_peerCount')
add_method_with_lock(rpc_methods.evm_snapshot, 'evm_snapshot')
add_method_with_lock(rpc_methods.evm_revert, 'evm_revert')
add_method_with_lock(rpc_methods.evm_mine, 'evm_mine')
add_method_with_lock(rpc_methods.testing_timeTravel, 'testing_timeTravel')

@Request.application
def application(request):
response = JSONRPCResponseManager.handle(
request.data,
dispatcher,
)
response = Response(
json.dumps(response.data),
headers=RESPONSE_HEADERS,
mimetype='application/json',
)
return response

application.rpc_methods = rpc_methods

return application
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"twine",
"ipython",
],
'gevent': [
"gevent>=1.1.1,<1.2.0",
]
}

extras_require['dev'] = (
Expand All @@ -51,6 +54,8 @@
"cytoolz>=0.9.0,<1.0.0;implementation_name=='cpython'",
"eth-tester[py-evm]==0.1.0b30",
'json-rpc>=1.10.3',
'Werkzeug>=0.11.10',
'click>=6.6',
],
setup_requires=['setuptools-markdown'],
python_requires='>=3.5, <4',
Expand Down

0 comments on commit 5c2b5de

Please sign in to comment.