Skip to content
This repository was archived by the owner on Nov 15, 2021. It is now read-only.

[refactor-prompt] add debugstorage command #784

Merged
merged 2 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions neo/Prompt/Commands/SC.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from neo.Prompt.Utils import get_arg
from neo.Prompt.Commands.BuildNRun import Build, BuildAndRun, LoadAndRun
from neo.Core.Blockchain import Blockchain
from neo.Implementations.Blockchains.LevelDB.DebugStorage import DebugStorage
from distutils import util
from neo.Settings import settings

from neo.logging import log_manager
import json


logger = log_manager.getLogger()


Expand All @@ -18,6 +20,7 @@ def __init__(self):
self.register_sub_command(CommandSCBuild())
self.register_sub_command(CommandSCBuildRun())
self.register_sub_command(CommandSCLoadRun())
self.register_sub_command(CommandSCDebugStorage())

def command_desc(self):
return CommandDesc('sc', 'develop smart contracts')
Expand Down Expand Up @@ -85,7 +88,8 @@ def command_desc(self):
p8 = ParameterDesc('--no-parse-addr', 'a flag to turn off address parsing when input into the smart contract', optional=True)
p9 = ParameterDesc('--from-addr', 'source address to take fee funds from (if not specified, take first address in wallet)', optional=True)
p10 = ParameterDesc('--owners', 'a list of NEO addresses indicating the transaction owners e.g. --owners=[address1,address2]', optional=True)
p11 = ParameterDesc('--tx-attr', 'a list of transaction attributes to attach to the transaction\n\n'
p11 = ParameterDesc('--tx-attr',
'a list of transaction attributes to attach to the transaction\n\n'
f"{' ':>17} See: http://docs.neo.org/en-us/network/network-protocol.html section 4 for a description of possible attributes\n\n"
f"{' ':>17} Example\n"
f"{' ':>20} --tx-attr=[{{\"usage\": <value>,\"data\":\"<remark>\"}}, ...]\n"
Expand Down Expand Up @@ -127,11 +131,46 @@ def command_desc(self):
p6 = ParameterDesc('returntype', 'the returntype of the smart contract output')
p7 = ParameterDesc('inputs', 'the test parameters fed to the smart contract, or use "--i" for prompted parameter input')
p8 = ParameterDesc('--no-parse-addr', 'a flag to turn off address parsing when input into the smart contract', optional=True)
p9 = ParameterDesc('--from-addr', 'source address to take fee funds from (if not specified, take first address in wallet)\n\n'
p9 = ParameterDesc('--from-addr',
'source address to take fee funds from (if not specified, take first address in wallet)\n\n'
f"{' ':>17} Usage Examples:\n"
f"{' ':>20} load_run path.py True False False 0710 05 input1 input2\n"
f"{' ':>20} load_run path.py True False False 0710 05 --i\n\n"
f"{' ':>17} For more information about parameter types see\n"
f"{' ':>17} https://neo-python.readthedocs.io/en/latest/data-types.html#contractparametertypes\n", optional=True)
params = [p1, p2, p3, p4, p5, p6, p7, p8, p9]
return CommandDesc('load_run', 'load a specified smart contract (.avm) file and test it', params=params)


class CommandSCDebugStorage(CommandBase):
def __init__(self):
super().__init__()

def execute(self, arguments):
if len(arguments) != 1:
print("Please specify the required parameter")
return False

what = arguments[0]
if what == 'reset':
DebugStorage.instance().reset()
print("Reset debug storage")
return True

try:
flag = bool(util.strtobool(what))
except ValueError:
print("Invalid option")
return False

settings.USE_DEBUG_STORAGE = flag
if flag:
print("Debug storage ON")
else:
print("Debug storage OFF")

return True

def command_desc(self):
p1 = ParameterDesc('attribute', 'either "on"|"off" or 1|0, or "reset"')
return CommandDesc('debugstorage', 'use a separate database for smart contract storage item debugging', params=[p1])
58 changes: 49 additions & 9 deletions neo/Prompt/Commands/tests/test_sc_commands.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import os
import shutil
import json
import warnings
from neo.Utils.WalletFixtureTestCase import WalletFixtureTestCase
from neo.Wallets.utils import to_aes_key
from neo.Implementations.Wallets.peewee.UserWallet import UserWallet
from neo.Core.Blockchain import Blockchain
from neocore.UInt160 import UInt160
from neo.Prompt.Commands.SC import CommandSC
from neo.Prompt.PromptData import PromptData
from neo.Settings import settings
from mock import patch
from io import StringIO

Expand Down Expand Up @@ -95,7 +94,8 @@ def test_sc_buildrun(self):

# test no open wallet
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['build_run', 'neo/Prompt/Commands/tests/SampleSC.py', 'True', 'False', 'False', '070502', '02', 'add' 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy' '3']
args = ['build_run', 'neo/Prompt/Commands/tests/SampleSC.py', 'True', 'False', 'False', '070502', '02',
'add' 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy' '3']
tx, result, total_ops, engine = CommandSC().execute(args)
self.assertEqual(tx, None)
self.assertEqual(result, None)
Expand All @@ -106,15 +106,17 @@ def test_sc_buildrun(self):
# test bad args
PromptData.Wallet = self.GetWallet1(recreate=True)
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['build_run', 'neo/Prompt/Commands/tests/SampleSC.py', 'True', 'False', '070502', '02', 'add', 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy', '3'] # missing payable flag
args = ['build_run', 'neo/Prompt/Commands/tests/SampleSC.py', 'True', 'False', '070502', '02', 'add', 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy',
'3'] # missing payable flag
res = CommandSC().execute(args)
self.assertFalse(res)
self.assertIn("run `sc build_run help` to see supported queries", mock_print.getvalue())

# test successful build and run
PromptData.Wallet = self.GetWallet1(recreate=True)
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['build_run', 'neo/Prompt/Commands/tests/SampleSC.py', 'True', 'False', 'False', '070502', '02', 'add', 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy', '3']
args = ['build_run', 'neo/Prompt/Commands/tests/SampleSC.py', 'True', 'False', 'False', '070502', '02', 'add', 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy',
'3']
tx, result, total_ops, engine = CommandSC().execute(args)
self.assertTrue(tx)
self.assertEqual(str(result[0]), '3')
Expand All @@ -133,7 +135,8 @@ def test_sc_buildrun(self):
# test invoke failure (SampleSC requires three inputs)
PromptData.Wallet = self.GetWallet1(recreate=True)
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['build_run', 'neo/Prompt/Commands/tests/SampleSC.py', 'True', 'False', 'False', '0705', '02', 'balance', 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy']
args = ['build_run', 'neo/Prompt/Commands/tests/SampleSC.py', 'True', 'False', 'False', '0705', '02', 'balance',
'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy']
tx, result, total_ops, engine = CommandSC().execute(args)
self.assertIsNone(tx)
self.assertIn("Test invoke failed", mock_print.getvalue())
Expand Down Expand Up @@ -163,7 +166,8 @@ def test_sc_loadrun(self):

# test no open wallet
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['load_run', 'neo/Prompt/Commands/tests/SampleSC.avm', 'True', 'False', 'False', '070502', '02', 'add' 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy' '3']
args = ['load_run', 'neo/Prompt/Commands/tests/SampleSC.avm', 'True', 'False', 'False', '070502', '02',
'add' 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy' '3']
tx, result, total_ops, engine = CommandSC().execute(args)
self.assertEqual(tx, None)
self.assertEqual(result, None)
Expand All @@ -174,15 +178,51 @@ def test_sc_loadrun(self):
# test bad args
PromptData.Wallet = self.GetWallet1(recreate=True)
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['load_run', 'neo/Prompt/Commands/tests/SampleSC.avm', 'True', 'False', '070502', '02', 'balance', 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy', '0'] # missing payable flag
args = ['load_run', 'neo/Prompt/Commands/tests/SampleSC.avm', 'True', 'False', '070502', '02', 'balance', 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy',
'0'] # missing payable flag
res = CommandSC().execute(args)
self.assertFalse(res)
self.assertIn("run `sc load_run help` to see supported queries", mock_print.getvalue())

# test successful load and run with from-addr
PromptData.Wallet = self.GetWallet1(recreate=True)
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['load_run', 'neo/Prompt/Commands/tests/SampleSC.avm', 'True', 'False', 'False', '070502', '02', 'balance', 'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy', '0', '--from-addr=' + self.wallet_1_addr]
args = ['load_run', 'neo/Prompt/Commands/tests/SampleSC.avm', 'True', 'False', 'False', '070502', '02', 'balance',
'AG4GfwjnvydAZodm4xEDivguCtjCFzLcJy', '0', '--from-addr=' + self.wallet_1_addr]
tx, result, total_ops, engine = CommandSC().execute(args)
self.assertTrue(tx)
self.assertIn("Test deploy invoke successful", mock_print.getvalue())

def test_sc_debugstorage(self):
# test with insufficient parameters
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['debugstorage']
res = CommandSC().execute(args)
self.assertFalse(res)
self.assertIn("Please specify the required parameter", mock_print.getvalue())

# test with bad parameter
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['debugstorage', 'blah']
res = CommandSC().execute(args)
self.assertFalse(res)
self.assertIn("Invalid option", mock_print.getvalue())

# test with reset parameter
with patch('sys.stdout', new=StringIO()) as mock_print:
args = ['debugstorage', 'reset']
res = CommandSC().execute(args)
self.assertTrue(res)
self.assertIn("Reset debug storage", mock_print.getvalue())

# test turning on
args = ['debugstorage', 'on']
res = CommandSC().execute(args)
self.assertTrue(res)
self.assertTrue(settings.USE_DEBUG_STORAGE)

# test turning off
args = ['debugstorage', 'off']
res = CommandSC().execute(args)
self.assertTrue(res)
self.assertFalse(settings.USE_DEBUG_STORAGE)