Skip to content

Commit

Permalink
Merge pull request #1311 from crytic/dev-fix-read-storage
Browse files Browse the repository at this point in the history
Multiple improvements to slither-read-storage
  • Loading branch information
montyly authored Aug 4, 2022
2 parents e34fcfa + 5287b0a commit ce9dbf6
Show file tree
Hide file tree
Showing 8 changed files with 478 additions and 318 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,6 @@ ENV/

# Test results
test_artifacts/

# crytic export
crytic-export/
6 changes: 3 additions & 3 deletions slither/core/solidity_types/elementary_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import itertools
from typing import Optional, Tuple
from typing import Tuple

from slither.core.solidity_types.type import Type

Expand Down Expand Up @@ -176,7 +176,7 @@ def name(self) -> str:
return self.type

@property
def size(self) -> Optional[int]:
def size(self) -> int:
"""
Return the size in bits
Return None if the size is not known
Expand All @@ -194,7 +194,7 @@ def size(self) -> Optional[int]:
return int(160)
if t.startswith("bytes") and t != "bytes":
return int(t[len("bytes") :]) * 8
return None
raise SlitherException(f"{t} does not have a size")

@property
def storage_size(self) -> Tuple[int, bool]:
Expand Down
43 changes: 29 additions & 14 deletions slither/tools/read_storage/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def parse_args() -> argparse.Namespace:
+ "To retrieve a contract's storage layout:\n"
+ "\tslither-read-storage $TARGET address --contract-name $NAME --layout\n"
+ "To retrieve a contract's storage layout and values:\n"
+ "\tslither-read-storage $TARGET address --contract-name $NAME --layout --values\n"
+ "\tslither-read-storage $TARGET address --contract-name $NAME --layout --value\n"
+ "TARGET can be a contract address or project directory"
),
)
Expand Down Expand Up @@ -73,9 +73,9 @@ def parse_args() -> argparse.Namespace:
)

parser.add_argument(
"--layout",
action="store_true",
help="Toggle used to write a JSON file with the entire storage layout.",
"--json",
action="store",
help="Save the result in a JSON file.",
)

parser.add_argument(
Expand All @@ -84,6 +84,18 @@ def parse_args() -> argparse.Namespace:
help="Toggle used to include values in output.",
)

parser.add_argument(
"--table",
action="store_true",
help="Print table view of storage layout",
)

parser.add_argument(
"--silent",
action="store_true",
help="Silence log outputs",
)

parser.add_argument("--max-depth", help="Max depth to search in data structure.", default=20)

cryticparser.init(parser)
Expand Down Expand Up @@ -120,25 +132,28 @@ def main() -> None:

srs.rpc = args.rpc_url

if args.layout:
srs.get_all_storage_variables()
srs.get_storage_layout()
else:
assert args.variable_name
if args.variable_name:
# Use a lambda func to only return variables that have same name as target.
# x is a tuple (`Contract`, `StateVariable`).
srs.get_all_storage_variables(lambda x: bool(x[1].name == args.variable_name))
srs.get_target_variables(**vars(args))
else:
srs.get_all_storage_variables()
srs.get_storage_layout()

# To retrieve slot values an rpc url is required.
if args.value:
assert args.rpc_url
srs.get_slot_values()
srs.walk_slot_info(srs.get_slot_values)

if args.table:
srs.walk_slot_info(srs.convert_slot_info_to_rows)
print(srs.table)

# Only write file if storage layout is used.
if len(srs.slot_info) > 1:
with open("storage_layout.json", "w", encoding="utf-8") as file:
json.dump(srs.slot_info, file, indent=4)
if args.json:
with open(args.json, "w", encoding="utf-8") as file:
slot_infos_json = srs.to_json()
json.dump(slot_infos_json, file, indent=4)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit ce9dbf6

Please sign in to comment.