Skip to content

Commit

Permalink
CU-86dtd9az1 - It might be necessary to add notify event names on the…
Browse files Browse the repository at this point in the history
… manifest
  • Loading branch information
luc10921 committed May 16, 2024
1 parent 49731b6 commit 3912663
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
13 changes: 13 additions & 0 deletions boa3/internal/analyser/moduleanalyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from boa3.internal.model.builtin.compile_time.neometadatatype import MetadataTypeSingleton
from boa3.internal.model.builtin.decorator import ContractDecorator
from boa3.internal.model.builtin.decorator.builtindecorator import IBuiltinDecorator
from boa3.internal.model.builtin.interop.runtime import NotifyMethod
from boa3.internal.model.builtin.method.builtinmethod import IBuiltinMethod
from boa3.internal.model.callable import Callable
from boa3.internal.model.decorator import IDecorator
Expand Down Expand Up @@ -1332,6 +1333,18 @@ def visit_Call(self, call: ast.Call) -> IType | None:
if updated_symbol.identifier != func_id:
self.__include_callable(updated_symbol.identifier, updated_symbol)
return self.get_type(updated_symbol)
elif isinstance(func_symbol, NotifyMethod) and len(call.args) == 2:
if isinstance(call.args[1], ast.Constant):
notification_name = self.visit(call.args[1])
named_notification = Event(notification_name, {'state': Variable(Type.any)})
named_notification.add_call_origin(call)
self._current_module.include_symbol(named_notification.identifier, named_notification)
else:
self._log_error(
CompilerError.InvalidUsage(
call.args[1].lineno, call.args[1].col_offset,
'The notification name should be a constant, otherwise it won\'t be added to the manifest')
)

return self.get_type(call.func)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from boa3.sc.compiletime import public

from boa3.sc.runtime import notify


@public
def Main(notify_name: str):
notify(10, notify_name)
4 changes: 2 additions & 2 deletions boa3_test/test_sc/interop_test/runtime/NotifyWithName.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@


@public
def Main(notify_name: str):
notify(10, notify_name)
def Main():
notify(10, 'unit_test')
11 changes: 7 additions & 4 deletions boa3_test/tests/compiler_tests/test_interop/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,16 @@ async def test_notify_sequence_run(self):
self.assertEqual(1, len(event_notifications))
self.assertEqual(([2, 3, 5, 7],), event_notifications[0].state)

async def test_notify_with_dynamic_name_run(self):
self.assertCompilerLogs(CompilerError.InvalidUsage, 'NotifyWithDynamicName.py')

def test_notify_with_name_compile(self):
from boa3.internal.model.builtin.interop.interop import Interop
unit_test = String('unit_test').to_bytes()

expected_output = (
Opcode.INITSLOT
+ b'\x00\x01'
+ Opcode.LDARG0
Opcode.PUSHDATA1 # 'unit_test
+ Integer(len(unit_test)).to_byte_array() + unit_test
+ Opcode.PUSH10
+ Opcode.PUSH1
+ Opcode.PACK
Expand All @@ -283,7 +286,7 @@ async def test_notify_with_name_run(self):
await self.set_up_contract('NotifyWithName.py')

event_name = 'unit_test'
result, notifications = await self.call('Main', [event_name], return_type=None)
result, notifications = await self.call('Main', [], return_type=None)
self.assertIsNone(result)

event_notifications = self.filter_events(notifications,
Expand Down

0 comments on commit 3912663

Please sign in to comment.