Skip to content

Commit

Permalink
feat: query module hashes by height (cosmos#343)
Browse files Browse the repository at this point in the history
* test

* more logs

* test

* test

* attempt at sorting

* clean up

* clean up

* whitespace

* add back path

* add to mock

* reorder

* Update baseapp/abci.go

Co-authored-by: Aleksandr Bezobchuk <[email protected]>

* make changes from code review

* change hashes to info

* add const

* code review suggestion

Co-authored-by: Aleksandr Bezobchuk <[email protected]>
  • Loading branch information
czarcas7ic and alexanderbez authored Dec 2, 2022
1 parent 17be107 commit 2e0d6c4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
24 changes: 24 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

const initialAppVersion = 0
const StoreInfoPath = "info"

type AppVersionError struct {
Actual uint64
Expand Down Expand Up @@ -826,7 +827,30 @@ func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) abci.R
resp := queryable.Query(req)
resp.Height = req.Height

if path[1] == StoreInfoPath {
commitInfo, err := app.cms.GetCommitInfoFromDB(resp.Height)
if err != nil {
return sdkerrors.QueryResult(err)
}

// sort the commitInfo, otherwise it returns in a different order every height
sort.Slice(commitInfo.StoreInfos, func(i, j int) bool {
return commitInfo.StoreInfos[i].Name < commitInfo.StoreInfos[j].Name
})

bz, err := codec.ProtoMarshalJSON(commitInfo, app.interfaceRegistry)
if err != nil {
return sdkerrors.QueryResult(sdkerrors.Wrap(err, "failed to JSON encode simulation response"))
}

return abci.ResponseQuery{
Codespace: sdkerrors.RootCodespace,
Height: req.Height,
Value: bz,
}
}
return resp

}

func handleQueryP2P(app *BaseApp, path []string) abci.ResponseQuery {
Expand Down
4 changes: 4 additions & 0 deletions server/mock/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (ms multiStore) GetCommitKVStore(key sdk.StoreKey) sdk.CommitKVStore {
panic("not implemented")
}

func (ms multiStore) GetCommitInfoFromDB(ver int64) (*store.CommitInfo, error) {
panic("not implemented")
}

func (ms multiStore) GetCommitStore(key sdk.StoreKey) sdk.CommitStore {
panic("not implemented")
}
Expand Down
8 changes: 4 additions & 4 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
// load old data if we are not version 0
if ver != 0 {
var err error
cInfo, err = rs.getCommitInfoFromDb(ver)
cInfo, err = rs.GetCommitInfoFromDB(ver)
if err != nil {
return err
}
Expand Down Expand Up @@ -625,7 +625,7 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery {
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "proof is unexpectedly empty; ensure height has not been pruned"))
}

commitInfo, err := rs.getCommitInfoFromDb(res.Height)
commitInfo, err := rs.GetCommitInfoFromDB(res.Height)
if err != nil {
return sdkerrors.QueryResult(err)
}
Expand Down Expand Up @@ -983,7 +983,7 @@ func (rs *Store) flushLastCommitInfo(cInfo *types.CommitInfo) {
}

// Gets commitInfo from disk.
func (rs *Store) getCommitInfoFromDb(ver int64) (*types.CommitInfo, error) {
func (rs *Store) GetCommitInfoFromDB(ver int64) (*types.CommitInfo, error) {
cInfoKey := fmt.Sprintf(commitInfoKeyFmt, ver)

bz, err := rs.db.Get([]byte(cInfoKey))
Expand Down Expand Up @@ -1025,7 +1025,7 @@ func (rs *Store) GetAppVersion() (uint64, error) {
}

func (rs *Store) doProofsQuery(req abci.RequestQuery) abci.ResponseQuery {
commitInfo, err := rs.getCommitInfoFromDb(req.Height)
commitInfo, err := rs.GetCommitInfoFromDB(req.Height)
if err != nil {
return sdkerrors.QueryResult(err)
}
Expand Down
10 changes: 5 additions & 5 deletions store/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestMultistoreLoadWithUpgrade(t *testing.T) {
expectedCommitID := getExpectedCommitID(store, 1)
checkStore(t, store, expectedCommitID, commitID)

ci, err := store.getCommitInfoFromDb(1)
ci, err := store.GetCommitInfoFromDB(1)
require.NoError(t, err)
require.Equal(t, int64(1), ci.Version)
require.Equal(t, 3, len(ci.StoreInfos))
Expand Down Expand Up @@ -298,7 +298,7 @@ func TestMultistoreLoadWithUpgrade(t *testing.T) {
require.Equal(t, v4, rl4.Get(k4))

// check commitInfo in storage
ci, err = store.getCommitInfoFromDb(2)
ci, err = store.GetCommitInfoFromDB(2)
require.NoError(t, err)
require.Equal(t, int64(2), ci.Version)
require.Equal(t, 4, len(ci.StoreInfos), ci.StoreInfos)
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestMultiStoreRestart(t *testing.T) {

multi.Commit()

cinfo, err := multi.getCommitInfoFromDb(int64(i))
cinfo, err := multi.GetCommitInfoFromDB(int64(i))
require.NoError(t, err)
require.Equal(t, int64(i), cinfo.Version)
}
Expand All @@ -369,7 +369,7 @@ func TestMultiStoreRestart(t *testing.T) {

multi.Commit()

flushedCinfo, err := multi.getCommitInfoFromDb(3)
flushedCinfo, err := multi.GetCommitInfoFromDB(3)
require.Nil(t, err)
require.NotEqual(t, initCid, flushedCinfo, "CID is different after flush to disk")

Expand All @@ -379,7 +379,7 @@ func TestMultiStoreRestart(t *testing.T) {

multi.Commit()

postFlushCinfo, err := multi.getCommitInfoFromDb(4)
postFlushCinfo, err := multi.GetCommitInfoFromDB(4)
require.NoError(t, err)
require.Equal(t, int64(4), postFlushCinfo.Version, "Commit changed after in-memory commit")

Expand Down
3 changes: 3 additions & 0 deletions store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ type CommitMultiStore interface {
// Panics on a nil key.
GetCommitKVStore(key StoreKey) CommitKVStore

// Panics on a nil version.
GetCommitInfoFromDB(ver int64) (*CommitInfo, error)

// Load the latest persisted version. Called once after all calls to
// Mount*Store() are complete.
LoadLatestVersion() error
Expand Down

0 comments on commit 2e0d6c4

Please sign in to comment.