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

Commit

Permalink
Merge 0ee313d into 48e42d2
Browse files Browse the repository at this point in the history
  • Loading branch information
belane committed Aug 9, 2018
2 parents 48e42d2 + 0ee313d commit c793d89
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project are documented in this file.

[0.7.7-dev] in progress
------------------------
- Adds support for ``IsPayable`` flag in prompt.
- Fix Block header problems with ``block_import.py`` script


Expand Down
11 changes: 11 additions & 0 deletions neo/Core/FunctionCode.py
Expand Up @@ -41,6 +41,17 @@ def HasDynamicInvoke(self):
from neo.Core.State.ContractState import ContractPropertyState
return self.ContractProperties & ContractPropertyState.HasDynamicInvoke > 0

@property
def IsPayable(self):
"""
Flag indicating if the contract accepts payments.
Returns:
bool: True if supported. False otherwise.
"""
from neo.Core.State.ContractState import ContractPropertyState
return self.ContractProperties & ContractPropertyState.Payable > 0

def __init__(self, script=None, param_list=None, return_type=255, contract_properties=0):
self.Script = script
if param_list is None:
Expand Down
3 changes: 2 additions & 1 deletion neo/Core/State/ContractState.py
Expand Up @@ -203,7 +203,8 @@ def ToJson(self):
'description': self.Description.decode('utf-8'),
'properties': {
'storage': self.HasStorage,
'dynamic_invoke': self.HasDynamicInvoke
'dynamic_invoke': self.HasDynamicInvoke,
'payable': self.Payable
}
}

