From 681b063cf7e9e64da30bacc5fafca1145601085a Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Tue, 4 Dec 2018 04:57:34 -0800 Subject: [PATCH 01/16] Update prompt.py - add CommandShow and CommandSearch to prompt --- neo/bin/prompt.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/neo/bin/prompt.py b/neo/bin/prompt.py index a955e0c83..e8d0290a9 100755 --- a/neo/bin/prompt.py +++ b/neo/bin/prompt.py @@ -16,6 +16,8 @@ from neo.Implementations.Notifications.LevelDB.NotificationDB import NotificationDB from neo.Network.NodeLeader import NodeLeader from neo.Prompt.Commands.Wallet import CommandWallet +from neo.Prompt.Commands.Show import CommandShow +from neo.Prompt.Commands.Search import CommandSearch from neo.Prompt.PromptData import PromptData from neo.Prompt.InputParser import InputParser from neo.Settings import settings, PrivnetConnectionError @@ -70,7 +72,7 @@ class PromptInterface: _known_things = [] _commands = [ - CommandWallet(), + CommandWallet(), CommandShow(), CommandSearch() ] _command_descs = [desc for c in _commands for desc in c.command_descs_with_sub_commands()] From 5b8c2268ea3746b5c69d08cbdfdc23bb3b07ab74 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Tue, 4 Dec 2018 04:58:26 -0800 Subject: [PATCH 02/16] Update prompt.py revert changes --- neo/bin/prompt.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/neo/bin/prompt.py b/neo/bin/prompt.py index e8d0290a9..a955e0c83 100755 --- a/neo/bin/prompt.py +++ b/neo/bin/prompt.py @@ -16,8 +16,6 @@ from neo.Implementations.Notifications.LevelDB.NotificationDB import NotificationDB from neo.Network.NodeLeader import NodeLeader from neo.Prompt.Commands.Wallet import CommandWallet -from neo.Prompt.Commands.Show import CommandShow -from neo.Prompt.Commands.Search import CommandSearch from neo.Prompt.PromptData import PromptData from neo.Prompt.InputParser import InputParser from neo.Settings import settings, PrivnetConnectionError @@ -72,7 +70,7 @@ class PromptInterface: _known_things = [] _commands = [ - CommandWallet(), CommandShow(), CommandSearch() + CommandWallet(), ] _command_descs = [desc for c in _commands for desc in c.command_descs_with_sub_commands()] From 6b31e9e812e92d05b5484e96ab1d5ffd1c7e9831 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Wed, 5 Dec 2018 06:51:25 -0800 Subject: [PATCH 03/16] Update prompt.py - Add CommandShow --- neo/bin/prompt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neo/bin/prompt.py b/neo/bin/prompt.py index a955e0c83..e298edf28 100755 --- a/neo/bin/prompt.py +++ b/neo/bin/prompt.py @@ -16,6 +16,7 @@ from neo.Implementations.Notifications.LevelDB.NotificationDB import NotificationDB from neo.Network.NodeLeader import NodeLeader from neo.Prompt.Commands.Wallet import CommandWallet +from neo.Prompt.Commands.Show import CommandShow from neo.Prompt.PromptData import PromptData from neo.Prompt.InputParser import InputParser from neo.Settings import settings, PrivnetConnectionError @@ -70,7 +71,7 @@ class PromptInterface: _known_things = [] _commands = [ - CommandWallet(), + CommandWallet(), CommandShow() ] _command_descs = [desc for c in _commands for desc in c.command_descs_with_sub_commands()] From 2c2fce76c1ad3e6cb02bda5f30f852871a75bd91 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Wed, 5 Dec 2018 06:56:04 -0800 Subject: [PATCH 04/16] Create Show.py Adds Command Show and supports `show block` through `show nodes` --- neo/Prompt/Commands/Show.py | 167 ++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 neo/Prompt/Commands/Show.py diff --git a/neo/Prompt/Commands/Show.py b/neo/Prompt/Commands/Show.py new file mode 100644 index 000000000..9aee8ce20 --- /dev/null +++ b/neo/Prompt/Commands/Show.py @@ -0,0 +1,167 @@ +import os +import psutil +import datetime +from neo.Prompt.CommandBase import CommandBase, CommandDesc, ParameterDesc +from neo.Prompt.PromptData import PromptData +from neo.Prompt.Utils import get_arg +from neo.Core.Blockchain import Blockchain +from prompt_toolkit.shortcuts import print_formatted_text +from prompt_toolkit.formatted_text import FormattedText +from neocore.UInt256 import UInt256 +from neo.IO.MemoryStream import StreamManager +from neo.Network.NodeLeader import NodeLeader +from neo.Implementations.Notifications.LevelDB.NotificationDB import NotificationDB +from neo.logging import log_manager +import json + + +logger = log_manager.getLogger() + + +class CommandShow(CommandBase): + def __init__(self): + super().__init__() + + self.register_sub_command(CommandShowBlock()) + self.register_sub_command(CommandShowHeader()) + self.register_sub_command(CommandShowTx()) + self.register_sub_command(CommandShowMem()) + self.register_sub_command(CommandShowNodes(), ['node']) + + def command_desc(self): + return CommandDesc('show', 'show data from the blockchain') + + def execute(self, arguments): + item = get_arg(arguments) + + if not item: + print("run `show help` to see supported queries") + return + + try: + return self.execute_sub_command(item, arguments[1:]) + except KeyError: + print(f"show: {item} is an invalid parameter") + return + + +class CommandShowBlock(CommandBase): + def __init__(self): + super().__init__() + + def execute(self, arguments): + item = get_arg(arguments) + txarg = get_arg(arguments, 1) + + block = Blockchain.Default().GetBlock(item) + + if block is not None: + block.LoadTransactions() + bjson = json.dumps(block.ToJson(), indent=4) + if txarg and 'tx' in txarg: + txs = [] + for tx in block.FullTransactions: + tjson = json.dumps(tx.ToJson(), indent=4) + tokens = [("class:number", tjson)] + print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + txs.append(tx.ToJson()) + return txs + + tokens = [("class:number", bjson)] + print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + return block.ToJson() + + else: + print("Could not locate block %s" % item) + return + + def command_desc(self): + p1 = ParameterDesc('index/hash', 'the index or scripthash of the block') + p2 = ParameterDesc('tx', 'arg to only show block transactions', optional=True) + return CommandDesc('block', 'show a specified block', [p1, p2]) + + +class CommandShowHeader(CommandBase): + def __init__(self): + super().__init__() + + def execute(self, arguments): + item = get_arg(arguments) + + header = Blockchain.Default().GetHeaderBy(item) + if header is not None: + hjson = (json.dumps(header.ToJson(), indent=4)) + tokens = [("class:number", hjson)] + print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + return header.ToJson() + else: + print("Could not locate header %s\n" % item) + return + + def command_desc(self): + p1 = ParameterDesc('index/hash', 'the index or scripthash of the block header') + return CommandDesc('header', 'show the header of a specified block', [p1]) + + +class CommandShowTx(CommandBase): + def __init__(self): + super().__init__() + + def execute(self, arguments): + try: + txid = UInt256.ParseString(get_arg(arguments)) + tx, height = Blockchain.Default().GetTransaction(txid) + if height > -1: + jsn = tx.ToJson() + jsn['height'] = height + jsn['unspents'] = [uns.ToJson(tx.outputs.index(uns)) for uns in + Blockchain.Default().GetAllUnspent(txid)] + tokens = [("class:command", json.dumps(jsn, indent=4))] + print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + return jsn + else: + print(f"Could not find transaction for hash {txid}") + return + except Exception as e: + print("Could not find transaction from args: %s (%s)" % (e, arguments)) + return + + def command_desc(self): + p1 = ParameterDesc('hash', 'the scripthash of the transaction') + return CommandDesc('tx', 'show a specified transaction', [p1]) + + +class CommandShowMem(CommandBase): + def __init__(self): + super().__init__() + + def execute(self, arguments=None): + process = psutil.Process(os.getpid()) + total = process.memory_info().rss + totalmb = total / (1024 * 1024) + out = "Total: %s MB\n" % totalmb + out += "Total buffers: %s\n" % StreamManager.TotalBuffers() + print_formatted_text(FormattedText([("class:number", out)]), style=PromptData.Prompt.token_style) + return out + + def command_desc(self): + return CommandDesc('mem', 'show memory in use and number of buffers') + + +class CommandShowNodes(CommandBase): + def __init__(self): + super().__init__() + + def execute(self, arguments=None): + if len(NodeLeader.Instance().Peers) > 0: + out = "Total Connected: %s\n" % len(NodeLeader.Instance().Peers) + for peer in NodeLeader.Instance().Peers: + out += "Peer %s - IO: %s\n" % (peer.Name(), peer.IOStats()) + print_formatted_text(FormattedText([("class:number", out)]), style=PromptData.Prompt.token_style) + return out + else: + print("Not connected yet\n") + return + + def command_desc(self): + return CommandDesc('nodes', 'show connected peers') From ea08db1222b558423292eb21ebda0828853ae71e Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Wed, 5 Dec 2018 07:00:28 -0800 Subject: [PATCH 05/16] Create test_show_commands.py add tests for `show block` through `show nodes` --- .../Commands/tests/test_show_commands.py | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 neo/Prompt/Commands/tests/test_show_commands.py diff --git a/neo/Prompt/Commands/tests/test_show_commands.py b/neo/Prompt/Commands/tests/test_show_commands.py new file mode 100644 index 000000000..f87f65ebe --- /dev/null +++ b/neo/Prompt/Commands/tests/test_show_commands.py @@ -0,0 +1,158 @@ +import os +from neo.Settings import settings +from neo.Utils.BlockchainFixtureTestCase import BlockchainFixtureTestCase +from neo.Prompt.Commands.Show import CommandShow +from neo.Prompt.Commands.Wallet import CommandWallet +from neo.Prompt.PromptData import PromptData +from neo.bin.prompt import PromptInterface +from copy import deepcopy +from neo.Network.NodeLeader import NodeLeader, NeoNode +from neo.Core.Blockchain import Blockchain +from neo.Implementations.Wallets.peewee.UserWallet import UserWallet +from mock import patch + + +class CommandShowTestCase(BlockchainFixtureTestCase): + + @classmethod + def leveldb_testpath(self): + return os.path.join(settings.DATA_DIR_PATH, 'fixtures/test_chain') + + @classmethod + def tearDown(cls): + PromptData.Prompt = None + PromptData.Wallet = None + + def test_show(self): + # with no subcommand + res = CommandShow().execute(None) + self.assertFalse(res) + + # with invalid command + args = ['badcommand'] + res = CommandShow().execute(args) + self.assertFalse(res) + + def test_show_block(self): + # setup + PromptInterface() + + # show good block by index + args = ['block', '9'] + res = CommandShow().execute(args) + self.assertTrue(res) + self.assertEqual(res['index'], 9) + self.assertIn('tx', res) + + # show good block by hash + args = ['block', "0x7c5b4c8a70336bf68e8679be7c9a2a15f85c0f6d0e14389019dcc3edfab2bb4b"] + res = CommandShow().execute(args) + self.assertTrue(res) + self.assertEqual(res['index'], 9) + self.assertIn('tx', res) + + # show the block's transactions only + args = ['block', '9', "tx"] + res = CommandShow().execute(args) + self.assertTrue(res) + self.assertEqual(len(res), 2) + self.assertEqual(res[0]['type'], "MinerTransaction") + self.assertEqual(res[1]['type'], "ContractTransaction") + + # request bad block + args = ['block', 'blah'] + res = CommandShow().execute(args) + self.assertFalse(res) + + def test_show_header(self): + # setup + PromptInterface() + + # show good header by index + args = ['header', '9'] + res = CommandShow().execute(args) + self.assertTrue(res) + self.assertEqual(res['index'], 9) + self.assertNotIn('tx', res) + + # show good header by hash + args = ['header', "0x7c5b4c8a70336bf68e8679be7c9a2a15f85c0f6d0e14389019dcc3edfab2bb4b"] + res = CommandShow().execute(args) + self.assertTrue(res) + self.assertEqual(res['index'], 9) + self.assertNotIn('tx', res) + + # request bad header + args = ['header', 'blah'] + res = CommandShow().execute(args) + self.assertFalse(res) + + def test_show_tx(self): + # setup + PromptInterface() + + # show good tx + txid = '0x83df8bd085fcb60b2789f7d0a9f876e5f3908567f7877fcba835e899b9dea0b5' + args = ['tx', txid] + res = CommandShow().execute(args) + self.assertTrue(res) + self.assertEqual(res['txid'], txid) + self.assertIn('height', res) + self.assertIn('unspents', res) + + # query a bad tx + args = ['tx', '0x83df8bd085fcb60b2789f7d0a9f876e5f3908567f7877fcba835e899b9dea0b6'] + res = CommandShow().execute(args) + self.assertFalse(res) + + # query with bad args + args = ['tx', 'blah'] + res = CommandShow().execute(args) + self.assertFalse(res) + + def test_show_mem(self): + # setup + PromptInterface() + + args = ['mem'] + res = CommandShow().execute(args) + self.assertTrue(res) + + def test_show_nodes(self): + # setup + PromptInterface() + + # query nodes with no NodeLeader.Instance() + with patch('neo.Network.NodeLeader.NodeLeader.Instance'): + args = ['nodes'] + res = CommandShow().execute(args) + self.assertFalse(res) + + # query nodes with connected peers + # first make sure we have a predictable state + leader = NodeLeader.Instance() + old_leader = deepcopy(leader) + leader.ADDRS = ["127.0.0.1:20333", "127.0.0.2:20334"] + leader.DEAD_ADDRS = ["127.0.0.1:20335"] + test_node = NeoNode() + test_node.host = "127.0.0.1" + test_node.port = 20333 + leader.Peers = [test_node] + + # now show nodes + with patch('neo.Network.NeoNode.NeoNode.Name', return_value="test name"): + args = ['nodes'] + res = CommandShow().execute(args) + self.assertTrue(res) + self.assertIn('Total Connected: 1', res) + self.assertIn('Peer test name - IO: 0.0 MB in / 0.0 MB out', res) + + # now use "node" + args = ['node'] + res = CommandShow().execute(args) + self.assertTrue(res) + self.assertIn('Total Connected: 1', res) + self.assertIn('Peer test name - IO: 0.0 MB in / 0.0 MB out', res) + + # restore whatever state the instance was in + NodeLeader._LEAD = old_leader From 1ddacb053813a2f209a21cd619cdec2dbe900286 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Wed, 5 Dec 2018 07:12:36 -0800 Subject: [PATCH 06/16] Update prompt.py remove space for `make lint` --- neo/bin/prompt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/bin/prompt.py b/neo/bin/prompt.py index e298edf28..e5c652257 100755 --- a/neo/bin/prompt.py +++ b/neo/bin/prompt.py @@ -71,7 +71,7 @@ class PromptInterface: _known_things = [] _commands = [ - CommandWallet(), CommandShow() + CommandWallet(), CommandShow() ] _command_descs = [desc for c in _commands for desc in c.command_descs_with_sub_commands()] From 02b0f694822946e794d19f21dfb5a8f6618ffec4 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 00:15:27 -0800 Subject: [PATCH 07/16] Update Show.py - update per https://github.com/CityOfZion/neo-python/pull/739#pullrequestreview-181821006 --- neo/Prompt/Commands/Show.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/neo/Prompt/Commands/Show.py b/neo/Prompt/Commands/Show.py index 9aee8ce20..623150ce7 100644 --- a/neo/Prompt/Commands/Show.py +++ b/neo/Prompt/Commands/Show.py @@ -5,8 +5,6 @@ from neo.Prompt.PromptData import PromptData from neo.Prompt.Utils import get_arg from neo.Core.Blockchain import Blockchain -from prompt_toolkit.shortcuts import print_formatted_text -from prompt_toolkit.formatted_text import FormattedText from neocore.UInt256 import UInt256 from neo.IO.MemoryStream import StreamManager from neo.Network.NodeLeader import NodeLeader @@ -35,13 +33,13 @@ def execute(self, arguments): item = get_arg(arguments) if not item: - print("run `show help` to see supported queries") + print("run `%s help` to see supported queries" % CommandShow().command_desc().command) return try: return self.execute_sub_command(item, arguments[1:]) except KeyError: - print(f"show: {item} is an invalid parameter") + print(f"{item} is an invalid parameter") return @@ -57,18 +55,15 @@ def execute(self, arguments): if block is not None: block.LoadTransactions() - bjson = json.dumps(block.ToJson(), indent=4) + if txarg and 'tx' in txarg: txs = [] for tx in block.FullTransactions: - tjson = json.dumps(tx.ToJson(), indent=4) - tokens = [("class:number", tjson)] - print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + print(json.dumps(tx.ToJson(), indent=4)) txs.append(tx.ToJson()) return txs - tokens = [("class:number", bjson)] - print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + print(json.dumps(block.ToJson(), indent=4)) return block.ToJson() else: @@ -90,9 +85,7 @@ def execute(self, arguments): header = Blockchain.Default().GetHeaderBy(item) if header is not None: - hjson = (json.dumps(header.ToJson(), indent=4)) - tokens = [("class:number", hjson)] - print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + print(json.dumps(header.ToJson(), indent=4)) return header.ToJson() else: print("Could not locate header %s\n" % item) @@ -116,8 +109,7 @@ def execute(self, arguments): jsn['height'] = height jsn['unspents'] = [uns.ToJson(tx.outputs.index(uns)) for uns in Blockchain.Default().GetAllUnspent(txid)] - tokens = [("class:command", json.dumps(jsn, indent=4))] - print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + print(json.dumps(jsn, indent=4)) return jsn else: print(f"Could not find transaction for hash {txid}") @@ -141,7 +133,7 @@ def execute(self, arguments=None): totalmb = total / (1024 * 1024) out = "Total: %s MB\n" % totalmb out += "Total buffers: %s\n" % StreamManager.TotalBuffers() - print_formatted_text(FormattedText([("class:number", out)]), style=PromptData.Prompt.token_style) + print(out) return out def command_desc(self): @@ -157,7 +149,7 @@ def execute(self, arguments=None): out = "Total Connected: %s\n" % len(NodeLeader.Instance().Peers) for peer in NodeLeader.Instance().Peers: out += "Peer %s - IO: %s\n" % (peer.Name(), peer.IOStats()) - print_formatted_text(FormattedText([("class:number", out)]), style=PromptData.Prompt.token_style) + print(out) return out else: print("Not connected yet\n") From d10cc2d324a01f697c84f2e77aa72c3900416580 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 00:16:40 -0800 Subject: [PATCH 08/16] Update test_show_commands.py - update per https://github.com/CityOfZion/neo-python/pull/739#pullrequestreview-181821006 --- neo/Prompt/Commands/tests/test_show_commands.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/neo/Prompt/Commands/tests/test_show_commands.py b/neo/Prompt/Commands/tests/test_show_commands.py index f87f65ebe..a3a00dee1 100644 --- a/neo/Prompt/Commands/tests/test_show_commands.py +++ b/neo/Prompt/Commands/tests/test_show_commands.py @@ -34,9 +34,6 @@ def test_show(self): self.assertFalse(res) def test_show_block(self): - # setup - PromptInterface() - # show good block by index args = ['block', '9'] res = CommandShow().execute(args) @@ -65,9 +62,6 @@ def test_show_block(self): self.assertFalse(res) def test_show_header(self): - # setup - PromptInterface() - # show good header by index args = ['header', '9'] res = CommandShow().execute(args) @@ -88,9 +82,6 @@ def test_show_header(self): self.assertFalse(res) def test_show_tx(self): - # setup - PromptInterface() - # show good tx txid = '0x83df8bd085fcb60b2789f7d0a9f876e5f3908567f7877fcba835e899b9dea0b5' args = ['tx', txid] @@ -111,17 +102,11 @@ def test_show_tx(self): self.assertFalse(res) def test_show_mem(self): - # setup - PromptInterface() - args = ['mem'] res = CommandShow().execute(args) self.assertTrue(res) def test_show_nodes(self): - # setup - PromptInterface() - # query nodes with no NodeLeader.Instance() with patch('neo.Network.NodeLeader.NodeLeader.Instance'): args = ['nodes'] From 202f64d8b1aaa5dc78956dcbba19b0d5284c2fed Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 00:36:48 -0800 Subject: [PATCH 09/16] Update Wallet.py - update per review --- neo/Prompt/Commands/Wallet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Prompt/Commands/Wallet.py b/neo/Prompt/Commands/Wallet.py index 55639490b..a2cc19147 100644 --- a/neo/Prompt/Commands/Wallet.py +++ b/neo/Prompt/Commands/Wallet.py @@ -61,7 +61,7 @@ def execute(self, arguments): try: return self.execute_sub_command(item, arguments[1:]) except KeyError: - print(f"Wallet: {item} is an invalid parameter") + print(f"{item} is an invalid parameter") return From 31eb6991da54bca33ebe8ac36b51c1f5196ae85e Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 01:02:17 -0800 Subject: [PATCH 10/16] Update prompt.py - update for compatibility --- neo/bin/prompt.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neo/bin/prompt.py b/neo/bin/prompt.py index e5c652257..a955e0c83 100755 --- a/neo/bin/prompt.py +++ b/neo/bin/prompt.py @@ -16,7 +16,6 @@ from neo.Implementations.Notifications.LevelDB.NotificationDB import NotificationDB from neo.Network.NodeLeader import NodeLeader from neo.Prompt.Commands.Wallet import CommandWallet -from neo.Prompt.Commands.Show import CommandShow from neo.Prompt.PromptData import PromptData from neo.Prompt.InputParser import InputParser from neo.Settings import settings, PrivnetConnectionError @@ -71,7 +70,7 @@ class PromptInterface: _known_things = [] _commands = [ - CommandWallet(), CommandShow() + CommandWallet(), ] _command_descs = [desc for c in _commands for desc in c.command_descs_with_sub_commands()] From 87717667aebf3c503000689b65486a04ced289ee Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 01:04:57 -0800 Subject: [PATCH 11/16] Update prompt.py - re-add CommandShow --- neo/bin/prompt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neo/bin/prompt.py b/neo/bin/prompt.py index 6cbf1f489..e8d0290a9 100755 --- a/neo/bin/prompt.py +++ b/neo/bin/prompt.py @@ -16,6 +16,7 @@ from neo.Implementations.Notifications.LevelDB.NotificationDB import NotificationDB from neo.Network.NodeLeader import NodeLeader from neo.Prompt.Commands.Wallet import CommandWallet +from neo.Prompt.Commands.Show import CommandShow from neo.Prompt.Commands.Search import CommandSearch from neo.Prompt.PromptData import PromptData from neo.Prompt.InputParser import InputParser @@ -71,7 +72,7 @@ class PromptInterface: _known_things = [] _commands = [ - CommandWallet(), CommandSearch() + CommandWallet(), CommandShow(), CommandSearch() ] _command_descs = [desc for c in _commands for desc in c.command_descs_with_sub_commands()] From 3edaff032f77ffa9e03bfdff797d3a3746f7539d Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 01:05:40 -0800 Subject: [PATCH 12/16] Update Search.py - update per review --- neo/Prompt/Commands/Search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Prompt/Commands/Search.py b/neo/Prompt/Commands/Search.py index 8c23e8aeb..1b2433385 100644 --- a/neo/Prompt/Commands/Search.py +++ b/neo/Prompt/Commands/Search.py @@ -23,7 +23,7 @@ def execute(self, arguments): item = get_arg(arguments) if not item: - print("run `%s help` to see supported queries" % CommandSearch().command_desc().command) + print("run `%s help` to see supported queries" % self.command_desc().command) return try: From 474caa1445bba0eebe7c86304ad8efad7787dea0 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 01:06:07 -0800 Subject: [PATCH 13/16] Update Show.py - update per review --- neo/Prompt/Commands/Show.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Prompt/Commands/Show.py b/neo/Prompt/Commands/Show.py index 623150ce7..d9b0b890f 100644 --- a/neo/Prompt/Commands/Show.py +++ b/neo/Prompt/Commands/Show.py @@ -33,7 +33,7 @@ def execute(self, arguments): item = get_arg(arguments) if not item: - print("run `%s help` to see supported queries" % CommandShow().command_desc().command) + print("run `%s help` to see supported queries" % self.command_desc().command) return try: From b6074e186489df28ea87bdeb80e1255278992455 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 01:32:22 -0800 Subject: [PATCH 14/16] Update Show.py - update for missing arguments --- neo/Prompt/Commands/Show.py | 78 +++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/neo/Prompt/Commands/Show.py b/neo/Prompt/Commands/Show.py index d9b0b890f..1df011ae2 100644 --- a/neo/Prompt/Commands/Show.py +++ b/neo/Prompt/Commands/Show.py @@ -27,7 +27,7 @@ def __init__(self): self.register_sub_command(CommandShowNodes(), ['node']) def command_desc(self): - return CommandDesc('show', 'show data from the blockchain') + return CommandDesc('show', 'show useful data') def execute(self, arguments): item = get_arg(arguments) @@ -50,24 +50,27 @@ def __init__(self): def execute(self, arguments): item = get_arg(arguments) txarg = get_arg(arguments, 1) + if item is not None: + block = Blockchain.Default().GetBlock(item) - block = Blockchain.Default().GetBlock(item) + if block is not None: + block.LoadTransactions() - if block is not None: - block.LoadTransactions() + if txarg and 'tx' in txarg: + txs = [] + for tx in block.FullTransactions: + print(json.dumps(tx.ToJson(), indent=4)) + txs.append(tx.ToJson()) + return txs - if txarg and 'tx' in txarg: - txs = [] - for tx in block.FullTransactions: - print(json.dumps(tx.ToJson(), indent=4)) - txs.append(tx.ToJson()) - return txs - - print(json.dumps(block.ToJson(), indent=4)) - return block.ToJson() + print(json.dumps(block.ToJson(), indent=4)) + return block.ToJson() + else: + print("Could not locate block %s" % item) + return else: - print("Could not locate block %s" % item) + print("please specify a block") return def command_desc(self): @@ -82,13 +85,16 @@ def __init__(self): def execute(self, arguments): item = get_arg(arguments) - - header = Blockchain.Default().GetHeaderBy(item) - if header is not None: - print(json.dumps(header.ToJson(), indent=4)) - return header.ToJson() + if item is not None: + header = Blockchain.Default().GetHeaderBy(item) + if header is not None: + print(json.dumps(header.ToJson(), indent=4)) + return header.ToJson() + else: + print("Could not locate header %s\n" % item) + return else: - print("Could not locate header %s\n" % item) + print("Please specify a header") return def command_desc(self): @@ -101,21 +107,25 @@ def __init__(self): super().__init__() def execute(self, arguments): - try: - txid = UInt256.ParseString(get_arg(arguments)) - tx, height = Blockchain.Default().GetTransaction(txid) - if height > -1: - jsn = tx.ToJson() - jsn['height'] = height - jsn['unspents'] = [uns.ToJson(tx.outputs.index(uns)) for uns in - Blockchain.Default().GetAllUnspent(txid)] - print(json.dumps(jsn, indent=4)) - return jsn - else: - print(f"Could not find transaction for hash {txid}") + if len(arguments): + try: + txid = UInt256.ParseString(get_arg(arguments)) + tx, height = Blockchain.Default().GetTransaction(txid) + if height > -1: + jsn = tx.ToJson() + jsn['height'] = height + jsn['unspents'] = [uns.ToJson(tx.outputs.index(uns)) for uns in + Blockchain.Default().GetAllUnspent(txid)] + print(json.dumps(jsn, indent=4)) + return jsn + else: + print(f"Could not find transaction for hash {txid}") + return + except Exception: + print("Could not find transaction from args: %s" % arguments) return - except Exception as e: - print("Could not find transaction from args: %s (%s)" % (e, arguments)) + else: + print("Please specify a TX hash") return def command_desc(self): From a57ab9f0d96cfdd850c76ae7fca2f51a219e0479 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 01:33:22 -0800 Subject: [PATCH 15/16] Update test_show_commands.py - add tests for updated missing arguments --- neo/Prompt/Commands/tests/test_show_commands.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/neo/Prompt/Commands/tests/test_show_commands.py b/neo/Prompt/Commands/tests/test_show_commands.py index a3a00dee1..a297abc0c 100644 --- a/neo/Prompt/Commands/tests/test_show_commands.py +++ b/neo/Prompt/Commands/tests/test_show_commands.py @@ -34,6 +34,11 @@ def test_show(self): self.assertFalse(res) def test_show_block(self): + # test no block input + args = ['block'] + res = CommandShow().execute(args) + self.assertFalse(res) + # show good block by index args = ['block', '9'] res = CommandShow().execute(args) @@ -62,6 +67,11 @@ def test_show_block(self): self.assertFalse(res) def test_show_header(self): + # test no header input + args = ['header'] + res = CommandShow().execute(args) + self.assertFalse(res) + # show good header by index args = ['header', '9'] res = CommandShow().execute(args) @@ -82,6 +92,11 @@ def test_show_header(self): self.assertFalse(res) def test_show_tx(self): + # test no tx input + args = ['tx'] + res = CommandShow().execute(args) + self.assertFalse(res) + # show good tx txid = '0x83df8bd085fcb60b2789f7d0a9f876e5f3908567f7877fcba835e899b9dea0b5' args = ['tx', txid] From 18eb43883b65bf8fcbcca4dd0b35869cf4884f00 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 01:34:59 -0800 Subject: [PATCH 16/16] Update Show.py - updated indentation for `make lint` --- neo/Prompt/Commands/Show.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Prompt/Commands/Show.py b/neo/Prompt/Commands/Show.py index 1df011ae2..b3214ad1f 100644 --- a/neo/Prompt/Commands/Show.py +++ b/neo/Prompt/Commands/Show.py @@ -115,7 +115,7 @@ def execute(self, arguments): jsn = tx.ToJson() jsn['height'] = height jsn['unspents'] = [uns.ToJson(tx.outputs.index(uns)) for uns in - Blockchain.Default().GetAllUnspent(txid)] + Blockchain.Default().GetAllUnspent(txid)] print(json.dumps(jsn, indent=4)) return jsn else: