Ejemplo n.º 1
0
    def test_sample_result(self):
        from_ = Address.from_data(AddressPrefix.EOA, b'from')
        to_ = Address.from_data(AddressPrefix.CONTRACT, b'to')
        self._mock_context.tx = Transaction(os.urandom(32), 1234, from_, to_,
                                            0)
        self._mock_context.msg = Message(from_)

        IconScoreContext.engine.deploy.invoke = Mock()
        self._icon_service_engine._process_transaction = Mock(return_value=to_)

        tx_result = self._icon_service_engine._handle_icx_send_transaction(
            self._mock_context, {
                'from': from_,
                'to': to_
            })

        tx_result.score_address = to_
        tx_result.event_logs = [
            EventLog(
                Address.from_data(AddressPrefix.CONTRACT, b'addr_to'),
                [b'indexed',
                 Address.from_data(AddressPrefix.EOA, b'index')],
                [True, 1234, 'str', None, b'test'])
        ]
        tx_result.logs_bloom = BloomFilter()
        tx_result.logs_bloom.add(b'1')
        tx_result.logs_bloom.add(b'2')
        tx_result.logs_bloom.add(b'3')
        tx_result.block = Block(123,
                                hashlib.sha3_256(b'block').digest(), 1, None,
                                0)

        camel_dict = tx_result.to_dict(to_camel_case)

        self.assertIn('txHash', camel_dict)
        self.assertIn('blockHeight', camel_dict)
        self.assertIn('txIndex', camel_dict)
        self.assertIn('to', camel_dict)
        self.assertIn('scoreAddress', camel_dict)
        self.assertIn('stepUsed', camel_dict)
        self.assertIn('stepPrice', camel_dict)
        self.assertIn('eventLogs', camel_dict)
        self.assertIn('logsBloom', camel_dict)
        self.assertIn('status', camel_dict)
        self.assertEqual(1, len(camel_dict['eventLogs']))
        self.assertIn('scoreAddress', camel_dict['eventLogs'][0])
        self.assertIn('indexed', camel_dict['eventLogs'][0])
        self.assertIn('data', camel_dict['eventLogs'][0])
        self.assertEqual(256, len(camel_dict['logsBloom']))

        converted_result = TypeConverter.convert_type_reverse(camel_dict)
        self.assertFalse(converted_result['txHash'].startswith('0x'))
        self.assertTrue(converted_result['blockHeight'].startswith('0x'))
        self.assertTrue(converted_result['txIndex'].startswith('0x'))
        self.assertTrue(converted_result['to'].startswith('cx'))
        self.assertTrue(converted_result['scoreAddress'].startswith('cx'))
        self.assertTrue(converted_result['stepUsed'].startswith('0x'))
        self.assertTrue(converted_result['logsBloom'].startswith('0x'))
        self.assertTrue(converted_result['status'].startswith('0x'))
Ejemplo n.º 2
0
    def test_sample_result(self):
        from_ = Address.from_data(AddressPrefix.EOA, b'from')
        to_ = Address.from_data(AddressPrefix.CONTRACT, b'to')
        self._mock_context.tx.index = 1234
        self._mock_context.tx.hash = hashlib.sha256(b'hash').digest()
        self._icon_service_engine._icon_score_deploy_engine.attach_mock(
            Mock(return_value=False), 'is_data_type_supported')

        tx_result = self._icon_service_engine._handle_icx_send_transaction(
            self._mock_context, {'from': from_, 'to': to_})

        tx_result.score_address = \
            Address.from_data(AddressPrefix.CONTRACT, b'score_address')
        tx_result.event_logs = [
            EventLog(
                Address.from_data(AddressPrefix.CONTRACT, b'addr_to'),
                [b'indexed', Address.from_data(AddressPrefix.EOA, b'index')],
                [True, 1234, 'str', None, b'test']
            )
        ]
        tx_result.logs_bloom = BloomFilter()
        tx_result.logs_bloom.add(b'1')
        tx_result.logs_bloom.add(b'2')
        tx_result.logs_bloom.add(b'3')
        tx_result.block = Block(123, hashlib.sha256(b'block').digest(), 1, None)

        camel_dict = tx_result.to_dict(to_camel_case)

        self.assertIn('txHash', camel_dict)
        self.assertIn('blockHeight', camel_dict)
        self.assertIn('txIndex', camel_dict)
        self.assertIn('to', camel_dict)
        self.assertIn('scoreAddress', camel_dict)
        self.assertIn('stepUsed', camel_dict)
        self.assertIn('stepPrice', camel_dict)
        self.assertIn('eventLogs', camel_dict)
        self.assertIn('logsBloom', camel_dict)
        self.assertIn('status', camel_dict)
        self.assertEqual(1, len(camel_dict['eventLogs']))
        self.assertIn('scoreAddress', camel_dict['eventLogs'][0])
        self.assertIn('indexed', camel_dict['eventLogs'][0])
        self.assertIn('data', camel_dict['eventLogs'][0])
        self.assertEqual(256, len(camel_dict['logsBloom']))

        converted_result = TypeConverter.convert_type_reverse(camel_dict)
        self.assertFalse(converted_result['txHash'].startswith('0x'))
        self.assertTrue(converted_result['blockHeight'].startswith('0x'))
        self.assertTrue(converted_result['txIndex'].startswith('0x'))
        self.assertTrue(converted_result['to'].startswith('cx'))
        self.assertTrue(converted_result['scoreAddress'].startswith('cx'))
        self.assertTrue(converted_result['stepUsed'].startswith('0x'))
        self.assertTrue(converted_result['logsBloom'].startswith('0x'))
        self.assertTrue(converted_result['status'].startswith('0x'))
 def _data(score_address, indexed, data):
     return EventLog(score_address, indexed, data)