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

Add support for querying protocol state by state hash or height #15893

Merged
merged 4 commits into from
Jul 31, 2024

Conversation

MartinMinkov
Copy link
Member

@MartinMinkov MartinMinkov commented Jul 30, 2024

Context

This pull request enhances the protocolState GraphQL query to allow users to retrieve the protocol state for a specific block by providing either the state hash or the height of the desired block. This improvement increases the flexibility and usability of the API, enabling more granular analysis and exploration of the Mina protocol's history. This was a specific ask from Lambda for their Mina -> ETH bridge work.

Description

The changes include:

  • Adding stateHash and height arguments to the protocolState query
  • Updating the resolver to handle these new arguments and retrieve the protocol state based on the provided state hash or height
  • Implementing error handling and validation to ensure that exactly one of state hash or height is provided
  • Maintaining the previous behavior of returning the current best tip's protocol state when neither argument is provided
  • Updating the query description and GraphQL schema to reflect the new functionality

Changes made

  1. Updated src/lib/mina_graphql/mina_graphql.ml:
    • Added stateHash and height arguments to the protocolState query
    • Modified the resolver to handle the new arguments and retrieve the appropriate protocol state
    • Improved error handling and validation
  2. Updated graphql_schema.json:
    • Added height and stateHash arguments to the protocolState query schema
    • Updated the query description

…hash or height

The `protocolState` GraphQL query has been updated to allow users to retrieve the protocol state for a specific block by providing either the state hash or the height of the desired block. This enhances the flexibility and usability of the API.

Changes include:
- Added `stateHash` argument to specify the state hash of the desired block
- Added `height` argument to specify the height of the desired block in the best chain
- Updated the resolver to handle the new arguments and retrieve the protocol state based on the provided state hash or height
- If neither state hash nor height is provided, the protocol state of the current best tip is returned (previous behavior)
- Improved error handling and validation to ensure that exactly one of state hash or height is provided
- Updated documentation to reflect the new functionality

This feature allows users to query historical protocol states and retrieve information about specific blocks, enabling more granular analysis and exploration of the Mina protocol's history.
The protocolState query now accepts two new optional arguments:
- height: to specify the height of the desired block in the best chain
- stateHash: to specify the state hash of the desired block

This allows querying the protocol state for a specific block, instead of only the current state. The description of the query was updated to reflect this change.
@MartinMinkov MartinMinkov changed the title Get the protocol state for a given block Add support for querying protocol state by state hash or height Jul 30, 2024
@MartinMinkov MartinMinkov marked this pull request as ready for review July 30, 2024 21:12
@MartinMinkov MartinMinkov requested a review from a team as a code owner July 30, 2024 21:12
@svv232
Copy link
Member

svv232 commented Jul 31, 2024

!ci-build-me

@svv232
Copy link
Member

svv232 commented Jul 31, 2024

pr looks good, i will run the container once it builds in CI to just manually check if this is backwards compatible with the old api. For the most part i think it is because of the way you are handling optional arguments, but testing will just seal the deal.

@svv232
Copy link
Member

svv232 commented Jul 31, 2024

okay the end point seems to be backwards compatible

