Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slither-read-storage native POA support #1843

Merged
merged 26 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8cee05d
Native support for POA networks in read_storage
webthethird Apr 17, 2023
c4bb804
Set `srs.rpc` before `srs.block`
webthethird Apr 27, 2023
b490c54
Type hint
webthethird Apr 27, 2023
5bcaf41
New RpcInfo class w/ RpcInfo.web3 and RpcInfo.block
webthethird Apr 27, 2023
e900e79
Black
webthethird Apr 27, 2023
5b90a52
Update test_read_storage.py
webthethird Apr 27, 2023
55817ab
Add import in __init__.py
webthethird Apr 27, 2023
b28e350
Avoid instantiating SRS twice
webthethird Apr 28, 2023
2645d5e
Add comment about `get_block` for POA networks
webthethird Apr 28, 2023
71e13aa
Pylint
webthethird Apr 28, 2023
4fbbfff
Black
webthethird Apr 28, 2023
b6bc234
Allow other valid block string arguments
webthethird Apr 28, 2023
610052d
`args.block` can be in ["latest", "earliest", "pending", "safe", "fin…
webthethird May 4, 2023
034d12d
Use BlockTag enum class for valid `str` arguments
webthethird May 4, 2023
87f94b4
Tweak `RpcInfo.__init__()` signature
webthethird May 4, 2023
ee42026
get rid of `or "latest"`
webthethird May 4, 2023
80b688d
Import BlockTag
webthethird May 4, 2023
24813cc
Use `web3.types.BlockIdentifier`
webthethird May 4, 2023
d462c12
Revert BlockTag enum
webthethird May 11, 2023
c4cec4c
Pylint and black
webthethird May 11, 2023
5e33519
Replace missing newline
webthethird May 11, 2023
a1f6235
Update slither/tools/read_storage/__main__.py
webthethird May 12, 2023
f38eabe
Drop try/except around args.block parsing
webthethird May 12, 2023
a86ee8f
Remove unused import
webthethird May 18, 2023
83e727e
Merge branch 'dev' into dev-read-storage-poa-support
webthethird May 19, 2023
e5c87aa
Merge branch 'dev' into dev-read-storage-poa-support
webthethird May 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions slither/tools/read_storage/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ def main() -> None:

srs = SlitherReadStorage(contracts, args.max_depth)

try:
srs.block = int(args.block)
except ValueError:
srs.block = str(args.block or "latest")

if args.rpc_url:
# Remove target prefix e.g. rinkeby:0x0 -> 0x0.
address = target[target.find(":") + 1 :]
Expand All @@ -143,6 +138,11 @@ def main() -> None:

srs.rpc = args.rpc_url

try:
srs.block = int(args.block)
except ValueError:
srs.block = str(args.block or "latest")
webthethird marked this conversation as resolved.
Show resolved Hide resolved

if args.variable_name:
# Use a lambda func to only return variables that have same name as target.
# x is a tuple (`Contract`, `StateVariable`).
Expand Down
24 changes: 23 additions & 1 deletion slither/tools/read_storage/read_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from eth_typing.evm import ChecksumAddress
from eth_utils import keccak
from web3 import Web3
from web3.exceptions import ExtraDataLengthError
from web3.middleware import geth_poa_middleware

from slither.core.declarations import Contract, Structure
from slither.core.solidity_types import ArrayType, ElementaryType, MappingType, UserDefinedType
Expand Down Expand Up @@ -52,7 +54,7 @@ def __init__(self, contracts: List[Contract], max_depth: int) -> None:
self._slot_info: Dict[str, SlotInfo] = {}
self._target_variables: List[Tuple[Contract, StateVariable]] = []
self._web3: Optional[Web3] = None
self.block: Union[str, int] = "latest"
self._block: Optional[int] = None
self.rpc: Optional[str] = None
self.storage_address: Optional[str] = None
self.table: Optional[MyPrettyTable] = None
Expand All @@ -73,6 +75,26 @@ def log(self) -> str:
def log(self, log: str) -> None:
self._log = log

@property
def block(self) -> int:
"""If the RPC is for a POA network, the first call to get_block fails, so we inject geth_poa_middleware"""
if not self._block:
try:
self._block = self.web3.eth.get_block("latest")["number"]
except ExtraDataLengthError:
self.web3.middleware_onion.inject(geth_poa_middleware, layer=0)
self._block = self.web3.eth.get_block("latest")["number"]
webthethird marked this conversation as resolved.
Show resolved Hide resolved
return self._block

@block.setter
def block(self, block: Union[str, int]) -> None:
"""If the RPC is for a POA network, the first call to get_block fails, so we inject geth_poa_middleware"""
try:
self._block = self.web3.eth.get_block(block)["number"]
except ExtraDataLengthError:
self.web3.middleware_onion.inject(geth_poa_middleware, layer=0)
self._block = self.web3.eth.get_block(block)["number"]

@property
def web3(self) -> Web3:
if not self._web3:
Expand Down