Skip to content

Commit

Permalink
Merge pull request #80 from peaqnetwork/bug/1208879955079916_allow_ch…
Browse files Browse the repository at this point in the history
…ange_wait_time

Bug/1208879955079916 allow change wait time
  • Loading branch information
sfffaaa authored Dec 9, 2024
2 parents 30a68e4 + 53e9cf7 commit 6f4fd1f
Show file tree
Hide file tree
Showing 12 changed files with 527 additions and 251 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ Solution: Please install the wheel and reinstall the dependency library again. [
pip3 install wheel
pip3 install -r requirements.txt
```

# Enviroment parameter
- RUNTIME_UPGRADE_PATH: The runtime upgrade path. If we want to test the runtime upgrade, we should set this parameter.

# Stress tools
```
python3 tools/stress/stress_token_economy_v2.py -r wss://docker-test.peaq.network --test-session-num 10 -t distribution
python3 tools/stress/stress_token_economy_v2.py -r wss://docker-test.peaq.network --test-session-num 10 -t validator
python3 tools/stress/stress_token_economy_v2.py -r wss://docker-test.peaq.network --test-session-num 10 -t traverse
```
6 changes: 3 additions & 3 deletions tests/bridge_multiple_collator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class bridge_parachain_staking_collators_test(unittest.TestCase):
@classmethod
def setUpClass(cls):
restart_parachain_and_runtime_upgrade()
wait_until_block_height(SubstrateInterface(url=RELAYCHAIN_WS_URL), 1)
wait_until_block_height(SubstrateInterface(url=WS_URL), 1)
wait_until_block_height(SubstrateInterface(url=RELAYCHAIN_WS_URL), 2)
wait_until_block_height(SubstrateInterface(url=WS_URL), 2)

def setUp(self):
wait_until_block_height(SubstrateInterface(url=WS_URL), 1)
wait_until_block_height(SubstrateInterface(url=WS_URL), 3)

self._substrate = SubstrateInterface(url=WS_URL)
self._w3 = Web3(Web3.HTTPProvider(ETH_URL))
Expand Down
117 changes: 102 additions & 15 deletions tests/delegator_issue_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,40 @@ def setUp(self):
Keypair.create_from_mnemonic(Keypair.generate_mnemonic()),
Keypair.create_from_mnemonic(Keypair.generate_mnemonic())
]
self.ori_reward_config = self.substrate.query(
module='BlockReward',
storage_function='RewardDistributionConfigStorage',
receipt = self.set_collator_delegator_precentage()
self.assertTrue(receipt.is_success, 'cannot set the reward rate')

receipt = self.set_commission_rate(50)
self.assertTrue(receipt.is_success, 'cannot set the commission rate')

receipt = self.set_round_length(100)
self.assertTrue(receipt.is_success, 'cannot set the round length')

@classmethod
def tearDownClass(cls):
restart_parachain_and_runtime_upgrade()

def set_commission_rate(self, rate):
batch = ExtrinsicBatch(self.substrate, KP_COLLATOR)
batch.compose_call(
'ParachainStaking',
'set_commission',
{
'commission': rate * 10_000,
}
)
self.set_collator_delegator_precentage()
return batch.execute()

def tearDown(self):
receipt = set_block_reward_configuration(self.substrate, self.ori_reward_config.value)
self.assertTrue(receipt.is_success, 'cannot reset the block reward configuration')
def set_round_length(self, length):
batch = ExtrinsicBatch(self.substrate, KP_GLOBAL_SUDO)
batch.compose_sudo_call(
'ParachainStaking',
'set_blocks_per_round',
{
'new': length,
}
)
return batch.execute()

def set_collator_delegator_precentage(self):
# If the collator/delegator reward distirbution is less than ED, the collator/delegator cannot receive rewards
Expand All @@ -95,9 +120,7 @@ def set_collator_delegator_precentage(self):
'coretime_percent': 40000000,
'subsidization_pool_percent': 660000000,
}
receipt = set_block_reward_configuration(self.substrate, set_value)
self.assertTrue(receipt.is_success,
'cannot setup the block reward configuration')
return set_block_reward_configuration(self.substrate, set_value)

def get_one_collator_without_delegator(self, keys):
for key in keys:
Expand Down Expand Up @@ -144,11 +167,13 @@ def test_delegator_issue(self):
first_receipt = batch.execute()
self.assertTrue(first_receipt.is_success)

# Just send a random extrinsic for tx fee
receipt = set_max_candidate_stake(self.substrate, 10 ** 7 * mega_tokens)
self.assertTrue(receipt.is_success, 'cannot set the commission rate')

second_receipt = batch.execute()
self.assertTrue(second_receipt.is_success)

time.sleep(12 * 2)

first_new_session_block_hash = self.substrate.get_block_hash(first_receipt.block_number + 1)
second_new_session_block_hash = self.substrate.get_block_hash(second_receipt.block_number + 1)

Expand All @@ -172,6 +197,68 @@ def test_delegator_issue(self):
1, 7,
f'{total_diff} v.s. {pot_transferable_balance} is not equal')

self.assertEqual(now_c_balance - prev_c_balance, now_d_1_balance - prev_d_1_balance)
self.assertEqual(now_c_balance - prev_c_balance, now_d_2_balance - prev_d_2_balance)
self.assertEqual(now_d_1_balance - prev_d_1_balance, now_d_2_balance - prev_d_2_balance)
self.assertAlmostEquals(
(now_c_balance - prev_c_balance) / ((now_d_1_balance - prev_d_1_balance) * 4),
1, 7,
f'{now_c_balance - prev_c_balance} v.s. {(now_d_1_balance - prev_d_1_balance) * 4} is not equal')

self.assertAlmostEquals(
(now_c_balance - prev_c_balance) / ((now_d_2_balance - prev_d_2_balance) * 4),
1, 7,
f'{now_c_balance - prev_c_balance} v.s. {(now_d_2_balance - prev_d_2_balance) * 4} is not equal')

self.assertAlmostEquals(
(now_d_1_balance - prev_d_1_balance) / (now_d_2_balance - prev_d_2_balance),
1, 7,
f'{now_d_1_balance - prev_d_1_balance} v.s. {now_d_2_balance - prev_d_2_balance} is not equal')

def test_commission_rate_in_snapshot(self):
mega_tokens = 500000 * 10 ** 18
batch = ExtrinsicBatch(self.substrate, KP_GLOBAL_SUDO)
batch.compose_sudo_call('ParachainStaking', 'set_max_candidate_stake', {
'new': 10 ** 5 * mega_tokens
})
batch_fund(batch, KP_COLLATOR, 20 * mega_tokens)
batch_fund(batch, self.delegators[0], 10 * mega_tokens)
receipt = batch.execute()
self.assertTrue(receipt.is_success, f'batch execute failed, error: {receipt.error_message}')

# Get the collator account
receipt = collator_stake_more(self.substrate, KP_COLLATOR, 5 * mega_tokens)
self.assertTrue(receipt.is_success, 'Stake failed')

collator = self.get_one_collator_without_delegator(self.collator)
self.assertGreaterEqual(int(str(collator['stake'])), 5 * mega_tokens)
self.assertNotEqual(collator, None)

# Add the delegator
receipt = add_delegator(self.substrate, self.delegators[0], str(collator['id']), int(str(collator['stake'])))
self.assertTrue(receipt.is_success, 'Add delegator failed')

receipt = self.set_commission_rate(100)
# Avoid the session in block height 10
time.sleep(12 * 2)
# Check the delegator's issue number
batch = ExtrinsicBatch(self.substrate, KP_GLOBAL_SUDO)
batch.compose_sudo_call(
'ParachainStaking',
'force_new_round',
{}
)
first_receipt = batch.execute()
self.assertTrue(first_receipt.is_success)

receipt = self.set_commission_rate(0)
self.assertTrue(receipt.is_success, 'cannot set the commission rate')

second_receipt = batch.execute()
self.assertTrue(second_receipt.is_success)

first_new_session_block_hash = self.substrate.get_block_hash(first_receipt.block_number + 1)
second_new_session_block_hash = self.substrate.get_block_hash(second_receipt.block_number + 1)

# Check all collator reward in collators
prev_d_1_balance = get_account_balance(self.substrate, self.delegators[0].ss58_address, first_new_session_block_hash)
now_d_1_balance = get_account_balance(self.substrate, self.delegators[0].ss58_address, second_new_session_block_hash)

self.assertEqual(now_d_1_balance - prev_d_1_balance, 0)
8 changes: 5 additions & 3 deletions tests/token_economy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from substrateinterface import SubstrateInterface, Keypair
from tools.utils import get_modified_chain_spec
from tools.constants import WS_URL
from tools.constants import WS_URL, ACA_WS_URL
from peaq.utils import get_block_height, get_block_hash, get_chain
from tests.utils_func import restart_parachain_and_runtime_upgrade
from tools.runtime_upgrade import wait_until_block_height
Expand Down Expand Up @@ -66,8 +66,8 @@
'depin_incentivization_percent': 150000000,
},
'peaq-network-fork': {
'treasury_percent': 650000000,
'collators_delegators_percent': 0,
'treasury_percent': 250000000,
'collators_delegators_percent': 400000000,
'coretime_percent': 100000000,
'subsidization_pool_percent': 50000000,
'depin_staking_percent': 50000000,
Expand Down Expand Up @@ -169,6 +169,8 @@ def setUpClass(cls):
wait_until_block_height(SubstrateInterface(url=WS_URL), 1)

def setUp(self):
wait_until_block_height(SubstrateInterface(url=WS_URL), 2)
wait_until_block_height(SubstrateInterface(url=ACA_WS_URL), 2)
self._substrate = SubstrateInterface(url=WS_URL)
current_height = get_block_height(self._substrate)
self._block_hash = get_block_hash(self._substrate, current_height)
Expand Down
Loading

0 comments on commit 6f4fd1f

Please sign in to comment.