def test_account_for_delegation(self): target_accounts = [] src_delegation_part: 'DelegationPart' = DelegationPart() src_account = Account(create_address(), 0, Revision.IISS.value, delegation_part=src_delegation_part) preps: list = [] for _ in range(0, 10): address: 'Address' = create_address() target_delegation_part: 'DelegationPart' = DelegationPart() target_account: 'Account' = \ Account(address, 0, Revision.IISS.value, delegation_part=target_delegation_part) target_accounts.append(target_account) target_account.update_delegated_amount(10) preps.append((address, 10)) src_account.set_delegations(preps) self.assertEqual(10, len(src_account.delegation_part.delegations)) for i in range(10): self.assertEqual( 10, target_accounts[i].delegation_part.delegated_amount)
def test_delegation_part_from_bytes_to_bytes(self): account = DelegationPart() data = account.to_bytes() self.assertTrue(isinstance(data, bytes)) self.assertEqual(4, len(data)) account2 = DelegationPart.from_bytes(data) self.assertEqual(account.delegated_amount, account2.delegated_amount) self.assertEqual(account.delegations, account2.delegations)
def test_delegation_part_make_key(self): key = DelegationPart.make_key(create_address()) self.assertEqual( ICON_EOA_ADDRESS_BYTES_SIZE + len(DelegationPart.PREFIX) + 1, len(key)) key = DelegationPart.make_key(create_address(1)) self.assertEqual( ICON_CONTRACT_ADDRESS_BYTES_SIZE + len(DelegationPart.PREFIX), len(key))
def test_account_delegated_amount(self): value: int = 0 delegation_part = DelegationPart(delegated_amount=value) account = Account(create_address(), 0, delegation_part=delegation_part) self.assertEqual(value, account.delegated_amount) value: int = 100 delegation_part = DelegationPart(delegated_amount=value) account = Account(create_address(), 0, delegation_part=delegation_part) self.assertEqual(value, account.delegated_amount)
def test_account_delegations(self): value: list = [] delegation_part = DelegationPart(delegations=value) account = Account(create_address(), 0, delegation_part=delegation_part) self.assertEqual(value, account.delegations) delegations = [(create_address(), 100), (create_address(), 200), (create_address(), 300)] delegation_part = DelegationPart(delegations=delegations) account = Account(create_address(), 0, delegation_part=delegation_part) self.assertEqual(delegations, account.delegations) self.assertEqual(600, account.delegations_amount)
def test_delegation_part_set_delegations_overflow(self): count = 10 + 1 amount = 10 delegations = [] for _ in range(count): delegations.append((create_address(), amount)) part = DelegationPart() with self.assertRaises(InvalidParamsException) as e: part.set_delegations(delegations) self.assertEqual(ExceptionCode.INVALID_PARAMETER, e.exception.code) self.assertEqual('Delegations overflow', e.exception.message)
def test_delegation_part_for_delegation(self): src = DelegationPart() preps: list = [] for _ in range(0, 10): prep: 'DelegationPart' = DelegationPart() prep.update_delegated_amount(10) preps.append((prep, 10)) src.set_delegations(preps) self.assertEqual(10, len(src.delegations)) for i in range(0, 10): prep, value = preps[i] self.assertEqual(10, prep.delegated_amount)
def test_delegation_part_delegations(self): count = 10 amount = 10 delegations = [] for _ in range(count): delegations.append((create_address(), amount)) part = DelegationPart(delegations=delegations) self.assertEqual(delegations, part.delegations) delegations_amount = amount * count self.assertEqual(delegations_amount, part.delegations_amount)
def create_account( address: 'Address', balance: int, stake: int, unstake: int, unstake_block_height: int, delegated_amount: int, delegations: List[Tuple[Address, int]]) -> 'Account': coin_part = CoinPart(balance=balance) stake_part = StakePart(stake, unstake, unstake_block_height) delegation_part = DelegationPart(delegated_amount, delegations) return Account( address, 1024, coin_part=coin_part, stake_part=stake_part, delegation_part=delegation_part)
def test_delegation_part_update_delegated_amount(self): offset = 100 part = DelegationPart() self.assertFalse(part.is_dirty()) part.update_delegated_amount(offset) self.assertTrue(part.is_dirty()) self.assertEqual(offset, part.delegated_amount)
def test_delegation_part_equal(self): part1 = DelegationPart() part2 = DelegationPart() self.assertEqual(part1, part2) offset = 100 part1.update_delegated_amount(offset) part3 = DelegationPart(delegated_amount=offset) self.assertEqual(part1, part3)
def test_account_flag(self): address: 'Address' = create_address() account: 'Account' = Account(address, 0, Revision.IISS.value) self.assertIsNone(account.coin_part) self.assertIsNone(account.stake_part) self.assertIsNone(account.delegation_part) coin_part: 'CoinPart' = CoinPart() account: 'Account' = Account(address, 0, Revision.IISS.value, coin_part=coin_part) self.assertIsNotNone(account.coin_part) self.assertIsNone(account.stake_part) self.assertIsNone(account.delegation_part) stake_part: 'StakePart' = StakePart() account: 'Account' = Account(address, 0, Revision.IISS.value, stake_part=stake_part) self.assertIsNone(account.coin_part) self.assertIsNotNone(account.stake_part) self.assertIsNone(account.delegation_part) delegation_part: 'DelegationPart' = DelegationPart() account: 'Account' = Account(address, 0, Revision.IISS.value, delegation_part=delegation_part) self.assertIsNone(account.coin_part) self.assertIsNone(account.stake_part) self.assertIsNotNone(account.delegation_part) account: 'Account' = Account(address, 0, Revision.IISS.value, coin_part=coin_part, stake_part=stake_part, delegation_part=delegation_part) self.assertIsNotNone(account.coin_part) self.assertIsNotNone(account.stake_part) self.assertIsNotNone(account.delegation_part)
def test_delegation_part_set_delegations(self): count = 10 amount = 10 delegations = [] for _ in range(count): delegations.append((create_address(), amount)) part = DelegationPart() self.assertFalse(part.is_dirty()) part.set_delegations(delegations) self.assertTrue(part.is_dirty()) self.assertEqual(delegations, part.delegations)
def get_account(context: 'IconScoreContext', address: 'Address', intent: 'Intent' = Intent.TRANSFER) -> 'Account': """Returns the account indicated by address. :param context: :param address: account address :param intent: :return: (Account) If the account indicated by address is not present, create a new account. """ if address == SENDER_ADDRESS: return create_sender_account(stake=10_000) coin_part: Optional['CoinPart'] = None stake_part: Optional['StakePart'] = None delegation_part: Optional['DelegationPart'] = None part_flags: 'AccountPartFlag' = AccountPartFlag(intent) if AccountPartFlag.COIN in part_flags: coin_part: 'CoinPart' = CoinPart(balance=0) if AccountPartFlag.STAKE in part_flags: stake_part = StakePart(0, 0, 0) if AccountPartFlag.DELEGATION in part_flags: value: int = int.from_bytes(address.body, "big") delegated_amount = value if 1 <= value <= 10 else 0 delegation_part = DelegationPart(delegated_amount=delegated_amount) return Account(address, context.block.height, coin_part=coin_part, stake_part=stake_part, delegation_part=delegation_part)
def _convert_delegation_parts(cls, key: bytes, value: bytes): ret = DelegationPart.from_bytes(value) return str(Address.from_bytes(key[len(StakePart.PREFIX):])), str(ret)
def test_delegation_part_delegated_amount(self): delegated_amount = 10000 part = DelegationPart(delegated_amount=delegated_amount) self.assertEqual(delegated_amount, part.delegated_amount)