Skip to content

Commit b1d1575

Browse files
authored
Merge pull request #3248 from darwintree/internal-contracts-test
refactor: migrate SponsorControlTest tests
2 parents 9b00b9c + 8863402 commit b1d1575

File tree

2 files changed

+112
-88
lines changed

2 files changed

+112
-88
lines changed

tests/commission_privilege_test.py renamed to integration_tests/tests/internal_contracts/commission_privilege_test.py

Lines changed: 73 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,66 @@
1-
#!/usr/bin/env python3
2-
from conflux.transactions import CONTRACT_DEFAULT_GAS, COLLATERAL_UNIT_IN_DRIP, charged_of_huge_gas
3-
from test_framework.test_framework import ConfluxTestFramework
4-
from test_framework.mininode import *
5-
from test_framework.util import *
6-
7-
class CommissionPrivilegeTest(ConfluxTestFramework):
8-
def set_test_params(self):
9-
self.num_nodes = 1
10-
11-
def run_test(self):
12-
self.w3 = self.cw3
13-
self.sponsorControl = self.internal_contract(name="SponsorWhitelistControl")
1+
# Migrated from tests/commission_privilege_test.py
2+
3+
import pytest
4+
from integration_tests.test_framework.test_framework import ConfluxTestFramework
5+
from integration_tests.test_framework.util import assert_equal, assert_tx_exec_error, assert_greater_than
6+
from integration_tests.conflux.transactions import COLLATERAL_UNIT_IN_DRIP, CONTRACT_DEFAULT_GAS, charged_of_huge_gas
7+
from integration_tests.conflux.config import default_config
8+
from integration_tests.conflux.address import ZERO_ADDRESS
9+
10+
@pytest.fixture(scope="module")
11+
def framework_class():
12+
class CommissionPrivilegeTest(ConfluxTestFramework):
13+
def set_test_params(self):
14+
self.num_nodes = 1
15+
return CommissionPrivilegeTest
16+
17+
def test_commission_privilege(network):
18+
# introduce extra indent to make it easier for code review
19+
# can be removed after code review
20+
if True:
21+
network.sponsorControl = network.internal_contract(name="SponsorWhitelistControl")
1422
bytes_per_key = 64
1523
collateral_per_storage_key = COLLATERAL_UNIT_IN_DRIP * 64
1624
# change upper tx gas limit to (GENESIS_GAS_LIMIT/2 - 1); -1 because below gas is set to upper_bound + 1
1725
tx_gas_upper_bound = int(default_config["GENESIS_GAS_LIMIT"] / 2 - 1) # type: ignore
1826

19-
self.log.info("Initializing contract")
20-
genesis_addr = self.core_accounts[0].hex_address
21-
self.log.info("genesis_addr={}".format(genesis_addr))
27+
network.log.info("Initializing contract")
28+
genesis_addr = network.core_accounts[0].hex_address
29+
network.log.info("genesis_addr={}".format(genesis_addr))
2230
gas_price = 1
2331
gas = CONTRACT_DEFAULT_GAS
24-
self.start_block_gen()
32+
network.start_block_gen()
2533

26-
client = self.client
34+
client = network.client
2735
(addr1, priv_key1) = client.rand_account()
2836
(addr2, priv_key2) = client.rand_account()
2937
(addr3, priv_key3) = client.rand_account()
3038
(addr4, priv_key4) = client.rand_account()
31-
self.w3.wallet.add_account(priv_key1)
32-
self.w3.wallet.add_account(priv_key2)
33-
self.w3.wallet.add_account(priv_key3)
34-
self.w3.wallet.add_account(priv_key4)
35-
self.cfx_transfer(addr1, value = 1)
39+
network.cw3.wallet.add_account(priv_key1)
40+
network.cw3.wallet.add_account(priv_key2)
41+
network.cw3.wallet.add_account(priv_key3)
42+
network.cw3.wallet.add_account(priv_key4)
43+
network.cfx.send_transaction({
44+
"to": network.cfx.address(addr1),
45+
"value": network.cw3.to_wei(1, "ether"),
46+
}).executed()
3647
assert_equal(client.get_balance(addr1), 10 ** 18)
3748

38-
self.cfx_transfer(addr2, value = 1)
49+
network.cfx.send_transaction({
50+
"to": network.cfx.address(addr2),
51+
"value": network.cw3.to_wei(1, "ether"),
52+
}).executed()
3953
assert_equal(client.get_balance(addr2), 10 ** 18)
4054

