Skip to content

Commit

Permalink
Merge pull request #78 from peaqnetwork/feat/1208684949341989_merge_back
Browse files Browse the repository at this point in the history
Feat/1208684949341989 merge back
  • Loading branch information
sfffaaa authored Nov 12, 2024
2 parents fc7d892 + 103d460 commit d3e267c
Show file tree
Hide file tree
Showing 23 changed files with 991 additions and 46 deletions.
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
eventlet==0.33.3
requests==2.31.0
scalecodec==1.2.7
substrate_interface==1.7.4
substrate_interface==1.7.11
behave==1.2.6
peaq-py==0.2.2
eth-account==0.9.0
web3==6.11.2
pytest==7.4.3
python-on-whales==0.66.0
eth-typing==3.5.2
eth-utils==2.3.1
4 changes: 4 additions & 0 deletions tests/bridge_balance_erc20_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def test_balance_erc20_total_issuance(self):
f'Error: {data} != {total_balance.value}')

def test_balance_erc20_balance_of(self):
batch = ExtrinsicBatch(self._substrate, KP_GLOBAL_SUDO)
batch_fund(batch, self._eth_kp_src['substrate'], 100 * 10 ** 18)
receipt = batch.execute()
self.assertTrue(receipt.is_success)
contract = get_contract(self._w3, BALANCE_ERC20_ADDR, BALANCE_ERC20_ABI_FILE)
evm_balance = contract.functions.balanceOf(self._eth_kp_src['eth']).call()
sub_balance = get_account_balance(self._substrate, self._eth_kp_src['substrate'])
Expand Down
196 changes: 196 additions & 0 deletions tests/bridge_multiple_collator_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import pytest
import unittest
from tests.utils_func import restart_parachain_and_runtime_upgrade
from tools.runtime_upgrade import wait_until_block_height
from substrateinterface import SubstrateInterface, Keypair
from tools.constants import WS_URL, ETH_URL, RELAYCHAIN_WS_URL
from tools.peaq_eth_utils import sign_and_submit_evm_transaction
from peaq.utils import ExtrinsicBatch
from tools.peaq_eth_utils import get_contract
from tools.peaq_eth_utils import get_eth_chain_id
from tools.peaq_eth_utils import get_eth_info
from tools.constants import KP_GLOBAL_SUDO, KP_COLLATOR
from peaq.utils import get_block_hash
from web3 import Web3


PARACHAIN_STAKING_ABI_FILE = 'ETH/parachain-staking/abi'
PARACHAIN_STAKING_ADDR = '0x0000000000000000000000000000000000000807'


# [TODO] Should refine the functions
@pytest.mark.relaunch
@pytest.mark.eth
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)

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

self._substrate = SubstrateInterface(url=WS_URL)
self._w3 = Web3(Web3.HTTPProvider(ETH_URL))
self._kp_moon = get_eth_info()
self._kp_mars = get_eth_info()
self._eth_chain_id = get_eth_chain_id(self._substrate)
self._kp_src = Keypair.create_from_uri('//Moon')

def _fund_users(self, num=100 * 10 ** 18):
if num < 100 * 10 ** 18:
num = 100 * 10 ** 18
# Fund users
batch = ExtrinsicBatch(self._substrate, KP_GLOBAL_SUDO)
batch.compose_sudo_call(
'Balances',
'force_set_balance',
{
'who': self._kp_moon['substrate'],
'new_free': num,
}
)
batch.compose_sudo_call(
'Balances',
'force_set_balance',
{
'who': self._kp_mars['substrate'],
'new_free': num,
}
)
batch.compose_sudo_call(
'Balances',
'force_set_balance',
{
'who': self._kp_src.ss58_address,
'new_free': num,
}
)
return batch.execute()

def evm_join_delegators(self, contract, eth_kp_src, sub_collator_addr, stake):
w3 = self._w3
nonce = w3.eth.get_transaction_count(eth_kp_src.ss58_address)
tx = contract.functions.joinDelegators(sub_collator_addr, stake).build_transaction({
'from': eth_kp_src.ss58_address,
'nonce': nonce,
'chainId': self._eth_chain_id})

