diff --git a/examples/json-rpc-api-server.py b/examples/json-rpc-api-server.py index b58ac8181..a248d9cf7 100644 --- a/examples/json-rpc-api-server.py +++ b/examples/json-rpc-api-server.py @@ -35,7 +35,6 @@ def main(): parser.add_argument("-p", "--privnet", action="store_true", default=False, help="Use PrivNet instead of the default TestNet") parser.add_argument("-c", "--config", action="store", help="Use a specific config file") - parser.add_argument("--port", action="store", help="Port to use", default=8000) parser.add_argument("-t", "--set-default-theme", dest="theme", choices=["dark", "light"], help="Set the default theme to be loaded from the config file. Default: 'dark'") @@ -73,13 +72,15 @@ def main(): ndb = NotificationDB.instance() ndb.start() - api_server = JsonRpcApi(args.port) + api_server = JsonRpcApi(settings.NODE_PORT) # Run reactor.suggestThreadPoolSize(15) NodeLeader.Instance().Start() - api_server.app.run('0.0.0.0', args.port) + host = "0.0.0.0" + print("Starting server on %s:%s" % (host, settings.NODE_PORT)) + api_server.app.run(host, settings.NODE_PORT) if __name__ == "__main__": diff --git a/neo/Settings.py b/neo/Settings.py index 587203900..bab174f58 100644 --- a/neo/Settings.py +++ b/neo/Settings.py @@ -10,6 +10,8 @@ import os import logging from json.decoder import JSONDecodeError + +from neo import __version__ from neocore.Cryptography import Helper import logzero @@ -81,6 +83,9 @@ def net_name(self): return 'TestNet' return 'PrivateNet' + def __init__(self): + self.VERSION_NAME = "/NEO-PYTHON:%s/" % __version__ + # Setup methods def setup(self, config_file): """ Load settings from a JSON config file """ @@ -106,7 +111,7 @@ def setup(self, config_file): self.NODE_PORT = int(config['NodePort']) self.WS_PORT = config['WsPort'] self.URI_PREFIX = config['UriPrefix'] - self.VERSION_NAME = config['VersionName'] + self.BOOTSTRAP_FILE = config['BootstrapFile'] Helper.ADDRESS_VERSION = self.ADDRESS_VERSION diff --git a/neo/api/JSONRPC/JsonRpcApi.py b/neo/api/JSONRPC/JsonRpcApi.py index 7c2fdbc44..39ad8533b 100644 --- a/neo/api/JSONRPC/JsonRpcApi.py +++ b/neo/api/JSONRPC/JsonRpcApi.py @@ -7,12 +7,14 @@ * http://www.jsonrpc.org/specification """ import json +import random from json.decoder import JSONDecodeError from klein import Klein from logzero import logger from neo import __version__ +from neo.Settings import settings from neo.Core.Blockchain import Blockchain from neo.api.utils import json_response from neo.Core.State.AccountState import AccountState @@ -65,10 +67,16 @@ def internalError(message=None): class JsonRpcApi(object): app = Klein() port = None + nonce = None def __init__(self, port): self.port = port + # `nonce` in neo-cli this comes from https://github.com/neo-project/neo/blob/46c635adda6cf61090a67a3bef0f3bb1bc85dd81/neo/Network/LocalNode.cs#L68 + # In neo-python the closest thing to it is https://github.com/CityOfZion/neo-python/blob/master/neo/Network/NeoNode.py#L34 + # since we don't get direct access to this NeoNode, we generate a new nonce here + self.nonce = random.randint(1294967200, 4294967200) + # # JSON-RPC API Route # @@ -203,8 +211,8 @@ def json_rpc_method_handler(self, method, params): elif method == "getversion": return { "port": self.port, - "nonce": 771199013, # TODO: make this the real nonce, currently taken straight from testnet neo-cli - "useragent": "/neo-python:%s/" % __version__ # This ok, or should we mimic `/NEO:2.6.0/`? + "nonce": self.nonce, + "useragent": settings.VERSION_NAME } elif method == "getrawtransaction": diff --git a/neo/api/JSONRPC/neo-cli-json-rpc-docs.md b/neo/api/JSONRPC/neo-cli-json-rpc-docs.md index 2b874a276..6fe913764 100644 --- a/neo/api/JSONRPC/neo-cli-json-rpc-docs.md +++ b/neo/api/JSONRPC/neo-cli-json-rpc-docs.md @@ -26,6 +26,8 @@ Documentation for the JSON-RPC implementation curl -X POST http://seed2.neo.org:20332 -H 'Content-Type: application/json' -d '{ "jsonrpc": "2.0", "id": 5, "method": "getrawmempool", "params": [] }' { "jsonrpc": "2.0", "id": 5, "result": [] } +On MainNet there are actually entries, each of which has this format: `0xde3bc1dead8a89b06787db663b59c4f33efe0afe6b97b1c9c997f2695d7ae0da` + ## `getversion` curl -X POST http://seed2.neo.org:20332 -H 'Content-Type: application/json' -d '{ "jsonrpc": "2.0", "id": 5, "method": "getversion", "params": [] }' diff --git a/neo/api/JSONRPC/test_json_rpc_api.py b/neo/api/JSONRPC/test_json_rpc_api.py index 504e0b4b8..46cfdfe01 100644 --- a/neo/api/JSONRPC/test_json_rpc_api.py +++ b/neo/api/JSONRPC/test_json_rpc_api.py @@ -224,4 +224,4 @@ def test_get_version(self): mock_req = mock_request(json.dumps(req).encode("utf-8")) res = json.loads(self.app.home(mock_req)) self.assertEqual(res["result"]["port"], 20333) - self.assertEqual(res["result"]["useragent"], "/neo-python:%s/" % __version__) + self.assertEqual(res["result"]["useragent"], "/NEO-PYTHON:%s/" % __version__) diff --git a/neo/api/REST/NotificationRestApi.py b/neo/api/REST/NotificationRestApi.py index ff5a69561..09d765d36 100644 --- a/neo/api/REST/NotificationRestApi.py +++ b/neo/api/REST/NotificationRestApi.py @@ -1,6 +1,4 @@ """ - - The REST API is using the Python package 'klein', which makes it possible to create HTTP routes and handlers with Twisted in a similar style to Flask: https://github.com/twisted/klein diff --git a/protocol.coz.json b/protocol.coz.json index 27bdd099a..2db088be6 100644 --- a/protocol.coz.json +++ b/protocol.coz.json @@ -36,8 +36,7 @@ "UriPrefix": [ "http://*:20332"], "SslCert": "", "SslCertPassword": "", - "VersionName":"/NEO-PYTHON:2.3.4/", "BootstrapFile":"", "DebugStorage":1 } -} \ No newline at end of file +} diff --git a/protocol.mainnet.json b/protocol.mainnet.json index e11d44502..78eead417 100644 --- a/protocol.mainnet.json +++ b/protocol.mainnet.json @@ -45,8 +45,7 @@ "UriPrefix": [ "http://*:10332" ], "SslCert": "", "SslCertPassword": "", - "VersionName":"/NEO-PYTHON:2.3.4/", "BootstrapFile":"https://s3.us-east-2.amazonaws.com/cityofzion/bootstrap_main/Chain1550000.tar.gz", "DebugStorage":1 } -} \ No newline at end of file +} diff --git a/protocol.privnet.json b/protocol.privnet.json index 7f38f28ba..136adddb5 100644 --- a/protocol.privnet.json +++ b/protocol.privnet.json @@ -33,8 +33,7 @@ "UriPrefix": [ "http://*:20332" ], "SslCert": "", "SslCertPassword": "", - "VersionName":"/NEO-PYTHON:2.3.4/", "BootstrapFile":"", "DebugStorage":1 } -} \ No newline at end of file +} diff --git a/protocol.testnet.json b/protocol.testnet.json index 073025407..7a2ae4df8 100644 --- a/protocol.testnet.json +++ b/protocol.testnet.json @@ -8,7 +8,6 @@ "UriPrefix": [ "http://*:20332" ], - "VersionName": "/NEO-PYTHON:2.3.4/", "WsPort": 20334, "BootstrapFile":"https://s3.us-east-2.amazonaws.com/cityofzion/bootstrap_testnet/Chain780000.tar.gz", "DebugStorage":1 @@ -51,4 +50,4 @@ "RegisterTransaction": 100 } } -} \ No newline at end of file +}