From 3d575edd72b68b621c0fd722235ff692d7d0db8e Mon Sep 17 00:00:00 2001 From: jaypan Date: Thu, 19 Dec 2024 00:24:22 +0100 Subject: [PATCH] Add testing --- ETH/parachain-staking/abi | 35 ++++++++ tests/bridge_parachain_staking_test.py | 112 +++++++++++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/ETH/parachain-staking/abi b/ETH/parachain-staking/abi index ca41663..aa20f2c 100644 --- a/ETH/parachain-staking/abi +++ b/ETH/parachain-staking/abi @@ -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[]", diff --git a/tests/bridge_parachain_staking_test.py b/tests/bridge_parachain_staking_test.py index 6d078aa..68d3847 100644 --- a/tests/bridge_parachain_staking_test.py +++ b/tests/bridge_parachain_staking_test.py @@ -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: @@ -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): @@ -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()