return sign_and_submit_evm_transaction(tx, w3, eth_kp_src)

def evm_delegate_another_candidate(self, contract, eth_kp_src, sub_collator_addr, stake):
w3 = self._w3
nonce = w3.eth.get_transaction_count(eth_kp_src.ss58_address)
tx = contract.functions.delegateAnotherCandidate(sub_collator_addr, stake).build_transaction({
'from': eth_kp_src.ss58_address,
'nonce': nonce,
'chainId': self._eth_chain_id})

return sign_and_submit_evm_transaction(tx, w3, eth_kp_src)

def evm_delegator_leave_delegators(self, contract, eth_kp_src):
w3 = self._w3
nonce = w3.eth.get_transaction_count(eth_kp_src.ss58_address)
tx = contract.functions.leaveDelegators().build_transaction({
'from': eth_kp_src.ss58_address,
'nonce': nonce,
'chainId': self._eth_chain_id})

return sign_and_submit_evm_transaction(tx, w3, eth_kp_src)

def evm_delegator_revoke_delegation(self, contract, eth_kp_src, sub_collator_addr):
w3 = self._w3
nonce = w3.eth.get_transaction_count(eth_kp_src.ss58_address)
tx = contract.functions.revokeDelegation(sub_collator_addr).build_transaction({
'from': eth_kp_src.ss58_address,
'nonce': nonce,
'chainId': self._eth_chain_id})

return sign_and_submit_evm_transaction(tx, w3, eth_kp_src)

def evm_delegator_unlock_unstaked(self, contract, eth_kp_src, eth_addr):
w3 = self._w3
nonce = w3.eth.get_transaction_count(eth_kp_src.ss58_address)
tx = contract.functions.unlockUnstaked(eth_addr).build_transaction({
'from': eth_kp_src.ss58_address,
'nonce': nonce,
'chainId': self._eth_chain_id})

return sign_and_submit_evm_transaction(tx, w3, eth_kp_src)

def get_stake_number(self, sub_addr):
data = self._substrate.query('ParachainStaking', 'DelegatorState', [sub_addr])
# {'delegations':
# [{'owner': '5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL', 'amount': 262144000}],
# 'total': 262144000}
return data.value

def get_event(self, block_hash, module, event):
events = self._substrate.get_events(block_hash)
for e in events:
if e.value['event']['module_id'] == module and e.value['event']['event_id'] == event:
return {'attributes': e.value['event']['attributes']}
return None

def test_delegator_another_candidate(self):
contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE)
out = contract.functions.getCollatorList().call()
collator_num = out[0][1]
collator_eth_addr = out[0][0]

# Fund users
receipt = self._fund_users(collator_num * 3)
self.assertEqual(receipt.is_success, True, f'fund_users fails, receipt: {receipt}')

evm_receipt = self.evm_join_delegators(contract, self._kp_moon['kp'], collator_eth_addr, collator_num)
self.assertEqual(evm_receipt['status'], 1, f'join fails, evm_receipt: {evm_receipt}')
bl_hash = get_block_hash(self._substrate, evm_receipt['blockNumber'])
event = self.get_event(bl_hash, 'ParachainStaking', 'Delegation')
self.assertEqual(
event['attributes'],
(self._kp_moon['substrate'], collator_num, KP_COLLATOR.ss58_address, 2 * collator_num),
f'join fails, event: {event}')

# Check the delegator's stake
stake = self.get_stake_number(self._kp_moon['substrate'])
self.assertEqual(stake['total'], collator_num, f'join fails, stake: {stake}, collator_num: {collator_num}')

batch = ExtrinsicBatch(self._substrate, self._kp_src)
batch.compose_call(
'ParachainStaking',
'join_candidates',
{
'stake': collator_num
}
)
receipt = batch.execute()
self.assertEqual(receipt.is_success, True, f'joinCandidate fails, receipt: {receipt}')

# Force session * 2
batch = ExtrinsicBatch(self._substrate, KP_GLOBAL_SUDO)
batch.compose_sudo_call(
'ParachainStaking',
'force_new_round',
{}
)
receipt = batch.execute()
self.assertEqual(receipt.is_success, True, f'force_new_round fails, receipt: {receipt}')
receipt = batch.execute()
self.assertEqual(receipt.is_success, True, f'force_new_round fails, receipt: {receipt}')

out = contract.functions.getCollatorList().call()
collator_eth_addr = out[0][0]
collator_eth_addr = [out[i][0] for i in range(len(out)) if out[i][0] != collator_eth_addr][0]

evm_receipt = self.evm_delegate_another_candidate(contract, self._kp_moon['kp'], collator_eth_addr, collator_num)
self.assertEqual(evm_receipt['status'], 1, f'join fails, evm_receipt: {evm_receipt}')
stake = self.get_stake_number(self._kp_moon['substrate'])
self.assertEqual(stake['total'], collator_num * 2, f'join fails, stake: {stake}, collator_num: {collator_num * 2}')

# Force leave all
evm_receipt = self.evm_delegator_leave_delegators(contract, self._kp_moon['kp'])
self.assertEqual(evm_receipt['status'], 1, f'leave fails, evm_receipt: {evm_receipt}')
stake = self.get_stake_number(self._kp_moon['substrate'])
self.assertEqual(stake, None)
14 changes: 12 additions & 2 deletions tests/bridge_parachain_staking_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import unittest
from tests.utils_func import restart_parachain_and_runtime_upgrade
from tools.runtime_upgrade import wait_until_block_height
from substrateinterface import SubstrateInterface
from tools.constants import WS_URL, ETH_URL
from substrateinterface import SubstrateInterface, Keypair
from tools.constants import WS_URL, ETH_URL, RELAYCHAIN_WS_URL
from tools.peaq_eth_utils import sign_and_submit_evm_transaction
from peaq.utils import ExtrinsicBatch
from tools.peaq_eth_utils import get_contract
Expand All @@ -27,6 +27,7 @@ class bridge_parachain_staking_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)

def setUp(self):
Expand All @@ -37,6 +38,7 @@ def setUp(self):
self._kp_moon = get_eth_info()
self._kp_mars = get_eth_info()
self._eth_chain_id = get_eth_chain_id(self._substrate)
self._kp_src = Keypair.create_from_uri('//Moon')

def _fund_users(self, num=100 * 10 ** 18):
if num < 100 * 10 ** 18:
Expand All @@ -59,6 +61,14 @@ def _fund_users(self, num=100 * 10 ** 18):
'new_free': num,
}
)
batch.compose_sudo_call(
'Balances',
'force_set_balance',
{
'who': self._kp_src.ss58_address,
'new_free': num,
}
)
return batch.execute()

def evm_join_delegators(self, contract, eth_kp_src, sub_collator_addr, stake):
Expand Down
3 changes: 2 additions & 1 deletion tests/bridge_xcmutils_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from tests.utils_func import restart_parachain_and_runtime_upgrade
from tools.constants import WS_URL, ETH_URL, ACA_WS_URL
from tools.constants import WS_URL, ETH_URL, ACA_WS_URL, RELAYCHAIN_WS_URL
from tools.constants import ACA_PD_CHAIN_ID
from tools.runtime_upgrade import wait_until_block_height
from tools.peaq_eth_utils import get_contract
Expand Down Expand Up @@ -33,6 +33,7 @@ class TestBridgeXCMUtils(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=ACA_WS_URL), 1)

Expand Down
23 changes: 20 additions & 3 deletions tests/evm_eth_rpc_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from substrateinterface import SubstrateInterface, Keypair, KeypairType
from tools.runtime_upgrade import wait_until_block_height
from peaq.eth import calculate_evm_account, calculate_evm_addr
from peaq.extrinsic import transfer
from peaq.utils import ExtrinsicBatch
Expand All @@ -12,6 +13,7 @@
from tools.peaq_eth_utils import call_eth_transfer_a_lot
from tools.peaq_eth_utils import get_eth_balance, get_contract
from tools.peaq_eth_utils import TX_SUCCESS_STATUS
from peaq.utils import get_account_balance
from tests import utils_func as TestUtils
from tools.peaq_eth_utils import get_eth_info
from tools.utils import batch_fund
Expand All @@ -24,7 +26,7 @@
ERC_TOKEN_TRANSFER = 34
HEX_STR = '1111'
GAS_LIMIT = 4294967
TOKEN_NUM = 10000 * pow(10, 15)
TOKEN_NUM = 10 * 10 ** 18
ABI_FILE = 'ETH/identity/abi'
TOKEN_NUM_BASE = pow(10, 18)

