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/12] 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/12] 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 ebdd8ca18f5b97485fc42e200ce7ef0f016186c0 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Wed, 5 Dec 2018 07:08:20 -0800 Subject: [PATCH 03/12] Update prompt.py - Add CommandSearch --- 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..6cbf1f489 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.Search import CommandSearch 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(), CommandSearch() ] _command_descs = [desc for c in _commands for desc in c.command_descs_with_sub_commands()] From 63c437eab50844d90472373a978b4d06608050e8 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Wed, 5 Dec 2018 07:09:19 -0800 Subject: [PATCH 04/12] Add Search.py --- neo/Prompt/Commands/Search.py | 75 +++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 neo/Prompt/Commands/Search.py diff --git a/neo/Prompt/Commands/Search.py b/neo/Prompt/Commands/Search.py new file mode 100644 index 000000000..4404f92b9 --- /dev/null +++ b/neo/Prompt/Commands/Search.py @@ -0,0 +1,75 @@ +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 neo.logging import log_manager +import json + + +logger = log_manager.getLogger() + + +class CommandSearch(CommandBase): + def __init__(self): + super().__init__() + + self.register_sub_command(CommandSearchAsset()) + self.register_sub_command(CommandSearchContract()) + + def command_desc(self): + return CommandDesc('search', 'search the blockchain') + + def execute(self, arguments): + item = get_arg(arguments) + + if not item: + print("run `search help` to see supported queries") + return + + try: + return self.execute_sub_command(item, arguments[1:]) + except KeyError: + print(f"search: {item} is an invalid parameter") + return + + +class CommandSearchAsset(CommandBase): + def __init__(self): + super().__init__() + + def execute(self, arguments): + item = get_arg(arguments) + + results = Blockchain.Default().SearchAssetState(item) + print("Found %s results for %s" % (len(results), item)) + for asset in results: + bjson = json.dumps(asset.ToJson(), indent=4) + tokens = [("class:number", bjson)] + print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + return results + + def command_desc(self): + p1 = ParameterDesc('query', 'supports name, issuer, or admin searches') + return CommandDesc('asset', 'perform an asset search', [p1]) + + +class CommandSearchContract(CommandBase): + def __init__(self): + super().__init__() + + def execute(self, arguments): + item = get_arg(arguments) + + contracts = Blockchain.Default().SearchContracts(query=item) + print("Found %s results for %s" % (len(contracts), item)) + for contract in contracts: + bjson = json.dumps(contract.ToJson(), indent=4) + tokens = [("class:number", bjson)] + print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + return contracts + + def command_desc(self): + p1 = ParameterDesc('query', 'supports name, author, description, or email searches') + return CommandDesc('contract', 'perform a contract search', [p1]) From c263e3b09d2a1d26da0f1b89c4affabc5bd0b3c2 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Wed, 5 Dec 2018 07:10:03 -0800 Subject: [PATCH 05/12] Add test_search_commands.py --- .../Commands/tests/test_search_commands.py | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 neo/Prompt/Commands/tests/test_search_commands.py diff --git a/neo/Prompt/Commands/tests/test_search_commands.py b/neo/Prompt/Commands/tests/test_search_commands.py new file mode 100644 index 000000000..3bcc15f1d --- /dev/null +++ b/neo/Prompt/Commands/tests/test_search_commands.py @@ -0,0 +1,79 @@ +import os +from neo.Settings import settings +from neo.Utils.BlockchainFixtureTestCase import BlockchainFixtureTestCase +from neo.Prompt.Commands.Search import CommandSearch +from neo.Prompt.PromptData import PromptData +from neo.bin.prompt import PromptInterface + + +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 + + def test_search(self): + # with no subcommand + res = CommandSearch().execute(None) + self.assertFalse(res) + + # with invalid command + args = ['badcommand'] + res = CommandSearch().execute(args) + self.assertFalse(res) + + def test_search_asset(self): + # setup + PromptInterface() + + # successful search by name + args = ['asset', "AntShare"] + res = CommandSearch().execute(args) + self.assertTrue(res) + + # successful search by issuer and admin (same address) + args = ['asset', "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt"] + res = CommandSearch().execute(args) + self.assertTrue(res) + + # unsuccessful search + args = ['asset', 'blah'] + res = CommandSearch().execute(args) + self.assertFalse(res) + + def test_search_contract(self): + # setup + PromptInterface() + + # successful search by name + args = ['contract', "test NEX Template V4"] + res = CommandSearch().execute(args) + self.assertTrue(res) + self.assertEqual(len(res), 1) + + # successful search by author + args = ['contract', "dauTT"] + res = CommandSearch().execute(args) + self.assertTrue(res) + self.assertEqual(len(res), 3) + + # successful search by description + args = ['contract', "neo-ico-template"] + res = CommandSearch().execute(args) + self.assertTrue(res) + self.assertEqual(len(res), 1) + + # successful search by email (as entered) + args = ['contract', ""] + res = CommandSearch().execute(args) + self.assertTrue(res) + self.assertEqual(len(res), 6) + + # bad search input + args = ['contract', "blah"] + res = CommandSearch().execute(args) + self.assertFalse(res) From 4396d7e20395b487cb6cda69078e83000b938feb Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Wed, 5 Dec 2018 10:23:24 -0800 Subject: [PATCH 06/12] Update Search.py - update per https://github.com/CityOfZion/neo-python/pull/739#pullrequestreview-181821006 --- neo/Prompt/Commands/Search.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/neo/Prompt/Commands/Search.py b/neo/Prompt/Commands/Search.py index 4404f92b9..8c23e8aeb 100644 --- a/neo/Prompt/Commands/Search.py +++ b/neo/Prompt/Commands/Search.py @@ -2,8 +2,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 neo.logging import log_manager import json @@ -25,13 +23,13 @@ def execute(self, arguments): item = get_arg(arguments) if not item: - print("run `search help` to see supported queries") + print("run `%s help` to see supported queries" % CommandSearch().command_desc().command) return try: return self.execute_sub_command(item, arguments[1:]) except KeyError: - print(f"search: {item} is an invalid parameter") + print(f"{item} is an invalid parameter") return @@ -45,9 +43,7 @@ def execute(self, arguments): results = Blockchain.Default().SearchAssetState(item) print("Found %s results for %s" % (len(results), item)) for asset in results: - bjson = json.dumps(asset.ToJson(), indent=4) - tokens = [("class:number", bjson)] - print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + print(json.dumps(asset.ToJson(), indent=4)) return results def command_desc(self): @@ -65,9 +61,7 @@ def execute(self, arguments): contracts = Blockchain.Default().SearchContracts(query=item) print("Found %s results for %s" % (len(contracts), item)) for contract in contracts: - bjson = json.dumps(contract.ToJson(), indent=4) - tokens = [("class:number", bjson)] - print_formatted_text(FormattedText(tokens), style=PromptData.Prompt.token_style) + print(json.dumps(contract.ToJson(), indent=4)) return contracts def command_desc(self): From 64693ca3d8f520992dfe2825ea05478c24176ac0 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Wed, 5 Dec 2018 10:24:34 -0800 Subject: [PATCH 07/12] Update test_search_commands.py - update per https://github.com/CityOfZion/neo-python/pull/739#pullrequestreview-181821006 --- neo/Prompt/Commands/tests/test_search_commands.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/neo/Prompt/Commands/tests/test_search_commands.py b/neo/Prompt/Commands/tests/test_search_commands.py index 3bcc15f1d..509766729 100644 --- a/neo/Prompt/Commands/tests/test_search_commands.py +++ b/neo/Prompt/Commands/tests/test_search_commands.py @@ -3,7 +3,6 @@ from neo.Utils.BlockchainFixtureTestCase import BlockchainFixtureTestCase from neo.Prompt.Commands.Search import CommandSearch from neo.Prompt.PromptData import PromptData -from neo.bin.prompt import PromptInterface class CommandShowTestCase(BlockchainFixtureTestCase): @@ -12,10 +11,6 @@ class CommandShowTestCase(BlockchainFixtureTestCase): def leveldb_testpath(self): return os.path.join(settings.DATA_DIR_PATH, 'fixtures/test_chain') - @classmethod - def tearDown(cls): - PromptData.Prompt = None - def test_search(self): # with no subcommand res = CommandSearch().execute(None) @@ -27,9 +22,6 @@ def test_search(self): self.assertFalse(res) def test_search_asset(self): - # setup - PromptInterface() - # successful search by name args = ['asset', "AntShare"] res = CommandSearch().execute(args) @@ -46,9 +38,6 @@ def test_search_asset(self): self.assertFalse(res) def test_search_contract(self): - # setup - PromptInterface() - # successful search by name args = ['contract', "test NEX Template V4"] res = CommandSearch().execute(args) From 843ae57ce94ed46de051987268f9dc61d9b75e3f Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 00:30:35 -0800 Subject: [PATCH 08/12] Update LevelDBBlockchain.py - improve search functionality --- .../Blockchains/LevelDB/LevelDBBlockchain.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/neo/Implementations/Blockchains/LevelDB/LevelDBBlockchain.py b/neo/Implementations/Blockchains/LevelDB/LevelDBBlockchain.py index c46ed96e0..02ef4f25b 100644 --- a/neo/Implementations/Blockchains/LevelDB/LevelDBBlockchain.py +++ b/neo/Implementations/Blockchains/LevelDB/LevelDBBlockchain.py @@ -340,6 +340,12 @@ def SearchAssetState(self, query): assets = DBCollection(self._db, DBPrefix.ST_Asset, AssetState) keys = assets.Keys + if query.lower() == "neo": + query = "AntShare" + + if query.lower() in {"gas", "neogas"}: + query = "AntCoin" + for item in keys: asset = assets.TryGet(keyval=item) if query in asset.Name.decode('utf-8'): From 156907a222e645a38845a3c55c0a9eed0ea90f0b Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 00:32:17 -0800 Subject: [PATCH 09/12] Update test_search_commands.py - update tests from improvement in `SearchAssetState` --- .../Commands/tests/test_search_commands.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/neo/Prompt/Commands/tests/test_search_commands.py b/neo/Prompt/Commands/tests/test_search_commands.py index 509766729..14143e310 100644 --- a/neo/Prompt/Commands/tests/test_search_commands.py +++ b/neo/Prompt/Commands/tests/test_search_commands.py @@ -22,11 +22,31 @@ def test_search(self): self.assertFalse(res) def test_search_asset(self): - # successful search by name + # successful search asset NEO + args = ['asset', "NEO"] + res = CommandSearch().execute(args) + self.assertTrue(res) + + # successful search asset gas + args = ['asset', "gas"] + res = CommandSearch().execute(args) + self.assertTrue(res) + + # successful search asset NEOGas + args = ['asset', "NEOGas"] + res = CommandSearch().execute(args) + self.assertTrue(res) + + # successful search asset AntShare args = ['asset', "AntShare"] res = CommandSearch().execute(args) self.assertTrue(res) + # successful search asset AntShare + args = ['asset', "AntCoin"] + res = CommandSearch().execute(args) + self.assertTrue(res) + # successful search by issuer and admin (same address) args = ['asset', "Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt"] res = CommandSearch().execute(args) From 487d294276b1a19364844d0fba4f0aeec1cfc4f6 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 00:34:39 -0800 Subject: [PATCH 10/12] Update test_search_commands.py remove unnecessary import --- neo/Prompt/Commands/tests/test_search_commands.py | 1 - 1 file changed, 1 deletion(-) diff --git a/neo/Prompt/Commands/tests/test_search_commands.py b/neo/Prompt/Commands/tests/test_search_commands.py index 14143e310..8f7950c1c 100644 --- a/neo/Prompt/Commands/tests/test_search_commands.py +++ b/neo/Prompt/Commands/tests/test_search_commands.py @@ -2,7 +2,6 @@ from neo.Settings import settings from neo.Utils.BlockchainFixtureTestCase import BlockchainFixtureTestCase from neo.Prompt.Commands.Search import CommandSearch -from neo.Prompt.PromptData import PromptData class CommandShowTestCase(BlockchainFixtureTestCase): From 446732f7fcfda73c7a9c94d8c0f435fc25941bb1 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 00:35:41 -0800 Subject: [PATCH 11/12] 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 bed8d09cc1a7ff5b55304b590cd3b45ef13e2a86 Mon Sep 17 00:00:00 2001 From: jseagrave21 <40873301+jseagrave21@users.noreply.github.com> Date: Thu, 6 Dec 2018 00:43:59 -0800 Subject: [PATCH 12/12] Update test_search_commands.py - fix typo --- neo/Prompt/Commands/tests/test_search_commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Prompt/Commands/tests/test_search_commands.py b/neo/Prompt/Commands/tests/test_search_commands.py index 8f7950c1c..b591aaf96 100644 --- a/neo/Prompt/Commands/tests/test_search_commands.py +++ b/neo/Prompt/Commands/tests/test_search_commands.py @@ -41,7 +41,7 @@ def test_search_asset(self): res = CommandSearch().execute(args) self.assertTrue(res) - # successful search asset AntShare + # successful search asset AntCoin args = ['asset', "AntCoin"] res = CommandSearch().execute(args) self.assertTrue(res)