Esempio n. 1
0
    def test_transaction_result_on_sharing_fee_user_ratio100(
            self, score_invoke):
        score_invoke.side_effect = mock_score_invoke
        IconScoreContext.engine.fee.charge_transaction_fee = \
            Mock(return_value={self.from_: 9000})
        tx_hash = bytes.hex(os.urandom(32))

        data = {
            "method": "transfer",
            "params": {
                "to": f"hx{'2'*40}",
                "amount": hex(100)
            }
        }

        request = create_request(
            [ReqData(tx_hash, self.from_, str(self.score), 0, "call", data)])
        result = self._inner_task_invoke(request)

        expected_event_log = [{
            "scoreAddress":
            str(self.score),
            "indexed":
            ['Transfer(Address,Address,int)', f"hx{'1'*40}", f"hx{'2'*40}"],
            "data": [hex(100)]
        }]

        tx_result = result['txResults'][tx_hash]

        self.assertEqual('0x1', tx_result['status'])
        self.assertEqual(expected_event_log, tx_result['eventLogs'])
        self.assertFalse(tx_result.get('detailStepUsed'))
Esempio n. 2
0
    def test_withdraw_deposit(self):
        tx_hash = os.urandom(32)
        tx_hash_hex = bytes.hex(tx_hash)
        deposit_id = self.test_add_deposit()
        amount, penalty = 4700, 300

        IconScoreContext.engine.fee.withdraw_deposit = Mock(
            return_value=(amount, penalty))
        IconScoreContext.engine.fee.charge_transaction_fee = \
            Mock(return_value={self.from_: 9000})

        data = {'action': 'withdraw', 'id': f"0x{bytes.hex(deposit_id)}"}

        expected_event_log = [{
            "scoreAddress":
            str(self.score),
            "indexed": [
                "DepositWithdrawn(bytes,Address,int,int)",
                f"0x{bytes.hex(deposit_id)}",
                str(self.from_)
            ],
            "data": [hex(amount), hex(penalty)]
        }]

        request = create_request([
            ReqData(tx_hash_hex, self.from_, self.score, 0, 'deposit', data),
        ])

        result = self._inner_task_invoke(request)
        tx_result = result['txResults'][tx_hash_hex]

        self.assertEqual('0x1', tx_result['status'])
        self.assertEqual(expected_event_log, tx_result['eventLogs'])
Esempio n. 3
0
    def test_request(self, score_invoke):
        inner_task = generate_inner_task(3)
        IconScoreContext.engine.prep.preps = Mock()
        IconScoreContext.engine.prep.prep_address_converter = Mock()

        # noinspection PyUnusedLocal
        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()

        score_invoke.side_effect = intercept_invoke

        from_ = create_address(AddressPrefix.EOA, b'from')
        to_ = create_address(AddressPrefix.CONTRACT, b'score')

        request = create_request([
            ReqData(bytes.hex(create_tx_hash(b'tx1')), from_, to_, 0, 'call', {}),
            ReqData(bytes.hex(create_tx_hash(b'tx2')), from_, to_, 0, 'call', {})
        ])
        response = inner_task._invoke(request)

        step_total = 0

        for tx_hash in response['txResults'].keys():
            result = response['txResults'][tx_hash]
            step_total += int(result['stepUsed'], 16)
            self.assertIn('status', result)
            self.assertIn('txHash', result)
            self.assertIn('txIndex', result)
            self.assertIn('blockHeight', result)
            self.assertIn('blockHash', result)
            self.assertIn('cumulativeStepUsed', result)
            self.assertIn('stepUsed', result)
            self.assertEqual(1, len(result['eventLogs']))
            self.assertEqual(step_total, int(result['cumulativeStepUsed'], 16))

        clear_inner_task()
Esempio n. 4
0
    def test_add_deposit(self):
        tx_hash = os.urandom(32)
        tx_hash_hex = bytes.hex(tx_hash)
        term, amount = hex(BLOCKS_IN_ONE_MONTH), 5000

        mock_score_info = Mock(spec=DepositInfo)
        mock_score_info.configure_mock(sharing_ratio=50)
        # transfer, get_balance
        IconScoreContext.engine.fee.charge_transaction_fee = Mock(
            return_value={})
        IconScoreContext.engine.fee.can_charge_fee_from_score = Mock()

        IconScoreContext.engine.fee.add_deposit = Mock(
            return_value=[tx_hash, self.score, self.from_, amount, term])
        IconScoreContext.engine.fee.charge_transaction_fee = \
            Mock(return_value={self.from_: 9000})

        data = {
            'action': 'add',
        }

        expected_event_log = [{
            "scoreAddress":
            str(self.score),
            "indexed": [
                "DepositAdded(bytes,Address,int,int)", f"0x{tx_hash_hex}",
                str(self.from_)
            ],
            "data": [hex(amount), term]
        }]

        request = create_request([
            ReqData(tx_hash_hex, self.from_, self.score, amount, 'deposit',
                    data),
        ])

        result = self._inner_task_invoke(request)
        tx_result = result['txResults'][tx_hash_hex]

        self.assertEqual('0x1', tx_result['status'])
        self.assertEqual(expected_event_log, tx_result['eventLogs'])

        return tx_hash