Expand Down Expand Up @@ -73,6 +75,7 @@ def call_copy(w3, address, kp_src, eth_chain_id, file_name, data):
@pytest.mark.eth
class TestEVMEthRPC(unittest.TestCase):
def setUp(self):
wait_until_block_height(SubstrateInterface(url=WS_URL), 3)
self._conn = SubstrateInterface(url=WS_URL)
self._eth_chain_id = get_eth_chain_id(self._conn)
self._kp_src = Keypair.create_from_uri('//Alice')
Expand All @@ -82,6 +85,19 @@ def setUp(self):
self._w3 = Web3(Web3.HTTPProvider(ETH_URL))
self._eth_deposited_src = calculate_evm_account(self._eth_src)

def test_evm_api_balance_same(self):
self._kp_moon = get_eth_info()
self._kp_mars = get_eth_info()

batch = ExtrinsicBatch(self._conn, KP_GLOBAL_SUDO)
batch_fund(batch, self._kp_moon['substrate'], int(1.05 * 10 ** 18))
receipt = batch.execute()
self.assertTrue(receipt.is_success)

sub_balance = get_account_balance(self._conn, self._kp_moon['substrate'])
eth_balance = self._w3.eth.get_balance(self._kp_moon['kp'].ss58_address)
self.assertEqual(sub_balance, eth_balance, f"sub: {sub_balance} != eth: {eth_balance}")

@pytest.mark.skipif(TestUtils.is_not_peaq_chain() is True, reason='Only peaq chain evm tx change')
def test_evm_fee(self):
self._kp_moon = get_eth_info()
Expand All @@ -93,13 +109,14 @@ def test_evm_fee(self):

prev_balance = self._w3.eth.get_balance(self._kp_moon['kp'].ss58_address)
nonce = self._w3.eth.get_transaction_count(self._kp_moon['kp'].ss58_address)
# gas/maxFeePerGas/maxPriorityFeePerGas is decided by metamask's value
tx = {
'from': self._kp_moon['kp'].ss58_address,
'to': self._kp_mars['kp'].ss58_address,
'value': 1 * 10 ** 18,
'gas': 21000,
'maxFeePerGas': 100 * 10 ** 9,
'maxPriorityFeePerGas': 1 * 10 ** 9,
'maxFeePerGas': 1000 * 10 ** 9,
'maxPriorityFeePerGas': 1000 * 10 ** 9,
'nonce': nonce,
'chainId': self._eth_chain_id
}
Expand Down
17 changes: 2 additions & 15 deletions tests/existential_deposits_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from substrateinterface import SubstrateInterface, Keypair
from tools.utils import get_existential_deposit
from tools.constants import WS_URL
from peaq.extrinsic import transfer


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

def test_local_token(self):
token = self.get_existential_deposit()
self.assertGreater(token, 2)
token /= 2

# Execute -> Send local token to another account but below the Exitential Deposits
receipt = transfer(
self.substrate,
self.alice,
self.kp.ss58_address,
token,
1
)

# Check: the error happens
self.assertFalse(receipt.is_success)
self.assertEqual(token, 0)
16 changes: 16 additions & 0 deletions tests/metadata_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import unittest
import pytest

from substrateinterface import SubstrateInterface
from tools.constants import WS_URL


@pytest.mark.substrate
class TestMetadata(unittest.TestCase):
def setUp(self):
self.substrate = SubstrateInterface(url=WS_URL)

def test_check_meta(self):
metadata = self.substrate.get_block_metadata()
self.assertTrue('frame_metadata_hash_extension' in str(metadata.value))
self.assertTrue('CheckMetadataHash' in str(metadata.value))
Loading

0 comments on commit d3e267c

Please sign in to comment.