Skip to content

Debug tools for loading SSZ object from file#3355

Closed
hwwhww wants to merge 5 commits intodevfrom
tools
Closed

Debug tools for loading SSZ object from file#3355
hwwhww wants to merge 5 commits intodevfrom
tools

Conversation

@hwwhww
Copy link
Copy Markdown
Contributor

@hwwhww hwwhww commented May 12, 2023

Installation

cd consensus-specs/scripts

pip3 install -r requirements.txt

Example code

Load JSON file from Beacon API into Remerkleable Python SSZ object

import eth2spec.capella.mainnet as spec
from eth2spec.debug.tools import get_ssz_object_from_json_file

file_dir = '<YOUR DIR PATH>'

# Load JSON file from Beacon API into Remerkleable Python SSZ object
pre_state = get_ssz_object_from_json_file(spec.BeaconState, f"{file_dir}/state_1.ssz")
post_state = get_ssz_object_from_json_file(spec.BeaconState, f"{file_dir}/state_2.ssz")
signed_block = get_ssz_object_from_json_file(spec.SignedBeaconBlock, f"{file_dir}/block_2.ssz")

state = pre_state.copy()
spec.state_transition(state, signed_block)

assert state == post_state

Load SSZ-snappy file into Remerkleable Python SSZ object

import eth2spec.capella.mainnet as spec
from eth2spec.debug.tools import get_ssz_object_from_ssz_encoded

file_dir = '<YOUR DIR PATH>'

# Load SSZ-snappy file into Remerkleable Python SSZ object
pre_state = get_ssz_object_from_ssz_encoded(spec.BeaconState, f"{file_dir}/state_1.ssz_snappy")
post_state = get_ssz_object_from_ssz_encoded(spec.BeaconState, f"{file_dir}/state_2.ssz_snappy")

Dump SSZ to JSON file

python inspect_state.py --state-path=<The path of ssz_snappy state file> --output-dir=. --fork=capella --field=validators --is-snappy

See

# Output specific field to file
if field is not None and field in state.fields():
value = state.__getattr__(field)
output_ssz_to_file(output_dir, value, dump_yaml_fn)

@ralexstokes
Copy link
Copy Markdown
Member

haven't reviewed in depth but have written various versions of these utilities over the years and I'd support having a standard collection somewhere

@hwwhww hwwhww added the testing CI, actions, tests, testing infra label May 16, 2023
@hwwhww hwwhww marked this pull request as ready for review November 9, 2023 13:33
@jtraglia
Copy link
Copy Markdown
Member

jtraglia commented Jun 4, 2025

This is neat but not really necessary. Clients have tools for this too. Eg teku's pretty-print does this: https://github.com/Consensys/teku/blob/master/teku/src/main/java/tech/pegasys/teku/cli/subcommand/debug/PrettyPrintCommand.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing CI, actions, tests, testing infra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants