-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters