def context(score_db):
    context = IconScoreContext(IconScoreContextType.DIRECT)
    context.current_address = score_db.address

    ContextContainer._push_context(context)
    yield context
    ContextContainer._clear_context()
Beispiel #2
0
def context(settable_inv_container: INVContainer):
    prep_engine = PRepEngine()
    prep_engine.prep_address_converter = mock.Mock()
    inv_engine = INVEngine()
    settable_inv_container.set_inv(StepPrice(10**10))
    settable_inv_container.set_inv(StepCosts(STEP_COSTS))
    settable_inv_container.set_inv(
        MaxStepLimits({IconScoreContextType.INVOKE: 2_500_000_000}))
    settable_inv_container.set_inv(RevisionCode(Revision.THREE.value))
    inv_engine._inv_container = settable_inv_container

    IconScoreContext.engine = ContextEngine(prep=prep_engine, inv=inv_engine)
    context_factory = IconScoreContextFactory()

    block = Block(block_height=1,
                  block_hash=b"1" * 40,
                  prev_hash=b"0" * 40,
                  timestamp=0)
    context = context_factory.create(IconScoreContextType.INVOKE, block)

    step_limit = 1_000_000_000
    context.set_step_counter(step_limit)

    ContextContainer._push_context(context)
    yield context
    ContextContainer._pop_context()
Beispiel #3
0
def context():
    ctx = IconScoreContext(IconScoreContextType.DIRECT)
    block = Block(0, None, 0, None, 0)
    ctx.block = block
    ContextContainer._push_context(ctx)
    yield ctx
    ContextContainer._pop_context()
Beispiel #4
0
def context():
    context = IconScoreContext(IconScoreContextType.DIRECT)
    context.tx_batch = TransactionBatch()
    context.block_batch = BlockBatch()

    ContextContainer._push_context(context)
    yield context
    ContextContainer._pop_context()
def context():
    IconScoreContext.icon_score_deploy_engine = Mock(spec=DeployEngine)
    context = Mock(spec=IconScoreContext)
    context.attach_mock(Mock(spec=Transaction), "tx")
    context.attach_mock(Mock(spec=Block), "block")
    context.attach_mock(Mock(spec=Message), "msg")
    ContextContainer._push_context(context)
    yield context
    ContextContainer._pop_context()
