示例#1
0
    def test_handle_set_delegation_with_1_account(self):
        context = Mock()
        context.revision = Revision.DECENTRALIZATION.value
        context.tx.origin = SENDER_ADDRESS
        context.msg = Message(SENDER_ADDRESS, 0)
        context.storage.icx.get_account = Mock(side_effect=get_account)

        new_delegations = [{
            "address": Address.from_prefix_and_int(AddressPrefix.EOA, 1),
            "value": 100
        }]

        class IISSEngineListenerImpl(IISSEngineListener):
            def on_set_stake(self, _context: 'IconScoreContext', account: 'Account'):
                assert False

            def on_set_delegation(self, _context: 'IconScoreContext', updated_accounts: List['Account']):
                # sender_account(1) + updated_delegated_account(10)
                assert len(updated_accounts) == 11

                delegated_address = Address.from_prefix_and_int(AddressPrefix.EOA, 1)

                for i, account in enumerate(updated_accounts):
                    assert isinstance(account, Account)
                    address = account.address

                    if i == 0:
                        # sender_account
                        assert account.delegated_amount == 0
                        assert len(account.delegations) == 1
                        assert account.delegation_part.delegations_amount == 100

                        item = account.delegations[0]
                        assert item[0] == Address.from_prefix_and_int(AddressPrefix.EOA, 1)
                        assert item[1] == 100
                    else:
                        assert account.delegations_amount == 0

                        if address == delegated_address:
                            assert account.delegated_amount == 100
                        else:
                            assert account.delegated_amount == 0

        engine = IISSEngine()
        engine.add_listener(IISSEngineListenerImpl())
        engine.handle_set_delegation(context, new_delegations)
示例#2
0
    def test_handle_set_delegation_with_21_accounts(self):
        context = Mock()
        context.revision = Revision.DECENTRALIZATION.value
        context.tx.origin = SENDER_ADDRESS
        context.msg = Message(SENDER_ADDRESS, 0)
        context.storage.icx.get_account = Mock(side_effect=get_account)
        total_delegating, delegations = create_delegations_param()

        class IISSEngineListenerImpl(IISSEngineListener):
            def on_set_stake(self, _context: 'IconScoreContext', account: 'Account'):
                assert False

            def on_set_delegation(self, _context: 'IconScoreContext', updated_accounts: List['Account']):
                assert len(updated_accounts) == 21

                for i, account in enumerate(updated_accounts):
                    assert isinstance(account, Account)
                    address = account.address
                    assert address == Address.from_prefix_and_int(AddressPrefix.EOA, i)

                    if i == 0:
                        # sender_account
                        assert account.delegated_amount == 0
                        assert len(account.delegations) == 10
                        assert account.delegation_part.delegations_amount == sum(range(11, 21))

                        for j, item in enumerate(account.delegations):
                            address: 'Address' = item[0]
                            value: int = item[1]

                            _id = j + 11
                            assert address == Address.from_prefix_and_int(AddressPrefix.EOA, _id)
                            assert value == _id
                    else:
                        assert account.delegations_amount == 0

                        if i <= 10:
                            assert account.delegated_amount == 0
                        else:
                            assert account.delegated_amount == i

        engine = IISSEngine()
        engine.add_listener(IISSEngineListenerImpl())
        engine.handle_set_delegation(context, delegations)