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

Debug tools for loading SSZ object from file #3355

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open

Debug tools for loading SSZ object from file #3355

wants to merge 5 commits into from

Conversation

hwwhww
Copy link
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
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 marked this pull request as ready for review November 9, 2023 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants