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_coin_part_equal(self): part1 = CoinPart() part2 = CoinPart() self.assertEqual(part1, part2) balance = 100 part1.deposit(balance) part3 = CoinPart(balance=balance) self.assertEqual(part1, part3)
def test_coin_part_type_property(self): part = CoinPart() self.assertEqual(CoinPartType.GENERAL, part.type) for coin_part_type in CoinPartType: part = CoinPart(coin_part_type=coin_part_type) self.assertEqual(coin_part_type, part.type) for coin_part_type in CoinPartType: part = CoinPart() part.type = coin_part_type self.assertEqual(coin_part_type, part.type) with self.assertRaises(ValueError) as e: part.type = len(CoinPartType) + 1 self.assertEqual("Invalid CoinPartType", e.exception.args[0])
def test_coin_part_revision_3(self): part1 = CoinPart() self.assertIsNotNone(part1) self.assertIs(CoinPartFlag.NONE, part1.flags) self.assertTrue(part1.balance == 0) part1.deposit(100) self.assertEqual(100, part1.balance) part1.withdraw(100) self.assertEqual(0, part1.balance) # wrong value self.assertRaises(InvalidParamsException, part1.deposit, -10) # 0 transfer is possible old = part1.balance part1.deposit(0) self.assertEqual(old, part1.balance) self.assertRaises(InvalidParamsException, part1.withdraw, -11234) self.assertRaises(OutOfBalanceException, part1.withdraw, 1) old = part1.balance part1.withdraw(0) self.assertEqual(old, part1.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 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_coin_part_withdraw(self): balance = 100 part = CoinPart() self.assertFalse(part.is_dirty()) part.deposit(balance) part.withdraw(balance) self.assertTrue(part.is_dirty()) self.assertEqual(0, part.balance)
def test_coin_part_flag(self): part1 = CoinPart() self.assertEqual(BasePartState.NONE, part1.states) part1._flags = set_flag(part1.flags, CoinPartFlag.HAS_UNSTAKE, True) self.assertIn(CoinPartFlag.HAS_UNSTAKE, part1.flags) part1._flags = set_flag(part1.flags, CoinPartFlag.HAS_UNSTAKE, False) self.assertEqual(CoinPartFlag.NONE, part1.flags) self.assertNotIn(CoinPartFlag.HAS_UNSTAKE, part1.flags)
def test_coin_part_from_bytes_to_bytes_old_db_load_revision_4(self): part1 = CoinPart() balance = 1024 part1.type = CoinPartType.GENERAL part1.deposit(balance) part2 = CoinPart.from_bytes(part1.to_bytes(Revision.THREE.value)) self.assertEqual(part1, part2) data: bytes = part1.to_bytes(Revision.FOUR.value) part3 = CoinPart.from_bytes(data) self.assertEqual(part1, part3)
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_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_coin_part_from_bytes_to_bytes_revision_4(self): part1 = CoinPart() data = part1.to_bytes(Revision.FOUR.value) self.assertTrue(isinstance(data, bytes)) part2 = CoinPart.from_bytes(data) self.assertEqual(part1, part2) self.assertEqual(CoinPartType.GENERAL, part2.type) self.assertEqual(0, part2.balance) self.assertEqual(CoinPartFlag.NONE, part2.flags) part1.type = CoinPartType.GENESIS part1.deposit(1024) part3 = CoinPart.from_bytes(part1.to_bytes(Revision.FOUR.value)) self.assertEqual(CoinPartType.GENESIS, part3.type) self.assertEqual(1024, part3.balance)
def test_coin_part_from_bytes_to_bytes_revision_3(self): part1 = CoinPart() revision = 3 data = part1.to_bytes(revision) self.assertTrue(isinstance(data, bytes)) self.assertEqual(36, len(data)) part2 = CoinPart.from_bytes(data) self.assertEqual(CoinPartType.GENERAL, part2.type) self.assertEqual(0, part2.balance) part1.type = CoinPartType.GENESIS part1.deposit(1024) part3 = CoinPart.from_bytes(part1.to_bytes(revision)) self.assertEqual(CoinPartType.GENESIS, part3.type) self.assertEqual(1024, part3.balance)
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_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_coin_part_flags(self): part = CoinPart(flags=CoinPartFlag.HAS_UNSTAKE) self.assertEqual(CoinPartFlag.HAS_UNSTAKE, part.flags)
def test_coin_part_balance(self): balance = 10000 part = CoinPart(balance=balance) self.assertEqual(balance, part.balance)