def test_Runtime(self): dispatched_events = [] dispatched_logs = [] def on_notif(evt): dispatched_events.append(evt) def on_log(evt): dispatched_logs.append(evt) events.on(SmartContractEvent.RUNTIME_NOTIFY, on_notif) events.on(SmartContractEvent.RUNTIME_LOG, on_log) output = Compiler.instance().load( '%s/boa_test/example/blockchain/RuntimeTest.py' % TestContract.dirname).default out = output.write() tx, results, total_ops, engine = TestBuild(out, ['get_time', 1], self.GetWallet1(), '0202', '02') self.assertEqual(len(results), 1) self.assertEqual(results[0].GetByteArray(), bytearray(b'\x01r\x04Z')) tx, results, total_ops, engine = TestBuild(out, [ 'check_witness', bytearray(b'S\xefB\xc8\xdf!^\xbeZ|z\xe8\x01\xcb\xc3\xac/\xacI)') ], self.GetWallet1(), '02', '02') self.assertEqual(len(results), 1) self.assertEqual(results[0].GetBoolean(), True) tx, results, total_ops, engine = TestBuild(out, ['log', 'hello'], self.GetWallet1(), '02', '02') self.assertEqual(len(results), 1) self.assertEqual(results[0].GetBoolean(), True) self.assertEqual(len(dispatched_logs), 1) self.assertEqual(dispatched_logs[0].event_payload.Value, 'hello') tx, results, total_ops, engine = TestBuild(out, ['notify', 1234], self.GetWallet1(), '02', '02') self.assertEqual(len(results), 1) self.assertEqual(results[0].GetBoolean(), True) self.assertEqual(len(dispatched_events), 1) self.assertEqual( int.from_bytes(dispatched_events[0].event_payload.Value, 'little'), 1234) tx, results, total_ops, engine = TestBuild(out, ['get_trigger', 1234], self.GetWallet1(), '02', '02') self.assertEqual(len(results), 1) self.assertEqual(results[0].GetBigInteger(), 16) events.off(SmartContractEvent.RUNTIME_NOTIFY, on_notif) events.off(SmartContractEvent.RUNTIME_LOG, on_log)
def test_Event1(self): dispatched_events = [] def on_notif(evt): dispatched_events.append(evt) events.on(SmartContractEvent.RUNTIME_NOTIFY, on_notif) output = Compiler.instance().load( '%s/boa_test/example/blockchain/EventTest.py' % TestContract.dirname).default out = output.write() tx, results, total_ops, engine = TestBuild(out, [], self.GetWallet1(), '', '07') self.assertEqual(len(results), 1) self.assertEqual(results[0].GetBigInteger(), 7) events.off(SmartContractEvent.RUNTIME_NOTIFY, on_notif) self.assertEqual(len(dispatched_events), 2) transfer_event_payload = dispatched_events[0].event_payload payload = transfer_event_payload.Value self.assertEqual(len(payload), 4) self.assertEqual(payload[0].Value, b'transfer_test') self.assertEqual(payload[1].Value, '2') self.assertEqual(payload[2].Value, '5') self.assertEqual(payload[3].Value, '7') refund_event_payload = dispatched_events[1].event_payload.Value self.assertEqual(len(refund_event_payload), 3) self.assertEqual(refund_event_payload[0].Value, b'refund') self.assertEqual(refund_event_payload[1].Value, b'me') self.assertEqual( int.from_bytes(refund_event_payload[2].Value, 'little'), 52)
def tearDown(self): events.off(SmartContractEvent.RUNTIME_NOTIFY, self.on_info_event) events.off(SmartContractEvent.RUNTIME_LOG, self.on_info_event) events.off(SmartContractEvent.EXECUTION, self.on_execution)