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_getStorageSize RPC call #1810

Merged
merged 21 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ad38cc2
feat: implement childstate_getKeys
EclesioMeloJunior Sep 21, 2021
7b634a1
chore: finish unit tests
EclesioMeloJunior Sep 21, 2021
88316c7
Merge branch 'development' into eclesio/rpc-child-state-getkeys
EclesioMeloJunior Sep 22, 2021
10e41bf
chore: add childstate to http.go module init
EclesioMeloJunior Sep 22, 2021
b3ad509
chore: address lint warns
EclesioMeloJunior Sep 22, 2021
99b8f30
chore: addressing test issues
EclesioMeloJunior Sep 22, 2021
08e9dc6
chore: address deepsource complaints
EclesioMeloJunior Sep 22, 2021
e658579
feat: implement childstate_getStorageHash
EclesioMeloJunior Sep 23, 2021
c39f2b3
Merge branch 'development' into eclesio/rpc-childState-storageHash
EclesioMeloJunior Sep 23, 2021
bad721c
feat: implement childstate_getStorageSize
EclesioMeloJunior Sep 24, 2021
5fe72c2
Merge branch 'development' into eclesio/rpc-childstate-storagesize
EclesioMeloJunior Sep 24, 2021
b1b6b9d
Merge branch 'development' into eclesio/rpc-childstate-storagesize
EclesioMeloJunior Sep 28, 2021
5e3e3fa
Merge branch 'development' into eclesio/rpc-childstate-storagesize
EclesioMeloJunior Sep 29, 2021
4d76ddb
Merge branch 'development' into eclesio/rpc-childstate-storagesize
EclesioMeloJunior Sep 30, 2021
82e0314
chore: check nil before return len
EclesioMeloJunior Sep 30, 2021
8563eb2
chore: fix export comment
EclesioMeloJunior Oct 1, 2021
bcfbda8
chore: fix conflicts
EclesioMeloJunior Oct 4, 2021
e097783
chore: transform common.Hash prop into pointer
EclesioMeloJunior Oct 4, 2021
2fe0153
chore: fix unit test
EclesioMeloJunior Oct 4, 2021
dc1a671
Merge branch 'development' into eclesio/rpc-childstate-storagesize
EclesioMeloJunior Oct 4, 2021
dc6a5ec
Merge branch 'development' into eclesio/rpc-childstate-storagesize
EclesioMeloJunior Oct 5, 2021
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
34 changes: 34 additions & 0 deletions dot/rpc/modules/childstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ type GetStorageHash struct {
Hash *common.Hash
}

// GetChildStorageRequest the request to get the entry child storage hash
type GetChildStorageRequest struct {
KeyChild []byte
EntryKey []byte
Hash *common.Hash
}

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

// GetStorageSize returns the size of a child storage entry.
func (cs *ChildStateModule) GetStorageSize(_ *http.Request, req *GetChildStorageRequest, res *uint64) error {
var hash common.Hash

if req.Hash == nil {
hash = cs.blockAPI.BestBlockHash()
} else {
hash = *req.Hash
}

stateRoot, err := cs.storageAPI.GetStateRootFromBlock(&hash)
if err != nil {
return err
}

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

if item != nil {
*res = uint64(len(item))
}

return nil
}

// GetStorageHash returns the hash of a child storage entry
func (cs *ChildStateModule) GetStorageHash(_ *http.Request, req *GetStorageHash, res *string) error {
var hash common.Hash
Expand Down
66 changes: 65 additions & 1 deletion dot/rpc/modules/childstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"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 @@ -70,6 +69,71 @@ func TestChildStateGetKeys(t *testing.T) {
}
}

func TestChildStateGetStorageSize(t *testing.T) {
mod, blockHash := setupChildStateStorage(t)
invalidHash := common.BytesToHash([]byte("invalid block hash"))

tests := []struct {
expect uint64
err error
hash *common.Hash
keyChild []byte
entry []byte
}{
{
err: nil,
expect: uint64(len([]byte(":child_first_value"))),
hash: nil,
entry: []byte(":child_first"),
keyChild: []byte(":child_storage_key"),
},
{
err: nil,
expect: uint64(len([]byte(":child_second_value"))),
hash: &blockHash,
entry: []byte(":child_second"),
keyChild: []byte(":child_storage_key"),
},
{
err: nil,
expect: 0,
hash: nil,
entry: []byte(":not_found_so_size_0"),
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: &invalidHash,
},
}

for _, test := range tests {
var req GetChildStorageRequest
var res uint64

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

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

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

require.Equal(t, test.expect, res)
}
}

func TestGetStorageHash(t *testing.T) {
mod, blockHash := setupChildStateStorage(t)
invalidBlockHash := common.BytesToHash([]byte("invalid block hash"))
Expand Down