Skip to content

Commit

Permalink
Add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypan authored and Jay Pan committed Dec 18, 2024
1 parent 9f84c64 commit 3d575ed
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ETH/parachain-staking/abi
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,41 @@
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "commission",
"type": "uint256"
}
],
"internalType": "struct ParachainStaking.CollatorInfo[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getWaitList",
"outputs": [
{
"components": [
{
"internalType": "bytes32",
"name": "owner",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "commission",
"type": "uint256"
}
],
"internalType": "struct ParachainStaking.CollatorInfo[]",
Expand Down
112 changes: 112 additions & 0 deletions tests/bridge_parachain_staking_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def setUp(self):
self._kp_mars = get_eth_info()
self._eth_chain_id = get_eth_chain_id(self._substrate)
self._kp_src = Keypair.create_from_uri('//Moon')
self._kp_new_collator = Keypair.create_from_uri('//NewMoon01')

def _fund_users(self, num=100 * 10 ** 18):
if num < 100 * 10 ** 18:
Expand Down Expand Up @@ -70,6 +71,14 @@ def _fund_users(self, num=100 * 10 ** 18):
'new_free': num,
}
)
batch.compose_sudo_call(
'Balances',
'force_set_balance',
{
'who': self._kp_new_collator.ss58_address,
'new_free': num,
}
)
return batch.execute()

def evm_join_delegators(self, contract, eth_kp_src, sub_collator_addr, stake):
Expand Down Expand Up @@ -293,6 +302,109 @@ def test_delegator_join_more_less_leave(self):
# Note: The unlock unstaked didn't success because we have to wait about 20+ blocks;
# therefore, we don't test here. Can just test maunally

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

def test_commission_rate(self):
# Set commission rate as 20
receipt = self.set_commission_rate(20)
self.assertEqual(receipt.is_success, True, f'set_commission fails, receipt: {receipt}')

contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
all_colators_info = self._substrate.query_map(
module='ParachainStaking',
storage_function='CandidatePool',
params=[],
start_key=None,
page_size=1000,
)

evm_out = {info[0]: {
'amount': info[1],
'commission': info[2],
} for info in out}

for collator_id, collator_info in all_colators_info.records:
pk = bytes.fromhex(self._substrate.ss58_decode(collator_info.value['id']))
self.assertEqual(
evm_out[pk]['commission'],
collator_info.value['commission'],
f'commission rate fails, out: {out}, all_colators_info: {all_colators_info}')
self.assertEqual(
evm_out[pk]['amount'],
collator_info.value['stake'],
f'commission rate fails, out: {out}, all_colators_info: {all_colators_info}')

receipt = self.set_commission_rate(0)
self.assertEqual(receipt.is_success, True, f'set_commission fails, receipt: {receipt}')

def set_parachain_staking_settings(self, length):
batch = ExtrinsicBatch(self._substrate, KP_GLOBAL_SUDO)
batch.compose_sudo_call(
'ParachainStaking',
'set_blocks_per_round',
{
'new': length,
}
)
batch.compose_sudo_call(
'ParachainStaking',
'set_max_selected_candidates',
{
'new': 4
}
)
batch.compose_sudo_call(
'ParachainStaking',
'force_new_round',
{}
)
return batch.execute()

def test_wait_list(self):
receipt = self.set_parachain_staking_settings(100000)
self.assertEqual(receipt.is_success, True, f'set_blocks_per_round fails, receipt: {receipt}')

contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
collator_num = out[0][1]
receipt = self._fund_users(collator_num * 2)

# Join one collator
batch = ExtrinsicBatch(self._substrate, self._kp_new_collator)
batch.compose_call(
'ParachainStaking',
'join_candidates',
{
'stake': collator_num,
}
)
receipt = batch.execute()
self.assertEqual(receipt.is_success, True, f'join_collator fails, receipt: {receipt}')
receipt = self.set_commission_rate(10, self._kp_new_collator)
self.assertEqual(receipt.is_success, True, f'set_commission fails, receipt: {receipt}')

wait_list = contract.functions.getWaitList().call()
self.assertEqual(len(wait_list), 1)
pk = bytes.fromhex(self._substrate.ss58_decode(self._kp_new_collator.ss58_address))
self.assertEqual(wait_list[0][0], pk)
self.assertEqual(wait_list[0][1], collator_num)
self.assertEqual(wait_list[0][2], 10 * 10_000)

# Check the wait list
collator_list = contract.functions.getCollatorList().call()
self.assertEqual(len(collator_list), 2)
self.assertTrue(wait_list[0][0] in [collator[0] for collator in collator_list])

def test_delegator_revoke(self):
contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
Expand Down

0 comments on commit 3d575ed

Please sign in to comment.