Skip to content

Commit 52e6ada

Browse files
kcloweswolovim
authored andcommitted
Move Parity module to use Method class
1 parent b9d7677 commit 52e6ada

File tree

6 files changed

+115
-84
lines changed

6 files changed

+115
-84
lines changed

tests/integration/parity/test_parity_http.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from .common import (
1717
CommonParityShhModuleTest,
1818
ParityEthModuleTest,
19+
ParityModuleTest,
1920
ParityPersonalModuleTest,
21+
ParitySetModuleTest,
2022
ParityTraceModuleTest,
2123
ParityWeb3ModuleTest,
2224
)
@@ -88,6 +90,10 @@ class TestParityEthModuleTest(ParityEthModuleTest):
8890
pass
8991

9092

93+
class TestParityModuleTest(ParityModuleTest):
94+
pass
95+
96+
9197
class TestParityVersionModule(VersionModuleTest):
9298
pass
9399

@@ -106,3 +112,7 @@ class TestParityTraceModuleTest(ParityTraceModuleTest):
106112

107113
class TestParityShhModuleTest(CommonParityShhModuleTest):
108114
pass
115+
116+
117+
class TestParitySetModuleTest(ParitySetModuleTest):
118+
pass

web3/_utils/method_formatters.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
from web3.datastructures import (
6868
AttributeDict,
6969
)
70+
from web3.exceptions import (
71+
InvalidParityMode,
72+
)
7073
from web3.types import (
7174
RPCEndpoint,
7275
TReturn,
@@ -467,6 +470,15 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
467470
ABI_REQUEST_FORMATTERS = abi_request_formatters(STANDARD_NORMALIZERS, RPC_ABIS)
468471

469472

473+
def raise_invalid_parity_mode(response):
474+
error_message = response['error']['message']
475+
raise InvalidParityMode(error_message)
476+
477+
478+
ERROR_FORMATTERS = {
479+
RPC.parity_setMode: raise_invalid_parity_mode,
480+
}
481+
470482
@to_tuple
471483
def combine_formatters(
472484
formatter_maps: Collection[Dict[RPCEndpoint, Callable[..., TReturn]]], method_name: RPCEndpoint
@@ -505,7 +517,7 @@ def get_error_formatters(
505517
) -> Dict[str, Callable[..., Any]]:
506518
# Note error formatters work on the full response dict
507519
# TODO - test this function
508-
error_formatter_maps = ()
520+
error_formatter_maps = (ERROR_FORMATTERS,)
509521
formatters = combine_formatters(error_formatter_maps, method_name)
510522

511523
return compose(*formatters)

web3/_utils/module_testing/parity_module.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
from web3._utils.formatters import (
1616
hex_to_integer,
1717
)
18+
from web3.exceptions import (
19+
InvalidParityMode,
20+
)
1821
from web3.types import (
1922
BlockData,
2023
EnodeURI,
@@ -154,11 +157,11 @@ def test_set_mode(self, web3: "Web3", mode: ParityMode) -> None:
154157
assert web3.parity.setMode(mode) is True
155158

156159
def test_set_mode_with_bad_string(self, web3: "Web3") -> None:
157-
with pytest.raises(ValueError, match="Couldn't parse parameters: mode"):
160+
with pytest.raises(InvalidParityMode, match="Couldn't parse parameters: mode"):
158161
# type ignored b/c it's an invalid literal ParityMode
159162
web3.parity.setMode('not a mode') # type: ignore
160163

161164
def test_set_mode_with_no_argument(self, web3: "Web3") -> None:
162-
with pytest.raises(TypeError, match='missing 1 required positional argument'):
165+
with pytest.raises(InvalidParityMode, match='Invalid params: invalid length 0, expected a tuple of size 1.'):
163166
# type ignored b/c setMode expects arguments
164167
web3.parity.setMode() # type: ignore

web3/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,11 @@ class InvalidEventABI(ValueError):
192192
Raised when the event ABI is invalid.
193193
"""
194194
pass
195+
196+
197+
class InvalidParityMode(TypeError, ValueError):
198+
# Inherits from TypeError for backwards compatibility
199+
"""
200+
Raised when web3.parity.setMode() is called with no args
201+
"""
202+
pass

web3/module.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,5 @@ def __init__(self, web3: "Web3") -> None:
9999
self.retrieve_caller_fn = retrieve_async_method_call_fn(web3, self)
100100
else:
101101
self.retrieve_caller_fn = retrieve_blocking_method_call_fn(web3, self)
102+
#TODO: Think about what this could be. web3.eth? web3.something else?
103+
self.web3 = web3

web3/parity.py

Lines changed: 77 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
from web3._utils.rpc_abi import (
4444
RPC,
4545
)
46+
from web3.method import (
47+
Method,
48+
default_root_munger,
49+
)
4650
from web3.module import (
4751
Module,
4852
ModuleV2,
@@ -117,21 +121,20 @@ class ParityPersonal(ModuleV2):
117121
unlockAccount = unlockAccount
118122

119123

120-
class Parity(Module):
124+
class Parity(ModuleV2):
121125
"""
122126
https://paritytech.github.io/wiki/JSONRPC-parity-module
123127
"""
124128
defaultBlock: Literal["latest"] = "latest" # noqa: E704
125129
shh: ParityShh
126130
personal: ParityPersonal
127131

128-
def enode(self) -> EnodeURI:
129-
return self.web3.manager.request_blocking(
130-
RPC.parity_enode,
131-
[],
132-
)
132+
enode = Method(
133+
RPC.parity_enode,
134+
mungers=None,
135+
)
133136

134-
def listStorageKeys(
137+
def list_storage_keys_munger(
135138
self,
136139
address: Union[Address, ChecksumAddress, ENS],
137140
quantity: int,
@@ -140,91 +143,84 @@ def listStorageKeys(
140143
) -> List[Hash32]:
141144
if block_identifier is None:
142145
block_identifier = self.defaultBlock
143-
return self.web3.manager.request_blocking(
144-
RPC.parity_listStorageKeys,
145-
[address, quantity, hash_, block_identifier],
146-
)
147-
148-
def netPeers(self) -> ParityNetPeers:
149-
return self.web3.manager.request_blocking(
150-
RPC.parity_netPeers,
151-
[],
152-
)
153-
154-
def addReservedPeer(self, url: EnodeURI) -> bool:
155-
return self.web3.manager.request_blocking(
156-
RPC.parity_addReservedPeer,
157-
[url],
158-
)
159-
160-
def traceReplayTransaction(
161-
self, transaction_hash: _Hash32, mode: ParityTraceMode=['trace']
162-
) -> ParityBlockTrace:
163-
return self.web3.manager.request_blocking(
164-
RPC.trace_replayTransaction,
165-
[transaction_hash, mode],
166-
)
146+
return [address, quantity, hash_, block_identifier]
147+
148+
listStorageKeys = Method(
149+
RPC.parity_listStorageKeys,
150+
mungers=[list_storage_keys_munger],
151+
)
167152

168-
def traceReplayBlockTransactions(
153+
netPeers = Method(
154+
RPC.parity_netPeers,
155+
mungers=None
156+
)
157+
158+
addReservedPeer = Method(
159+
RPC.parity_addReservedPeer,
160+
mungers=[default_root_munger],
161+
)
162+
163+
def trace_transactions_munger(
169164
self, block_identifier: BlockIdentifier, mode: ParityTraceMode=['trace']
170165
) -> List[ParityBlockTrace]:
171-
return self.web3.manager.request_blocking(
172-
RPC.trace_replayBlockTransactions,
173-
[block_identifier, mode]
174-
)
175-
176-
def traceBlock(self, block_identifier: BlockIdentifier) -> List[ParityBlockTrace]:
177-
return self.web3.manager.request_blocking(
178-
RPC.trace_block,
179-
[block_identifier]
180-
)
181-
182-
def traceFilter(self, params: ParityFilterParams) -> List[ParityFilterTrace]:
183-
return self.web3.manager.request_blocking(
184-
RPC.trace_filter,
185-
[params]
186-
)
187-
188-
def traceTransaction(self, transaction_hash: _Hash32) -> List[ParityFilterTrace]:
189-
return self.web3.manager.request_blocking(
190-
RPC.trace_transaction,
191-
[transaction_hash]
192-
)
193-
194-
def traceCall(
166+
return [block_identifier, mode]
167+
168+
traceReplayTransaction = Method(
169+
RPC.trace_replayTransaction,
170+
mungers=[trace_transactions_munger],
171+
)
172+
173+
traceReplayBlockTransactions = Method(
174+
RPC.trace_replayBlockTransactions,
175+
mungers=[trace_transactions_munger]
176+
)
177+
178+
traceBlock = Method(
179+
RPC.trace_block,
180+
mungers=[default_root_munger],
181+
)
182+
183+
traceFilter = Method(
184+
RPC.trace_filter,
185+
mungers=[default_root_munger],
186+
)
187+
188+
traceTransaction = Method(
189+
RPC.trace_transaction,
190+
mungers=[default_root_munger],
191+
)
192+
193+
def trace_call_munger(
195194
self,
196195
transaction: TxParams,
197196
mode: ParityTraceMode=['trace'],
198-
block_identifier: Optional[BlockIdentifier] = None
197+
block_identifier: BlockIdentifier=None
199198
) -> ParityBlockTrace:
200-
# TODO: move to middleware
201199
if 'from' not in transaction and is_checksum_address(self.web3.eth.defaultAccount):
202200
transaction = assoc(transaction, 'from', self.web3.eth.defaultAccount)
203201

204202
# TODO: move to middleware
205203
if block_identifier is None:
206204
block_identifier = self.defaultBlock
207-
return self.web3.manager.request_blocking(
208-
RPC.trace_call,
209-
[transaction, mode, block_identifier],
210-
)
211205

212-
def traceRawTransaction(
213-
self, raw_transaction: HexStr, mode: ParityTraceMode=['trace']
214-
) -> ParityBlockTrace:
215-
return self.web3.manager.request_blocking(
216-
RPC.trace_rawTransaction,
217-
[raw_transaction, mode],
218-
)
219-
220-
def setMode(self, mode: ParityMode) -> bool:
221-
return self.web3.manager.request_blocking(
222-
RPC.parity_setMode,
223-
[mode]
224-
)
225-
226-
def mode(self) -> ParityMode:
227-
return self.web3.manager.request_blocking(
228-
RPC.parity_mode,
229-
[]
230-
)
206+
return [transaction, mode, block_identifier]
207+
208+
traceCall = Method(
209+
RPC.trace_call,
210+
mungers=[trace_call_munger],
211+
)
212+
213+
traceRawTransaction = Method(
214+
RPC.trace_rawTransaction,
215+
mungers=[trace_transactions_munger],
216+
)
217+
218+
setMode = Method(
219+
RPC.parity_setMode,
220+
mungers=[default_root_munger],
221+
)
222+
223+
mode = Method(
224+
RPC.parity_mode,
225+
mungers=None
226+
)

0 commit comments

Comments
 (0)