Expand Down
2 changes: 1 addition & 1 deletion neo/Prompt/Commands/BuildNRun.py
Expand Up @@ -71,7 +71,7 @@ def DoRun(contract_script, arguments, wallet, path, verbose=True,
if wallet is not None:

f_args = arguments[2:]
i_args = arguments[6:]
i_args = arguments[7:]

script = GatherLoadedContractParams(f_args, contract_script)

Expand Down
17 changes: 13 additions & 4 deletions neo/Prompt/Commands/LoadSmartContract.py
Expand Up @@ -57,8 +57,8 @@ def ImportContractAddr(wallet, args):


def LoadContract(args):
if len(args) < 5:
print("please specify contract to load like such: 'import contract {path} {params} {return_type} {needs_storage} {needs_dynamic_invoke}'")
if len(args) < 6:
print("please specify contract to load like such: 'import contract {path} {params} {return_type} {needs_storage} {needs_dynamic_invoke} {is_payable}'")
return

path = args[0]
Expand All @@ -71,6 +71,7 @@ def LoadContract(args):

needs_storage = bool(parse_param(args[3]))
needs_dynamic_invoke = bool(parse_param(args[4]))
is_payable = bool(parse_param(args[5]))

contract_properties = 0

Expand All @@ -80,6 +81,9 @@ def LoadContract(args):
if needs_dynamic_invoke:
contract_properties += ContractPropertyState.HasDynamicInvoke

if is_payable:
contract_properties += ContractPropertyState.Payable

script = None

if '.py' in path:
Expand Down Expand Up @@ -114,8 +118,8 @@ def LoadContract(args):


def GatherLoadedContractParams(args, script):
if len(args) < 4:
raise Exception("please specify contract properties like {params} {return_type} {needs_storage} {needs_dynamic_invoke}")
if len(args) < 5:
raise Exception("please specify contract properties like {params} {return_type} {needs_storage} {needs_dynamic_invoke} {is_payable}")
params = parse_param(args[0], ignore_int=True, prefer_hex=False)

if type(params) is str:
Expand All @@ -125,6 +129,7 @@ def GatherLoadedContractParams(args, script):

needs_storage = bool(parse_param(args[2]))
needs_dynamic_invoke = bool(parse_param(args[3]))
is_payable = bool(parse_param(args[4]))

contract_properties = 0

Expand All @@ -134,6 +139,9 @@ def GatherLoadedContractParams(args, script):
if needs_dynamic_invoke:
contract_properties += ContractPropertyState.HasDynamicInvoke

if is_payable:
contract_properties += ContractPropertyState.Payable

out = generate_deploy_script(script, contract_properties=contract_properties, return_type=return_type, parameter_list=params)

return out
Expand Down Expand Up @@ -162,6 +170,7 @@ def GatherContractDetails(function_code):
print(" Description: %s " % description)
print(" Needs Storage: %s " % function_code.HasStorage)
print(" Needs Dynamic Invoke: %s " % function_code.HasDynamicInvoke)
print(" Is Payable: %s " % function_code.IsPayable)
print(json.dumps(function_code.ToJson(), indent=4))

return generate_deploy_script(function_code.Script, name, version, author, email, description,
Expand Down
Binary file added neo/SmartContract/tests/PayableTest.avm
Binary file not shown.
4 changes: 2 additions & 2 deletions neo/SmartContract/tests/test_breakpoints.py
Expand Up @@ -41,7 +41,7 @@ def test_debug_contract_1(self):

wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/BreakpointTest.py", "test", "02", "01", "True", "False", "1", ]
arguments = ["neo/SmartContract/tests/BreakpointTest.py", "test", "02", "01", "True", "False", "True", "1", ]
dbg = VMDebugger
# dbg.end = MagicMock(return_value=None)
dbg.start = MagicMock(return_value=None)
Expand All @@ -60,7 +60,7 @@ def test_debug_contract_1(self):
def test_debug_contract_2(self):
wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/BreakpointTest.py", "test", "02", "01", "True", "False", "4", ]
arguments = ["neo/SmartContract/tests/BreakpointTest.py", "test", "02", "01", "True", "False", "True", "4", ]
dbg = VMDebugger
# dbg.end = MagicMock(return_value=None)
dbg.start = MagicMock(return_value=None)
Expand Down
12 changes: 6 additions & 6 deletions neo/SmartContract/tests/test_gas_costs.py
Expand Up @@ -34,7 +34,7 @@ def test_build_contract(self):

wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put", "key1", "b'ab'", "--from-addr=" + self.wallet_1_addr]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "True", "put", "key1", "b'ab'", "--from-addr=" + self.wallet_1_addr]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False, min_fee=Fixed8.FromDecimal(.0004))

Expand All @@ -54,7 +54,7 @@ def test_build_contract_2(self):

wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put_5", "key1", "b'abababababab'"]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "True", "put_5", "key1", "b'abababababab'"]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False)

Expand All @@ -74,7 +74,7 @@ def test_build_contract_3(self):
"""
wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put_and_get", "key1", "b'abababababab'"]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "True", "put_and_get", "key1", "b'abababababab'"]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False)

Expand All @@ -94,7 +94,7 @@ def test_build_contract_4(self):
"""
wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put_and_get", "key1", self.big_str]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "True", "put_and_get", "key1", self.big_str]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False)

Expand Down Expand Up @@ -122,7 +122,7 @@ def test_build_contract_5(self):
"""
wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put_5", "key1", self.big_str]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "True", "put_5", "key1", self.big_str]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False)

Expand All @@ -139,7 +139,7 @@ def test_build_contract_6(self):
"""
wallet = self.GetWallet1()

arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "put_9", "key1", "b'ababababab'"]
arguments = ["neo/SmartContract/tests/StorageTest.py", "test", "070705", "05", "True", "False", "True", "put_9", "key1", "b'ababababab'"]

tx, result, total_ops, engine = BuildAndRun(arguments, wallet, False)

Expand Down
65 changes: 65 additions & 0 deletions neo/SmartContract/tests/test_payable.py
@@ -0,0 +1,65 @@
from neo.Utils.WalletFixtureTestCase import WalletFixtureTestCase
from neo.Implementations.Wallets.peewee.UserWallet import UserWallet
from neo.Prompt.Commands.BuildNRun import DoRun
from neo.Wallets.utils import to_aes_key
import binascii


class SmartContractPayable(WalletFixtureTestCase):

_wallet1 = None
_path = "neo/SmartContract/tests/PayableTest.avm"

@classmethod
def GetWallet1(cls, recreate=False):
if cls._wallet1 is None or recreate:
cls._wallet1 = UserWallet.Open(SmartContractPayable.wallet_1_dest(), to_aes_key(SmartContractPayable.wallet_1_pass()))
return cls._wallet1

def test_is_payable(self):
"""
Result [{'type': 'Boolean', 'value': True}]
"""

wallet = self.GetWallet1()

arguments = [self._path, "test", "07", "01", "False", "False", "True", "payable"]

with open(self._path, 'rb') as f:

content = f.read()

try:
content = binascii.unhexlify(content)
except Exception as e:
pass

script = content

tx, result, total_ops, engine = DoRun(script, arguments, wallet, self._path)

self.assertTrue(result[0].GetBoolean())

def test_is_not_payable(self):
"""
Result [{'type': 'Boolean', 'value': False}]
"""

wallet = self.GetWallet1()

arguments = [self._path, "test", "07", "01", "False", "False", "False", "payable"]

with open(self._path, 'rb') as f:

content = f.read()

try:
content = binascii.unhexlify(content)
except Exception as e:
pass

script = content

tx, result, total_ops, engine = DoRun(script, arguments, wallet, self._path)

self.assertFalse(result[0].GetBoolean())
1 change: 1 addition & 0 deletions neo/SmartContract/tests/test_smart_contract3.py
Expand Up @@ -44,6 +44,7 @@ def test_contract_create_block(self):
self.assertIsNotNone(contract_added)

self.assertEqual(contract_added.HasStorage, False)
self.assertEqual(contract_added.Payable, False)
self.assertEqual(contract_added.Name, b'test create')
self.assertEqual(contract_added.Email, b'flow@neo.org')

Expand Down
6 changes: 3 additions & 3 deletions neo/bin/prompt.py
Expand Up @@ -113,11 +113,11 @@ class PromptInterface:
'config maxpeers {num_peers}',
'config node-requests {reqsize} {queuesize}',
'config node-requests {slow/normal/fast}',
'build {path/to/file.py} (test {params} {returntype} {needs_storage} {needs_dynamic_invoke} [{test_params} or --i]) --no-parse-addr (parse address strings to script hash bytearray)',
'load_run {path/to/file.avm} (test {params} {returntype} {needs_storage} {needs_dynamic_invoke} [{test_params} or --i]) --no-parse-addr (parse address strings to script hash bytearray)',
'build {path/to/file.py} (test {params} {returntype} {needs_storage} {needs_dynamic_invoke} {is_payable} [{test_params} or --i]) --no-parse-addr (parse address strings to script hash bytearray)',
'load_run {path/to/file.avm} (test {params} {returntype} {needs_storage} {needs_dynamic_invoke} {is_payable} [{test_params} or --i]) --no-parse-addr (parse address strings to script hash bytearray)',
'import wif {wif}',
'import nep2 {nep2_encrypted_key}',
'import contract {path/to/file.avm} {params} {returntype} {needs_storage} {needs_dynamic_invoke}',
'import contract {path/to/file.avm} {params} {returntype} {needs_storage} {needs_dynamic_invoke} {is_payable}',
'import contract_addr {contract_hash} {pubkey}',
'import multisig_addr {pubkey in wallet} {minimum # of signatures required} {signing pubkey 1} {signing pubkey 2}...',
'import watch_addr {address}',
Expand Down

0 comments on commit c793d89

Please sign in to comment.