This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix a incorrect return value of getBalance caused by state mismatch between coin_part and stake_part
- Loading branch information
Showing
9 changed files
with
656 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
tests/integrate_test/iiss/decentralized/test_iiss_unstake.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright 2018 ICON Foundation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""IconScoreEngine testcase | ||
""" | ||
from typing import TYPE_CHECKING, List | ||
|
||
from iconservice.icon_constant import Revision, ICX_IN_LOOP | ||
from tests.integrate_test.iiss.test_iiss_base import TestIISSBase | ||
|
||
if TYPE_CHECKING: | ||
from iconservice.iconscore.icon_score_result import TransactionResult | ||
|
||
|
||
class TestIISSUnStake(TestIISSBase): | ||
def test_unstake_balance_rev_10(self): | ||
self._test_unstake_balance( | ||
rev=Revision.FIX_UNSTAKE_BUG.value, | ||
expected_expired_ustake_cnt=2, | ||
expected_last_balance=0 | ||
) | ||
|
||
def test_unstake_balance_rev_11(self): | ||
self._test_unstake_balance( | ||
rev=Revision.FIX_BALANCE_BUG.value, | ||
expected_expired_ustake_cnt=3, | ||
expected_last_balance=1 * ICX_IN_LOOP | ||
) | ||
|
||
def _test_unstake_balance( | ||
self, | ||
rev: int, | ||
expected_expired_ustake_cnt: int, | ||
expected_last_balance: int | ||
): | ||
self.update_governance() | ||
|
||
# set Revision REV_IISS | ||
self.set_revision(Revision.IISS.value) | ||
|
||
# gain 10 icx | ||
balance: int = 10 * ICX_IN_LOOP | ||
self.distribute_icx( | ||
accounts=self._accounts[:1], | ||
init_balance=balance | ||
) | ||
|
||
# set stake | ||
target_stake: int = 8 | ||
stake: int = target_stake * ICX_IN_LOOP | ||
tx_results: List['TransactionResult'] = self.set_stake( | ||
from_=self._accounts[0], | ||
value=stake | ||
) | ||
fee = tx_results[0].step_used * tx_results[0].step_price | ||
expected_balance: int = balance - stake - fee | ||
response: int = self.get_balance(self._accounts[0]) | ||
self.assertEqual(expected_balance, response) | ||
|
||
self.set_revision(Revision.MULTIPLE_UNSTAKE.value) | ||
|
||
for i in range(6): | ||
self.set_stake( | ||
from_=self._accounts[0], | ||
value=stake - (i+1) * ICX_IN_LOOP | ||
) | ||
|
||
last_balance: int = self.get_balance(self._accounts[0]) | ||
|
||
actual_res: dict = self.get_stake(self._accounts[0]) | ||
first_remaining_blocks: int = 14 | ||
expected_res = { | ||
"stake": stake - 1 * ICX_IN_LOOP * 6, | ||
"unstakes": [ | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 25, "remainingBlocks": first_remaining_blocks}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 26, "remainingBlocks": first_remaining_blocks+1}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 27, "remainingBlocks": first_remaining_blocks+2}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 28, "remainingBlocks": first_remaining_blocks+3}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 29, "remainingBlocks": first_remaining_blocks+4}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 30, "remainingBlocks": first_remaining_blocks+5}, | ||
] | ||
} | ||
self.assertEqual(expected_res, actual_res) | ||
|
||
self.set_revision(rev) | ||
|
||
# 1st expired unstake | ||
self.make_empty_blocks(first_remaining_blocks) | ||
|
||
res: dict = self.get_stake(self._accounts[0]) | ||
expected_res = { | ||
"stake": stake - 1 * ICX_IN_LOOP * 6, | ||
"unstakes": [ | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 26, "remainingBlocks": 0}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 27, "remainingBlocks": 1}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 28, "remainingBlocks": 2}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 29, "remainingBlocks": 3}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 30, "remainingBlocks": 4}, | ||
] | ||
} | ||
self.assertEqual(res, expected_res) | ||
|
||
tx_results = self.transfer_icx(from_=self._accounts[0], to_=self._accounts[1], value=0) | ||
|
||
res: dict = self.get_stake(self._accounts[0]) | ||
expected_res = { | ||
"stake": stake - 1 * ICX_IN_LOOP * 6, | ||
"unstakes": [ | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 27, "remainingBlocks": 0}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 28, "remainingBlocks": 1}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 29, "remainingBlocks": 2}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 30, "remainingBlocks": 3}, | ||
] | ||
} | ||
self.assertEqual(res, expected_res) | ||
|
||
estimate_fee = tx_results[0].step_used * tx_results[0].step_price | ||
|
||
# 2nd expired unstake | ||
self.make_empty_blocks(1) | ||
|
||
res: dict = self.get_stake(self._accounts[0]) | ||
expected_res = { | ||
"stake": stake - 1 * ICX_IN_LOOP * 6, | ||
"unstakes": [ | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 28, "remainingBlocks": 0}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 29, "remainingBlocks": 1}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 30, "remainingBlocks": 2}, | ||
] | ||
} | ||
self.assertEqual(res, expected_res) | ||
|
||
current_balance: int = self.get_balance(self._accounts[0]) | ||
expected_exipred_stake_balance: int = 1 * ICX_IN_LOOP * expected_expired_ustake_cnt | ||
expected_balance: int = last_balance + expected_exipred_stake_balance - estimate_fee | ||
self.assertEqual(current_balance, expected_balance) | ||
|
||
self.transfer_icx( | ||
from_=self._accounts[0], | ||
to_=self._accounts[1], | ||
value=expected_balance-estimate_fee, | ||
disable_pre_validate=True, | ||
step_limit=1 * 10 ** 5, | ||
expected_status=True | ||
) | ||
|
||
res: dict = self.get_stake(self._accounts[0]) | ||
expected_res = { | ||
"stake": stake - 1 * ICX_IN_LOOP * 6, | ||
"unstakes": [ | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 29, "remainingBlocks": 0}, | ||
{"unstake": 1 * ICX_IN_LOOP, "unstakeBlockHeight": 30, "remainingBlocks": 1}, | ||
] | ||
} | ||
self.assertEqual(res, expected_res) | ||
|
||
balance: int = self.get_balance(self._accounts[0]) | ||
self.assertEqual(balance, expected_last_balance) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2019 ICON Foundation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import copy | ||
from unittest.mock import PropertyMock | ||
|
||
import pytest | ||
|
||
from iconservice import Address | ||
from iconservice.base.block import Block | ||
from iconservice.icon_constant import Revision, IconScoreContextType | ||
from iconservice.iconscore.context.context import ContextContainer | ||
from iconservice.iconscore.icon_score_context import IconScoreContext | ||
from iconservice.icx.coin_part import CoinPart, CoinPartFlag, CoinPartType | ||
from iconservice.icx.icx_account import Account | ||
from iconservice.icx.stake_part import StakePart | ||
|
||
ADDRESS = Address.from_string(f"hx{'1234'*10}") | ||
UNSTAKE_LOCK_PERIOD = 20 | ||
WITHDRAWAL_AMOUNT = 10 | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def context(): | ||
ctx = IconScoreContext(IconScoreContextType.DIRECT) | ||
block = Block(0, None, 0, None, 0) | ||
ctx.block = block | ||
ContextContainer._push_context(ctx) | ||
yield ctx | ||
ContextContainer._pop_context() | ||
|
||
|
||
class TestAccount: | ||
|
||
@pytest.mark.parametrize("revision", [ | ||
revision.value for revision in Revision if revision.value >= Revision.MULTIPLE_UNSTAKE.value | ||
]) | ||
@pytest.mark.parametrize("flag", [CoinPartFlag.NONE, CoinPartFlag.HAS_UNSTAKE]) | ||
@pytest.mark.parametrize("unstakes_info, current_block_height, expected_balance", [ | ||
(None, 20, 100), | ||
([], 20, 100), | ||
([[10, 20]], 5, 100), | ||
([[10, 20]], 20, 100), | ||
([[10, 20]], 25, 110), | ||
([[10, 20], [10, 30]], 15, 100), | ||
([[10, 20], [10, 30]], 20, 100), | ||
([[10, 20], [10, 30]], 25, 110), | ||
([[10, 20], [10, 30]], 30, 110), | ||
([[10, 20], [10, 30]], 35, 120), | ||
]) | ||
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 |
Oops, something went wrong.