-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dot/rpc): Implement
childstate_getKeys
rpc call (#1800)
* feat: implement childstate_getKeys * chore: finish unit tests * chore: add childstate to http.go module init * chore: address lint warns * chore: addressing test issues * chore: address deepsource complaints * chore: apply changes and add copyright * chore: use reflect.Pointer as Ptr was deprected * chore: increase dot/rpc/http unit test coverage * chore: revert Pointer to Ptr due to go version
- Loading branch information
1 parent
96c30a6
commit 9b2f41e
Showing
15 changed files
with
272 additions
and
9 deletions.
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
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,70 @@ | ||
// Copyright 2019 ChainSafe Systems (ON) Corp. | ||
// This file is part of gossamer. | ||
// | ||
// The gossamer library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The gossamer library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
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 { | ||
hexKeys[idx] = common.BytesToHex(k) | ||
} | ||
|
||
*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,112 @@ | ||
// Copyright 2019 ChainSafe Systems (ON) Corp. | ||
// This file is part of gossamer. | ||
// | ||
// The gossamer library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The gossamer library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.