Skip to content

Commit d3e267c

Browse files
authored
Merge pull request #78 from peaqnetwork/feat/1208684949341989_merge_back
Feat/1208684949341989 merge back
2 parents fc7d892 + 103d460 commit d3e267c

23 files changed

+991
-46
lines changed

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
eventlet==0.33.3
22
requests==2.31.0
3-
scalecodec==1.2.7
4-
substrate_interface==1.7.4
3+
substrate_interface==1.7.11
54
behave==1.2.6
65
peaq-py==0.2.2
76
eth-account==0.9.0
87
web3==6.11.2
98
pytest==7.4.3
109
python-on-whales==0.66.0
1110
eth-typing==3.5.2
11+
eth-utils==2.3.1

tests/bridge_balance_erc20_test.py

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ def test_balance_erc20_total_issuance(self):
121121
f'Error: {data} != {total_balance.value}')
122122

123123
def test_balance_erc20_balance_of(self):
124+
batch = ExtrinsicBatch(self._substrate, KP_GLOBAL_SUDO)
125+
batch_fund(batch, self._eth_kp_src['substrate'], 100 * 10 ** 18)
126+
receipt = batch.execute()
127+
self.assertTrue(receipt.is_success)
124128
contract = get_contract(self._w3, BALANCE_ERC20_ADDR, BALANCE_ERC20_ABI_FILE)
125129
evm_balance = contract.functions.balanceOf(self._eth_kp_src['eth']).call()
126130
sub_balance = get_account_balance(self._substrate, self._eth_kp_src['substrate'])
+196
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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)

tests/bridge_parachain_staking_test.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import unittest
33
from tests.utils_func import restart_parachain_and_runtime_upgrade
44
from tools.runtime_upgrade import wait_until_block_height
5-
from substrateinterface import SubstrateInterface
6-
from tools.constants import WS_URL, ETH_URL
5+
from substrateinterface import SubstrateInterface, Keypair
6+
from tools.constants import WS_URL, ETH_URL, RELAYCHAIN_WS_URL
77
from tools.peaq_eth_utils import sign_and_submit_evm_transaction
88
from peaq.utils import ExtrinsicBatch
99
from tools.peaq_eth_utils import get_contract
@@ -27,6 +27,7 @@ class bridge_parachain_staking_test(unittest.TestCase):
2727
@classmethod
2828
def setUpClass(cls):
2929
restart_parachain_and_runtime_upgrade()
30+
wait_until_block_height(SubstrateInterface(url=RELAYCHAIN_WS_URL), 1)
3031
wait_until_block_height(SubstrateInterface(url=WS_URL), 1)
3132

3233
def setUp(self):
@@ -37,6 +38,7 @@ def setUp(self):
3738
self._kp_moon = get_eth_info()
3839
self._kp_mars = get_eth_info()
3940
self._eth_chain_id = get_eth_chain_id(self._substrate)
41+
self._kp_src = Keypair.create_from_uri('//Moon')
4042

4143
def _fund_users(self, num=100 * 10 ** 18):
4244
if num < 100 * 10 ** 18:
@@ -59,6 +61,14 @@ def _fund_users(self, num=100 * 10 ** 18):
5961
'new_free': num,
6062
}
6163
)
64+
batch.compose_sudo_call(
65+
'Balances',
66+
'force_set_balance',
67+
{
68+
'who': self._kp_src.ss58_address,
69+
'new_free': num,
70+
}
71+
)
6272
return batch.execute()
6373

6474
def evm_join_delegators(self, contract, eth_kp_src, sub_collator_addr, stake):

tests/bridge_xcmutils_test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
from tests.utils_func import restart_parachain_and_runtime_upgrade
3-
from tools.constants import WS_URL, ETH_URL, ACA_WS_URL
3+
from tools.constants import WS_URL, ETH_URL, ACA_WS_URL, RELAYCHAIN_WS_URL
44
from tools.constants import ACA_PD_CHAIN_ID
55
from tools.runtime_upgrade import wait_until_block_height
66
from tools.peaq_eth_utils import get_contract
@@ -33,6 +33,7 @@ class TestBridgeXCMUtils(unittest.TestCase):
3333
@classmethod
3434
def setUpClass(cls):
3535
restart_parachain_and_runtime_upgrade()
36+
wait_until_block_height(SubstrateInterface(url=RELAYCHAIN_WS_URL), 1)
3637
wait_until_block_height(SubstrateInterface(url=WS_URL), 1)
3738
wait_until_block_height(SubstrateInterface(url=ACA_WS_URL), 1)
3839

