feat(store/v2): Implement the GetProof for multi store (#18736)

This commit is contained in:
cool-developer
2024-01-17 13:52:12 +00:00
committed by GitHub
parent d9a5b1e9c9
commit bf37398dda
13 changed files with 584 additions and 78 deletions
+4 -12
View File
@@ -185,12 +185,10 @@ func (s *Store) Query(storeKey string, version uint64, key []byte, prove bool) (
}
if prove {
proof, err := s.stateCommitment.GetProof(storeKey, version, key)
result.ProofOps, err = s.stateCommitment.GetProof(storeKey, version, key)
if err != nil {
return store.QueryResult{}, err
}
result.Proof = store.NewIAVLCommitmentOp(key, proof)
}
return result, nil
@@ -331,10 +329,7 @@ func (s *Store) writeSC(cs *store.Changeset) error {
version = previousHeight + 1
}
s.lastCommitInfo = &store.CommitInfo{
Version: version,
StoreInfos: s.stateCommitment.WorkingStoreInfos(version),
}
s.lastCommitInfo = s.stateCommitment.WorkingCommitInfo(version)
return nil
}
@@ -344,15 +339,12 @@ func (s *Store) writeSC(cs *store.Changeset) error {
// solely commits that batch. An error is returned if commit fails or if the
// resulting commit hash is not equivalent to the working hash.
func (s *Store) commitSC(cs *store.Changeset) error {
commitStoreInfos, err := s.stateCommitment.Commit()
cInfo, err := s.stateCommitment.Commit(s.lastCommitInfo.Version)
if err != nil {
return fmt.Errorf("failed to commit SC store: %w", err)
}
commitHash := (&store.CommitInfo{
Version: s.lastCommitInfo.Version,
StoreInfos: commitStoreInfos,
}).Hash()
commitHash := cInfo.Hash()
workingHash, err := s.WorkingHash(cs)
if err != nil {
+39 -5
View File
@@ -18,7 +18,9 @@ import (
)
const (
testStoreKey = "test_store_key"
testStoreKey = "test_store_key"
testStoreKey2 = "test_store_key2"
testStoreKey3 = "test_store_key3"
)
type RootStoreTestSuite struct {
@@ -39,7 +41,9 @@ func (s *RootStoreTestSuite) SetupTest() {
ss := storage.NewStorageStore(sqliteDB)
tree := iavl.NewIavlTree(dbm.NewMemDB(), noopLog, iavl.DefaultConfig())
sc, err := commitment.NewCommitStore(map[string]commitment.Tree{testStoreKey: tree}, noopLog)
tree2 := iavl.NewIavlTree(dbm.NewMemDB(), noopLog, iavl.DefaultConfig())
tree3 := iavl.NewIavlTree(dbm.NewMemDB(), noopLog, iavl.DefaultConfig())
sc, err := commitment.NewCommitStore(map[string]commitment.Tree{testStoreKey: tree, testStoreKey2: tree2, testStoreKey3: tree3}, dbm.NewMemDB(), noopLog)
s.Require().NoError(err)
rs, err := New(noopLog, ss, sc, pruning.DefaultOptions(), pruning.DefaultOptions(), nil)
@@ -96,9 +100,39 @@ func (s *RootStoreTestSuite) TestQuery() {
// ensure the proof is non-nil for the corresponding version
result, err := s.rootStore.Query(testStoreKey, 1, []byte("foo"), true)
s.Require().NoError(err)
s.Require().NotNil(result.Proof.Proof)
s.Require().Equal([]byte("foo"), result.Proof.Proof.GetExist().Key)
s.Require().Equal([]byte("bar"), result.Proof.Proof.GetExist().Value)
s.Require().NotNil(result.ProofOps)
s.Require().Equal([]byte("foo"), result.ProofOps[0].Key)
}
func (s *RootStoreTestSuite) TestQueryProof() {
cs := store.NewChangeset()
// testStoreKey
cs.Add(testStoreKey, []byte("key1"), []byte("value1"))
cs.Add(testStoreKey, []byte("key2"), []byte("value2"))
// testStoreKey2
cs.Add(testStoreKey2, []byte("key3"), []byte("value3"))
// testStoreKey3
cs.Add(testStoreKey3, []byte("key4"), []byte("value4"))
// commit
_, err := s.rootStore.WorkingHash(cs)
s.Require().NoError(err)
_, err = s.rootStore.Commit(cs)
s.Require().NoError(err)
// query proof for testStoreKey
result, err := s.rootStore.Query(testStoreKey, 1, []byte("key1"), true)
s.Require().NoError(err)
s.Require().NotNil(result.ProofOps)
cInfo, err := s.rootStore.GetStateCommitment().GetCommitInfo(1)
s.Require().NoError(err)
storeHash := cInfo.GetStoreCommitID(testStoreKey).Hash
treeRoots, err := result.ProofOps[0].Run([][]byte{[]byte("value1")})
s.Require().NoError(err)
s.Require().Equal(treeRoots[0], storeHash)
expRoots, err := result.ProofOps[1].Run([][]byte{storeHash})
s.Require().NoError(err)
s.Require().Equal(expRoots[0], cInfo.Hash())
}
func (s *RootStoreTestSuite) TestLoadVersion() {