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 state_getReadProof rpc call #1768

Merged
merged 48 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
0e5afc8
feat: implement chain subscribe all heads
EclesioMeloJunior Aug 19, 2021
4586709
chore: add unit tests to subscribe all heads
EclesioMeloJunior Aug 20, 2021
a47cc08
Merge branch 'development' into eclesio/rpc-subscribe-all-heads
EclesioMeloJunior Aug 20, 2021
91c0756
chore: fix imports
EclesioMeloJunior Aug 23, 2021
659c503
feat: implement state_getReadProof rpc call
EclesioMeloJunior Aug 25, 2021
8160592
chore: fix unused import
EclesioMeloJunior Aug 25, 2021
3f016f9
Merge branch 'development' into eclesio/get-read-proof-rpc
EclesioMeloJunior Aug 25, 2021
8d1b73b
chore: fix lint warnings
EclesioMeloJunior Aug 25, 2021
30c4581
wip: proof generation algotithm
EclesioMeloJunior Sep 7, 2021
e0f4411
Merge branch 'development' into eclesio/get-read-proof-rpc
EclesioMeloJunior Sep 8, 2021
58c73bb
wip: append not working
EclesioMeloJunior Sep 14, 2021
4c1e63e
wip: fixing functions
EclesioMeloJunior Sep 15, 2021
26e1ad4
wip: remove unused code
EclesioMeloJunior Sep 15, 2021
1758518
feat: proof generation working
EclesioMeloJunior Sep 17, 2021
b52a767
chore: add multiple keys proof generation
EclesioMeloJunior Sep 17, 2021
b723923
chore: add unit tests
EclesioMeloJunior Sep 17, 2021
ae9e8ec
chore: add unit test
EclesioMeloJunior Sep 20, 2021
725c949
chore: remove database exposure
EclesioMeloJunior Sep 20, 2021
3079a97
chore: fix lint warnings
EclesioMeloJunior Sep 20, 2021
298eeaf
Merge branch 'development' into eclesio/get-read-proof-rpc
EclesioMeloJunior Sep 21, 2021
56a7263
chore: update GenerateTrieProof from block to storage state
EclesioMeloJunior Sep 21, 2021
449acb4
chore: remove repeated code
EclesioMeloJunior Sep 22, 2021
6ce6884
chore: resolve fix lint
EclesioMeloJunior Sep 22, 2021
f6aafb0
chore: update tests
EclesioMeloJunior Sep 22, 2021
bdfbe80
chore: simplify to sliced value itself
EclesioMeloJunior Sep 22, 2021
7f130a8
chore: deepsource ignore auto generated files
EclesioMeloJunior Sep 22, 2021
e800b0b
chore: remove unused refs
EclesioMeloJunior Sep 22, 2021
76ff9c9
chore: addressing deepsource warngins
EclesioMeloJunior Sep 22, 2021
1758e91
chore: addressing deepsource wars
EclesioMeloJunior Sep 22, 2021
3abc4b0
Merge branch 'development' into eclesio/get-read-proof-rpc
EclesioMeloJunior Sep 23, 2021
6fd30fc
chore: remove DS_Store
EclesioMeloJunior Sep 24, 2021
c68fc54
chore: fix typo
EclesioMeloJunior Sep 24, 2021
ba421be
chore: use IsEmpty as record method
EclesioMeloJunior Sep 24, 2021
d652444
chore: update comment export
EclesioMeloJunior Sep 24, 2021
9a06653
chore: resolve errors
EclesioMeloJunior Sep 27, 2021
b48fbce
chore: resolve exported
EclesioMeloJunior Sep 27, 2021
9fc6711
chore: resolve test to dont exported
EclesioMeloJunior Sep 27, 2021
87d9909
Merge branch 'development' into eclesio/get-read-proof-rpc
EclesioMeloJunior Sep 27, 2021
3406405
chore: resolve interface mocks
EclesioMeloJunior Sep 27, 2021
17c4302
chore: remove unused code
EclesioMeloJunior Sep 27, 2021
391401e
chore: resolve unues funcs
EclesioMeloJunior Sep 27, 2021
00d3f7c
chore: resolve failing unit tests
EclesioMeloJunior Sep 27, 2021
9861aa2
Merge branch 'development' into eclesio/get-read-proof-rpc
EclesioMeloJunior Sep 27, 2021
f5b151b
chore: update dot/core/services.go unit test coverage
EclesioMeloJunior Sep 28, 2021
73d6955
chore: remove skip from test
EclesioMeloJunior Sep 28, 2021
d840770
chore: remove comment
EclesioMeloJunior Sep 28, 2021
70adfe1
Merge branch 'development' into eclesio/get-read-proof-rpc
EclesioMeloJunior Sep 29, 2021
b403553
Merge branch 'development' into eclesio/get-read-proof-rpc
EclesioMeloJunior Sep 30, 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
2 changes: 2 additions & 0 deletions dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type BlockState interface {
AddBlock(*types.Block) error
GetAllBlocksAtDepth(hash common.Hash) []common.Hash
GetBlockByHash(common.Hash) (*types.Block, error)
GetBlockStateRoot(bhash common.Hash) (common.Hash, error)
GenesisHash() common.Hash
GetSlotForBlock(common.Hash) (uint64, error)
GetFinalisedHeader(uint64, uint64) (*types.Header, error)
Expand All @@ -62,6 +63,7 @@ type StorageState interface {
StoreTrie(*rtstorage.TrieState, *types.Header) error
GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error)
GetStorage(root *common.Hash, key []byte) ([]byte, error)
GenerateTrieProof(stateRoot common.Hash, keys [][]byte) ([][]byte, error)
sync.Locker
}

