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_get_put_account(self): context = self.context address = create_address(AddressPrefix.EOA) coin_part: 'CoinPart' = CoinPart() account: 'Account' = Account(address, 0, Revision.IISS.value, coin_part=coin_part) account.deposit(10**19) self.storage.put_account(context, account) address: 'Address' = create_address(AddressPrefix.EOA) coin_part: 'CoinPart' = CoinPart() account: 'Account' = Account(address, self.context.block.height, Revision.IISS.value, coin_part=coin_part) account.deposit(10**19) self.storage.put_account(self.context, account) account2 = self.storage.get_account(self.context, account.address) self.assertEqual(account, account2)
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_balance(self): value: int = 0 coin_part = Mock(spec=CoinPart, balance=value) account = Account(create_address(), 0, coin_part=coin_part) self.assertEqual(value, account.balance) value: int = 100 coin_part = Mock(spec=CoinPart, balance=value) account = Account(create_address(), 0, coin_part=coin_part) self.assertEqual(value, account.balance)
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_account_unstake_block_height(self): value: int = 0 stake_part = StakePart(unstake_block_height=value) stake_part.set_complete(True) account = Account(create_address(), 0, stake_part=stake_part) self.assertEqual(0, account.unstake_block_height) value: int = 300 stake_part = StakePart(unstake_block_height=value) stake_part.set_complete(True) account = Account(create_address(), 0, stake_part=stake_part) self.assertEqual(value, account.unstake_block_height)
def test_account_stake(self): value: int = 0 stake_part = StakePart(stake=value) stake_part.set_complete(True) account = Account(create_address(), 0, stake_part=stake_part) self.assertEqual(value, account.stake) value: int = 100 stake_part = StakePart(stake=value) stake_part.set_complete(True) account = Account(create_address(), 0, stake_part=stake_part) self.assertEqual(value, account.stake)
def test_account(self): account1 = Account() self.assertIsNotNone(account1) self.assertIsNone(account1.address) self.assertTrue(account1.icx == 0) self.assertFalse(account1.locked) self.assertFalse(account1.c_rep) account1.address = create_address() account1.deposit(100) self.assertEqual(100, account1.icx) account1.withdraw(100) self.assertEqual(0, account1.icx) # wrong value self.assertRaises(InvalidParamsException, account1.deposit, -10) # 0 transfer is possible old = account1.icx account1.deposit(0) self.assertEqual(old, account1.icx) self.assertRaises(InvalidParamsException, account1.withdraw, -11234) self.assertRaises(OutOfBalanceException, account1.withdraw, 1) old = account1.icx account1.withdraw(0) self.assertEqual(old, account1.icx)
def test_coin_part(self): address: 'Address' = create_address() coin_part: 'CoinPart' = CoinPart() account: 'Account' = Account(address, 0, Revision.IISS.value, coin_part=coin_part) self.assertEqual(address, account.address) self.assertEqual(0, account.balance) account.deposit(100) self.assertEqual(100, account.balance) account.withdraw(100) self.assertEqual(0, account.balance) # wrong value self.assertRaises(InvalidParamsException, account.deposit, -10) # 0 transfer is possible old = account.balance account.deposit(0) self.assertEqual(old, account.balance) self.assertRaises(InvalidParamsException, account.withdraw, -11234) self.assertRaises(OutOfBalanceException, account.withdraw, 1) old = account.balance account.withdraw(0) self.assertEqual(old, account.balance)
def test_normalize(self, context, mocker, revision, unstakes_info, current_block_height, flag, expected_balance): unstakes_info = copy.deepcopy(unstakes_info) mocker.patch.object(IconScoreContext, "revision", PropertyMock(return_value=revision)) stake, balance = 100, 100 coin_part = CoinPart(CoinPartType.GENERAL, flag, balance) stake_part = StakePart(stake=stake, unstake=0, unstake_block_height=0, unstakes_info=unstakes_info) account = Account(ADDRESS, current_block_height, revision, coin_part=coin_part, stake_part=stake_part) if unstakes_info is None: remaining_unstakes = [] else: remaining_unstakes = [ unstake_info for unstake_info in unstakes_info if unstake_info[1] >= current_block_height ] account.normalize(revision) assert account.stake == stake assert account.balance == expected_balance assert account.unstakes_info == remaining_unstakes
def get_account(self, address: "Address", current_block_height: int, revision: int) -> Optional["Account"]: """Read the account from statedb :param address: :param current_block_height: :param revision: :return: """ value: bytes = self._db.get(address.to_bytes()) if value is None: return None coin_part = self._get_part(CoinPart, address) stake_part = self._get_part(StakePart, address) print("coinPart flag : ", coin_part.flags) print("coinPart balance : ", coin_part.balance) print("coinPart has Unstake : ", CoinPartFlag.HAS_UNSTAKE in coin_part.flags) print("stakePart stake : ", stake_part.stake) print("stakePart unstake : ", stake_part.unstake) print("stakePart unstakes_info : ", stake_part.unstakes_info) print("stakePart unstakeBlockHeight : ", stake_part.unstake_block_height) return Account(address, current_block_height, revision, coin_part=coin_part, stake_part=stake_part)
def test_account_unstake(self): value: int = 0 stake_part = StakePart(unstake=value) stake_part.set_complete(True) account = Account(create_address(), 0, Revision.IISS.value, stake_part=stake_part) self.assertEqual(0, account.unstake) value: int = 200 stake_part = StakePart(unstake=value) stake_part.set_complete(True) account = Account(create_address(), 0, Revision.IISS.value, stake_part=stake_part) self.assertEqual(value, account.unstake)
def test_account_unstakes_info(self): value: int = 0 stake_part = StakePart(unstake=value) stake_part.set_complete(True) account = Account(create_address(), 0, Revision.MULTIPLE_UNSTAKE.value, stake_part=stake_part) self.assertEqual([], account.unstakes_info) info = [(10, 1), (20, 5)] stake_part = StakePart(unstakes_info=info) stake_part.set_complete(True) account = Account(create_address(), 0, Revision.MULTIPLE_UNSTAKE.value, stake_part=stake_part) self.assertEqual(info, account.unstakes_info)
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_get_put_account(self): context = self.context account = Account() account.address = create_address(AddressPrefix.EOA) account.deposit(10**19) self.storage.put_account(context, account.address, account) account2 = self.storage.get_account(context, account.address) self.assertEqual(account, account2)
def test_get_put_account(self): context = self.context account = Account() account.deposit(10**19) for address in self.addresses: account.address = address self.storage.put_account(context, account.address, account) account2 = self.storage.get_account(context, account.address) self.assertEqual(account, account2)
def test_delete_account(self): context = self.context account = Account() account.address = create_address(AddressPrefix.EOA) self.storage.put_account(context, account.address, account) ret = self.storage.is_address_present(context, account.address) self.assertTrue(ret) self.storage.delete_account(context, account.address) ret = self.storage.is_address_present(context, self.address) self.assertFalse(ret)
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_account_for_stake_rev_multiple_unstake1(self): address: 'Address' = create_address() context: 'IconScoreContext' = Mock(spec=IconScoreContext) unstake_slot_max = 10 context.configure_mock(unstake_slot_max=unstake_slot_max) context.configure_mock(revision=Revision.MULTIPLE_UNSTAKE.value) coin_part: 'CoinPart' = CoinPart() stake_part: 'StakePart' = StakePart() account = Account(address, 0, Revision.MULTIPLE_UNSTAKE.value, coin_part=coin_part, stake_part=stake_part) balance = 1000 account.deposit(balance) stake1 = 500 unstake_block_height = 0 remain_balance = balance - stake1 account.set_stake(context, stake1, 0) self.assertEqual(stake1, account.stake) self.assertEqual(0, account.unstake) self.assertEqual(unstake_block_height, account.unstake_block_height) self.assertEqual(remain_balance, account.balance) stake2 = 100 block_height = 10 unstake = stake1 - stake2 remain_balance = balance - stake1 account.set_stake(context, stake2, block_height) expected_unstake_info = [[unstake, block_height]] self.assertEqual(stake2, account.stake) self.assertEqual(0, account.unstake) self.assertEqual(0, account.unstake_block_height) self.assertEqual(expected_unstake_info, account.unstakes_info) self.assertEqual(remain_balance, account.balance) stake3 = 600 block_height = 15 account.set_stake(context, stake3, block_height) expected_unstake_info = [] expected_balance = 400 self.assertEqual(stake3, account.stake) self.assertEqual(expected_unstake_info, account.unstakes_info) self.assertEqual(expected_balance, account.balance)
def test_get_put_account(self): accounts: list = [] for address in self.addresses: coin_part: 'CoinPart' = CoinPart() account: 'Account' = Account(address, self.context.block.height, coin_part=coin_part) account.deposit(10**19) accounts.append(account) for account in accounts: self.storage.put_account(self.context, account) account2 = self.storage.get_account(self.context, account.address) self.assertEqual(account, account2)
def test_delete_account(self): context = self.context account = Account() for address in self.addresses: account.address = address self.storage.put_account(context, account.address, account) ret = self.storage.is_address_present(context, account.address) self.assertTrue(ret) self.storage.delete_account(context, account.address) ret = self.storage.is_address_present(context, account.address) self.assertFalse(ret)
def test_account_for_unstake_slot_max_case_1(self): address: 'Address' = create_address() context: 'IconScoreContext' = Mock(spec=IconScoreContext) unstake_slot_max = 10 context.configure_mock(unstake_slot_max=unstake_slot_max) context.configure_mock(revision=Revision.MULTIPLE_UNSTAKE.value) coin_part: 'CoinPart' = CoinPart() stake_part: 'StakePart' = StakePart() account = Account(address, 0, Revision.MULTIPLE_UNSTAKE.value, coin_part=coin_part, stake_part=stake_part) balance = 2000 account.deposit(balance) stake = 2000 unstake_block_height = 0 remain_balance = balance - stake account.set_stake(context, stake, 0) self.assertEqual(stake, account.stake) self.assertEqual(0, account.unstake) self.assertEqual(unstake_block_height, account.unstake_block_height) self.assertEqual(remain_balance, account.balance) unstake = 1 total_unstake = 0 expected_unstake_info = [] for i in range(unstake_slot_max): expected_unstake_info.append([unstake, unstake_slot_max + i]) stake -= unstake total_unstake += unstake account.set_stake(context, account.stake - unstake, unstake_slot_max + i) self.assertEqual(stake, account.stake) self.assertEqual(total_unstake, account.stake_part.total_unstake) self.assertEqual(remain_balance, account.balance) self.assertEqual(expected_unstake_info, account.unstakes_info) last_unstake = 100 account.set_stake(context, account.stake - last_unstake, unstake_slot_max + 2) expected_unstake_info[-1] = [101, unstake_slot_max * 2 - 1] self.assertEqual(expected_unstake_info, account.unstakes_info)
def test_account_for_stake(self): address: 'Address' = create_address() context: 'IconScoreContext' = IconScoreContext() coin_part: 'CoinPart' = CoinPart() stake_part: 'StakePart' = StakePart() account = Account(address, 0, Revision.IISS.value, coin_part=coin_part, stake_part=stake_part) balance = 1000 account.deposit(balance) stake1 = 500 unstake_block_height = 0 remain_balance = balance - stake1 account.set_stake(context, stake1, 0) self.assertEqual(stake1, account.stake) self.assertEqual(0, account.unstake) self.assertEqual(unstake_block_height, account.unstake_block_height) self.assertEqual(remain_balance, account.balance) stake2 = 100 block_height = 10 unstake = stake1 - stake2 remain_balance = balance - stake1 account.set_stake(context, stake2, block_height) self.assertEqual(stake2, account.stake) self.assertEqual(unstake, account.unstake) self.assertEqual(block_height, account.unstake_block_height) self.assertEqual(remain_balance, account.balance) remain_balance = remain_balance + unstake account._current_block_height += 11 account.normalize(Revision.IISS.value) self.assertEqual(remain_balance, account.balance)
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 test_get_account(self, storage, context, mocker, flag, unstakes_info, current_block_height, expected_balance): unstakes_info = copy.deepcopy(unstakes_info) # test whether the `Account` saved in the wrong format is properly got on revision11. revision = Revision.FIX_BALANCE_BUG.value mocker.patch.object(IconScoreContext, "revision", PropertyMock(return_value=revision)) stake, balance = 100, 100 coin_part = CoinPart(CoinPartType.GENERAL, flag, balance) coin_part.set_dirty(True) stake_part = StakePart(stake=stake, unstake=0, unstake_block_height=0, unstakes_info=unstakes_info) stake_part.set_dirty(True) account = Account(ADDRESS, current_block_height, revision, coin_part=coin_part, stake_part=stake_part) account.coin_part._flags = flag context.block._height = current_block_height storage.put_account(context, account) if unstakes_info is None: remaining_unstakes = [] else: remaining_unstakes = [ unstake_info for unstake_info in unstakes_info if unstake_info[1] >= current_block_height ] account = storage.get_account(context, ADDRESS) assert account.balance == expected_balance assert account.unstakes_info == remaining_unstakes
def test_account_from_bytes_to_bytes(self): account = Account() data = account.to_bytes() self.assertEqual(bytes(account), data) self.assertTrue(isinstance(data, bytes)) self.assertEqual(36, len(data)) account2 = Account.from_bytes(data) self.assertFalse(account2.locked) self.assertFalse(account2.c_rep) self.assertEqual(AccountType.GENERAL, account2.type) self.assertEqual(0, account2.icx) account.type = AccountType.GENESIS account.locked = True account.c_rep = True account.deposit(1024) account3 = Account.from_bytes(account.to_bytes()) self.assertTrue(account3.locked) self.assertTrue(account3.c_rep) self.assertEqual(AccountType.GENESIS, account3.type) self.assertEqual(1024, account3.icx)