Skip to content

Commit

Permalink
limit notifications neo-project/neo#2747 (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
ixje authored Jul 5, 2022
1 parent 93e9d3d commit fce8862
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
4 changes: 3 additions & 1 deletion neo3/contracts/applicationengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self,
self.gas_amount = self.GAS_FREE + gas
self._invocation_counter: Dict[types.UInt160, int] = {}
#: Notifications (Notify SYSCALLs) that occured while executing the script.
self.notifications: List[Tuple[payloads.IVerifiable, types.UInt160, bytes, vm.ArrayStackItem]] = []
self.notifications: List[Tuple[payloads.IVerifiable, types.UInt160, str, vm.ArrayStackItem]] = []
if self.snapshot is None or self.snapshot.persisting_block is None or self.snapshot.persisting_block.index == 0:
self.exec_fee_factor = contracts.PolicyContract().DEFAULT_EXEC_FEE_FACTOR
self.storage_price = contracts.PolicyContract().DEFAULT_STORAGE_PRICE
Expand Down Expand Up @@ -324,6 +324,8 @@ def context_unloaded(self, ctx: vm.ExecutionContext):
self.notifications = self.notifications[:-ctx.notification_count]

def _send_notification(self, contract_hash: types.UInt160, event_name: str, state: vm.ArrayStackItem) -> None:
_state = cast(vm.ArrayStackItem, state.deep_copy(True))
self.notifications.append((self.script_container, contract_hash, event_name, _state))
msgrouter.interop_notify(contract_hash, event_name, state)
self.current_context.notification_count += 1

Expand Down
3 changes: 1 addition & 2 deletions neo3/contracts/interop/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ def do_notify(engine: contracts.ApplicationEngine, event_name: bytes, state: vm.
f"Notify event name length ({len(event_name)}) exceeds maximum allowed ({engine.MAX_EVENT_SIZE})")
# will validate size + cyclic references
contracts.BinarySerializer.serialize(state, engine.MAX_NOTIFICATION_SIZE)
engine.notifications.append((engine.script_container, engine.current_scripthash, event_name, state))
msgrouter.interop_notify(engine.current_scripthash, event_name.decode('utf-8'), state)
engine._send_notification(engine.current_scripthash, event_name.decode('utf-8'), state)


@register("System.Runtime.GetNotifications", 1 << 12, contracts.CallFlags.NONE)
Expand Down
2 changes: 1 addition & 1 deletion neo3/contracts/native/designate.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def designate_as_role(self,
state = vm.ArrayStackItem()
state.append(vm.IntegerStackItem(role.value))
state.append(vm.IntegerStackItem(engine.snapshot.persisting_block.index))
msgrouter.interop_notify(self.hash, "Designation", state)
engine._send_notification(self.hash, "Designation", state)

def _to_uint32(self, value: int) -> bytes:
return struct.pack(">I", value)
2 changes: 1 addition & 1 deletion neo3/contracts/native/fungible.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def _post_transfer(self,
state.append(vm.ByteStringStackItem(account_to.to_array()))
state.append(vm.IntegerStackItem(amount))

msgrouter.interop_notify(self.hash, "Transfer", state)
engine._send_notification(self.hash, "Transfer", state)

# wallet or smart contract
if not call_on_payment \
Expand Down
24 changes: 12 additions & 12 deletions neo3/contracts/native/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ def contract_create_with_data(self,
if method_descriptor is not None:
engine.call_from_native(self.hash, hash_, method_descriptor.name, [data, vm.BooleanStackItem(False)])

msgrouter.interop_notify(self.hash,
"Deploy",
vm.ArrayStackItem(vm.ByteStringStackItem(contract.hash.to_array()))
)
engine._send_notification(self.hash,
"Deploy",
vm.ArrayStackItem(vm.ByteStringStackItem(contract.hash.to_array()))
)
return contract

@register("update", contracts.CallFlags.ALL)
Expand Down Expand Up @@ -167,10 +167,10 @@ def contract_update_with_data(self,
method_descriptor.name,
[data, vm.BooleanStackItem(True)])

msgrouter.interop_notify(self.hash,
"Update",
vm.ArrayStackItem(vm.ByteStringStackItem(contract.hash.to_array()))
)
engine._send_notification(self.hash,
"Update",
vm.ArrayStackItem(vm.ByteStringStackItem(contract.hash.to_array()))
)

@register("destroy", contracts.CallFlags.STATES | contracts.CallFlags.ALLOW_NOTIFY, cpu_price=1 << 15)
def contract_destroy(self, engine: contracts.ApplicationEngine) -> None:
Expand All @@ -187,10 +187,10 @@ def contract_destroy(self, engine: contracts.ApplicationEngine) -> None:

contracts.PolicyContract()._block_account_internal(engine.snapshot, hash_)

msgrouter.interop_notify(self.hash,
"Destroy",
vm.ArrayStackItem(vm.ByteStringStackItem(contract.hash.to_array()))
)
engine._send_notification(self.hash,
"Destroy",
vm.ArrayStackItem(vm.ByteStringStackItem(contract.hash.to_array()))
)

@register("getMinimumDeploymentFee", contracts.CallFlags.READ_STATES, cpu_price=1 << 15)
def get_minimum_deployment_fee(self, snapshot: storage.Snapshot) -> int:
Expand Down
4 changes: 2 additions & 2 deletions neo3/contracts/native/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def finish(self, engine: contracts.ApplicationEngine) -> None:
]
)

msgrouter.interop_notify(self.hash, "OracleResponse", state)
engine._send_notification(self.hash, "OracleResponse", state)

user_data = contracts.BinarySerializer.deserialize(request.user_data,
engine.MAX_STACK_SIZE)
Expand Down Expand Up @@ -197,7 +197,7 @@ def _request(self,
]
)

msgrouter.interop_notify(self.hash, "OracleRequest", state)
engine._send_notification(self.hash, "OracleRequest", state)

@register("verify", contracts.CallFlags.READ_ONLY, cpu_price=1 << 15)
def _verify(self, engine: contracts.ApplicationEngine) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion tests/contracts/interop/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def runtime_notify(script_hash: types.UInt160, msg: str, state: vm.ArrayStackIte
item = engine.notifications[0]
self.assertIsNone(item[0]) # == engine.script_container
self.assertEqual(engine.current_scripthash, item[1])
self.assertEqual(expected_message, item[2].decode())
self.assertEqual(expected_message, item[2])

def test_runtime_notify_exceed_size(self):
engine = test_engine()
Expand Down

0 comments on commit fce8862

Please sign in to comment.