Beispiel #6
0
        def intercept_invoke(*args, **kwargs):
            ContextContainer._push_context(args[0])

            context_db = inner_task._icon_service_engine._icx_context_db

            score_address = create_address(AddressPrefix.CONTRACT, b'address')
            score = SampleScore(IconScoreDatabase(score_address, context_db))

            address = create_address(AddressPrefix.EOA, b'address')
            score.SampleEvent(b'i_data', address, 10, b'data', 'text')

            ContextContainer._pop_context()
    def tearDown(self):
        self._engine = None
        ContextContainer._pop_context()
        self._icon_score_mapper.close()

        path = os.path.join(TEST_ROOT_PATH, 'tests')
        remove_path(path)
        path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH)
        remove_path(path)
        IconScoreContextUtil.validate_score_blacklist = VALIDATE_SCORE_BLACK_LIST
        IconScoreContextUtil.get_owner = GET_OWNER
        IconScoreContextUtil.get_icon_score = GET_ICON_SCORE
        IconScoreContextUtil.is_service_flag_on = IS_SERVICE_FLAG_ON
    def test_reserved_event_log(self):
        context = ContextContainer._get_context()
        context.func_type = IconScoreFuncType.READONLY

        address = Address.from_data(AddressPrefix.EOA, os.urandom(20))
        with self.assertRaises(InvalidEventLogException):
            self._mock_score.ICXTransfer(address, address, 0)
    def test_bytes_index_event(self):
        context = ContextContainer._get_context()

        data = b'0123456789abc'

        # Tests simple event emit
        self._mock_score.BytesIndexEvent(data)
        self.assertEqual(len(context.event_logs), 1)
        event_log = context.event_logs[0]
        self.assertEqual(2, len(event_log.indexed))
        self.assertEqual(0, len(event_log.data))

        logs_bloom = IconServiceEngine._generate_logs_bloom(context.event_logs)

        # Asserts whether the SCORE address is included in the bloom
        self.assert_score_address_in_bloom(logs_bloom)

        event_bloom_data = \
            int(0).to_bytes(1, DATA_BYTE_ORDER) + \
            'BytesIndexEvent(bytes)'.encode('utf-8')
        self.assertIn(event_bloom_data, logs_bloom)

        indexed_bloom_data = \
            int(1).to_bytes(1, DATA_BYTE_ORDER) + data
        self.assertIn(indexed_bloom_data, logs_bloom)
    def test_address_index_event(self):
        context = ContextContainer._get_context()

        address = Address.from_data(AddressPrefix.EOA, os.urandom(20))

        # Tests simple event emit
        self._mock_score.AddressIndexEvent(address)
        self.assertEqual(1, len(context.event_logs))
        event_log = context.event_logs[0]
        self.assertEqual(2, len(event_log.indexed))
        self.assertEqual(0, len(event_log.data))

        logs_bloom = IconServiceEngine._generate_logs_bloom(context.event_logs)

        # Asserts whether the SCORE address is included in the bloom
        self.assert_score_address_in_bloom(logs_bloom)

        event_bloom_data = \
            int(0).to_bytes(1, DATA_BYTE_ORDER) + \
            'AddressIndexEvent(Address)'.encode('utf-8')
        self.assertIn(event_bloom_data, logs_bloom)

        indexed_bloom_data = int(1).to_bytes(1, DATA_BYTE_ORDER) + \
                             address.prefix.value.to_bytes(1, DATA_BYTE_ORDER) + address.body
        self.assertEqual(ICON_ADDRESS_BYTES_SIZE + 1, len(indexed_bloom_data))
        self.assertIn(indexed_bloom_data, logs_bloom)
