-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathgeth.py
327 lines (256 loc) · 7.2 KB
/
geth.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from typing import (
Awaitable,
Callable,
List,
Optional,
Protocol,
Tuple,
Union,
)
from eth_typing.evm import (
ChecksumAddress,
)
from web3._utils.rpc_abi import (
RPC,
)
from web3.method import (
Method,
default_root_munger,
)
from web3.module import (
Module,
)
from web3.types import (
CallTrace,
DiffModeTrace,
EnodeURI,
FourByteTrace,
NodeInfo,
OpcodeTrace,
Peer,
PrestateTrace,
TraceConfig,
TxPoolContent,
TxPoolInspect,
TxPoolStatus,
_Hash32,
)
class UnlockAccountWrapper(Protocol):
def __call__(
self,
account: ChecksumAddress,
passphrase: str,
duration: Optional[int] = None,
) -> bool:
pass
class GethTxPool(Module):
"""
https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-txpool
"""
is_async = False
content: Method[Callable[[], TxPoolContent]] = Method(
RPC.txpool_content,
is_property=True,
)
inspect: Method[Callable[[], TxPoolInspect]] = Method(
RPC.txpool_inspect,
is_property=True,
)
status: Method[Callable[[], TxPoolStatus]] = Method(
RPC.txpool_status,
is_property=True,
)
class ServerConnection(Protocol):
def __call__(
self,
host: str = "localhost",
port: int = 8546,
cors: str = "",
apis: str = "eth,net,web3",
) -> bool:
pass
def admin_start_params_munger(
_module: Module,
host: str = "localhost",
port: int = 8546,
cors: str = "",
apis: str = "eth,net,web3",
) -> Tuple[str, int, str, str]:
return (host, port, cors, apis)
class GethAdmin(Module):
"""
https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-admin
"""
is_async = False
add_peer: Method[Callable[[EnodeURI], bool]] = Method(
RPC.admin_addPeer,
mungers=[default_root_munger],
)
datadir: Method[Callable[[], str]] = Method(
RPC.admin_datadir,
is_property=True,
)
node_info: Method[Callable[[], NodeInfo]] = Method(
RPC.admin_nodeInfo,
is_property=True,
)
peers: Method[Callable[[], List[Peer]]] = Method(
RPC.admin_peers,
is_property=True,
)
start_http: Method[ServerConnection] = Method(
RPC.admin_startHTTP,
mungers=[admin_start_params_munger],
)
start_ws: Method[ServerConnection] = Method(
RPC.admin_startWS,
mungers=[admin_start_params_munger],
)
stop_http: Method[Callable[[], bool]] = Method(
RPC.admin_stopHTTP,
is_property=True,
)
stop_ws: Method[Callable[[], bool]] = Method(
RPC.admin_stopWS,
is_property=True,
)
class GethDebug(Module):
"""
https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug
"""
def trace_transaction_munger(
self,
transaction_hash: _Hash32,
trace_config: Optional[TraceConfig] = None,
) -> Tuple[_Hash32, TraceConfig]:
return (transaction_hash, trace_config)
trace_transaction: Method[
Callable[
...,
Union[CallTrace, PrestateTrace, OpcodeTrace, DiffModeTrace, FourByteTrace],
]
] = Method(
RPC.debug_traceTransaction,
mungers=[trace_transaction_munger],
)
class Geth(Module):
admin: GethAdmin
txpool: GethTxPool
debug: GethDebug
# --- async --- #
class AsyncGethTxPool(Module):
"""
https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-txpool
"""
is_async = True
_content: Method[Callable[[], Awaitable[TxPoolContent]]] = Method(
RPC.txpool_content,
is_property=True,
)
async def content(self) -> TxPoolContent:
return await self._content()
_inspect: Method[Callable[[], Awaitable[TxPoolInspect]]] = Method(
RPC.txpool_inspect,
is_property=True,
)
async def inspect(self) -> TxPoolInspect:
return await self._inspect()
_status: Method[Callable[[], Awaitable[TxPoolStatus]]] = Method(
RPC.txpool_status,
is_property=True,
)
async def status(self) -> TxPoolStatus:
return await self._status()
class AsyncGethAdmin(Module):
"""
https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-admin
"""
is_async = True
_add_peer: Method[Callable[[EnodeURI], Awaitable[bool]]] = Method(
RPC.admin_addPeer,
mungers=[default_root_munger],
)
async def add_peer(self, node_url: EnodeURI) -> bool:
return await self._add_peer(node_url)
_datadir: Method[Callable[[], Awaitable[str]]] = Method(
RPC.admin_datadir,
is_property=True,
)
async def datadir(self) -> str:
return await self._datadir()
_node_info: Method[Callable[[], Awaitable[NodeInfo]]] = Method(
RPC.admin_nodeInfo,
is_property=True,
)
async def node_info(self) -> NodeInfo:
return await self._node_info()
_peers: Method[Callable[[], Awaitable[List[Peer]]]] = Method(
RPC.admin_peers,
is_property=True,
)
async def peers(self) -> List[Peer]:
return await self._peers()
# start_http and stop_http
_start_http: Method[Callable[[str, int, str, str], Awaitable[bool]]] = Method(
RPC.admin_startHTTP,
mungers=[admin_start_params_munger],
)
_stop_http: Method[Callable[[], Awaitable[bool]]] = Method(
RPC.admin_stopHTTP,
is_property=True,
)
async def start_http(
self,
host: str = "localhost",
port: int = 8546,
cors: str = "",
apis: str = "eth,net,web3",
) -> bool:
return await self._start_http(host, port, cors, apis)
async def stop_http(self) -> bool:
return await self._stop_http()
# start_ws and stop_ws
_start_ws: Method[Callable[[str, int, str, str], Awaitable[bool]]] = Method(
RPC.admin_startWS,
mungers=[admin_start_params_munger],
)
_stop_ws: Method[Callable[[], Awaitable[bool]]] = Method(
RPC.admin_stopWS,
is_property=True,
)
async def start_ws(
self,
host: str = "localhost",
port: int = 8546,
cors: str = "",
apis: str = "eth,net,web3",
) -> bool:
return await self._start_ws(host, port, cors, apis)
async def stop_ws(self) -> bool:
return await self._stop_ws()
class AsyncGethDebug(Module):
"""
https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-debug
"""
is_async = True
_trace_transaction: Method[
Callable[
...,
Awaitable[
Union[
CallTrace, PrestateTrace, OpcodeTrace, FourByteTrace, DiffModeTrace
]
],
]
] = Method(RPC.debug_traceTransaction)
async def trace_transaction(
self,
transaction_hash: _Hash32,
trace_config: Optional[TraceConfig] = None,
) -> Union[CallTrace, PrestateTrace, OpcodeTrace, FourByteTrace, DiffModeTrace]:
return await self._trace_transaction(transaction_hash, trace_config)
class AsyncGeth(Module):
is_async = True
admin: AsyncGethAdmin
txpool: AsyncGethTxPool
debug: AsyncGethDebug