def test_calc_delegations(self): cached_accounts: Dict['Address', Tuple['Account', int]] = OrderedDict() context = Mock() context.storage.icx.get_account = Mock(side_effect=get_account) new_delegations: List[Tuple['Address', int]] = [] for i in range(10): value: int = i + 11 address = Address.from_prefix_and_int(AddressPrefix.EOA, value) new_delegations.append((address, value)) sender_account = context.storage.icx.get_account(context, SENDER_ADDRESS, Intent.ALL) cached_accounts[SENDER_ADDRESS] = sender_account, 0 # Prepare old delegations for i in range(10): value: int = i + 1 address = Address.from_prefix_and_int(AddressPrefix.EOA, value) account = context.storage.icx.get_account(context, address, Intent.DELEGATED) cached_accounts[address] = account, -value IISSEngine._calc_delegations(context, new_delegations, cached_accounts) # 0: sender_account, 1~10: old_accounts 11~20: new_accounts for i, address in enumerate(cached_accounts): account, delegated_offset = cached_accounts[address] assert account.address == address if i == 0: assert address == SENDER_ADDRESS assert delegated_offset == 0 elif 1 <= i <= 10: assert delegated_offset == -i else: # 11 <= i <= 20 assert delegated_offset == i
def test_internal_handle_set_delegation(self): """Test case old_delegations: 1 ~ 10 delegated amount new_delegations: 101 ~ 110 delegated amount sender_account does not delegate to itself :return: """ context = Mock() context.revision = Revision.DECENTRALIZATION.value context.storage.icx.get_account = Mock(side_effect=get_account) total_delegating, delegations = create_delegations_param() ret_total_delegating, ret_delegations = IISSEngine._convert_params_of_set_delegation(context, delegations) assert ret_total_delegating == total_delegating for i in range(len(delegations)): item: Tuple['Address', int] = ret_delegations[i] address: 'Address' = item[0] value: int = item[1] assert address, value == delegations[i]["address"] assert value == delegations[i]["value"] # IISSEngine._check_voting_power_is_enough() cached_accounts: Dict['Address', Tuple['Account', int]] = {} IISSEngine._check_voting_power_is_enough( context, SENDER_ADDRESS, total_delegating, cached_accounts) assert len(cached_accounts) == 1 assert cached_accounts[SENDER_ADDRESS][0] == create_sender_account(stake=10_000) assert cached_accounts[SENDER_ADDRESS][1] == 0 # delegated_amount sender_account: 'Account' = cached_accounts[SENDER_ADDRESS][0] # Get old delegations from delegating accounts IISSEngine._get_old_delegations_from_sender_account(context, SENDER_ADDRESS, cached_accounts) assert len(cached_accounts) == 11 # sender_account(1) + old delegated_accounts(10) for i, address in enumerate(cached_accounts): item: Tuple['Account', int] = cached_accounts[address] account = item[0] delegated_offset = item[1] assert address == account.address if i > 0: delegation: Tuple['Account', int] = sender_account.delegations[i - 1] assert address == delegation[0] assert delegated_offset == -delegation[1] IISSEngine._calc_delegations(context, ret_delegations, cached_accounts) # 0: sender_account, 1~10: old_accounts 11~20: new_accounts for i, address in enumerate(cached_accounts): account, delegated_offset = cached_accounts[address] assert account.address == address if i == 0: assert address == SENDER_ADDRESS assert delegated_offset == 0 elif 1 <= i <= 10: assert delegated_offset == -i else: # 11 <= i <= 20 assert delegated_offset == i updated_accounts: List['Account'] = \ IISSEngine._put_delegation_to_state_db(context, SENDER_ADDRESS, ret_delegations, cached_accounts) # sender_account(1) + old_delegated_accounts(10) + new_delegated_accounts(10) assert len(updated_accounts) == len(cached_accounts) == 21 assert len(context.storage.icx.put_account.call_args_list) == len(cached_accounts) for i, address in enumerate(cached_accounts): call_args = context.storage.icx.put_account.call_args_list[i] account = call_args[0][1] assert isinstance(account, Account) assert address == account.address assert account == updated_accounts[i] if i == 0: assert account.address == SENDER_ADDRESS assert account.delegated_amount == 0 elif 1 <= i <= 10: # old delegations assert account.delegated_amount == 0 else: assert account.delegated_amount == cached_accounts[address][1]