Skip to content

Commit 77f3458

Browse files
EclesioMeloJuniortimwu20
authored andcommitted
feat(rpc): Implement childstate_getStorageSize RPC call (ChainSafe#1810)
* 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 * feat: implement childstate_getStorageHash * feat: implement childstate_getStorageSize * chore: check nil before return len * chore: fix export comment * chore: transform common.Hash prop into pointer * chore: fix unit test
1 parent c4cdb8a commit 77f3458

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

dot/rpc/modules/childstate.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ type GetStorageHash struct {
3636
Hash *common.Hash
3737
}
3838

39+
// GetChildStorageRequest the request to get the entry child storage hash
40+
type GetChildStorageRequest struct {
41+
KeyChild []byte
42+
EntryKey []byte
43+
Hash *common.Hash
44+
}
45+
3946
// ChildStateModule is the module responsible to implement all the childstate RPC calls
4047
type ChildStateModule struct {
4148
storageAPI StorageAPI
@@ -80,6 +87,33 @@ func (cs *ChildStateModule) GetKeys(_ *http.Request, req *GetKeysRequest, res *[
8087
return nil
8188
}
8289

90+
// GetStorageSize returns the size of a child storage entry.
91+
func (cs *ChildStateModule) GetStorageSize(_ *http.Request, req *GetChildStorageRequest, res *uint64) error {
92+
var hash common.Hash
93+
94+
if req.Hash == nil {
95+
hash = cs.blockAPI.BestBlockHash()
96+
} else {
97+
hash = *req.Hash
98+
}
99+
100+
stateRoot, err := cs.storageAPI.GetStateRootFromBlock(&hash)
101+
if err != nil {
102+
return err
103+
}
104+
105+
item, err := cs.storageAPI.GetStorageFromChild(stateRoot, req.KeyChild, req.EntryKey)
106+
if err != nil {
107+
return err
108+
}
109+
110+
if item != nil {
111+
*res = uint64(len(item))
112+
}
113+
114+
return nil
115+
}
116+
83117
// GetStorageHash returns the hash of a child storage entry
84118
func (cs *ChildStateModule) GetStorageHash(_ *http.Request, req *GetStorageHash, res *string) error {
85119
var hash common.Hash

dot/rpc/modules/childstate_test.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"testing"
2323

2424
"github.com/ChainSafe/chaindb"
25-
2625
"github.com/ChainSafe/gossamer/dot/types"
2726
"github.com/ChainSafe/gossamer/lib/common"
2827
"github.com/ChainSafe/gossamer/lib/trie"
@@ -70,6 +69,71 @@ func TestChildStateGetKeys(t *testing.T) {
7069
}
7170
}
7271

72+
func TestChildStateGetStorageSize(t *testing.T) {
73+
mod, blockHash := setupChildStateStorage(t)
74+
invalidHash := common.BytesToHash([]byte("invalid block hash"))
75+
76+
tests := []struct {
77+
expect uint64
78+
err error
79+
hash *common.Hash
80+
keyChild []byte
81+
entry []byte
82+
}{
83+
{
84+
err: nil,
85+
expect: uint64(len([]byte(":child_first_value"))),
86+
hash: nil,
87+
entry: []byte(":child_first"),
88+
keyChild: []byte(":child_storage_key"),
89+
},
90+
{
91+
err: nil,
92+
expect: uint64(len([]byte(":child_second_value"))),
93+
hash: &blockHash,
94+
entry: []byte(":child_second"),
95+
keyChild: []byte(":child_storage_key"),
96+
},
97+
{
98+
err: nil,
99+
expect: 0,
100+
hash: nil,
101+
entry: []byte(":not_found_so_size_0"),
102+
keyChild: []byte(":child_storage_key"),
103+
},
104+
{
105+
err: fmt.Errorf("child trie does not exist at key %s%s", trie.ChildStorageKeyPrefix, []byte(":not_exist")),
106+
hash: &blockHash,
107+
entry: []byte(":child_second"),
108+
keyChild: []byte(":not_exist"),
109+
},
110+
{
111+
err: chaindb.ErrKeyNotFound,
112+
hash: &invalidHash,
113+
},
114+
}
115+
116+
for _, test := range tests {
117+
var req GetChildStorageRequest
118+
var res uint64
119+
120+
req.Hash = test.hash
121+
req.EntryKey = test.entry
122+
req.KeyChild = test.keyChild
123+
124+
err := mod.GetStorageSize(nil, &req, &res)
125+
126+
if test.err != nil {
127+
require.Error(t, err)
128+
require.Equal(t, err, test.err)
129+
} else {
130+
require.NoError(t, err)
131+
}
132+
133+
require.Equal(t, test.expect, res)
134+
}
135+
}
136+
73137
func TestGetStorageHash(t *testing.T) {
74138
mod, blockHash := setupChildStateStorage(t)
75139
invalidBlockHash := common.BytesToHash([]byte("invalid block hash"))

0 commit comments

Comments
 (0)