tests/evm_eth_rpc_test.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from substrateinterface import SubstrateInterface, Keypair, KeypairType
4+
from tools.runtime_upgrade import wait_until_block_height
45
from peaq.eth import calculate_evm_account, calculate_evm_addr
56
from peaq.extrinsic import transfer
67
from peaq.utils import ExtrinsicBatch
@@ -12,6 +13,7 @@
1213
from tools.peaq_eth_utils import call_eth_transfer_a_lot
1314
from tools.peaq_eth_utils import get_eth_balance, get_contract
1415
from tools.peaq_eth_utils import TX_SUCCESS_STATUS
16+
from peaq.utils import get_account_balance
1517
from tests import utils_func as TestUtils
1618
from tools.peaq_eth_utils import get_eth_info
1719
from tools.utils import batch_fund
@@ -24,7 +26,7 @@
2426
ERC_TOKEN_TRANSFER = 34
2527
HEX_STR = '1111'
2628
GAS_LIMIT = 4294967
27-
TOKEN_NUM = 10000 * pow(10, 15)
29+
TOKEN_NUM = 10 * 10 ** 18
2830
ABI_FILE = 'ETH/identity/abi'
2931
TOKEN_NUM_BASE = pow(10, 18)
3032

@@ -73,6 +75,7 @@ def call_copy(w3, address, kp_src, eth_chain_id, file_name, data):
7375
@pytest.mark.eth
7476
class TestEVMEthRPC(unittest.TestCase):
7577
def setUp(self):
78+
wait_until_block_height(SubstrateInterface(url=WS_URL), 3)
7679
self._conn = SubstrateInterface(url=WS_URL)
7780
self._eth_chain_id = get_eth_chain_id(self._conn)
7881
self._kp_src = Keypair.create_from_uri('//Alice')
@@ -82,6 +85,19 @@ def setUp(self):
8285
self._w3 = Web3(Web3.HTTPProvider(ETH_URL))
8386
self._eth_deposited_src = calculate_evm_account(self._eth_src)
8487

88+
def test_evm_api_balance_same(self):
89+
self._kp_moon = get_eth_info()
90+
self._kp_mars = get_eth_info()
91+
92+
batch = ExtrinsicBatch(self._conn, KP_GLOBAL_SUDO)
93+
batch_fund(batch, self._kp_moon['substrate'], int(1.05 * 10 ** 18))
94+
receipt = batch.execute()
95+
self.assertTrue(receipt.is_success)
96+
97+
sub_balance = get_account_balance(self._conn, self._kp_moon['substrate'])
98+
eth_balance = self._w3.eth.get_balance(self._kp_moon['kp'].ss58_address)
99+
self.assertEqual(sub_balance, eth_balance, f"sub: {sub_balance} != eth: {eth_balance}")
100+
85101
@pytest.mark.skipif(TestUtils.is_not_peaq_chain() is True, reason='Only peaq chain evm tx change')
86102
def test_evm_fee(self):
87103
self._kp_moon = get_eth_info()
@@ -93,13 +109,14 @@ def test_evm_fee(self):
93109

94110
prev_balance = self._w3.eth.get_balance(self._kp_moon['kp'].ss58_address)
95111
nonce = self._w3.eth.get_transaction_count(self._kp_moon['kp'].ss58_address)
112+
# gas/maxFeePerGas/maxPriorityFeePerGas is decided by metamask's value
96113
tx = {
97114
'from': self._kp_moon['kp'].ss58_address,
98115
'to': self._kp_mars['kp'].ss58_address,
99116
'value': 1 * 10 ** 18,
100117
'gas': 21000,
101-
'maxFeePerGas': 100 * 10 ** 9,
102-
'maxPriorityFeePerGas': 1 * 10 ** 9,
118+
'maxFeePerGas': 1000 * 10 ** 9,
119+
'maxPriorityFeePerGas': 1000 * 10 ** 9,
103120
'nonce': nonce,
104121
'chainId': self._eth_chain_id
105122
}

tests/existential_deposits_test.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from substrateinterface import SubstrateInterface, Keypair
44
from tools.utils import get_existential_deposit
55
from tools.constants import WS_URL
6-
from peaq.extrinsic import transfer
76

87

98
@pytest.mark.substrate
9+
@pytest.mark.skip(reason="Only test for the charging simulator")
1010
class TestExitentialDeposits(unittest.TestCase):
1111
def get_existential_deposit(self):
1212
return get_existential_deposit(self.substrate)
@@ -18,17 +18,4 @@ def setUp(self):
1818

1919
def test_local_token(self):
2020
token = self.get_existential_deposit()
21-
self.assertGreater(token, 2)
22-
token /= 2
23-
24-
# Execute -> Send local token to another account but below the Exitential Deposits
25-
receipt = transfer(
26-
self.substrate,
27-
self.alice,
28-
self.kp.ss58_address,
29-
token,
30-
1
31-
)
32-
33-
# Check: the error happens
34-
self.assertFalse(receipt.is_success)
21+
self.assertEqual(token, 0)

tests/metadata_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
import pytest
3+
4+
from substrateinterface import SubstrateInterface
5+
from tools.constants import WS_URL
6+
7+
8+
@pytest.mark.substrate
9+
class TestMetadata(unittest.TestCase):
10+
def setUp(self):
11+
self.substrate = SubstrateInterface(url=WS_URL)
12+
13+
def test_check_meta(self):
14+
metadata = self.substrate.get_block_metadata()
15+
self.assertTrue('frame_metadata_hash_extension' in str(metadata.value))
16+
self.assertTrue('CheckMetadataHash' in str(metadata.value))

0 commit comments

Comments
 (0)