Skip to content

Commit

Permalink
https://github.com/neo-project/neo/pull/1938
Browse files Browse the repository at this point in the history
  • Loading branch information
ixje committed Jan 18, 2021
1 parent 61e7950 commit 61a87b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
18 changes: 12 additions & 6 deletions neo3/contracts/native/nativecontract.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,10 @@ def _set_max_block_size(self, engine: contracts.ApplicationEngine, value: int) -
"""
Should only be called through syscalls
"""
if not self._check_committee(engine):
return False

if value >= message.Message.PAYLOAD_MAX_SIZE:
raise ValueError("New blocksize exceeds PAYLOAD_MAX_SIZE")

if not self._check_committee(engine):
return False

storage_key = storage.StorageKey(self.script_hash, self._PREFIX_MAX_BLOCK_SIZE)
Expand All @@ -473,6 +473,9 @@ def _set_max_transactions_per_block(self, engine: contracts.ApplicationEngine, v
"""
Should only be called through syscalls
"""
if value > 0xFFFE: # MaxTransactionsPerBlock
raise ValueError("New value exceeds MAX_TRANSACTIONS_PER_BLOCK")

if not self._check_committee(engine):
return False

Expand All @@ -492,13 +495,13 @@ def _set_max_block_system_fee(self, engine: contracts.ApplicationEngine, value:
"""
Should only be called through syscalls
"""
if not self._check_committee(engine):
return False

# unknown magic value
if value <= 4007600:
return False

if not self._check_committee(engine):
return False

storage_key = storage.StorageKey(self.script_hash, self._PREFIX_MAX_BLOCK_SYSTEM_FEE)
storage_item = engine.snapshot.storages.try_get(
storage_key,
Expand All @@ -515,6 +518,9 @@ def _set_fee_per_byte(self, engine: contracts.ApplicationEngine, value: int) ->
"""
Should only be called through syscalls
"""
if value < 0 or value > 100000000:
raise ValueError("New value exceeds FEE_PER_BYTE limits")

if not self._check_committee(engine):
return False

Expand Down
24 changes: 14 additions & 10 deletions tests/contracts/interop/test_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def test_policy_limit_setters(self):
policy = contracts.PolicyContract()
D = namedtuple('D', ['test_func', 'value', 'expected_return', 'storage_prefix'])
testdata = [
D(policy._set_max_block_size, message.Message.PAYLOAD_MAX_SIZE, False, policy._PREFIX_MAX_BLOCK_SIZE),
D(policy._set_max_block_size, message.Message.PAYLOAD_MAX_SIZE +1, ValueError, policy._PREFIX_MAX_BLOCK_SIZE),
D(policy._set_max_block_size, 123, True, policy._PREFIX_MAX_BLOCK_SIZE),
D(policy._set_max_transactions_per_block, 123, True, policy._PREFIX_MAX_TRANSACTIONS_PER_BLOCK),
D(policy._set_max_block_system_fee, 123, False, policy._PREFIX_MAX_BLOCK_SYSTEM_FEE),
Expand All @@ -271,11 +271,15 @@ def test_policy_limit_setters(self):
engine.script_container.script_hashes = [script_hash]

for d in testdata:
self.assertEqual(d.expected_return, d.test_func(engine, d.value))
if d.expected_return is True:
item = engine.snapshot.storages.try_get(storage.StorageKey(policy.script_hash, d.storage_prefix))
self.assertIsNotNone(item)
self.assertEqual(d.value, int.from_bytes(item.value, 'little'))
if isinstance(d.expected_return, type) and issubclass(d.expected_return, Exception):
with self.assertRaises(d.expected_return):
d.test_func(engine, d.value)
else:
self.assertEqual(d.expected_return, d.test_func(engine, d.value))
if d.expected_return is True:
item = engine.snapshot.storages.try_get(storage.StorageKey(policy.script_hash, d.storage_prefix))
self.assertIsNotNone(item)
self.assertEqual(d.value, int.from_bytes(item.value, 'little'))

def test_policy_setters_fail_without_signatures(self):
# cover set functions where check_committee fails
Expand All @@ -286,10 +290,10 @@ def test_policy_setters_fail_without_signatures(self):
engine.invoke_syscall_by_name("Neo.Native.Deploy")
engine.script_container = TestIVerifiable()

self.assertFalse(policy._set_max_block_size(engine, None))
self.assertFalse(policy._set_max_transactions_per_block(engine, None))
self.assertFalse(policy._set_max_block_system_fee(engine, None))
self.assertFalse(policy._set_fee_per_byte(engine, None))
self.assertFalse(policy._set_max_block_size(engine, 0))
self.assertFalse(policy._set_max_transactions_per_block(engine, 0))
self.assertFalse(policy._set_max_block_system_fee(engine, 0))
self.assertFalse(policy._set_fee_per_byte(engine, 0))
self.assertFalse(policy._block_account(engine, None))
self.assertFalse(policy._unblock_account(engine, None))

Expand Down

0 comments on commit 61a87b3

Please sign in to comment.