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

feat(rpc): Implement childstate_getStorageHash RPC call #1805

Merged
merged 15 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type StorageAPI interface {
GetStorage(root *common.Hash, key []byte) ([]byte, error)
GetStorageChild(root *common.Hash, keyToChild []byte) (*trie.Trie, error)
GetStorageFromChild(root *common.Hash, keyToChild, key []byte) ([]byte, error)
GetStorageByBlockHash(bhash common.Hash, key []byte) ([]byte, error)
Entries(root *common.Hash) (map[string][]byte, error)
GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error)
Expand Down
30 changes: 30 additions & 0 deletions dot/rpc/modules/childstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ type GetKeysRequest struct {
Hash common.Hash
}

// GetStorageHash the request to get the entry child storage hash
type GetStorageHash struct {
KeyChild []byte
EntryKey []byte
Hash common.Hash
Copy link
Contributor

Choose a reason for hiding this comment

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

since block hash is optional, can you make this *common.Hash?

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!

}

// ChildStateModule is the module responsible to implement all the childstate RPC calls
type ChildStateModule struct {
storageAPI StorageAPI
Expand Down Expand Up @@ -68,3 +75,26 @@ func (cs *ChildStateModule) GetKeys(_ *http.Request, req *GetKeysRequest, res *[
*res = hexKeys
return nil
}

// GetStorageHash returns the hash of a child storage entry
func (cs *ChildStateModule) GetStorageHash(_ *http.Request, req *GetStorageHash, 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
}

item, err := cs.storageAPI.GetStorageFromChild(stateRoot, req.KeyChild, req.EntryKey)
if err != nil {
return err
}

if len(item) > 0 {
*res = common.BytesToHash(item).String()
}

Copy link
Contributor

Choose a reason for hiding this comment

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

the return value should also be an optional, but this will return the same value for item == nil and an item with length zero that isn't nil

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!

return nil
}
62 changes: 62 additions & 0 deletions dot/rpc/modules/childstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package modules

import (
"fmt"
"math/big"
"testing"

"github.com/ChainSafe/chaindb"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie"
Expand Down Expand Up @@ -67,6 +70,65 @@ func TestChildStateGetKeys(t *testing.T) {
}
}

func TestGetStorageHash(t *testing.T) {
mod, blockHash := setupChildStateStorage(t)

tests := []struct {
expect string
err error
hash common.Hash
keyChild []byte
entry []byte
}{
{
err: nil,
expect: common.BytesToHash([]byte(":child_first_value")).String(),
hash: common.EmptyHash,
entry: []byte(":child_first"),
keyChild: []byte(":child_storage_key"),
},
{
err: nil,
expect: common.BytesToHash([]byte(":child_second_value")).String(),
hash: blockHash,
entry: []byte(":child_second"),
keyChild: []byte(":child_storage_key"),
},
{
err: fmt.Errorf("child trie does not exist at key %s%s", trie.ChildStorageKeyPrefix, []byte(":not_exist")),
hash: blockHash,
entry: []byte(":child_second"),
keyChild: []byte(":not_exist"),
},
{
err: chaindb.ErrKeyNotFound,
hash: common.BytesToHash([]byte("invalid block hash")),
},
}

for _, test := range tests {
var req GetStorageHash
var res string

req.Hash = test.hash
req.EntryKey = test.entry
req.KeyChild = test.keyChild

err := mod.GetStorageHash(nil, &req, &res)

if test.err != nil {
require.Error(t, err)
require.Equal(t, err, test.err)
} else {
require.NoError(t, err)
}

if test.expect != "" {
require.Equal(t, test.expect, res)
}
}
}

func setupChildStateStorage(t *testing.T) (*ChildStateModule, common.Hash) {
t.Helper()

Expand Down
23 changes: 23 additions & 0 deletions dot/rpc/modules/mocks/storage_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.