def context():
    ctx = IconScoreContext(IconScoreContextType.DIRECT)
    ctx.tx = Transaction(tx_hash=create_tx_hash(), origin=EOA1)
    ctx.block = Block(block_height=0, block_hash=create_block_hash(), timestamp=0, prev_hash=None)
    ctx.msg = Message(sender=EOA1, value=0)
    ctx.icon_score_mapper = IconScoreMapper()
    ctx.new_icon_score_mapper = {}
    ctx.step_counter = IconScoreStepCounter(1, {}, 1000, False)
    ctx.event_logs = []
    ctx.traces = []
    ctx.current_address = EOA1
    IconScoreContext.storage = ContextStorage(deploy=Mock(spec=Storage), fee=None, icx=None, iiss=None, prep=None,
                                              issue=None, meta=None, rc=None, inv=None)

    ContextContainer._push_context(ctx)
    yield ctx
    ContextContainer._pop_context()
    def make_context(self):
        self._tx_index += 1
        self._context = IconScoreContext(IconScoreContextType.DIRECT)
        self._context.msg = Message(self.from_address, 0)

        tx_hash = create_tx_hash()
        self._context.new_icon_score_mapper = IconScoreMapper()
        self._context.tx = Transaction(tx_hash,
                                       origin=self.from_address,
                                       timestamp=randrange(1, 1000),
                                       nonce=randrange(1, 1000))
        self._context.block = Block(1, create_block_hash(), 0, None, 0)
        self._context.icon_score_mapper = self._icon_score_mapper
        self._context.icx = IcxEngine()
        self._context.icx.open(self._icx_storage)
        ContextContainer._push_context(self._context)
        self._context.validate_score_blacklist = Mock()
        self._context.is_service_flag_on = Mock(return_value=False)
    def test_icx_transfer_event(self):
        context = ContextContainer._get_context()

        address = Address.from_data(AddressPrefix.EOA, os.urandom(20))

        # Tests simple event emit
        self._mock_score.icx.send(address, 1)
        self.assertEqual(len(context.event_logs), 1)
        event_log = context.event_logs[0]
        self.assertEqual(4, len(event_log.indexed))
        self.assertEqual(ICX_TRANSFER_EVENT_LOG, event_log.indexed[0])
        self.assertEqual(0, len(event_log.data))
    def test_revert(self, mocker):
        mocker.patch.object(IconServiceEngine, "_charge_transaction_fee")
        mocker.patch.object(IconScoreEngine, "invoke")

        context = ContextContainer._get_context()

        icon_service_engine = IconServiceEngine()
        icon_service_engine._icx_engine = Mock(spec=IcxEngine)
        icon_service_engine._icon_score_deploy_engine = \
            Mock(spec=DeployEngine)

        icon_service_engine._icon_pre_validator = Mock(spec=IconPreValidator)
        context.tx_batch = TransactionBatch()
        context.clear_batch = Mock()
        context.update_batch = Mock()

        from_ = Address.from_data(AddressPrefix.EOA, os.urandom(20))
        to_ = Address.from_data(AddressPrefix.CONTRACT, os.urandom(20))
        tx_index = randrange(0, 100)
        context.tx = Transaction(os.urandom(32), tx_index, from_, 0)
        context.msg = Message(from_)

        def intercept_charge_transaction_fee(*args, **kwargs):
            return {}, Mock(spec=int)

        IconServiceEngine._charge_transaction_fee.side_effect = \
            intercept_charge_transaction_fee

        icon_service_engine._icon_score_deploy_engine.attach_mock(
            Mock(return_value=False), 'is_data_type_supported')

        reason = Mock(spec=str)
        code = ExceptionCode.SCORE_ERROR
        mock_revert = Mock(side_effect=IconScoreException(reason))
        IconScoreEngine.invoke.side_effect = mock_revert

        raise_exception_start_tag("test_revert")
        tx_result = icon_service_engine._handle_icx_send_transaction(
            context, {
                'version': 3,
                'from': from_,
                'to': to_
            })
        raise_exception_end_tag("test_revert")
        assert tx_result.status == 0

        IconServiceEngine._charge_transaction_fee.assert_called()
        context.traces.append.assert_called()
        trace = context.traces.append.call_args[0][0]
        assert trace.trace == TraceType.REVERT
        assert trace.data[0] == code
        assert trace.data[1] == reason
    def setUp(self):
        address = Address.from_data(AddressPrefix.CONTRACT, os.urandom(20))
        db = Mock(spec=IconScoreDatabase)
        db.attach_mock(address, 'address')
        context = IconScoreContext()
        traces = Mock(spec=list)
        step_counter = Mock(spec=IconScoreStepCounter)

        IconScoreContext.engine = ContextEngine(
            icx=Mock(IcxEngine),
            deploy=Mock(DeployEngine),
            fee=None,
            iiss=None,
            prep=None,
            issue=None,
        )
        IconScoreContext.storage = ContextStorage(icx=Mock(IcxStorage),
                                                  deploy=Mock(DeployStorage),
                                                  fee=None,
                                                  iiss=None,
                                                  prep=None,
                                                  issue=None,
                                                  rc=None,
                                                  meta=None)

        IconScoreContext.icx_engine = Mock(spec=IcxEngine)
        context.type = IconScoreContextType.INVOKE
        context.func_type = IconScoreFuncType.WRITABLE
        context.tx_batch = TransactionBatch()
        context.event_logs = []
        context.traces = traces
        context.step_counter = step_counter
        context.get_owner = Mock()
        ContextContainer._push_context(context)

        self._mock_score = EventlogScore(db)
    def test_interface_call(self, mapped_test_score):
        context = ContextContainer._get_context()
        score_address = Mock(spec=Address)
        to_ = Mock(spec=Address)
        amount = 100

        mapped_test_score.test_interface_call(score_address, to_, amount)
        context.traces.append.assert_called()
        trace = context.traces.append.call_args[0][0]

        assert trace.trace == TraceType.CALL
        assert trace.data[0] == score_address
        assert trace.data[1] == 'interfaceCall'
        assert trace.data[2][0] == to_
        assert trace.data[2][1] == amount
    def test_transfer_and_send_should_have_same_trace(self, mapped_test_score,
                                                      func_name):
        context = ContextContainer._get_context()
        context.type = IconScoreContextType.INVOKE
        to_ = create_address(AddressPrefix.EOA)
        amount = 100

        # Call send or transfer method
        func = getattr(mapped_test_score.icx, func_name)
        func(to_, amount)

        context.traces.append.assert_called()
        trace = context.traces.append.call_args[0][0]
        assert trace.trace == TraceType.CALL
        assert trace.data[0] == to_
        assert trace.data[3] == amount
    def test_call(self, mapped_test_score):
        context = ContextContainer._get_context()
        score_address = Mock(spec=Address)
        func_name = "testCall"
        to_ = Mock(spec=Address)
        amount = 100
        params = {'to': to_, 'amount': amount}

        mapped_test_score.call(score_address, func_name, params)
        context.traces.append.assert_called()
        trace = context.traces.append.call_args[0][0]
        assert trace.trace == TraceType.CALL
        assert trace.data[0] == score_address
        assert trace.data[1] == func_name
        assert trace.data[2][0] == params['to']
        assert trace.data[2][1] == params['amount']
    def test_to_dict_camel(self, mapped_test_score):
        context = ContextContainer._get_context()
        score_address = Mock(spec=Address)
        func_name = "testCall"
        to_ = Mock(spec=Address)
        amount = 100
        params = {'to': to_, 'amount': amount}

        mapped_test_score.call(score_address, func_name, params)
        context.traces.append.assert_called()
        trace = context.traces.append.call_args[0][0]
        camel_dict = trace.to_dict(to_camel_case)
        assert 'scoreAddress' in camel_dict
        assert 'trace' in camel_dict
        assert 'data' in camel_dict
        assert TraceType.CALL.name == camel_dict['trace']
        assert 4 == len(camel_dict['data'])
    def test_to_dict_camel(self):
        context = ContextContainer._get_context()

        address = Address.from_data(AddressPrefix.EOA, os.urandom(20))
        age = 10
        data = b'0123456789abc'

        self._mock_score.MixedEvent(b'i_data', address, age, data, 'text')
        self.assertEqual(len(context.event_logs), 1)

        event_log = context.event_logs[0]

        camel_dict = event_log.to_dict(to_camel_case)
        self.assertIn('scoreAddress', camel_dict)
        self.assertIn('indexed', camel_dict)
        self.assertIn('data', camel_dict)
        self.assertEqual(3, len(camel_dict['indexed']))
        self.assertEqual(3, len(camel_dict['data']))
    def test_call_event(self):
        context = ContextContainer._get_context()

        name = "name"
        address = Address.from_data(AddressPrefix.EOA, os.urandom(20))
        age = 10
        phone_number = "000"

        # Tests simple event emit
        self._mock_score.ZeroIndexEvent(name, address, age)
        self.assertEqual(len(context.event_logs), 1)
        event_log = context.event_logs[0]
        self.assertEqual(1, len(event_log.indexed))
        self.assertEqual(3, len(event_log.data))

        # This event has a indexed parameter,
        # so the list of indexed Should have 2 items
        self._mock_score.OneIndexEvent(name, address, age)
        self.assertEqual(len(context.event_logs), 2)
        event_log = context.event_logs[1]
        self.assertEqual(2, len(event_log.indexed))
        self.assertEqual(2, len(event_log.data))

        logs_bloom = IconServiceEngine._generate_logs_bloom(context.event_logs)

        # Asserts whether the SCORE address is included in the bloom
        self.assert_score_address_in_bloom(logs_bloom)

        zero_event_bloom_data = \
            int(0).to_bytes(1, DATA_BYTE_ORDER) + \
            'ZeroIndexEvent(str,Address,int)'.encode('utf-8')
        self.assertIn(zero_event_bloom_data, logs_bloom)

        one_event_bloom_data = \
            int(0).to_bytes(1, DATA_BYTE_ORDER) + \
            'OneIndexEvent(str,Address,int)'.encode('utf-8')
        self.assertIn(one_event_bloom_data, logs_bloom)

        name_bloom_data = int(1).to_bytes(
            1, DATA_BYTE_ORDER) + name.encode('utf-8')
        self.assertIn(name_bloom_data, logs_bloom)
    def test_call_event_kwarg(self):
        context = ContextContainer._get_context()

        name = "name"
        address = Address.from_data(AddressPrefix.EOA, os.urandom(20))
        age = 10

        # Call with ordered arguments
        self._mock_score.OneIndexEvent(name, address, age)
        self.assertEqual(len(context.event_logs), 1)
        event_log_ordered_args = context.event_logs[0]

        # Call with ordered arguments and keyword arguments
        self._mock_score.OneIndexEvent(name, age=age, address=address)
        self.assertEqual(len(context.event_logs), 2)
        event_log_keyword_args = context.event_logs[1]

        self.assertEqual(event_log_ordered_args.score_address,
                         event_log_keyword_args.score_address)
        self.assertEqual(event_log_ordered_args.indexed,
                         event_log_keyword_args.indexed)
        self.assertEqual(event_log_ordered_args.data,
                         event_log_keyword_args.data)

        logs_bloom = IconServiceEngine._generate_logs_bloom(context.event_logs)

        # Asserts whether the SCORE address is included in the bloom
        self.assert_score_address_in_bloom(logs_bloom)

        one_event_bloom_data = \
            int(0).to_bytes(1, DATA_BYTE_ORDER) + \
            'OneIndexEvent(str,Address,int)'.encode('utf-8')
        self.assertIn(one_event_bloom_data, logs_bloom)

        name_bloom_data = int(1).to_bytes(
            1, DATA_BYTE_ORDER) + name.encode('utf-8')
        self.assertIn(name_bloom_data, logs_bloom)
    def test_call_event_mismatch_arg(self):
        context = ContextContainer._get_context()

        name = "name"
        address = Address.from_data(AddressPrefix.EOA, os.urandom(20))
        age = "10"
        # The hint of 'age' is int type but argument is str type

        self.assertRaises(InvalidEventLogException,
                          self._mock_score.OneIndexEvent, name, address, age)

        logs_bloom = IconServiceEngine._generate_logs_bloom(context.event_logs)

        # Asserts whether the SCORE address is not included in the bloom
        self.assert_score_address_not_in_bloom(logs_bloom)

        one_event_bloom_data = \
            int(0).to_bytes(1, DATA_BYTE_ORDER) + \
            'OneIndexEvent(str,Address,int)'.encode('utf-8')
        self.assertNotIn(one_event_bloom_data, logs_bloom)

        name_bloom_data = int(1).to_bytes(
            1, DATA_BYTE_ORDER) + name.encode('utf-8')
        self.assertNotIn(name_bloom_data, logs_bloom)
def set_container_and_intercall_before_test(monkeypatch, context):
    monkeypatch.setattr(InternalCall, "_other_score_call", Mock())
    ContextContainer._push_context(context)
    yield
    ContextContainer._clear_context()
    monkeypatch.undo()
Beispiel #25
0
 def tearDown(self):
     ContextContainer._clear_context()
     clear_inner_task()
Beispiel #26
0
 def tearDown(self):
     ContextContainer._clear_context()
     clear_inner_task()
     VirtualStepCalculator.calculate_virtual_step = calculate_virtual_step
    def test_event_log_on_readonly_method(self):
        context = ContextContainer._get_context()
        context.func_type = IconScoreFuncType.READONLY

        with self.assertRaises(InvalidEventLogException):
            self._mock_score.BoolIndexEvent(False)
 def tearDown(self):
     ContextContainer._clear_context()
     self._mock_icon_score = None