From 07053c729f5e6b27f3b029bd343ff80902e983fe Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Mon, 14 Jan 2019 10:34:09 +0100 Subject: [PATCH] Add gettransactionheight RPC method (#813) * Add undocumented gettransactionheight RPC method * add changelog --- CHANGELOG.rst | 1 + neo/api/JSONRPC/JsonRpcApi.py | 13 +++++++++++++ neo/api/JSONRPC/test_json_rpc_api.py | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 001b4dc96..0cca7fbcc 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -32,6 +32,7 @@ All notable changes to this project are documented in this file. - Add Reset logic to NodeLeader such that Blockchain fixture testcases reset properly `#809 `_ - Resolve peewee DeprecationWarning `#810 `_ - Add VM SafeReadBytes `#812 `_ +- Add gettransactionheight RPC method `#813 `_ - Update ``ApplicationEngine`` methods ``CheckStackSize`` and ``GetPrice`` `#814 `_ diff --git a/neo/api/JSONRPC/JsonRpcApi.py b/neo/api/JSONRPC/JsonRpcApi.py index f4a3c012d..ea3cb74f0 100644 --- a/neo/api/JSONRPC/JsonRpcApi.py +++ b/neo/api/JSONRPC/JsonRpcApi.py @@ -271,6 +271,19 @@ def json_rpc_method_handler(self, method, params): return storage_item.Value.hex() return None + elif method == "gettransactionheight": + try: + hash = UInt256.ParseString(params[0]) + except Exception: + # throws exception, not anything more specific + raise JsonRpcError(-100, "Unknown transaction") + + tx, height = Blockchain.Default().GetTransaction(hash) + if tx: + return height + else: + raise JsonRpcError(-100, "Unknown transaction") + elif method == "gettxout": hash = params[0].encode('utf-8') index = params[1] diff --git a/neo/api/JSONRPC/test_json_rpc_api.py b/neo/api/JSONRPC/test_json_rpc_api.py index df32f4b43..b12c79c18 100644 --- a/neo/api/JSONRPC/test_json_rpc_api.py +++ b/neo/api/JSONRPC/test_json_rpc_api.py @@ -1692,3 +1692,26 @@ def test_getblockheader_non_verbose(self): blockheader = Helper.AsSerializableWithType(output, 'neo.Core.Header.Header') self.assertEqual(blockheader.Index, 11) self.assertEqual(str(blockheader.Hash), GetBlockchain().GetBlockHash(11).decode('utf8')) + + def test_gettransactionheight(self): + txid = 'f999c36145a41306c846ea80290416143e8e856559818065be3f4e143c60e43a' + req = self._gen_post_rpc_req("gettransactionheight", params=[txid]) + mock_req = mock_post_request(json.dumps(req).encode("utf-8")) + res = json.loads(self.app.home(mock_req)) + self.assertEqual(9448, res['result']) + + def test_gettransactionheight_invalid_hash(self): + txid = 'invalid_tx_id' + req = self._gen_post_rpc_req("gettransactionheight", params=[txid]) + mock_req = mock_post_request(json.dumps(req).encode("utf-8")) + res = json.loads(self.app.home(mock_req)) + self.assertTrue('error' in res) + self.assertEqual(res['error']['message'], 'Unknown transaction') + + def test_gettransactionheight_invalid_hash2(self): + txid = 'a' * 64 # something the right length but unknown + req = self._gen_post_rpc_req("gettransactionheight", params=[txid]) + mock_req = mock_post_request(json.dumps(req).encode("utf-8")) + res = json.loads(self.app.home(mock_req)) + self.assertTrue('error' in res) + self.assertEqual(res['error']['message'], 'Unknown transaction')