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: implement querying for commit hash and proofs #156

Merged
merged 3 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
31 changes: 26 additions & 5 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ import (
"github.com/pkg/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/proto/tendermint/crypto"
dbm "github.com/tendermint/tm-db"
)

const (
latestVersionKey = "s/latest"
commitInfoKeyFmt = "s/%d" // s/<version>

proofsPath = "proofs"

// Do not change chunk size without new snapshot format (must be uniform across nodes)
snapshotChunkSize = uint64(10e6)
snapshotBufferSize = int(snapshotChunkSize)
Expand Down Expand Up @@ -567,19 +570,37 @@ func (rs *Store) getStoreByName(name string) types.Store {
// TODO: add proof for `multistore -> substore`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets update the godoc for the proofs special case?

Copy link
Member Author

@p0mvn p0mvn Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wdym by that exactly? Updating the method comment or anything else?

godoc seems to be deprecated: https://pkg.go.dev/golang.org/x/tools/cmd/godoc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to merge, will update this in another PR if I didn't get it right

Copy link
Member

@ValarDragon ValarDragon Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it just means adding a comment about the function for any exported token.

Theres sites that auto-compile this into a docs site. https://godocs.io/github.com/osmosis-labs/osmosis

func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery {
path := req.Path
storeName, subpath, err := parsePath(path)
firstPath, subpath, err := parsePath(path)
if err != nil {
return sdkerrors.QueryResult(err)
}

store := rs.getStoreByName(storeName)
if firstPath == proofsPath {
commitInfo, err := getCommitInfo(rs.db, req.Height)
if err != nil {
return sdkerrors.QueryResult(err)
}
res := abci.ResponseQuery{
Height: req.Height,
Key: []byte(proofsPath),
Value: commitInfo.CommitID().Hash,
ProofOps: &crypto.ProofOps{Ops: make([]crypto.ProofOp, 0, len(commitInfo.StoreInfos))},
}

for _, storeInfo := range commitInfo.StoreInfos {
res.ProofOps.Ops = append(res.ProofOps.Ops, commitInfo.ProofOp(storeInfo.Name))
}
return res
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved
}

store := rs.getStoreByName(firstPath)
if store == nil {
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "no such store: %s", storeName))
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "no such store: %s", firstPath))
}

queryable, ok := store.(types.Queryable)
if !ok {
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "store %s (type %T) doesn't support queries", storeName, store))
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "store %s (type %T) doesn't support queries", firstPath, store))
}

// trim the path and make the query
Expand Down Expand Up @@ -609,7 +630,7 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery {
}

// Restore origin path and append proof op.
res.ProofOps.Ops = append(res.ProofOps.Ops, commitInfo.ProofOp(storeName))
res.ProofOps.Ops = append(res.ProofOps.Ops, commitInfo.ProofOp(firstPath))

return res
}
Expand Down
26 changes: 23 additions & 3 deletions store/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestMultiStoreQuery(t *testing.T) {
k2, v2 := []byte("water"), []byte("flows")
// v3 := []byte("is cold")

cid := multi.Commit()
cid1 := multi.Commit()

// Make sure we can get by name.
garbage := multi.getStoreByName("bad-name")
Expand All @@ -431,8 +431,8 @@ func TestMultiStoreQuery(t *testing.T) {
store2.Set(k2, v2)

// Commit the multistore.
cid = multi.Commit()
ver := cid.Version
cid2 := multi.Commit()
ver := cid2.Version

// Reload multistore from database
multi = newMultiStoreWithMounts(db, pruningTypes.NewPruningOptions(pruningTypes.PruningNothing))
Expand Down Expand Up @@ -474,6 +474,26 @@ func TestMultiStoreQuery(t *testing.T) {
qres = multi.Query(query)
require.EqualValues(t, 0, qres.Code)
require.Equal(t, v2, qres.Value)

// Test proofs latest height
query.Path = fmt.Sprintf("/%s", proofsPath)
qres = multi.Query(query)
require.EqualValues(t, 0, qres.Code)
require.NotNil(t, qres.ProofOps)
require.Equal(t, []byte(proofsPath), qres.Key)
require.Equal(t, cid2.Hash, qres.Value)
require.Equal(t, cid2.Version, qres.Height)
require.Equal(t, 3, len(qres.ProofOps.Ops)) // 3 mounted stores

// Test proofs second latest height
query.Height = query.Height - 1
qres = multi.Query(query)
require.EqualValues(t, 0, qres.Code)
require.NotNil(t, qres.ProofOps)
require.Equal(t, []byte(proofsPath), qres.Key)
require.Equal(t, cid1.Hash, qres.Value)
require.Equal(t, cid1.Version, qres.Height)
require.Equal(t, 3, len(qres.ProofOps.Ops)) // 3 mounted stores
}

func TestMultiStore_Pruning(t *testing.T) {
Expand Down