From 5acd4ec4662bf4ab329feaa0cbf6d10c41caa9d0 Mon Sep 17 00:00:00 2001 From: Jeffrey Ryan Date: Fri, 22 Jan 2021 08:53:59 -0600 Subject: [PATCH] Improved test coverage for daemon (#86) Added test coverage for: 1. `headers` method 2. failing an RPC request 3. fetching a single transaction --- .../test_headers_2279790_2279799.json | 241 ++++++++++++++++++ .../test_invalid_param.json | 8 + .../test_transactions_single.json | 30 +++ tests/test_jsonrpcdaemon.py | 47 +++- 4 files changed, 321 insertions(+), 5 deletions(-) create mode 100644 tests/data/test_jsonrpcdaemon/test_headers_2279790_2279799.json create mode 100644 tests/data/test_jsonrpcdaemon/test_invalid_param.json create mode 100644 tests/data/test_jsonrpcdaemon/test_transactions_single.json diff --git a/tests/data/test_jsonrpcdaemon/test_headers_2279790_2279799.json b/tests/data/test_jsonrpcdaemon/test_headers_2279790_2279799.json new file mode 100644 index 0000000..23f1cb6 --- /dev/null +++ b/tests/data/test_jsonrpcdaemon/test_headers_2279790_2279799.json @@ -0,0 +1,241 @@ +{ + "id": "0", + "jsonrpc": "2.0", + "result": { + "credits": 0, + "headers": [{ + "block_size": 33421, + "block_weight": 33421, + "cumulative_difficulty": 84681468973096560, + "cumulative_difficulty_top64": 0, + "depth": 10, + "difficulty": 234430745816, + "difficulty_top64": 0, + "hash": "2763e0b9738c46317602a8e338b6b3ece893be4b9e1c4586824beb4f33286992", + "height": 2279790, + "long_term_weight": 33421, + "major_version": 14, + "miner_tx_hash": "9fbe9a7b712f855b593e1c4a92efd9c10f7e5f8147c947b1522e9a44752549a4", + "minor_version": 14, + "nonce": 15323, + "num_txes": 20, + "orphan_status": false, + "pow_hash": "", + "prev_hash": "22e6d498227fb2079fde1c70914ce3d17d4891ebd88ee50c04a1500c230b2b51", + "reward": 1219903401446, + "timestamp": 1611283959, + "wide_cumulative_difficulty": "0x12cd958ed331e70", + "wide_difficulty": "0x3695294cd8" + },{ + "block_size": 105, + "block_weight": 105, + "cumulative_difficulty": 84681703916115595, + "cumulative_difficulty_top64": 0, + "depth": 9, + "difficulty": 234943019035, + "difficulty_top64": 0, + "hash": "e135ecd3331b8ad2875faceeab7c97013b9235e26689e3c6dff35d65dd053dc1", + "height": 2279791, + "long_term_weight": 105, + "major_version": 14, + "miner_tx_hash": "d5d4f698eaf15c0b0df519bae66bcf73aa07678397059f337952204dd84ea900", + "minor_version": 14, + "nonce": 285520600, + "num_txes": 0, + "orphan_status": false, + "pow_hash": "", + "prev_hash": "2763e0b9738c46317602a8e338b6b3ece893be4b9e1c4586824beb4f33286992", + "reward": 1196393814502, + "timestamp": 1611283970, + "wide_cumulative_difficulty": "0x12cd98fa0e5168b", + "wide_difficulty": "0x36b3b1f81b" + },{ + "block_size": 74609, + "block_weight": 74609, + "cumulative_difficulty": 84681938452792099, + "cumulative_difficulty_top64": 0, + "depth": 8, + "difficulty": 234536676504, + "difficulty_top64": 0, + "hash": "646f63987fee9d81eb62c80e6564b78c9fba2c906448a060a69e2b466228e6ae", + "height": 2279792, + "long_term_weight": 74609, + "major_version": 14, + "miner_tx_hash": "b11eb2986cb54ef9563c2e0f2c28c9eea48842524640dbefda8d4c82d6f7d020", + "minor_version": 14, + "nonce": 2583935886, + "num_txes": 23, + "orphan_status": false, + "pow_hash": "", + "prev_hash": "e135ecd3331b8ad2875faceeab7c97013b9235e26689e3c6dff35d65dd053dc1", + "reward": 1225724442562, + "timestamp": 1611284205, + "wide_cumulative_difficulty": "0x12cd9c63c5ec323", + "wide_difficulty": "0x369b79ac98" + },{ + "block_size": 191380, + "block_weight": 191380, + "cumulative_difficulty": 84682170265376164, + "cumulative_difficulty_top64": 0, + "depth": 7, + "difficulty": 231812584065, + "difficulty_top64": 0, + "hash": "f2fd436cd1f5a62164bf72da7ba6ebca6b9af87f3e7a6d24abc3ab3e459fc90e", + "height": 2279793, + "long_term_weight": 191380, + "major_version": 14, + "miner_tx_hash": "b48060bfe3ddc06d2a4dac6ebe536cb75cbd11c1596a49c4c2badbdad779b679", + "minor_version": 14, + "nonce": 297405, + "num_txes": 91, + "orphan_status": false, + "pow_hash": "", + "prev_hash": "646f63987fee9d81eb62c80e6564b78c9fba2c906448a060a69e2b466228e6ae", + "reward": 1199549085626, + "timestamp": 1611284966, + "wide_cumulative_difficulty": "0x12cd9fc357a15a4", + "wide_difficulty": "0x35f91b5281" + },{ + "block_size": 58213, + "block_weight": 58213, + "cumulative_difficulty": 84682401988957477, + "cumulative_difficulty_top64": 0, + "depth": 6, + "difficulty": 231723581313, + "difficulty_top64": 0, + "hash": "fa6dbbbb982bd8e2c23b03c10a6d847c335ac975695328195a3cd51ed23da640", + "height": 2279794, + "long_term_weight": 58213, + "major_version": 14, + "miner_tx_hash": "35a195a3839f57ba67abe4f0b7f829b28e216c6e0152b3ba7beb673be41d170a", + "minor_version": 14, + "nonce": 1023444063, + "num_txes": 25, + "orphan_status": false, + "pow_hash": "", + "prev_hash": "f2fd436cd1f5a62164bf72da7ba6ebca6b9af87f3e7a6d24abc3ab3e459fc90e", + "reward": 1208514188695, + "timestamp": 1611285210, + "wide_cumulative_difficulty": "0x12cda3229475525", + "wide_difficulty": "0x35f3cd3f81" + },{ + "block_size": 12335, + "block_weight": 12335, + "cumulative_difficulty": 84682633140307783, + "cumulative_difficulty_top64": 0, + "depth": 5, + "difficulty": 231151350306, + "difficulty_top64": 0, + "hash": "3f80644b51d26b5110141164903e169c53f10f2f5478d803f5ffe36ce72b1cf6", + "height": 2279795, + "long_term_weight": 12335, + "major_version": 14, + "miner_tx_hash": "b236b10e716b4780b87b472e118c7d40c9b055da9c1114dc26f8956b03a162b1", + "minor_version": 14, + "nonce": 499213, + "num_txes": 7, + "orphan_status": false, + "pow_hash": "", + "prev_hash": "fa6dbbbb982bd8e2c23b03c10a6d847c335ac975695328195a3cd51ed23da640", + "reward": 1196531576767, + "timestamp": 1611285252, + "wide_cumulative_difficulty": "0x12cda67faf90747", + "wide_difficulty": "0x35d1b1b222" + },{ + "block_size": 115, + "block_weight": 115, + "cumulative_difficulty": 84682864159142056, + "cumulative_difficulty_top64": 0, + "depth": 4, + "difficulty": 231018834273, + "difficulty_top64": 0, + "hash": "7ce4457b05f65ccbdbf68d88930342e62553b71ecaca19f9312c1612fbbd8e41", + "height": 2279796, + "long_term_weight": 115, + "major_version": 14, + "miner_tx_hash": "00cd208e217cca5c074d1d9bcc511bf832cc96c8ddd397f729466e7461f5257f", + "minor_version": 14, + "nonce": 1692169, + "num_txes": 0, + "orphan_status": false, + "pow_hash": "", + "prev_hash": "3f80644b51d26b5110141164903e169c53f10f2f5478d803f5ffe36ce72b1cf6", + "reward": 1196382404845, + "timestamp": 1611285271, + "wide_cumulative_difficulty": "0x12cda9dc4c4b0a8", + "wide_difficulty": "0x35c9cba961" + },{ + "block_size": 34110, + "block_weight": 34110, + "cumulative_difficulty": 84683095224981016, + "cumulative_difficulty_top64": 0, + "depth": 3, + "difficulty": 231065838960, + "difficulty_top64": 0, + "hash": "fbcb95fdc7ce7a5be5be77c7a8527902d457d9c6afe4da07528efc1dab650caf", + "height": 2279797, + "long_term_weight": 34110, + "major_version": 14, + "miner_tx_hash": "bd0d6cf4239c5bd83870795cf199d6b193a614f014071a25574a2d87f2515443", + "minor_version": 14, + "nonce": 3355626117, + "num_txes": 19, + "orphan_status": false, + "pow_hash": "", + "prev_hash": "7ce4457b05f65ccbdbf68d88930342e62553b71ecaca19f9312c1612fbbd8e41", + "reward": 1196670892926, + "timestamp": 1611285471, + "wide_cumulative_difficulty": "0x12cdad3915d9618", + "wide_difficulty": "0x35cc98e570" + },{ + "block_size": 83871, + "block_weight": 83871, + "cumulative_difficulty": 84683326411541440, + "cumulative_difficulty_top64": 0, + "depth": 2, + "difficulty": 231186560424, + "difficulty_top64": 0, + "hash": "79011c2482ba222ca4814225c585aa47e3b93dcc9cca4e171b04271ebd5fba10", + "height": 2279798, + "long_term_weight": 83871, + "major_version": 14, + "miner_tx_hash": "eda1033a191925b63a25e99cbbf9935af8ee8387ab52781a7677760879f2d05f", + "minor_version": 14, + "nonce": 1593861047, + "num_txes": 4, + "orphan_status": false, + "pow_hash": "", + "prev_hash": "fbcb95fdc7ce7a5be5be77c7a8527902d457d9c6afe4da07528efc1dab650caf", + "reward": 1197420201012, + "timestamp": 1611285551, + "wide_cumulative_difficulty": "0x12cdb0965288bc0", + "wide_difficulty": "0x35d3caf5a8" + },{ + "block_size": 62965, + "block_weight": 62965, + "cumulative_difficulty": 84683557580329998, + "cumulative_difficulty_top64": 0, + "depth": 1, + "difficulty": 231168788558, + "difficulty_top64": 0, + "hash": "d0472efc50979830e901fd7cd9a0fa17106f999a14695c65540bb364275eb845", + "height": 2279799, + "long_term_weight": 62965, + "major_version": 14, + "miner_tx_hash": "d337fa952d8733ab29081ba67b62c4de00425b390c490b0f4642847388cdeac0", + "minor_version": 14, + "nonce": 275623, + "num_txes": 9, + "orphan_status": false, + "pow_hash": "", + "prev_hash": "79011c2482ba222ca4814225c585aa47e3b93dcc9cca4e171b04271ebd5fba10", + "reward": 1197252819103, + "timestamp": 1611285609, + "wide_cumulative_difficulty": "0x12cdb3f37e4540e", + "wide_difficulty": "0x35d2bbc84e" + }], + "status": "OK", + "top_hash": "", + "untrusted": false + } +} \ No newline at end of file diff --git a/tests/data/test_jsonrpcdaemon/test_invalid_param.json b/tests/data/test_jsonrpcdaemon/test_invalid_param.json new file mode 100644 index 0000000..0396084 --- /dev/null +++ b/tests/data/test_jsonrpcdaemon/test_invalid_param.json @@ -0,0 +1,8 @@ +{ + "error": { + "code": -32602, + "message": "Invalid params" + }, + "id": "0", + "jsonrpc": "2.0" +} \ No newline at end of file diff --git a/tests/data/test_jsonrpcdaemon/test_transactions_single.json b/tests/data/test_jsonrpcdaemon/test_transactions_single.json new file mode 100644 index 0000000..0d09276 --- /dev/null +++ b/tests/data/test_jsonrpcdaemon/test_transactions_single.json @@ -0,0 +1,30 @@ +{ + "credits": 0, + "status": "OK", + "top_hash": "", + "txs": [ + { + "as_hex": "", + "as_json": "{\n \"version\": 2, \n \"unlock_time\": 0, \n \"vin\": [ {\n \"key\": {\n \"amount\": 0, \n \"key_offsets\": [ 21232548, 2296164, 2442168, 193665, 6538, 3230, 191, 12317, 647, 1020, 274\n ], \n \"k_image\": \"f1c7bd0270a937f67b9b06bdf6707d13c75c0a2563e67c48eb804adc04b8d660\"\n }\n }, {\n \"key\": {\n \"amount\": 0, \n \"key_offsets\": [ 13137123, 11881714, 643349, 421652, 7427, 63245, 13648, 15277, 3469, 433, 1602\n ], \n \"k_image\": \"d6cd646f7dcb6dcdb7f4828227e7598fb106750c01e4c778a458b3e18ac632db\"\n }\n }\n ], \n \"vout\": [ {\n \"amount\": 0, \n \"target\": {\n \"key\": \"c4a1a768bbc3434bece66a3ec7d258fe21dea9a3b5ecd22efae77d1bc9d7d1fd\"\n }\n }, {\n \"amount\": 0, \n \"target\": {\n \"key\": \"96ca9e5b118639704881f99fa7a69e30e5c46b457386ae1db0ac1ea0872fa393\"\n }\n }\n ], \n \"extra\": [ 1, 251, 255, 38, 23, 61, 92, 77, 155, 139, 43, 198, 150, 232, 179, 116, 24, 65, 55, 74, 97, 144, 85, 197, 69, 59, 12, 71, 138, 232, 120, 230, 116, 2, 9, 1, 136, 122, 195, 120, 69, 251, 156, 30\n ], \n \"rct_signatures\": {\n \"type\": 5, \n \"txnFee\": 78370000, \n \"ecdhInfo\": [ {\n \"amount\": \"15279cd867dcd9ac\"\n }, {\n \"amount\": \"9ea455993afc7ed7\"\n }], \n \"outPk\": [ \"30e2fd204997ef76294bca451f8f061cc3cbf0432aca6b9e3e71c567feb4a698\", \"79af72a1d671bfb5d343f7131f1c5796746441da04c9aab2fb1716fb2bc640bc\"]\n }\n}", + "block_height": 2279770, + "block_timestamp": 1611281602, + "double_spend_seen": false, + "in_pool": false, + "output_indices": [ + 26189357, + 26189358 + ], + "prunable_as_hex": "", + "prunable_hash": "e4871fbdf5f5eb65095f8c41ed8573007fa81b8291841561a031c9beaea68f00", + "pruned_as_hex": "02000202000ba4f78f0ae4928c01b887950181e90b8a339e19bf019d608705fc079202f1c7bd0270a937f67b9b06bdf6707d13c75c0a2563e67c48eb804adc04b8d66002000be3e9a106f299d50595a22794de19833a8dee03d06aad778d1bb103c20cd6cd646f7dcb6dcdb7f4828227e7598fb106750c01e4c778a458b3e18ac632db020002c4a1a768bbc3434bece66a3ec7d258fe21dea9a3b5ecd22efae77d1bc9d7d1fd000296ca9e5b118639704881f99fa7a69e30e5c46b457386ae1db0ac1ea0872fa3932c01fbff26173d5c4d9b8b2bc696e8b3741841374a619055c5453b0c478ae878e674020901887ac37845fb9c1e05d0a9af2515279cd867dcd9ac9ea455993afc7ed730e2fd204997ef76294bca451f8f061cc3cbf0432aca6b9e3e71c567feb4a69879af72a1d671bfb5d343f7131f1c5796746441da04c9aab2fb1716fb2bc640bc", + "tx_hash": "bbc10f5944cc3e88be576d2ab9f4f5ab5a2b46d95a7cab1027bc15c17393102c" + } + ], + "txs_as_hex": [ + "" + ], + "txs_as_json": [ + "{\n \"version\": 2, \n \"unlock_time\": 0, \n \"vin\": [ {\n \"key\": {\n \"amount\": 0, \n \"key_offsets\": [ 21232548, 2296164, 2442168, 193665, 6538, 3230, 191, 12317, 647, 1020, 274\n ], \n \"k_image\": \"f1c7bd0270a937f67b9b06bdf6707d13c75c0a2563e67c48eb804adc04b8d660\"\n }\n }, {\n \"key\": {\n \"amount\": 0, \n \"key_offsets\": [ 13137123, 11881714, 643349, 421652, 7427, 63245, 13648, 15277, 3469, 433, 1602\n ], \n \"k_image\": \"d6cd646f7dcb6dcdb7f4828227e7598fb106750c01e4c778a458b3e18ac632db\"\n }\n }\n ], \n \"vout\": [ {\n \"amount\": 0, \n \"target\": {\n \"key\": \"c4a1a768bbc3434bece66a3ec7d258fe21dea9a3b5ecd22efae77d1bc9d7d1fd\"\n }\n }, {\n \"amount\": 0, \n \"target\": {\n \"key\": \"96ca9e5b118639704881f99fa7a69e30e5c46b457386ae1db0ac1ea0872fa393\"\n }\n }\n ], \n \"extra\": [ 1, 251, 255, 38, 23, 61, 92, 77, 155, 139, 43, 198, 150, 232, 179, 116, 24, 65, 55, 74, 97, 144, 85, 197, 69, 59, 12, 71, 138, 232, 120, 230, 116, 2, 9, 1, 136, 122, 195, 120, 69, 251, 156, 30\n ], \n \"rct_signatures\": {\n \"type\": 5, \n \"txnFee\": 78370000, \n \"ecdhInfo\": [ {\n \"amount\": \"15279cd867dcd9ac\"\n }, {\n \"amount\": \"9ea455993afc7ed7\"\n }], \n \"outPk\": [ \"30e2fd204997ef76294bca451f8f061cc3cbf0432aca6b9e3e71c567feb4a698\", \"79af72a1d671bfb5d343f7131f1c5796746441da04c9aab2fb1716fb2bc640bc\"]\n }\n}" + ], + "untrusted": false +} \ No newline at end of file diff --git a/tests/test_jsonrpcdaemon.py b/tests/test_jsonrpcdaemon.py index c7b1aa3..80d2e5b 100644 --- a/tests/test_jsonrpcdaemon.py +++ b/tests/test_jsonrpcdaemon.py @@ -1,10 +1,11 @@ import decimal +import logging import os import responses from monero.const import NET_STAGE from monero.daemon import Daemon -from monero.backends.jsonrpc import JSONRPCDaemon +from monero.backends.jsonrpc import JSONRPCDaemon, RPCError from monero.transaction import Transaction from .base import JSONTestCase @@ -19,6 +20,9 @@ class JSONRPCDaemonTestCase(JSONTestCase): def setUp(self): self.daemon = Daemon(JSONRPCDaemon()) + # this is disabled b/c raw_request logs errors + logging.getLogger('monero.backends.jsonrpc.daemon').disabled = True + @responses.activate def test_basic_info(self): responses.add(responses.POST, self.jsonrpc_url, @@ -84,6 +88,10 @@ class JSONRPCDaemonTestCase(JSONTestCase): # wrong arg type self.assertRaises(ValueError, lambda txid: txid in blk, 1245) + # block or hash not specified + with self.assertRaises(ValueError): + self.daemon.block() + @responses.activate def test_transactions(self): responses.add(responses.POST, self.transactions_url, @@ -118,6 +126,16 @@ class JSONRPCDaemonTestCase(JSONTestCase): self.assertEqual(txs[3].size, 2724) self.assertEqual(txs[3].fee, decimal.Decimal('0.000320650000')) + @responses.activate + def test_transactions_single(self): + responses.add(responses.POST, self.transactions_url, + json=self._read('test_transactions_single.json'), + status=200) + + tx = self.daemon.transactions('bbc10f5944cc3e88be576d2ab9f4f5ab5a2b46d95a7cab1027bc15c17393102c')[0] + + self.assertEqual(tx.height, 2279770) + @responses.activate def test_send_transaction(self): path = os.path.join( @@ -128,10 +146,11 @@ class JSONRPCDaemonTestCase(JSONTestCase): responses.add(responses.POST, self.sendrawtransaction_url, json=self._read('test_send_transaction.json'), status=200) - tx = Transaction( - blob=open(path, "rb").read()) - rsp = self.daemon.send_transaction(tx) - self.assertEqual(rsp["status"], "OK") + + with open(path, "rb") as blob_file: + tx = Transaction(blob=blob_file.read()) + rsp = self.daemon.send_transaction(tx) + self.assertEqual(rsp["status"], "OK") @responses.activate def test_chunking(self): @@ -148,6 +167,24 @@ class JSONRPCDaemonTestCase(JSONTestCase): self.assertEqual(len(blk.transactions), 105) self.assertEqual(len(set(blk.transactions)), 105) + @responses.activate + def test_headers(self): + responses.add(responses.POST, self.jsonrpc_url, + json=self._read('test_headers_2279790_2279799.json'), + status=200) + headers = self.daemon.headers(2279790, 2279799) + self.assertEqual(len(headers), 10) + self.assertEqual(headers[0]['hash'], '2763e0b9738c46317602a8e338b6b3ece893be4b9e1c4586824beb4f33286992') + self.assertEqual(headers[9]['nonce'], 275623) + + @responses.activate + def test_invalid_param(self): + responses.add(responses.POST, self.jsonrpc_url, + json=self._read('test_invalid_param.json'), + status=200) + with self.assertRaises(RPCError): + blk = self.daemon.block(height=-1) + def test_init_default_backend(self): daemon1 = Daemon(host='localhost') daemon2 = Daemon()