feat: add midnight_queryContractState RPC for lazy contract state access#1078
Open
RomarQ wants to merge 8 commits intomidnightntwrk:mainfrom
Open
feat: add midnight_queryContractState RPC for lazy contract state access#1078RomarQ wants to merge 8 commits intomidnightntwrk:mainfrom
RomarQ wants to merge 8 commits intomidnightntwrk:mainfrom
Conversation
Add a new RPC method that queries specific fields from a contract's state tree by path, without loading the full state. Each query navigates the state tree using serialized AlignedValue keys (array index, map key, or merkle tree position), mirroring the VM's idx instruction. The RPC reads the state key via a minimal runtime API getter (get_state_key), then calls the bridge directly for O(log n) lazy navigation of the contract state in ParityDB. Changes: - ledger: add resolve_state_path and query_contract_state bridge functions - ledger: expose query_contract_state via a dedicated rpc module - runtime_api: add get_state_key getter (bumps api_version to 6) - runtime: implement get_state_key - rpc: add midnight_queryContractState endpoint with input validation
Import RpcStateQuery/RpcStateQueryResult from pallet-midnight-rpc. Add query_contract_state method to MidnightClient. Tests deploy the test contract via DEPLOY_TX then exercise the RPC: - returns_expected_value: query path [0][1], deserialize, assert Cell(0u64) - batch_processes_all_queries: three queries in one call covering Cell value ([0][1] = 0u64), map hit ([0][2][key] = Null), and array out-of-bounds error ([0][99]) - nonexistent_contract: assert "Unable to query contract state" error
Replace Vec<String> with Vec<sc_client_api::StorageKey> in RpcStateQuery. StorageKey handles 0x-prefixed hex encoding at the serde layer, removing the need for manual hex decode on the server side. Also removes the BadQueryKey and QueryContractStateNotSupported error variants which are no longer needed.
This reverts commit a0bbd76.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Add a new RPC method
midnight_queryContractStatethat queries specific fields from a contract's state tree by path, without loading the full state. This enables clients to fetch individual contract fields in O(log n) instead of deserializing the entire state blob.Each query navigates the state tree using serialized
AlignedValuekeys (array index, map key, or merkle tree position), mirroring the VM'sidxinstruction.The RPC reads the state key via a minimal runtime API getter (
get_state_key), then calls the bridge directly for O(log n) lazy navigation of the contract state in ParityDB. No new types cross the WASM boundary.Changes
resolve_state_path(single path navigation) andquery_contract_state(batch) bridge functions, exposed via a dedicatedrpcmoduleget_state_keygetter (bumpsapi_versionto 6)get_state_keymidnight_queryContractStateendpoint with input validation (MAX_STATE_QUERIES=100,MAX_PATH_DEPTH=16)📌 Submission Checklist
🧪 Testing Evidence
Added tests to
tests/e2e/tests/lib.rsand tested it manually in project I have been working on the side https://github.com/RomarQ/midnight-rs🔱 Fork Strategy
get_state_key()is probably the best approach)Links