Expand Down
25 changes: 24 additions & 1 deletion dot/core/mocks/block_state.go

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

180 changes: 180 additions & 0 deletions dot/core/mocks/storage_state.go

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

20 changes: 20 additions & 0 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,23 @@ func (s *Service) tryQueryStorage(block common.Hash, keys ...string) (QueryKeyVa

return changes, nil
}

// GetReadProofAt will return an array with the proofs for the keys passed as params
// based on the block hash passed as param as well, if block hash is nil then the current state will take place
func (s *Service) GetReadProofAt(block common.Hash, keys [][]byte) (common.Hash, [][]byte, error) {
if common.EmptyHash.Equal(block) {
block = s.blockState.BestBlockHash()
}

stateRoot, err := s.blockState.GetBlockStateRoot(block)
if err != nil {
return common.EmptyHash, nil, err
}

proofForKeys, err := s.storageState.GenerateTrieProof(stateRoot, keys)
if err != nil {
return common.EmptyHash, nil, err
}

return block, proofForKeys, nil
noot marked this conversation as resolved.
Show resolved Hide resolved
}
76 changes: 74 additions & 2 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"time"

"github.com/ChainSafe/gossamer/dot/core/mocks"
coremocks "github.com/ChainSafe/gossamer/dot/core/mocks"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/sync"
Expand Down Expand Up @@ -107,7 +106,7 @@ func TestStartService(t *testing.T) {
}

func TestAnnounceBlock(t *testing.T) {
net := new(coremocks.MockNetwork)
net := new(mocks.MockNetwork)
cfg := &Config{
Network: net,
}
Expand Down Expand Up @@ -829,3 +828,76 @@ func TestDecodeSessionKeys_WhenGetRuntimeReturnError(t *testing.T) {
require.Error(t, err, "problems")
require.Nil(t, b)
}

func TestGetReadProofAt(t *testing.T) {
keysToProof := [][]byte{[]byte("first_key"), []byte("another_key")}
mockedProofs := [][]byte{[]byte("proof01"), []byte("proof02")}

t.Run("When Has Block Is Empty", func(t *testing.T) {
mockedStateRootHash := common.NewHash([]byte("state root hash"))
expectedBlockHash := common.NewHash([]byte("expected block hash"))

mockBlockState := new(mocks.MockBlockState)
mockBlockState.On("BestBlockHash").Return(expectedBlockHash)
mockBlockState.On("GetBlockStateRoot", expectedBlockHash).
Return(mockedStateRootHash, nil)

mockStorageStage := new(mocks.MockStorageState)
mockStorageStage.On("GenerateTrieProof", mockedStateRootHash, keysToProof).
Return(mockedProofs, nil)

s := &Service{
blockState: mockBlockState,
storageState: mockStorageStage,
}

b, p, err := s.GetReadProofAt(common.EmptyHash, keysToProof)
require.NoError(t, err)
require.Equal(t, p, mockedProofs)
require.Equal(t, expectedBlockHash, b)

mockBlockState.AssertCalled(t, "BestBlockHash")
mockBlockState.AssertCalled(t, "GetBlockStateRoot", expectedBlockHash)
mockStorageStage.AssertCalled(t, "GenerateTrieProof", mockedStateRootHash, keysToProof)
})

t.Run("When GetStateRoot fails", func(t *testing.T) {
mockedBlockHash := common.NewHash([]byte("fake block hash"))

mockBlockState := new(mocks.MockBlockState)
mockBlockState.On("GetBlockStateRoot", mockedBlockHash).
Return(common.EmptyHash, errors.New("problems while getting state root"))

s := &Service{
blockState: mockBlockState,
}

b, p, err := s.GetReadProofAt(mockedBlockHash, keysToProof)
require.Equal(t, common.EmptyHash, b)
require.Nil(t, p)
require.Error(t, err)
})

t.Run("When GenerateTrieProof fails", func(t *testing.T) {
mockedBlockHash := common.NewHash([]byte("fake block hash"))
mockedStateRootHash := common.NewHash([]byte("state root hash"))

mockBlockState := new(mocks.MockBlockState)
mockBlockState.On("GetBlockStateRoot", mockedBlockHash).
Return(mockedStateRootHash, nil)

mockStorageStage := new(mocks.MockStorageState)
mockStorageStage.On("GenerateTrieProof", mockedStateRootHash, keysToProof).
Return(nil, errors.New("problems to generate trie proof"))

s := &Service{
blockState: mockBlockState,
storageState: mockStorageStage,
}

b, p, err := s.GetReadProofAt(mockedBlockHash, keysToProof)
require.Equal(t, common.EmptyHash, b)
require.Nil(t, p)
require.Error(t, err)
})
}
1 change: 1 addition & 0 deletions dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type CoreAPI interface {
GetMetadata(bhash *common.Hash) ([]byte, error)
QueryStorage(from, to common.Hash, keys ...string) (map[common.Hash]core.QueryKeyValueChanges, error)
DecodeSessionKeys(enc []byte) ([]byte, error)
GetReadProofAt(block common.Hash, keys [][]byte) (common.Hash, [][]byte, error)
}

// RPCAPI is the interface for methods related to RPC service
Expand Down
32 changes: 32 additions & 0 deletions dot/rpc/modules/mocks/core_api.go

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

Loading