root@31d710159a0e:~# curl -g -X POST -H "Content-Type: application/json" -d '{"query":"query { protocolState }"}' http://127.0.0.1:3085/graphql
{"data":{"protocolState":"{\"previous_state_hash\":\"3NKrxRzh8wcLHNx4jX3mApGiBtJ5pNjcsDnvWc75okod6p1icP6h\",\"body\":{\"genesis_state_hash\":\"3NKrxRzh8wcLHNx4jX3mApGiBtJ5pNjcsDnvWc75okod6p1icP6h\",\"blockchain_state\":{\"staged_ledger_hash\":{\"non_snark\":{\"ledger_hash\":\"jwEEJ1ytcHRYVrXxXxdj3pTNnt3cX2A3FUrbQEKAnYUXa53KAD2\",\"aux_hash\":\"UDRUFHSvxUAtV8sh7gzMVPqpbd46roG1wzWR6dYvB6RunPihom\",\"pending_coinbase_aux\":\"WAAeUjUnP9Q2JiabhJzJozcjiEmkZe8ob4cfFKSuq6pQSNmHh7\"},\"pending_coinbase_hash\":\"2mzvU1MA5JjDUrLsKk6KFFj4CPn6bnHjN5Fi779U96JUbZNwoTvF\"},\"genesis_ledger_hash\":\"jwEEJ1ytcHRYVrXxXxdj3pTNnt3cX2A3FUrbQEKAnYUXa53KAD2\",\"ledger_proof_statement\":{\"source\":{\"first_pass_ledger\":\"jwEEJ1ytcHRYVrXxXxdj3pTNnt3cX2A3FUrbQEKAnYUXa53KAD2\",\"second_pass_ledger\":\"jwEEJ1ytcHRYVrXxXxdj3pTNnt3cX2A3FUrbQEKAnYUXa53KAD2\",\"pending_coinbase_stack\":{\"data\":\"4QNrZFBTDQCPfEZqBZsaPYx8qdaNFv1nebUyCUsQW9QUJqyuD3un\",\"state\":{\"init\":\"4Yx5U3t3EYQycZ91yj4478bHkLwGkhDHnPbCY9TxgUk69SQityej\",\"curr\":\"4Yx5U3t3EYQycZ91yj4478bHkLwGkhDHnPbCY9TxgUk69SQityej\"}},\"local_state\":{\"stack_frame\":\"0x0641662E94D68EC970D0AFC059D02729BBF4A2CD88C548CCD9FB1E26E570C66C\",\"call_stack\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"transaction_commitment\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"full_transaction_commitment\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"excess\":{\"magnitude\":\"0\",\"sgn\":[\"Pos\"]},\"supply_increase\":{\"magnitude\":\"0\",\"sgn\":[\"Pos\"]},\"ledger\":\"jw6bz2wud1N6itRUHZ5ypo3267stk4UgzkiuWtAMPRZo9g4Udyd\",\"success\":true,\"account_update_index\":\"0\",\"failure_status_tbl\":[],\"will_succeed\":true}},\"target\":{\"first_pass_ledger\":\"jwEEJ1ytcHRYVrXxXxdj3pTNnt3cX2A3FUrbQEKAnYUXa53KAD2\",\"second_pass_ledger\":\"jwEEJ1ytcHRYVrXxXxdj3pTNnt3cX2A3FUrbQEKAnYUXa53KAD2\",\"pending_coinbase_stack\":{\"data\":\"4QNrZFBTDQCPfEZqBZsaPYx8qdaNFv1nebUyCUsQW9QUJqyuD3un\",\"state\":{\"init\":\"4Yx5U3t3EYQycZ91yj4478bHkLwGkhDHnPbCY9TxgUk69SQityej\",\"curr\":\"4Yx5U3t3EYQycZ91yj4478bHkLwGkhDHnPbCY9TxgUk69SQityej\"}},\"local_state\":{\"stack_frame\":\"0x0641662E94D68EC970D0AFC059D02729BBF4A2CD88C548CCD9FB1E26E570C66C\",\"call_stack\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"transaction_commitment\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"full_transaction_commitment\":\"0x0000000000000000000000000000000000000000000000000000000000000000\",\"excess\":{\"magnitude\":\"0\",\"sgn\":[\"Pos\"]},\"supply_increase\":{\"magnitude\":\"0\",\"sgn\":[\"Pos\"]},\"ledger\":\"jw6bz2wud1N6itRUHZ5ypo3267stk4UgzkiuWtAMPRZo9g4Udyd\",\"success\":true,\"account_update_index\":\"0\",\"failure_status_tbl\":[],\"will_succeed\":true}},\"connecting_ledger_left\":\"jwEEJ1ytcHRYVrXxXxdj3pTNnt3cX2A3FUrbQEKAnYUXa53KAD2\",\"connecting_ledger_right\":\"jwEEJ1ytcHRYVrXxXxdj3pTNnt3cX2A3FUrbQEKAnYUXa53KAD2\",\"supply_increase\":{\"magnitude\":\"0\",\"sgn\":[\"Pos\"]},\"fee_excess\":[{\"token\":\"wSHV2S4qX9jFsLjQo8r1BsMLH2ZRKsZx6EJd1sbozGPieEC4Jf\",\"amount\":{\"magnitude\":\"0\",\"sgn\":[\"Pos\"]}},{\"token\":\"wSHV2S4qX9jFsLjQo8r1BsMLH2ZRKsZx6EJd1sbozGPieEC4Jf\",\"amount\":{\"magnitude\":\"0\",\"sgn\":[\"Pos\"]}}],\"sok_digest\":null},\"timestamp\":\"1706882461000\",\"body_reference\":\"36bda176656cc3be96c3d317db7b4ac06fdbc7f4eedcd6efdd20e28143d67421\"},\"consensus_state\":{\"blockchain_length\":\"1\",\"epoch_count\":\"0\",\"min_window_density\":\"77\",\"sub_window_densities\":[\"1\",\"7\",\"7\",\"7\",\"7\",\"7\",\"7\",\"7\",\"7\",\"7\",\"7\"],\"last_vrf_output\":\"39cyg4ZmMtnb_aFUIerNAoAJV8qtkfOpq0zFzPspjgM=\",\"total_currency\":\"1013459001000001000\",\"curr_global_slot_since_hard_fork\":{\"slot_number\":\"0\",\"slots_per_epoch\":\"7140\"},\"global_slot_since_genesis\":\"0\",\"staking_epoch_data\":{\"ledger\":{\"hash\":\"jwEEJ1ytcHRYVrXxXxdj3pTNnt3cX2A3FUrbQEKAnYUXa53KAD2\",\"total_currency\":\"1013459001000001000\"},\"seed\":\"2va9BGv9JrLTtrzZttiEMDYw1Zj6a6EHzXjmP9evHDTG3oEquURA\",\"start_checkpoint\":\"3NK2tkzqqK5spR2sZ7tujjqPksL45M3UUrcA4WhCkeiPtnugyE2x\",\"lock_checkpoint\":\"3NK2tkzqqK5spR2sZ7tujjqPksL45M3UUrcA4WhCkeiPtnugyE2x\",\"epoch_length\":\"1\"},\"next_epoch_data\":{\"ledger\":{\"hash\":\"jwEEJ1ytcHRYVrXxXxdj3pTNnt3cX2A3FUrbQEKAnYUXa53KAD2\",\"total_currency\":\"1013459001000001000\"},\"seed\":\"2vafPBQ3zQdHUEDDnFGuiNvJz7s2MhTLJgSzQSnu5fnZavT27cms\",\"start_checkpoint\":\"3NK2tkzqqK5spR2sZ7tujjqPksL45M3UUrcA4WhCkeiPtnugyE2x\",\"lock_checkpoint\":\"3NKrxRzh8wcLHNx4jX3mApGiBtJ5pNjcsDnvWc75okod6p1icP6h\",\"epoch_length\":\"2\"},\"has_ancestor_in_same_checkpoint_window\":true,\"block_stake_winner\":\"B62qiy32p8kAKnny8ZFwoMhYpBppM1DWVCqAPBYNcXnsAHhnfAAuXgg\",\"block_creator\":\"B62qiy32p8kAKnny8ZFwoMhYpBppM1DWVCqAPBYNcXnsAHhnfAAuXgg\",\"coinbase_receiver\":\"B62qiy32p8kAKnny8ZFwoMhYpBppM1DWVCqAPBYNcXnsAHhnfAAuXgg\",\"supercharge_coinbase\":true},\"constants\":{\"k\":\"290\",\"slots_per_epoch\":\"7140\",\"slots_per_sub_window\":\"7\",\"grace_period_slots\":\"2160\",\"delta\":\"0\",\"genesis_state_timestamp\":\"1706882461000\"}}}"}}

~resolve:(fun { ctx = mina; _ } () state_hash_base58_opt height_opt
encoding_opt ->
let open Deferred.Result.Let_syntax in
let%bind breadcrumb =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let%bind breadcrumb =
let%map breadcrumb =

NIT, but this would allow to remove Deferred.Result.return below and make the whole code a bit less multilayered.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! 882f94e

The protocol state encoding logic has been simplified:
- Instead of using Deferred.Result.return to wrap the encoding result, the encoding is now performed directly in the match expression.
- The match expression now directly returns the encoded protocol state string based on the selected encoding option.

This refactoring improves code readability and eliminates the need for an extra Deferred.Result layer, as the encoding operation itself does not involve any asynchronous or fallible computations.
@MartinMinkov
Copy link
Member Author

!ci-build-me

@MartinMinkov MartinMinkov merged commit 69b9679 into compatible Jul 31, 2024
43 checks passed
@MartinMinkov MartinMinkov deleted the feat/add-graphql-input-protocolstate branch July 31, 2024 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants