Skip to content

Commit 33d59e9

Browse files
committed
fix(state): Change StorageState.GetStorage() to expect a block hash
1 parent f412899 commit 33d59e9

File tree

15 files changed

+129
-219
lines changed

15 files changed

+129
-219
lines changed

dot/core/mock_state_test.go

Lines changed: 4 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func (nodeBuilder) loadRuntime(config *cfg.Config, ns *runtime.NodeStorage,
525525
runtimeCode := make(map[string]runtime.Instance)
526526
for i := range blocks {
527527
hash := &blocks[i]
528-
code, err := stateSrvc.Storage.GetStorageByBlockHash(hash, []byte(":code"))
528+
code, err := stateSrvc.Storage.GetStorage(hash, []byte(":code"))
529529
if err != nil {
530530
return err
531531
}

dot/rpc/interfaces.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ import (
2020

2121
// StorageAPI is the interface for the storage state
2222
type StorageAPI interface {
23-
GetStorage(root *common.Hash, key []byte) ([]byte, error)
23+
GetStorage(bhash *common.Hash, key []byte) ([]byte, error)
2424
GetStorageChild(root *common.Hash, keyToChild []byte) (trie.Trie, error)
2525
GetStorageFromChild(root *common.Hash, keyToChild, key []byte) ([]byte, error)
26-
GetStorageByBlockHash(bhash *common.Hash, key []byte) ([]byte, error)
2726
Entries(root *common.Hash) (map[string][]byte, error)
2827
GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error)
2928
GetKeysWithPrefix(root *common.Hash, prefix []byte) ([][]byte, error)

dot/rpc/modules/api.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ import (
1818

1919
// StorageAPI is the interface for the storage state
2020
type StorageAPI interface {
21-
GetStorage(root *common.Hash, key []byte) ([]byte, error)
21+
GetStorage(bhash *common.Hash, key []byte) ([]byte, error)
2222
GetStorageChild(root *common.Hash, keyToChild []byte) (trie.Trie, error)
2323
GetStorageFromChild(root *common.Hash, keyToChild, key []byte) ([]byte, error)
24-
GetStorageByBlockHash(bhash *common.Hash, key []byte) ([]byte, error)
2524
Entries(root *common.Hash) (map[string][]byte, error)
2625
GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error)
2726
GetKeysWithPrefix(root *common.Hash, prefix []byte) ([][]byte, error)

dot/rpc/modules/api_mocks.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ func NewMockAnyStorageAPI(ctrl *gomock.Controller) *modulesmocks.MockStorageAPI
1818
m.EXPECT().GetStorageFromChild(gomock.Any(), gomock.Any(), gomock.Any()).
1919
Return(nil, nil).AnyTimes()
2020
m.EXPECT().Entries(gomock.Any()).Return(nil, nil).AnyTimes()
21-
m.EXPECT().GetStorageByBlockHash(gomock.Any(), gomock.Any()).
22-
Return(nil, nil).AnyTimes()
2321
m.EXPECT().RegisterStorageObserver(gomock.Any()).AnyTimes()
2422
m.EXPECT().UnregisterStorageObserver(gomock.Any()).AnyTimes()
2523
m.EXPECT().GetStateRootFromBlock(gomock.Any()).Return(nil, nil).AnyTimes()

dot/rpc/modules/mocks/mocks.go

Lines changed: 4 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/mocks_test.go

Lines changed: 4 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/modules/state.go

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (sm *StateModule) GetPairs(_ *http.Request, req *StatePairRequest, res *Sta
234234

235235
*res = make([]interface{}, len(keys))
236236
for i, key := range keys {
237-
val, err := sm.storageAPI.GetStorage(stateRootHash, key)
237+
val, err := sm.storageAPI.GetStorage(req.Bhash, key)
238238
if err != nil {
239239
return err
240240
}
@@ -362,24 +362,15 @@ func (sm *StateModule) GetRuntimeVersion(
362362
// GetStorage Returns a storage entry at a specific block's state.
363363
// If not block hash is provided, the latest value is returned.
364364
func (sm *StateModule) GetStorage(
365-
_ *http.Request, req *StateStorageRequest, res *StateStorageResponse) error {
366-
var (
367-
item []byte
368-
err error
369-
)
370-
365+
_ *http.Request,
366+
req *StateStorageRequest,
367+
res *StateStorageResponse,
368+
) error {
371369
reqBytes, _ := common.HexToBytes(req.Key) // no need to catch error here
372370

373-
if req.Bhash != nil {
374-
item, err = sm.storageAPI.GetStorageByBlockHash(req.Bhash, reqBytes)
375-
if err != nil {
376-
return err
377-
}
378-
} else {
379-
item, err = sm.storageAPI.GetStorage(nil, reqBytes)
380-
if err != nil {
381-
return err
382-
}
371+
item, err := sm.storageAPI.GetStorage(req.Bhash, reqBytes)
372+
if err != nil {
373+
return err
383374
}
384375

385376
if len(item) > 0 {
@@ -392,24 +383,15 @@ func (sm *StateModule) GetStorage(
392383
// GetStorageHash returns the blake2b hash of a storage entry at a block's state.
393384
// If no block hash is provided, the latest value is returned.
394385
func (sm *StateModule) GetStorageHash(
395-
_ *http.Request, req *StateStorageHashRequest, res *StateStorageHashResponse) error {
396-
var (
397-
item []byte
398-
err error
399-
)
400-
386+
_ *http.Request,
387+
req *StateStorageHashRequest,
388+
res *StateStorageHashResponse,
389+
) error {
401390
reqBytes, _ := common.HexToBytes(req.Key)
402391

403-
if req.Bhash != nil {
404-
item, err = sm.storageAPI.GetStorageByBlockHash(req.Bhash, reqBytes)
405-
if err != nil {
406-
return err
407-
}
408-
} else {
409-
item, err = sm.storageAPI.GetStorage(nil, reqBytes)
410-
if err != nil {
411-
return err
412-
}
392+
item, err := sm.storageAPI.GetStorage(req.Bhash, reqBytes)
393+
if err != nil {
394+
return err
413395
}
414396

415397
hash, err := common.Blake2bHash(item)
@@ -425,23 +407,11 @@ func (sm *StateModule) GetStorageHash(
425407
// If no block hash is provided, the latest value is used.
426408
func (sm *StateModule) GetStorageSize(
427409
_ *http.Request, req *StateStorageSizeRequest, res *StateStorageSizeResponse) error {
428-
var (
429-
item []byte
430-
err error
431-
)
432-
433410
reqBytes, _ := common.HexToBytes(req.Key)
434411

435-
if req.Bhash != nil {
436-
item, err = sm.storageAPI.GetStorageByBlockHash(req.Bhash, reqBytes)
437-
if err != nil {
438-
return err
439-
}
440-
} else {
441-
item, err = sm.storageAPI.GetStorage(nil, reqBytes)
442-
if err != nil {
443-
return err
444-
}
412+
item, err := sm.storageAPI.GetStorage(req.Bhash, reqBytes)
413+
if err != nil {
414+
return err
445415
}
446416

447417
if len(item) > 0 {
@@ -487,7 +457,7 @@ func (sm *StateModule) QueryStorage(
487457
changes := make([][2]*string, 0, len(req.Keys))
488458

489459
for j, key := range req.Keys {
490-
value, err := sm.storageAPI.GetStorageByBlockHash(&blockHash, common.MustHexToBytes(key))
460+
value, err := sm.storageAPI.GetStorage(&blockHash, common.MustHexToBytes(key))
491461
if err != nil {
492462
return fmt.Errorf("getting value by block hash: %w", err)
493463
}
@@ -531,7 +501,7 @@ func (sm *StateModule) QueryStorageAt(
531501
changes := make([][2]*string, len(request.Keys))
532502

533503
for i, key := range request.Keys {
534-
value, err := sm.storageAPI.GetStorageByBlockHash(&atBlockHash, common.MustHexToBytes(key))
504+
value, err := sm.storageAPI.GetStorage(&atBlockHash, common.MustHexToBytes(key))
535505
if err != nil {
536506
return fmt.Errorf("getting value by block hash: %w", err)
537507
}

dot/rpc/modules/state_integration_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,11 @@ func TestStateModule_QueryStorage(t *testing.T) {
379379
mockBlockAPI.EXPECT().GetHashByNumber(uint(4)).Return(common.Hash{3, 4}, nil)
380380

381381
mockStorageAPI := NewMockStorageAPI(ctrl)
382-
mockStorageAPI.EXPECT().GetStorageByBlockHash(&common.Hash{1, 2}, []byte{144}).Return([]byte(`value`), nil)
383-
mockStorageAPI.EXPECT().GetStorageByBlockHash(&common.Hash{1, 2}, []byte{128}).
382+
mockStorageAPI.EXPECT().GetStorage(&common.Hash{1, 2}, []byte{144}).Return([]byte(`value`), nil)
383+
mockStorageAPI.EXPECT().GetStorage(&common.Hash{1, 2}, []byte{128}).
384384
Return([]byte(`another value`), nil)
385-
mockStorageAPI.EXPECT().GetStorageByBlockHash(&common.Hash{3, 4}, []byte{144}).Return([]byte(`value`), nil)
386-
mockStorageAPI.EXPECT().GetStorageByBlockHash(&common.Hash{3, 4}, []byte{128}).
385+
mockStorageAPI.EXPECT().GetStorage(&common.Hash{3, 4}, []byte{144}).Return([]byte(`value`), nil)
386+
mockStorageAPI.EXPECT().GetStorage(&common.Hash{3, 4}, []byte{128}).
387387
Return([]byte(`another value`), nil)
388388

389389
module := new(StateModule)

0 commit comments

Comments
 (0)