forked from ethereum/web3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_block_api.py
191 lines (176 loc) · 7.51 KB
/
test_block_api.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import pytest
from eth_utils import (
to_checksum_address,
)
from hexbytes import (
HexBytes,
)
from web3._utils.rpc_abi import (
RPC,
)
from web3.middleware import (
construct_result_generator_middleware,
)
@pytest.fixture(autouse=True)
def wait_for_first_block(w3, wait_for_block):
wait_for_block(w3)
def test_uses_default_block(w3, extra_accounts, wait_for_transaction):
assert w3.eth.default_block == "latest"
w3.eth.default_block = w3.eth.block_number
assert w3.eth.default_block == w3.eth.block_number
def test_get_block_formatters_with_null_values(w3):
null_values_block = {
"baseFeePerGas": None,
"extraData": None,
"gasLimit": None,
"gasUsed": None,
"size": None,
"timestamp": None,
"hash": None,
"logsBloom": None,
"miner": None,
"mixHash": None,
"nonce": None,
"number": None,
"parentHash": None,
"sha3Uncles": None,
"difficulty": None,
"receiptsRoot": None,
"stateRoot": None,
"totalDifficulty": None,
"transactionsRoot": None,
"transactions": [],
"withdrawalsRoot": None,
"withdrawals": [],
"blobGasUsed": None,
"excessBlobGas": None,
"parentBeaconBlockRoot": None,
}
result_middleware = construct_result_generator_middleware(
{
RPC.eth_getBlockByNumber: lambda *_: null_values_block,
}
)
w3.middleware_onion.inject(result_middleware, "result_middleware", layer=0)
received_block = w3.eth.get_block("pending")
assert received_block == null_values_block
def test_get_block_formatters_with_pre_formatted_values(w3):
unformatted_values_block = {
"baseFeePerGas": "0x3b9aca00",
"extraData": "0x",
"gasLimit": "0x1c9c380",
"gasUsed": "0x1ec30",
"size": "0x734",
"timestamp": "0x2dfdc1c3e",
"hash": "0x759bc3c1221beb27a7074dbf33faded2276b04df6aaf225d51c426b8c481e935",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", # noqa: E501
"miner": "0x0deadbeefdeadbeef72211ad2C21deadbeeffeed",
"mixHash": "0xfa167946892d77a2fe116fa81c53c18cfd163889cb08f74c23415c8d13762e1b",
"nonce": "0x0",
"number": "0x2dfdc1c3e",
"parentHash": (
"0xfe7bde5fac0a5b023504e4abba1165101ca7fe724e6ad29d0c5f122e6bb656fe"
),
"sha3Uncles": (
"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
),
"difficulty": "0x0",
"receiptsRoot": (
"0x5ffefe7b059459dd6e1bdecba354eafa33fa0ae619383658bac39b817fdebeef"
),
"stateRoot": (
"0xfeed54faf6bb151112970b460118ed790197d69df7fcdd79801d120851723c88"
),
"totalDifficulty": "0x9bf9a3",
"transactionsRoot": (
"0xafeedbeef5f30efc9baf28e49271302e9b569cfa1e8e9d9512360eb8e7c667a7"
),
"transactions": [
"0xfcdabb5af17478051493039875221e21282d64b4ed4cf9ba38dff222ecde2f88",
"0x2636dba09e226a39ad90df7cc43c6a3b353ef6f3ba754f33507a3c29e47467eb",
],
"withdrawalsRoot": (
"0xfeed23dfefd5340fddc3b23bd6c84a32b8ea964ba16dc4c961ed7caa494cfefe"
),
"withdrawals": [
{
"index": "0x3090f7",
"validatorIndex": "0xbfe1",
"address": "0xfe102287c050e5ba072211ad2C213eb5dae4feed",
"amount": "0x3f695",
},
{
"index": "0x309103",
"validatorIndex": "0xbfed",
"address": "0xf22E180C050E5AB072211AD2C213EB5AEE4DF123",
"amount": "0x3f695",
},
],
"blobGasUsed": "0x7ffff",
"excessBlobGas": "0x12c00000",
"parentBeaconBlockRoot": (
"0x6470e77f1b8a55a49a57b3f74c2a10a76185636d65122053752ea5e4bb4dac59"
),
}
result_middleware = construct_result_generator_middleware(
{
RPC.eth_getBlockByNumber: lambda *_: unformatted_values_block,
}
)
w3.middleware_onion.inject(result_middleware, "result_middleware", layer=0)
received_block = w3.eth.get_block("pending")
assert received_block == {
"baseFeePerGas": int(unformatted_values_block["baseFeePerGas"], 16),
"extraData": HexBytes(unformatted_values_block["extraData"]),
"gasLimit": int(unformatted_values_block["gasLimit"], 16),
"gasUsed": int(unformatted_values_block["gasUsed"], 16),
"size": int(unformatted_values_block["size"], 16),
"timestamp": int(unformatted_values_block["timestamp"], 16),
"hash": HexBytes(unformatted_values_block["hash"]),
"logsBloom": HexBytes(unformatted_values_block["logsBloom"]),
"miner": to_checksum_address(unformatted_values_block["miner"]),
"mixHash": HexBytes(unformatted_values_block["mixHash"]),
"nonce": HexBytes(unformatted_values_block["nonce"]),
"number": int(unformatted_values_block["number"], 16),
"parentHash": HexBytes(unformatted_values_block["parentHash"]),
"sha3Uncles": HexBytes(unformatted_values_block["sha3Uncles"]),
"difficulty": int(unformatted_values_block["difficulty"], 16),
"receiptsRoot": HexBytes(unformatted_values_block["receiptsRoot"]),
"stateRoot": HexBytes(unformatted_values_block["stateRoot"]),
"totalDifficulty": int(unformatted_values_block["totalDifficulty"], 16),
"transactionsRoot": HexBytes(unformatted_values_block["transactionsRoot"]),
"transactions": [
HexBytes(unformatted_values_block["transactions"][0]),
HexBytes(unformatted_values_block["transactions"][1]),
],
"withdrawalsRoot": HexBytes(unformatted_values_block["withdrawalsRoot"]),
"withdrawals": [
{
"index": int(unformatted_values_block["withdrawals"][0]["index"], 16),
"validatorIndex": int(
unformatted_values_block["withdrawals"][0]["validatorIndex"],
16,
),
"address": to_checksum_address(
unformatted_values_block["withdrawals"][0]["address"]
),
"amount": int(unformatted_values_block["withdrawals"][0]["amount"], 16),
},
{
"index": int(unformatted_values_block["withdrawals"][1]["index"], 16),
"validatorIndex": int(
unformatted_values_block["withdrawals"][1]["validatorIndex"],
16,
),
"address": to_checksum_address(
unformatted_values_block["withdrawals"][1]["address"]
),
"amount": int(unformatted_values_block["withdrawals"][1]["amount"], 16),
},
],
"blobGasUsed": int(unformatted_values_block["blobGasUsed"], 16),
"excessBlobGas": int(unformatted_values_block["excessBlobGas"], 16),
"parentBeaconBlockRoot": HexBytes(
unformatted_values_block["parentBeaconBlockRoot"]
),
}