|
| 1 | +import pytest |
| 2 | +import unittest |
| 3 | +from tests.utils_func import restart_parachain_and_runtime_upgrade |
| 4 | +from tools.runtime_upgrade import wait_until_block_height |
| 5 | +from substrateinterface import SubstrateInterface, Keypair |
| 6 | +from tools.constants import WS_URL, ETH_URL, RELAYCHAIN_WS_URL |
| 7 | +from tools.peaq_eth_utils import sign_and_submit_evm_transaction |
| 8 | +from peaq.utils import ExtrinsicBatch |
| 9 | +from tools.peaq_eth_utils import get_contract |
| 10 | +from tools.peaq_eth_utils import get_eth_chain_id |
| 11 | +from tools.peaq_eth_utils import get_eth_info |
| 12 | +from tools.constants import KP_GLOBAL_SUDO, KP_COLLATOR |
| 13 | +from peaq.utils import get_block_hash |
| 14 | +from web3 import Web3 |
| 15 | + |
| 16 | + |
| 17 | +PARACHAIN_STAKING_ABI_FILE = 'ETH/parachain-staking/abi' |
| 18 | +PARACHAIN_STAKING_ADDR = '0x0000000000000000000000000000000000000807' |
| 19 | + |
| 20 | + |
| 21 | +# [TODO] Should refine the functions |
| 22 | +@pytest.mark.relaunch |
| 23 | +@pytest.mark.eth |
| 24 | +class bridge_parachain_staking_collators_test(unittest.TestCase): |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def setUpClass(cls): |
| 28 | + restart_parachain_and_runtime_upgrade() |
| 29 | + wait_until_block_height(SubstrateInterface(url=RELAYCHAIN_WS_URL), 1) |
| 30 | + wait_until_block_height(SubstrateInterface(url=WS_URL), 1) |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + wait_until_block_height(SubstrateInterface(url=WS_URL), 1) |
| 34 | + |
| 35 | + self._substrate = SubstrateInterface(url=WS_URL) |
| 36 | + self._w3 = Web3(Web3.HTTPProvider(ETH_URL)) |
| 37 | + self._kp_moon = get_eth_info() |
| 38 | + self._kp_mars = get_eth_info() |
| 39 | + self._eth_chain_id = get_eth_chain_id(self._substrate) |
| 40 | + self._kp_src = Keypair.create_from_uri('//Moon') |
| 41 | + |
| 42 | + def _fund_users(self, num=100 * 10 ** 18): |
| 43 | + if num < 100 * 10 ** 18: |
| 44 | + num = 100 * 10 ** 18 |
| 45 | + # Fund users |
| 46 | + batch = ExtrinsicBatch(self._substrate, KP_GLOBAL_SUDO) |
| 47 | + batch.compose_sudo_call( |
| 48 | + 'Balances', |
| 49 | + 'force_set_balance', |
| 50 | + { |
| 51 | + 'who': self._kp_moon['substrate'], |
| 52 | + 'new_free': num, |
| 53 | + } |
| 54 | + ) |
| 55 | + batch.compose_sudo_call( |
| 56 | + 'Balances', |
| 57 | + 'force_set_balance', |
| 58 | + { |
| 59 | + 'who': self._kp_mars['substrate'], |
| 60 | + 'new_free': num, |
| 61 | + } |
| 62 | + ) |
| 63 | + batch.compose_sudo_call( |
| 64 | + 'Balances', |
| 65 | + 'force_set_balance', |
| 66 | + { |
| 67 | + 'who': self._kp_src.ss58_address, |
| 68 | + 'new_free': num, |
| 69 | + } |
| 70 | + ) |
| 71 | + return batch.execute() |
| 72 | + |
| 73 | + def evm_join_delegators(self, contract, eth_kp_src, sub_collator_addr, stake): |
| 74 | + w3 = self._w3 |
| 75 | + nonce = w3.eth.get_transaction_count(eth_kp_src.ss58_address) |
| 76 | + tx = contract.functions.joinDelegators(sub_collator_addr, stake).build_transaction({ |
| 77 | + 'from': eth_kp_src.ss58_address, |
| 78 | + 'nonce': nonce, |
| 79 | + 'chainId': self._eth_chain_id}) |
| 80 | + |
| 81 | + return sign_and_submit_evm_transaction(tx, w3, eth_kp_src) |
| 82 | + |
| 83 | + def evm_delegate_another_candidate(self, contract, eth_kp_src, sub_collator_addr, stake): |
| 84 | + w3 = self._w3 |
| 85 | + nonce = w3.eth.get_transaction_count(eth_kp_src.ss58_address) |
| 86 | + tx = contract.functions.delegateAnotherCandidate(sub_collator_addr, stake).build_transaction({ |
| 87 | + 'from': eth_kp_src.ss58_address, |
| 88 | + 'nonce': nonce, |
| 89 | + 'chainId': self._eth_chain_id}) |
| 90 | + |
| 91 | + return sign_and_submit_evm_transaction(tx, w3, eth_kp_src) |
| 92 | + |
| 93 | + def evm_delegator_leave_delegators(self, contract, eth_kp_src): |
| 94 | + w3 = self._w3 |
| 95 | + nonce = w3.eth.get_transaction_count(eth_kp_src.ss58_address) |
| 96 | + tx = contract.functions.leaveDelegators().build_transaction({ |
| 97 | + 'from': eth_kp_src.ss58_address, |
| 98 | + 'nonce': nonce, |
| 99 | + 'chainId': self._eth_chain_id}) |
| 100 | + |
| 101 | + return sign_and_submit_evm_transaction(tx, w3, eth_kp_src) |
| 102 | + |
| 103 | + def evm_delegator_revoke_delegation(self, contract, eth_kp_src, sub_collator_addr): |
| 104 | + w3 = self._w3 |
| 105 | + nonce = w3.eth.get_transaction_count(eth_kp_src.ss58_address) |
| 106 | + tx = contract.functions.revokeDelegation(sub_collator_addr).build_transaction({ |
| 107 | + 'from': eth_kp_src.ss58_address, |
| 108 | + 'nonce': nonce, |
| 109 | + 'chainId': self._eth_chain_id}) |
| 110 | + |
| 111 | + return sign_and_submit_evm_transaction(tx, w3, eth_kp_src) |
| 112 | + |
| 113 | + def evm_delegator_unlock_unstaked(self, contract, eth_kp_src, eth_addr): |
| 114 | + w3 = self._w3 |
| 115 | + nonce = w3.eth.get_transaction_count(eth_kp_src.ss58_address) |
| 116 | + tx = contract.functions.unlockUnstaked(eth_addr).build_transaction({ |
| 117 | + 'from': eth_kp_src.ss58_address, |
| 118 | + 'nonce': nonce, |
| 119 | + 'chainId': self._eth_chain_id}) |
| 120 | + |
| 121 | + return sign_and_submit_evm_transaction(tx, w3, eth_kp_src) |
| 122 | + |
| 123 | + def get_stake_number(self, sub_addr): |
| 124 | + data = self._substrate.query('ParachainStaking', 'DelegatorState', [sub_addr]) |
| 125 | + # {'delegations': |
| 126 | + # [{'owner': '5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL', 'amount': 262144000}], |
| 127 | + # 'total': 262144000} |
| 128 | + return data.value |
| 129 | + |
| 130 | + def get_event(self, block_hash, module, event): |
| 131 | + events = self._substrate.get_events(block_hash) |
| 132 | + for e in events: |
| 133 | + if e.value['event']['module_id'] == module and e.value['event']['event_id'] == event: |
| 134 | + return {'attributes': e.value['event']['attributes']} |
| 135 | + return None |
| 136 | + |
| 137 | + def test_delegator_another_candidate(self): |
| 138 | + contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE) |
| 139 | + out = contract.functions.getCollatorList().call() |
| 140 | + collator_num = out[0][1] |
| 141 | + collator_eth_addr = out[0][0] |
| 142 | + |
| 143 | + # Fund users |
| 144 | + receipt = self._fund_users(collator_num * 3) |
| 145 | + self.assertEqual(receipt.is_success, True, f'fund_users fails, receipt: {receipt}') |
| 146 | + |
| 147 | + evm_receipt = self.evm_join_delegators(contract, self._kp_moon['kp'], collator_eth_addr, collator_num) |
| 148 | + self.assertEqual(evm_receipt['status'], 1, f'join fails, evm_receipt: {evm_receipt}') |
| 149 | + bl_hash = get_block_hash(self._substrate, evm_receipt['blockNumber']) |
| 150 | + event = self.get_event(bl_hash, 'ParachainStaking', 'Delegation') |
| 151 | + self.assertEqual( |
| 152 | + event['attributes'], |
| 153 | + (self._kp_moon['substrate'], collator_num, KP_COLLATOR.ss58_address, 2 * collator_num), |
| 154 | + f'join fails, event: {event}') |
| 155 | + |
| 156 | + # Check the delegator's stake |
| 157 | + stake = self.get_stake_number(self._kp_moon['substrate']) |
| 158 | + self.assertEqual(stake['total'], collator_num, f'join fails, stake: {stake}, collator_num: {collator_num}') |
| 159 | + |
| 160 | + batch = ExtrinsicBatch(self._substrate, self._kp_src) |
| 161 | + batch.compose_call( |
| 162 | + 'ParachainStaking', |
| 163 | + 'join_candidates', |
| 164 | + { |
| 165 | + 'stake': collator_num |
| 166 | + } |
| 167 | + ) |
| 168 | + receipt = batch.execute() |
| 169 | + self.assertEqual(receipt.is_success, True, f'joinCandidate fails, receipt: {receipt}') |
| 170 | + |
| 171 | + # Force session * 2 |
| 172 | + batch = ExtrinsicBatch(self._substrate, KP_GLOBAL_SUDO) |
| 173 | + batch.compose_sudo_call( |
| 174 | + 'ParachainStaking', |
| 175 | + 'force_new_round', |
| 176 | + {} |
| 177 | + ) |
| 178 | + receipt = batch.execute() |
| 179 | + self.assertEqual(receipt.is_success, True, f'force_new_round fails, receipt: {receipt}') |
| 180 | + receipt = batch.execute() |
| 181 | + self.assertEqual(receipt.is_success, True, f'force_new_round fails, receipt: {receipt}') |
| 182 | + |
| 183 | + out = contract.functions.getCollatorList().call() |
| 184 | + collator_eth_addr = out[0][0] |
| 185 | + collator_eth_addr = [out[i][0] for i in range(len(out)) if out[i][0] != collator_eth_addr][0] |
| 186 | + |
| 187 | + evm_receipt = self.evm_delegate_another_candidate(contract, self._kp_moon['kp'], collator_eth_addr, collator_num) |
| 188 | + self.assertEqual(evm_receipt['status'], 1, f'join fails, evm_receipt: {evm_receipt}') |
| 189 | + stake = self.get_stake_number(self._kp_moon['substrate']) |
| 190 | + self.assertEqual(stake['total'], collator_num * 2, f'join fails, stake: {stake}, collator_num: {collator_num * 2}') |
| 191 | + |
| 192 | + # Force leave all |
| 193 | + evm_receipt = self.evm_delegator_leave_delegators(contract, self._kp_moon['kp']) |
| 194 | + self.assertEqual(evm_receipt['status'], 1, f'leave fails, evm_receipt: {evm_receipt}') |
| 195 | + stake = self.get_stake_number(self._kp_moon['substrate']) |
| 196 | + self.assertEqual(stake, None) |
0 commit comments