-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathtest_contract_init.py
101 lines (79 loc) · 3.44 KB
/
test_contract_init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import pytest
import time
from web3._utils.ens import (
contract_ens_addresses,
ens_addresses,
)
from web3.exceptions import (
BadFunctionCallOutput,
NameNotFound,
)
@pytest.fixture()
def math_addr(math_contract_factory, address_conversion_func):
w3 = math_contract_factory.w3
deploy_txn = math_contract_factory.constructor().transact(
{"from": w3.eth.default_account}
)
deploy_receipt = w3.eth.wait_for_transaction_receipt(deploy_txn)
assert deploy_receipt is not None
return address_conversion_func(deploy_receipt["contractAddress"])
def test_contract_with_unset_address(math_contract_factory):
with contract_ens_addresses(math_contract_factory, []):
with pytest.raises(NameNotFound):
math_contract_factory(address="unsetname.eth")
def test_contract_with_name_address(math_contract_factory, math_addr):
with contract_ens_addresses(math_contract_factory, [("thedao.eth", math_addr)]):
mc = math_contract_factory(address="thedao.eth")
caller = mc.w3.eth.default_account
assert mc.address == "thedao.eth"
assert mc.functions.return13().call({"from": caller}) == 13
def test_contract_with_name_address_from_eth_contract(
w3,
math_contract_abi,
math_contract_bytecode,
math_contract_runtime,
math_addr,
):
with ens_addresses(w3, [("thedao.eth", math_addr)]):
mc = w3.eth.contract(
address="thedao.eth",
abi=math_contract_abi,
bytecode=math_contract_bytecode,
bytecode_runtime=math_contract_runtime,
)
assert mc.address == "thedao.eth"
assert mc.functions.return13().call({"from": mc.w3.eth.default_account}) == 13
def test_contract_with_name_address_changing(math_contract_factory, math_addr):
# Contract address is validated once on creation
with contract_ens_addresses(math_contract_factory, [("thedao.eth", math_addr)]):
mc = math_contract_factory(address="thedao.eth")
caller = mc.w3.eth.default_account
assert mc.address == "thedao.eth"
# what happens when name returns no address at all
with contract_ens_addresses(mc, []):
with pytest.raises(NameNotFound):
mc.functions.return13().call({"from": caller})
# what happens when name returns address to different contract
with contract_ens_addresses(mc, [("thedao.eth", "0x" + "11" * 20)]):
with pytest.raises(BadFunctionCallOutput):
mc.functions.return13().call({"from": caller})
# contract works again when name resolves correctly
with contract_ens_addresses(mc, [("thedao.eth", math_addr)]):
assert mc.functions.return13().call({"from": caller}) == 13
def test_init_multiple_contracts_performance(w3, emitter_contract_data):
start_time = time.time()
for _ in range(500):
w3.eth.contract(
abi=emitter_contract_data["abi"], bytecode=emitter_contract_data["bytecode"]
)
# assert initializing 500 contracts is within a conservative / reasonable time
assert (time.time() - start_time) < 1.75
# -- async -- #
def test_async_init_multiple_contracts_performance(async_w3, emitter_contract_data):
start_time = time.time()
for _ in range(500):
async_w3.eth.contract(
abi=emitter_contract_data["abi"], bytecode=emitter_contract_data["bytecode"]
)
# assert initializing 500 contracts is within a conservative / reasonable time
assert (time.time() - start_time) < 1.75