Skip to content

Commit 70d1f7f

Browse files
committed
tests: add cip_145_fix tests
1 parent e0a14a0 commit 70d1f7f

File tree

3 files changed

+143
-7
lines changed

3 files changed

+143
-7
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import pytest
2+
from typing import Type
3+
from integration_tests.test_framework.test_framework import FrameworkOptions
4+
from integration_tests.tests.conftest import ConfluxTestFramework
5+
from cfx_utils import CFX
6+
from conflux_web3 import Web3
7+
from web3.exceptions import Web3RPCError
8+
9+
10+
class CIP145BeforeFixTestEnv(ConfluxTestFramework):
11+
def set_test_params(self):
12+
self.num_nodes = 1
13+
self.conf_parameters["eoa_code_transition_height"] = 1
14+
self.conf_parameters["cip151_transition_height"] = 1
15+
self.conf_parameters["cip645_transition_height"] = 1
16+
self.conf_parameters["public_evm_rpc_apis"] = '"all"'
17+
self.conf_parameters["executive_trace"] = "true"
18+
19+
def setup_network(self):
20+
self.add_nodes(self.num_nodes)
21+
self.start_node(0, ["--archive"])
22+
23+
class CIP145AfterFixTestEnv(ConfluxTestFramework):
24+
def set_test_params(self):
25+
self.num_nodes = 1
26+
self.conf_parameters["eoa_code_transition_height"] = 1
27+
self.conf_parameters["cip151_transition_height"] = 1
28+
self.conf_parameters["cip645_transition_height"] = 1
29+
self.conf_parameters["cip145_fix_transition_height"] = 1
30+
self.conf_parameters["public_evm_rpc_apis"] = '"all"'
31+
self.conf_parameters["executive_trace"] = "true"
32+
33+
def setup_network(self):
34+
self.add_nodes(self.num_nodes)
35+
self.start_node(0, ["--archive"])
36+
37+
38+
# copied from integration_tests/tests/conftest.py to overide fixture scope
39+
@pytest.fixture
40+
def network(framework_class: Type[ConfluxTestFramework], port_min: int, additional_secrets: int, args: FrameworkOptions, request: pytest.FixtureRequest):
41+
try:
42+
framework = framework_class(port_min, additional_secrets, options=args)
43+
except Exception as e:
44+
pytest.fail(f"Failed to setup framework: {e}")
45+
yield framework
46+
framework.teardown(request)
47+
48+
@pytest.fixture
49+
def gas_sponsored_contract(network):
50+
contract = network.deploy_contract("Receivable")
51+
contract_address = contract.address
52+
sponsor_whitelist_control = network.internal_contract("SponsorWhitelistControl")
53+
54+
# contract_addr, upperbound
55+
sponsor_whitelist_control.functions.setSponsorForGas(contract_address, 10 ** 9).transact({
56+
"value": CFX(1)
57+
}).executed()
58+
sponsor_whitelist_control.functions.addPrivilegeByAdmin(contract_address, [network.cw3.address.zero_address()]).transact().executed()
59+
return contract
60+
61+
def fund_new_account(cw3: Web3, value: int):
62+
account = cw3.cfx.account.create()
63+
cw3.wallet.add_account(account)
64+
cw3.cfx.send_transaction({
65+
"value": value,
66+
"to": account.address,
67+
}).executed()
68+
return account
69+
70+
@pytest.mark.parametrize("framework_class", [CIP145BeforeFixTestEnv, CIP145AfterFixTestEnv])
71+
def test_value_enough_behaviour(network, gas_sponsored_contract):
72+
network.client.generate_blocks_to_state()
73+
cw3 = network.cw3
74+
assert network.internal_contract("SponsorWhitelistControl").functions.isAllWhitelisted(gas_sponsored_contract.address).call()
75+
sender = fund_new_account(cw3, 100000)
76+
77+
raw_tx = sender.sign_transaction({
78+
"to": gas_sponsored_contract.address,
79+
"value": 100000,
80+
"from": sender.address,
81+
"chainId": network.cw3.cfx.chain_id,
82+
"gas": 30000,
83+
"gasPrice": 1,
84+
"storageLimit": 0,
85+
"epochHeight": 10,
86+
"nonce": 0,
87+
})
88+
receipt = cw3.cfx.send_raw_transaction(raw_tx.raw_transaction).executed()
89+
assert receipt["gasCoveredBySponsor"]
90+
91+
@pytest.mark.parametrize("framework_class", [CIP145BeforeFixTestEnv, CIP145AfterFixTestEnv])
92+
def test_cip145_activated_behaviour(network, gas_sponsored_contract):
93+
cw3 = network.cw3
94+
# ensure hardfork transition number
95+
network.client.generate_blocks_to_state()
96+
assert network.internal_contract("SponsorWhitelistControl").functions.isAllWhitelisted(gas_sponsored_contract.address).call()
97+
sender = fund_new_account(cw3, 100000)
98+
99+
raw_tx = sender.sign_transaction({
100+
"to": gas_sponsored_contract.address,
101+
"value": 100001,
102+
"from": sender.address,
103+
"chainId": network.cw3.cfx.chain_id,
104+
"gas": 30000,
105+
"gasPrice": 1,
106+
"storageLimit": 0,
107+
"epochHeight": 10,
108+
"nonce": 0,
109+
})
110+
with pytest.raises(Web3RPCError) as e:
111+
cw3.cfx.send_raw_transaction(raw_tx.raw_transaction)
112+
assert "is discarded due to out of balance, needs 100001 but account balance is 100000" in e.value.message
113+
114+
latest_block = network.cw3.cfx.get_block("latest_mined")
115+
new_block_hash = network.client.generate_custom_block(latest_block["hash"].to_0x_hex(), [], [raw_tx])
116+
parent_block = new_block_hash
117+
for _ in range(10):
118+
parent_block = network.client.generate_block_with_parent(parent_block, num_txs=1)
119+
120+
transaction_hash = raw_tx.hash
121+
transaction_receipt = network.cw3.cfx.get_transaction_receipt(transaction_hash)
122+
assert transaction_receipt["txExecErrorMsg"]== 'NotEnoughCash { required: 100001, got: 100000, actual_gas_cost: 30000, max_storage_limit_cost: 0 }'
123+
assert not transaction_receipt["gasCoveredBySponsor"]
124+
125+
126+

integration_tests/tests/rpc/espace/test_tx_and_receipt.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
from integration_tests.test_framework.test_framework import ConfluxTestFramework
3-
from integration_tests.test_framework.util import *
4-
from eth_utils import decode_hex
3+
from integration_tests.test_framework.util import assert_equal
54
from integration_tests.conflux.rpc import RpcClient
65
from integration_tests.test_framework.blocktools import encode_hex_0x
76

@@ -31,18 +30,29 @@ def test_cross_space_transfer(cw3, ew3, erc20_contract, evm_accounts, network):
3130
epoch = receipt["epochNumber"]
3231
ret = network.nodes[0].debug_getTransactionsByEpoch(hex(epoch))
3332
assert_equal(len(ret), 1)
34-
35-
def test_tx_and_receipt(ew3, evm_accounts, receiver_account, network):
33+
34+
@pytest.fixture(scope="module")
35+
def tx_hash(ew3, evm_accounts, receiver_account):
3636
account = evm_accounts[0]
37-
nonce = ew3.eth.get_transaction_count(account.address)
3837
tx_hash = ew3.eth.send_transaction({
3938
"from": account.address,
4039
"to": receiver_account.address,
4140
"value": ew3.to_wei(1, "ether"),
4241
"gasPrice": 1,
4342
"gas": 21000,
44-
"nonce": nonce,
4543
})
44+
ew3.eth.wait_for_transaction_receipt(tx_hash)
45+
return tx_hash
46+
47+
def test_tx_data(ew3, tx_hash, receiver_account):
48+
data = ew3.eth.get_transaction(tx_hash)
49+
assert data["gasPrice"] == 1
50+
assert data["gas"] == 21000
51+
assert data["to"] == receiver_account.address
52+
assert data["value"] == ew3.to_wei(1, "ether")
53+
54+
55+
def test_tx_and_receipt(ew3, network, tx_hash):
4656
receipt = ew3.eth.wait_for_transaction_receipt(tx_hash)
4757
assert receipt["status"] == 1
4858
assert receipt["gasUsed"] == 21000

0 commit comments

Comments
 (0)