-
Notifications
You must be signed in to change notification settings - Fork 146
feat(dot/rpc): implement RPC method state_queryStorageAt #3185
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
Changes from all commits
d1e9475
af38364
0c3214d
f7c95f6
683adc4
39b2b5c
49737fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Copyright 2023 ChainSafe Systems (ON) | ||
| // SPDX-License-Identifier: LGPL-3.0-only | ||
| package chain | ||
|
|
||
| import "embed" | ||
|
|
||
| // DefaultConfigTomlFiles is the embedded file system containing the default toml configurations. | ||
| // | ||
| //go:embed */*.toml | ||
| var DefaultConfigTomlFiles embed.FS |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,12 @@ type StateStorageQueryRangeRequest struct { | |
| EndBlock common.Hash `json:"block"` | ||
| } | ||
|
|
||
| // StateStorageQueryAtRequest holds json fields | ||
| type StateStorageQueryAtRequest struct { | ||
|
Comment on lines
+79
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would apply to its properties as well. I don't see any variable of type StateStorageQueryAtRequest getting used by scale. Because scale requires structs and properties to be exported.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think they are exported for the encoding/json encoder to work right?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, they are exported for encoding/json encoder to work.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still, is there a reason we keep
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we're using Gorilla web toolkit to expose functions through RPC, Gorilla exposes functions with a signature of containing exported structs for request and response. I tried changing StateStorageQueryAtRequest to un-exported and the RPC function was no longer accessible. |
||
| Keys []string `json:"keys" validate:"required"` | ||
| At common.Hash `json:"at"` | ||
| } | ||
|
|
||
| // StateStorageKeysQuery field to store storage keys | ||
| type StateStorageKeysQuery [][]byte | ||
|
|
||
|
|
@@ -467,10 +473,13 @@ func (sm *StateModule) QueryStorage( | |
| var hexValue *string | ||
| if len(value) > 0 { | ||
| hexValue = stringPtr(common.BytesToHex(value)) | ||
| } else if value != nil { // empty byte slice value | ||
| hexValue = stringPtr("0x") | ||
| } | ||
|
|
||
| differentValueEncountered := i == startBlockNumber || | ||
| lastValue[j] == nil && hexValue != nil || | ||
| lastValue[j] != nil && hexValue == nil || | ||
| lastValue[j] != nil && *lastValue[j] != *hexValue | ||
| if differentValueEncountered { | ||
| changes = append(changes, [2]*string{stringPtr(key), hexValue}) | ||
|
|
@@ -489,6 +498,40 @@ func (sm *StateModule) QueryStorage( | |
| return nil | ||
| } | ||
|
|
||
| // QueryStorageAt queries historical storage entries (by key) at the block hash given or | ||
| // the best block if the given block hash is nil | ||
| func (sm *StateModule) QueryStorageAt( | ||
| _ *http.Request, request *StateStorageQueryAtRequest, response *[]StorageChangeSetResponse) error { | ||
| atBlockHash := request.At | ||
| if atBlockHash.IsEmpty() { | ||
| atBlockHash = sm.blockAPI.BestBlockHash() | ||
| } | ||
|
|
||
| changes := make([][2]*string, len(request.Keys)) | ||
|
|
||
| for i, key := range request.Keys { | ||
| value, err := sm.storageAPI.GetStorageByBlockHash(&atBlockHash, common.MustHexToBytes(key)) | ||
| if err != nil { | ||
| return fmt.Errorf("getting value by block hash: %w", err) | ||
| } | ||
| var hexValue *string | ||
| if len(value) > 0 { | ||
edwardmack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| hexValue = stringPtr(common.BytesToHex(value)) | ||
| } else if value != nil { // empty byte slice value | ||
| hexValue = stringPtr("0x") | ||
| } | ||
|
|
||
| changes[i] = [2]*string{stringPtr(key), hexValue} | ||
| } | ||
|
|
||
| *response = []StorageChangeSetResponse{{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL you don't need to initialize the slice pointer which is weird AF
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is because |
||
| Block: &atBlockHash, | ||
| Changes: changes, | ||
| }} | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func stringPtr(s string) *string { return &s } | ||
|
|
||
| // SubscribeRuntimeVersion initialised a runtime version subscription and returns the current version | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.