41-
self.cfx_transfer(addr3, value = 3)
55+
network.cfx.send_transaction({
56+
"to": network.cfx.address(addr3),
57+
"value": network.cw3.to_wei(3, "ether"),
58+
}).executed()
4259
assert_equal(client.get_balance(addr3), 3 * 10 ** 18)
4360

4461
# setup contract
4562
before_setup_collateral = client.get_collateral_for_storage(genesis_addr)
46-
test_contract = self.deploy_contract("CommissionPrivilegeTest")
63+
test_contract = network.deploy_contract("CommissionPrivilegeTest")
4764
after_setup_collateral = client.get_collateral_for_storage(genesis_addr)
4865
contract_addr = test_contract.address.hex_address
4966
assert_equal(client.get_balance(contract_addr), 0)
@@ -53,7 +70,7 @@ def run_test(self):
5370
b0 = client.get_balance(genesis_addr)
5471
assert_equal(client.get_sponsor_balance_for_gas(contract_addr), 0)
5572

56-
assert_tx_exec_error(client, self.sponsorControl.functions.setSponsorForGas(contract_addr, tx_gas_upper_bound).transact({
73+
assert_tx_exec_error(client, network.sponsorControl.functions.setSponsorForGas(contract_addr, tx_gas_upper_bound).transact({
5774
"value": tx_gas_upper_bound * 1000 - 1,
5875
"gas": gas,
5976
"storageLimit": 0,
@@ -69,7 +86,7 @@ def run_test(self):
6986

7087
# sponsor the contract succeed
7188
b0 = client.get_balance(genesis_addr)
72-
self.sponsorControl.functions.setSponsorForGas(contract_addr, tx_gas_upper_bound).transact({
89+
network.sponsorControl.functions.setSponsorForGas(contract_addr, tx_gas_upper_bound).transact({
7390
"value": 10**18,
7491
"gas": gas,
7592
"gasPrice": 1,
@@ -131,7 +148,7 @@ def run_test(self):
131148
sb = client.get_sponsor_balance_for_gas(contract_addr)
132149
b1 = client.get_balance(addr1)
133150
test_contract.functions.foo().transact({
134-
"from": self.cfx.address(addr1),
151+
"from": network.cfx.address(addr1),
135152
"storageLimit": 0,
136153
"gas": gas,
137154
"gasPrice": 1,
@@ -143,7 +160,7 @@ def run_test(self):
143160
b1 = client.get_balance(addr1)
144161
sb = client.get_sponsor_balance_for_gas(contract_addr)
145162
test_contract.functions.foo().transact({
146-
"from": self.cfx.address(addr1),
163+
"from": network.cfx.address(addr1),
147164
"gas": tx_gas_upper_bound+1,
148165
"storageLimit": 0,
149166
"gasPrice": 1,
@@ -155,7 +172,7 @@ def run_test(self):
155172
b2 = client.get_balance(addr2)
156173
sb = client.get_sponsor_balance_for_gas(contract_addr)
157174
test_contract.functions.foo().transact({
158-
"from": self.cfx.address(addr2),
175+
"from": network.cfx.address(addr2),
159176
"storageLimit": 0,
160177
"gas": gas,
161178
"gasPrice": 1,
@@ -178,7 +195,7 @@ def run_test(self):
178195
sb = client.get_sponsor_balance_for_gas(contract_addr)
179196
b2 = client.get_balance(addr2)
180197
test_contract.functions.foo().transact({
181-
"from": self.cfx.address(addr2),
198+
"from": network.cfx.address(addr2),
182199
"storageLimit": 0,
183200
"gas": gas,
184201
"gasPrice": 1,
@@ -201,7 +218,7 @@ def run_test(self):
201218
sb = client.get_sponsor_balance_for_gas(contract_addr)
202219
b1 = client.get_balance(addr1)
203220
test_contract.functions.foo().transact({
204-
"from": self.cfx.address(addr1),
221+
"from": network.cfx.address(addr1),
205222
"storageLimit": 0,
206223
"gas": gas,
207224
"gasPrice": 1,
@@ -212,10 +229,10 @@ def run_test(self):
212229
# new sponsor failed due to small sponsor_balance
213230
b3 = client.get_balance(addr3)
214231
sb = client.get_sponsor_balance_for_gas(contract_addr)
215-
assert_tx_exec_error(client, self.sponsorControl.functions.setSponsorForGas(contract_addr, tx_gas_upper_bound).transact({
232+
assert_tx_exec_error(client, network.sponsorControl.functions.setSponsorForGas(contract_addr, tx_gas_upper_bound).transact({
216233
"value": int(0.5 * 10**18),
217234
"storageLimit": 0,
218-
"from": self.cfx.address(addr3),
235+
"from": network.cfx.address(addr3),
219236
"gas": gas,
220237
"gasPrice": 1,
221238
}).to_0x_hex(),
@@ -230,12 +247,12 @@ def run_test(self):
230247
sb = client.get_sponsor_balance_for_gas(contract_addr)
231248
assert_tx_exec_error(
232249
client,
233-
self.sponsorControl.functions.setSponsorForGas(contract_addr, tx_gas_upper_bound - 1).transact({
250+
network.sponsorControl.functions.setSponsorForGas(contract_addr, tx_gas_upper_bound - 1).transact({
234251
"value": 10**18,
235252
"storageLimit": 0,
236253
"gas": gas,
237254
"gasPrice": 1,
238-
"from": self.cfx.address(addr3),
255+
"from": network.cfx.address(addr3),
239256
}).to_0x_hex(),
240257
'VmError(InternalContract("upper_bound is not exceed previous sponsor"))'
241258
)
@@ -247,12 +264,12 @@ def run_test(self):
247264
b0 = client.get_balance(genesis_addr)
248265
b3 = client.get_balance(addr3)
249266
sb = client.get_sponsor_balance_for_gas(contract_addr)
250-
self.sponsorControl.functions.setSponsorForGas(contract_addr, tx_gas_upper_bound + 1).transact({
267+
network.sponsorControl.functions.setSponsorForGas(contract_addr, tx_gas_upper_bound + 1).transact({
251268
"value": 10**18,
252269
"storageLimit": 0,
253270
"gas": gas,
254271
"gasPrice": 1,
255-
"from": self.cfx.address(addr3),
272+
"from": network.cfx.address(addr3),
256273
}).executed()
257274
assert_equal(client.get_sponsor_balance_for_gas(contract_addr), 10 ** 18)
258275
assert_equal(client.get_sponsor_gas_bound(contract_addr), tx_gas_upper_bound + 1)
@@ -264,12 +281,12 @@ def run_test(self):
264281
b3 = client.get_balance(addr3)
265282
assert_tx_exec_error(
266283
client,
267-
self.sponsorControl.functions.setSponsorForCollateral(contract_addr).transact({
284+
network.sponsorControl.functions.setSponsorForCollateral(contract_addr).transact({
268285
"value": 0,
269286
"storageLimit": 0,
270287
"gas": gas,
271288
"gasPrice": 1,
272-
"from": self.cfx.address(addr3),
289+
"from": network.cfx.address(addr3),
273290
}).to_0x_hex(),
274291
'VmError(InternalContract("zero sponsor balance is not allowed"))'
275292
)
@@ -279,12 +296,12 @@ def run_test(self):
279296

280297
# sponsor the contract for collateral succeed
281298
b3 = client.get_balance(addr3)
282-
self.sponsorControl.functions.setSponsorForCollateral(contract_addr).transact({
299+
network.sponsorControl.functions.setSponsorForCollateral(contract_addr).transact({
283300
"value": 10 ** 18 - 1,
284301
"storageLimit": 0,
285302
"gas": gas,
286303
"gasPrice": 1,
287-
"from": self.cfx.address(addr3),
304+
"from": network.cfx.address(addr3),
288305
}).executed()
289306
assert_equal(client.get_sponsor_balance_for_collateral(contract_addr), 10 ** 18 - 1)
290307
assert_equal(client.get_sponsor_for_collateral(contract_addr), addr3.lower())
@@ -302,7 +319,7 @@ def run_test(self):
302319
assert_tx_exec_error(
303320
client,
304321
test_contract.functions.par_add(0,2).transact({
305-
"from": self.cfx.address(addr1),
322+
"from": network.cfx.address(addr1),
306323
"storageLimit": bytes_per_key,
307324
"gas": gas,
308325
"gasPrice": 1,
@@ -316,7 +333,7 @@ def run_test(self):
316333
# addr1 create 2 keys without privilege, and storage limit is 2, should succeed
317334
b1 = client.get_balance(addr1)
318335
test_contract.functions.par_add(0,2).transact({
319-
"from": self.cfx.address(addr1),
336+
"from": network.cfx.address(addr1),
320337
"storageLimit": bytes_per_key * 2,
321338
"gas": gas,
322339
"gasPrice": 1,
@@ -328,7 +345,7 @@ def run_test(self):
328345
# remove 1 key create by addr1
329346
b1 = client.get_balance(addr1)
330347
test_contract.functions.par_del(0,1).transact({
331-
"from": self.cfx.address(addr1),
348+
"from": network.cfx.address(addr1),
332349
"storageLimit": bytes_per_key * 2,
333350
"gas": gas,
334351
"gasPrice": 1,
@@ -352,7 +369,7 @@ def run_test(self):
352369
sbg = client.get_sponsor_balance_for_gas(contract_addr)
353370
b2 = client.get_balance(addr2)
354371
test_contract.functions.par_add(2,4).transact({
355-
"from": self.cfx.address(addr2),
372+
"from": network.cfx.address(addr2),
356373
"storageLimit": bytes_per_key,
357374
"gas": gas,
358375
"gasPrice": 1,
@@ -368,7 +385,7 @@ def run_test(self):
368385
sbg = client.get_sponsor_balance_for_gas(contract_addr)
369386
b2 = client.get_balance(addr2)
370387
test_contract.functions.par_add(4,17).transact({
371-
"from": self.cfx.address(addr2),
388+
"from": network.cfx.address(addr2),
372389
"storageLimit": 0,
373390
"gas": gas,
374391
"gasPrice": 1,
@@ -392,7 +409,7 @@ def run_test(self):
392409
assert_tx_exec_error(
393410
client,
394411
test_contract.functions.par_add(17,18).transact({
395-
"from": self.cfx.address(addr2),
412+
"from": network.cfx.address(addr2),
396413
"storageLimit": 0,
397414
"gas": gas,
398415
"gasPrice": 1,
@@ -410,7 +427,7 @@ def run_test(self):
410427
sbg = client.get_sponsor_balance_for_gas(contract_addr)
411428
b2 = client.get_balance(addr2)
412429
test_contract.functions.par_add(17, 18).transact({
413-
"from": self.cfx.address(addr2),
430+
"from": network.cfx.address(addr2),
414431
"storageLimit": bytes_per_key * 2,
415432
"gas": gas,
416433
"gasPrice": 1,
@@ -426,7 +443,7 @@ def run_test(self):
426443
sbg = client.get_sponsor_balance_for_gas(contract_addr)
427444
b2 = client.get_balance(addr2)
428445
test_contract.functions.par_del(2,12).transact({
429-
"from": self.cfx.address(addr2),
446+
"from": network.cfx.address(addr2),
430447
"storageLimit": bytes_per_key,
431448
"gas": gas,
432449
"gasPrice": 1,
@@ -440,10 +457,10 @@ def run_test(self):
440457
# addr3 sponsor more, treat as transfer
441458
b3 = client.get_balance(addr3)
442459
sb = client.get_sponsor_balance_for_collateral(contract_addr)
443-
self.sponsorControl.functions.setSponsorForCollateral(contract_addr).transact({
460+
network.sponsorControl.functions.setSponsorForCollateral(contract_addr).transact({
444461
"value": sb,
445462
"storageLimit": 0,
446-
"from": self.cfx.address(addr3),
463+
"from": network.cfx.address(addr3),
447464
"gas": gas,
448465
"gasPrice": 1,
449466
}).executed()
@@ -457,7 +474,7 @@ def run_test(self):
457474
err_msg = 'VmError(InternalContract("sponsor_balance is not enough to cover previous sponsor\'s sponsor_balance and collateral_for_storage"))'
458475
assert_tx_exec_error(
459476
client,
460-
self.sponsorControl.functions.setSponsorForCollateral(contract_addr).transact({
477+
network.sponsorControl.functions.setSponsorForCollateral(contract_addr).transact({
461478
"value": sb + 1,
462479
"storageLimit": 0,
463480
"gas": gas,
@@ -474,7 +491,7 @@ def run_test(self):
474491
b3 = client.get_balance(addr3)
475492
cfs = client.get_collateral_for_storage(contract_addr)
476493
sb = client.get_sponsor_balance_for_collateral(contract_addr)
477-
self.sponsorControl.functions.setSponsorForCollateral(contract_addr).transact({
494+
network.sponsorControl.functions.setSponsorForCollateral(contract_addr).transact({
478495
"value": sb + cfs + 1,
479496
"storageLimit": 0,
480497
"gas": gas,
@@ -547,8 +564,4 @@ def run_test(self):
547564
}).executed()
548565
assert_equal(client.get_collateral_for_storage(genesis_addr), c0 - 4 * collateral_per_storage_key)
549566

550-
self.log.info("Pass")
551-
552-
553-
if __name__ == "__main__":
554-
CommissionPrivilegeTest().main()
567+
network.log.info("Pass")

0 commit comments

Comments
 (0)