-
Notifications
You must be signed in to change notification settings - Fork 112
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
feat(dot/rpc): Implement childstate_getKeys
rpc call
#1800
Merged
EclesioMeloJunior
merged 14 commits into
development
from
eclesio/rpc-child-state-getkeys
Sep 27, 2021
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ad38cc2
feat: implement childstate_getKeys
EclesioMeloJunior 7b634a1
chore: finish unit tests
EclesioMeloJunior 88316c7
Merge branch 'development' into eclesio/rpc-child-state-getkeys
EclesioMeloJunior 10e41bf
chore: add childstate to http.go module init
EclesioMeloJunior b3ad509
chore: address lint warns
EclesioMeloJunior 99b8f30
chore: addressing test issues
EclesioMeloJunior 08e9dc6
chore: address deepsource complaints
EclesioMeloJunior 4a53896
Merge branch 'development' into eclesio/rpc-child-state-getkeys
EclesioMeloJunior 539bc7a
chore: apply changes and add copyright
EclesioMeloJunior 2b7544f
Merge branch 'development' into eclesio/rpc-child-state-getkeys
EclesioMeloJunior 4366810
Merge branch 'development' into eclesio/rpc-child-state-getkeys
EclesioMeloJunior 957e200
chore: use reflect.Pointer as Ptr was deprected
EclesioMeloJunior 486782e
chore: increase dot/rpc/http unit test coverage
EclesioMeloJunior dd325a5
chore: revert Pointer to Ptr due to go version
EclesioMeloJunior File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package modules | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
) | ||
|
||
// GetKeysRequest represents the request to retrieve the keys of a child storage | ||
type GetKeysRequest struct { | ||
Key []byte | ||
Prefix []byte | ||
Hash common.Hash | ||
} | ||
|
||
// ChildStateModule is the module responsible to implement all the childstate RPC calls | ||
type ChildStateModule struct { | ||
storageAPI StorageAPI | ||
blockAPI BlockAPI | ||
} | ||
|
||
// NewChildStateModule returns a new ChildStateModule | ||
func NewChildStateModule(s StorageAPI, b BlockAPI) *ChildStateModule { | ||
return &ChildStateModule{ | ||
storageAPI: s, | ||
blockAPI: b, | ||
} | ||
} | ||
|
||
// GetKeys returns the keys from the specified child storage. The keys can also be filtered based on a prefix. | ||
func (cs *ChildStateModule) GetKeys(_ *http.Request, req *GetKeysRequest, res *[]string) error { | ||
if req.Hash == common.EmptyHash { | ||
req.Hash = cs.blockAPI.BestBlockHash() | ||
} | ||
|
||
stateRoot, err := cs.storageAPI.GetStateRootFromBlock(&req.Hash) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
trie, err := cs.storageAPI.GetStorageChild(stateRoot, req.Key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
keys := trie.GetKeysWithPrefix(req.Prefix) | ||
hexKeys := make([]string, len(keys)) | ||
for idx, k := range keys { | ||
hex := common.BytesToHex(k) | ||
hexKeys[idx] = hex | ||
} | ||
|
||
*res = hexKeys | ||
return nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package modules | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ChainSafe/gossamer/dot/types" | ||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/ChainSafe/gossamer/lib/trie" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestChildStateGetKeys(t *testing.T) { | ||
childStateModule, currBlockHash := setupChildStateStorage(t) | ||
|
||
req := &GetKeysRequest{ | ||
Key: []byte(":child_storage_key"), | ||
Prefix: []byte{}, | ||
Hash: common.EmptyHash, | ||
} | ||
|
||
res := make([]string, 0) | ||
err := childStateModule.GetKeys(nil, req, &res) | ||
require.NoError(t, err) | ||
require.Len(t, res, 3) | ||
|
||
for _, r := range res { | ||
b, dErr := common.HexToBytes(r) | ||
require.NoError(t, dErr) | ||
require.Contains(t, []string{ | ||
":child_first", ":child_second", ":another_child", | ||
}, string(b)) | ||
} | ||
|
||
req = &GetKeysRequest{ | ||
Key: []byte(":child_storage_key"), | ||
Prefix: []byte(":child_"), | ||
Hash: currBlockHash, | ||
} | ||
|
||
err = childStateModule.GetKeys(nil, req, &res) | ||
require.NoError(t, err) | ||
require.Len(t, res, 2) | ||
|
||
for _, r := range res { | ||
b, err := common.HexToBytes(r) | ||
require.NoError(t, err) | ||
require.Contains(t, []string{ | ||
":child_first", ":child_second", | ||
}, string(b)) | ||
} | ||
} | ||
|
||
func setupChildStateStorage(t *testing.T) (*ChildStateModule, common.Hash) { | ||
t.Helper() | ||
|
||
st := newTestStateService(t) | ||
|
||
tr, err := st.Storage.TrieState(nil) | ||
require.NoError(t, err) | ||
|
||
tr.Set([]byte(":first_key"), []byte(":value1")) | ||
tr.Set([]byte(":second_key"), []byte(":second_value")) | ||
|
||
childTr := trie.NewEmptyTrie() | ||
childTr.Put([]byte(":child_first"), []byte(":child_first_value")) | ||
childTr.Put([]byte(":child_second"), []byte(":child_second_value")) | ||
childTr.Put([]byte(":another_child"), []byte("value")) | ||
|
||
err = tr.SetChild([]byte(":child_storage_key"), childTr) | ||
require.NoError(t, err) | ||
|
||
stateRoot, err := tr.Root() | ||
require.NoError(t, err) | ||
|
||
bb, err := st.Block.BestBlock() | ||
require.NoError(t, err) | ||
|
||
err = st.Storage.StoreTrie(tr, nil) | ||
require.NoError(t, err) | ||
|
||
b := &types.Block{ | ||
Header: types.Header{ | ||
ParentHash: bb.Header.Hash(), | ||
Number: big.NewInt(0).Add(big.NewInt(1), bb.Header.Number), | ||
StateRoot: stateRoot, | ||
}, | ||
Body: []byte{}, | ||
} | ||
|
||
err = st.Block.AddBlock(b) | ||
require.NoError(t, err) | ||
|
||
hash, _ := st.Block.GetBlockHash(b.Header.Number) | ||
return NewChildStateModule(st.Storage, st.Block), hash | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.