forked from ethereum/web3.py
-
Notifications
You must be signed in to change notification settings - Fork 6
/
conftest.py
110 lines (85 loc) · 2.76 KB
/
conftest.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
102
103
104
105
106
107
108
109
110
import pytest
import time
import warnings
from web3._utils.threads import (
Timeout,
)
from web3.main import (
Web3,
)
from web3.providers.eth_tester import (
EthereumTesterProvider,
)
class PollDelayCounter:
def __init__(self, initial_delay=0, max_delay=1, initial_step=0.01):
self.initial_delay = initial_delay
self.initial_step = initial_step
self.max_delay = max_delay
self.current_delay = initial_delay
def __call__(self):
delay = self.current_delay
if self.current_delay == 0:
self.current_delay += self.initial_step
else:
self.current_delay *= 2
self.current_delay = min(self.current_delay, self.max_delay)
return delay
def reset(self):
self.current_delay = self.initial_delay
@pytest.fixture()
def sleep_interval():
return PollDelayCounter()
def is_all_testrpc_providers(providers):
return all(
isinstance(provider, EthereumTesterProvider)
for provider
in providers
)
@pytest.fixture()
def skip_if_testrpc():
def _skip_if_testrpc(web3):
if is_all_testrpc_providers(web3.providers):
pytest.skip()
return _skip_if_testrpc
@pytest.fixture()
def wait_for_miner_start():
def _wait_for_miner_start(web3, timeout=60):
poll_delay_counter = PollDelayCounter()
with Timeout(timeout) as timeout:
while not web3.eth.mining or not web3.eth.hashrate:
time.sleep(poll_delay_counter())
timeout.check()
return _wait_for_miner_start
@pytest.fixture()
def wait_for_block():
def _wait_for_block(web3, block_number=1, timeout=None):
if not timeout:
timeout = (block_number - web3.eth.blockNumber) * 3
poll_delay_counter = PollDelayCounter()
with Timeout(timeout) as timeout:
while True:
if web3.eth.blockNumber >= block_number:
break
web3.manager.request_blocking("evm_mine", [])
timeout.sleep(poll_delay_counter())
return _wait_for_block
@pytest.fixture()
def wait_for_transaction():
def _wait_for_transaction(web3, txn_hash, timeout=120):
poll_delay_counter = PollDelayCounter()
with Timeout(timeout) as timeout:
while True:
txn_receipt = web3.eth.getTransactionReceipt(txn_hash)
if txn_receipt is not None:
break
time.sleep(poll_delay_counter())
timeout.check()
return txn_receipt
return _wait_for_transaction
@pytest.fixture()
def web3():
provider = EthereumTesterProvider()
return Web3(provider)
@pytest.fixture(autouse=True)
def print_warnings():
warnings.simplefilter('always')