Skip to content

Commit 1ec0d47

Browse files
authored
fix(dot/rpc/modules): rpc.state.queryStorage fixed (#2565)
1 parent b3698d7 commit 1ec0d47

19 files changed

+595
-510
lines changed

dot/core/service.go

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -560,61 +560,6 @@ func (s *Service) GetMetadata(bhash *common.Hash) ([]byte, error) {
560560
return rt.Metadata()
561561
}
562562

563-
// QueryStorage returns the key-value data by block based on `keys` params
564-
// on every block starting `from` until `to` block, if `to` is not nil
565-
func (s *Service) QueryStorage(from, to common.Hash, keys ...string) (map[common.Hash]QueryKeyValueChanges, error) {
566-
if to.IsEmpty() {
567-
to = s.blockState.BestBlockHash()
568-
}
569-
570-
blocksToQuery, err := s.blockState.SubChain(from, to)
571-
if err != nil {
572-
return nil, err
573-
}
574-
575-
queries := make(map[common.Hash]QueryKeyValueChanges)
576-
577-
for _, hash := range blocksToQuery {
578-
changes, err := s.tryQueryStorage(hash, keys...)
579-
if err != nil {
580-
return nil, err
581-
}
582-
583-
queries[hash] = changes
584-
}
585-
586-
return queries, nil
587-
}
588-
589-
// tryQueryStorage will try to get all the `keys` inside the block's current state
590-
func (s *Service) tryQueryStorage(block common.Hash, keys ...string) (QueryKeyValueChanges, error) {
591-
stateRootHash, err := s.storageState.GetStateRootFromBlock(&block)
592-
if err != nil {
593-
return nil, err
594-
}
595-
596-
changes := make(QueryKeyValueChanges)
597-
for _, k := range keys {
598-
keyBytes, err := common.HexToBytes(k)
599-
if err != nil {
600-
return nil, err
601-
}
602-
603-
storedData, err := s.storageState.GetStorage(stateRootHash, keyBytes)
604-
if err != nil {
605-
return nil, err
606-
}
607-
608-
if storedData == nil {
609-
continue
610-
}
611-
612-
changes[k] = common.BytesToHex(storedData)
613-
}
614-
615-
return changes, nil
616-
}
617-
618563
// GetReadProofAt will return an array with the proofs for the keys passed as params
619564
// based on the block hash passed as param as well, if block hash is nil then the current state will take place
620565
func (s *Service) GetReadProofAt(block common.Hash, keys [][]byte) (

dot/core/service_integration_test.go

Lines changed: 0 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ import (
3535

3636
//go:generate mockgen -destination=mock_telemetry_test.go -package $GOPACKAGE github.com/ChainSafe/gossamer/dot/telemetry Client
3737

38-
const testSlotNumber = 21
39-
4038
func balanceKey(t *testing.T, pub []byte) (bKey []byte) {
4139
t.Helper()
4240

@@ -53,35 +51,6 @@ func balanceKey(t *testing.T, pub []byte) (bKey []byte) {
5351
return
5452
}
5553

56-
func newTestDigest(t *testing.T, slotNumber uint64) scale.VaryingDataTypeSlice {
57-
t.Helper()
58-
testBabeDigest := types.NewBabeDigest()
59-
err := testBabeDigest.Set(types.BabeSecondaryPlainPreDigest{
60-
AuthorityIndex: 17,
61-
SlotNumber: slotNumber,
62-
})
63-
require.NoError(t, err)
64-
data, err := scale.Marshal(testBabeDigest)
65-
require.NoError(t, err)
66-
vdts := types.NewDigest()
67-
err = vdts.Add(
68-
types.PreRuntimeDigest{
69-
ConsensusEngineID: types.BabeEngineID,
70-
Data: data,
71-
},
72-
types.ConsensusDigest{
73-
ConsensusEngineID: types.BabeEngineID,
74-
Data: data,
75-
},
76-
types.SealDigest{
77-
ConsensusEngineID: types.BabeEngineID,
78-
Data: data,
79-
},
80-
)
81-
require.NoError(t, err)
82-
return vdts
83-
}
84-
8554
func generateTestValidRemarkTxns(t *testing.T, pubKey []byte, accInfo types.AccountInfo) ([]byte, runtime.Instance) {
8655
t.Helper()
8756
projectRootPath := filepath.Join(utils.GetProjectRootPathTest(t), "chain/gssmr/genesis.json")
@@ -759,176 +728,3 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
759728
rt.GetCodeHash(),
760729
"expected different code hash after runtime update")
761730
}
762-
763-
func TestTryQueryStore_WhenThereIsDataToRetrieve(t *testing.T) {
764-
s := NewTestService(t, nil)
765-
storageStateTrie, err := rtstorage.NewTrieState(trie.NewTrie(nil))
766-
767-
testKey, testValue := []byte("to"), []byte("0x1723712318238AB12312")
768-
storageStateTrie.Set(testKey, testValue)
769-
require.NoError(t, err)
770-
771-
digest := newTestDigest(t, testSlotNumber)
772-
header, err := types.NewHeader(s.blockState.GenesisHash(), storageStateTrie.MustRoot(), common.Hash{}, 1, digest)
773-
774-
require.NoError(t, err)
775-
776-
err = s.storageState.StoreTrie(storageStateTrie, header)
777-
require.NoError(t, err)
778-
779-
testBlock := &types.Block{
780-
Header: *header,
781-
Body: *types.NewBody([]types.Extrinsic{}),
782-
}
783-
784-
err = s.blockState.AddBlock(testBlock)
785-
require.NoError(t, err)
786-
787-
blockhash := testBlock.Header.Hash()
788-
hexKey := common.BytesToHex(testKey)
789-
keys := []string{hexKey}
790-
791-
changes, err := s.tryQueryStorage(blockhash, keys...)
792-
require.NoError(t, err)
793-
794-
require.Equal(t, changes[hexKey], common.BytesToHex(testValue))
795-
}
796-
797-
func TestTryQueryStore_WhenDoesNotHaveDataToRetrieve(t *testing.T) {
798-
s := NewTestService(t, nil)
799-
storageStateTrie, err := rtstorage.NewTrieState(trie.NewTrie(nil))
800-
require.NoError(t, err)
801-
802-
digest := newTestDigest(t, testSlotNumber)
803-
header, err := types.NewHeader(s.blockState.GenesisHash(), storageStateTrie.MustRoot(), common.Hash{}, 1, digest)
804-
require.NoError(t, err)
805-
806-
err = s.storageState.StoreTrie(storageStateTrie, header)
807-
require.NoError(t, err)
808-
809-
testBlock := &types.Block{
810-
Header: *header,
811-
Body: *types.NewBody([]types.Extrinsic{}),
812-
}
813-
814-
err = s.blockState.AddBlock(testBlock)
815-
require.NoError(t, err)
816-
817-
testKey := []byte("to")
818-
blockhash := testBlock.Header.Hash()
819-
hexKey := common.BytesToHex(testKey)
820-
keys := []string{hexKey}
821-
822-
changes, err := s.tryQueryStorage(blockhash, keys...)
823-
require.NoError(t, err)
824-
825-
require.Empty(t, changes)
826-
}
827-
828-
func TestTryQueryState_WhenDoesNotHaveStateRoot(t *testing.T) {
829-
s := NewTestService(t, nil)
830-
831-
digest := newTestDigest(t, testSlotNumber)
832-
header, err := types.NewHeader(
833-
s.blockState.GenesisHash(),
834-
common.Hash{}, common.Hash{}, 1, digest)
835-
require.NoError(t, err)
836-
837-
testBlock := &types.Block{
838-
Header: *header,
839-
Body: *types.NewBody([]types.Extrinsic{}),
840-
}
841-
842-
err = s.blockState.AddBlock(testBlock)
843-
require.NoError(t, err)
844-
845-
testKey := []byte("to")
846-
blockhash := testBlock.Header.Hash()
847-
hexKey := common.BytesToHex(testKey)
848-
keys := []string{hexKey}
849-
850-
changes, err := s.tryQueryStorage(blockhash, keys...)
851-
require.Error(t, err)
852-
require.Nil(t, changes)
853-
}
854-
855-
func TestQueryStorate_WhenBlocksHasData(t *testing.T) {
856-
keys := []string{
857-
common.BytesToHex([]byte("transfer.to")),
858-
common.BytesToHex([]byte("transfer.from")),
859-
common.BytesToHex([]byte("transfer.value")),
860-
}
861-
862-
s := NewTestService(t, nil)
863-
864-
firstKey, firstValue := []byte("transfer.to"), []byte("some-address-herer")
865-
firstBlock := createNewBlockAndStoreDataAtBlock(
866-
t, s, firstKey, firstValue, s.blockState.GenesisHash(), 1,
867-
)
868-
869-
secondKey, secondValue := []byte("transfer.from"), []byte("another-address-here")
870-
secondBlock := createNewBlockAndStoreDataAtBlock(
871-
t, s, secondKey, secondValue, firstBlock.Header.Hash(), 2,
872-
)
873-
874-
thirdKey, thirdValue := []byte("transfer.value"), []byte("value-gigamegablaster")
875-
thirdBlock := createNewBlockAndStoreDataAtBlock(
876-
t, s, thirdKey, thirdValue, secondBlock.Header.Hash(), 3,
877-
)
878-
879-
from := firstBlock.Header.Hash()
880-
data, err := s.QueryStorage(from, common.Hash{}, keys...)
881-
require.NoError(t, err)
882-
require.Len(t, data, 3)
883-
884-
require.Equal(t, data[firstBlock.Header.Hash()], QueryKeyValueChanges(
885-
map[string]string{
886-
common.BytesToHex(firstKey): common.BytesToHex(firstValue),
887-
},
888-
))
889-
890-
from = secondBlock.Header.Hash()
891-
to := thirdBlock.Header.Hash()
892-
893-
data, err = s.QueryStorage(from, to, keys...)
894-
require.NoError(t, err)
895-
require.Len(t, data, 2)
896-
897-
require.Equal(t, data[secondBlock.Header.Hash()], QueryKeyValueChanges(
898-
map[string]string{
899-
common.BytesToHex(secondKey): common.BytesToHex(secondValue),
900-
},
901-
))
902-
require.Equal(t, data[thirdBlock.Header.Hash()], QueryKeyValueChanges(
903-
map[string]string{
904-
common.BytesToHex(thirdKey): common.BytesToHex(thirdValue),
905-
},
906-
))
907-
}
908-
909-
func createNewBlockAndStoreDataAtBlock(t *testing.T, s *Service,
910-
key, value []byte, parentHash common.Hash,
911-
number uint) *types.Block {
912-
t.Helper()
913-
914-
storageStateTrie, err := rtstorage.NewTrieState(trie.NewTrie(nil))
915-
storageStateTrie.Set(key, value)
916-
require.NoError(t, err)
917-
918-
digest := newTestDigest(t, 421)
919-
header, err := types.NewHeader(parentHash, storageStateTrie.MustRoot(), common.Hash{}, number, digest)
920-
require.NoError(t, err)
921-
922-
err = s.storageState.StoreTrie(storageStateTrie, header)
923-
require.NoError(t, err)
924-
925-
testBlock := &types.Block{
926-
Header: *header,
927-
Body: *types.NewBody([]types.Extrinsic{}),
928-
}
929-
930-
err = s.blockState.AddBlock(testBlock)
931-
require.NoError(t, err)
932-
933-
return testBlock
934-
}

dot/core/service_test.go

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,102 +1229,6 @@ func TestServiceGetMetadata(t *testing.T) {
12291229
})
12301230
}
12311231

1232-
func TestService_tryQueryStorage(t *testing.T) {
1233-
t.Parallel()
1234-
execTest := func(t *testing.T, s *Service, block common.Hash, keys []string, exp QueryKeyValueChanges, expErr error) {
1235-
res, err := s.tryQueryStorage(block, keys...)
1236-
assert.ErrorIs(t, err, expErr)
1237-
if expErr != nil {
1238-
assert.EqualError(t, err, expErr.Error())
1239-
}
1240-
assert.Equal(t, exp, res)
1241-
}
1242-
1243-
t.Run("get state root error", func(t *testing.T) {
1244-
t.Parallel()
1245-
ctrl := gomock.NewController(t)
1246-
mockStorageState := NewMockStorageState(ctrl)
1247-
mockStorageState.EXPECT().GetStateRootFromBlock(&common.Hash{}).Return(nil, errDummyErr)
1248-
service := &Service{
1249-
storageState: mockStorageState,
1250-
}
1251-
execTest(t, service, common.Hash{}, nil, nil, errDummyErr)
1252-
})
1253-
1254-
t.Run("get storage error", func(t *testing.T) {
1255-
t.Parallel()
1256-
ctrl := gomock.NewController(t)
1257-
mockStorageState := NewMockStorageState(ctrl)
1258-
mockStorageState.EXPECT().GetStateRootFromBlock(&common.Hash{}).Return(&common.Hash{}, nil)
1259-
mockStorageState.EXPECT().GetStorage(&common.Hash{}, common.MustHexToBytes("0x01")).Return(nil, errDummyErr)
1260-
service := &Service{
1261-
storageState: mockStorageState,
1262-
}
1263-
execTest(t, service, common.Hash{}, []string{"0x01"}, nil, errDummyErr)
1264-
})
1265-
1266-
t.Run("happy path", func(t *testing.T) {
1267-
t.Parallel()
1268-
ctrl := gomock.NewController(t)
1269-
mockStorageState := NewMockStorageState(ctrl)
1270-
mockStorageState.EXPECT().GetStateRootFromBlock(&common.Hash{}).Return(&common.Hash{}, nil)
1271-
mockStorageState.EXPECT().GetStorage(&common.Hash{}, common.MustHexToBytes("0x01")).
1272-
Return([]byte{1, 2, 3}, nil)
1273-
expChanges := make(QueryKeyValueChanges)
1274-
expChanges["0x01"] = common.BytesToHex([]byte{1, 2, 3})
1275-
service := &Service{
1276-
storageState: mockStorageState,
1277-
}
1278-
execTest(t, service, common.Hash{}, []string{"0x01"}, expChanges, nil)
1279-
})
1280-
}
1281-
1282-
func TestService_QueryStorage(t *testing.T) {
1283-
t.Parallel()
1284-
execTest := func(t *testing.T, s *Service, from common.Hash, to common.Hash,
1285-
keys []string, exp map[common.Hash]QueryKeyValueChanges, expErr error) {
1286-
res, err := s.QueryStorage(from, to, keys...)
1287-
assert.ErrorIs(t, err, expErr)
1288-
if expErr != nil {
1289-
assert.EqualError(t, err, expErr.Error())
1290-
}
1291-
assert.Equal(t, exp, res)
1292-
}
1293-
1294-
t.Run("subchain error", func(t *testing.T) {
1295-
t.Parallel()
1296-
ctrl := gomock.NewController(t)
1297-
mockBlockState := NewMockBlockState(ctrl)
1298-
mockBlockState.EXPECT().BestBlockHash().Return(common.Hash{2})
1299-
mockBlockState.EXPECT().SubChain(common.Hash{1}, common.Hash{2}).Return(nil, errDummyErr)
1300-
service := &Service{
1301-
blockState: mockBlockState,
1302-
}
1303-
execTest(t, service, common.Hash{1}, common.Hash{}, nil, nil, errDummyErr)
1304-
})
1305-
1306-
t.Run("happy path", func(t *testing.T) {
1307-
t.Parallel()
1308-
ctrl := gomock.NewController(t)
1309-
mockBlockState := NewMockBlockState(ctrl)
1310-
mockBlockState.EXPECT().BestBlockHash().Return(common.Hash{2})
1311-
mockBlockState.EXPECT().SubChain(common.Hash{1}, common.Hash{2}).Return([]common.Hash{{0x01}}, nil)
1312-
mockStorageState := NewMockStorageState(ctrl)
1313-
mockStorageState.EXPECT().GetStateRootFromBlock(&common.Hash{0x01}).Return(&common.Hash{}, nil)
1314-
mockStorageState.EXPECT().GetStorage(&common.Hash{}, common.MustHexToBytes("0x01")).
1315-
Return([]byte{1, 2, 3}, nil)
1316-
expChanges := make(QueryKeyValueChanges)
1317-
expChanges["0x01"] = common.BytesToHex([]byte{1, 2, 3})
1318-
expQueries := make(map[common.Hash]QueryKeyValueChanges)
1319-
expQueries[common.Hash{0x01}] = expChanges
1320-
service := &Service{
1321-
blockState: mockBlockState,
1322-
storageState: mockStorageState,
1323-
}
1324-
execTest(t, service, common.Hash{1}, common.Hash{}, []string{"0x01"}, expQueries, nil)
1325-
})
1326-
}
1327-
13281232
func TestService_GetReadProofAt(t *testing.T) {
13291233
t.Parallel()
13301234
execTest := func(t *testing.T, s *Service, block common.Hash, keys [][]byte,

dot/rpc/http.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ func (h *HTTPServer) RegisterModules(mods []string) {
107107
case "grandpa":
108108
srvc = modules.NewGrandpaModule(h.serverConfig.BlockAPI, h.serverConfig.BlockFinalityAPI)
109109
case "state":
110-
srvc = modules.NewStateModule(h.serverConfig.NetworkAPI, h.serverConfig.StorageAPI, h.serverConfig.CoreAPI)
110+
srvc = modules.NewStateModule(h.serverConfig.NetworkAPI, h.serverConfig.StorageAPI,
111+
h.serverConfig.CoreAPI, h.serverConfig.BlockAPI)
111112
case "rpc":
112113
srvc = modules.NewRPCModule(h.serverConfig.RPCAPI)
113114
case "dev":

0 commit comments

Comments
 (0)