Skip to content

Commit

Permalink
feat: auto-set block timestamp for historical queries (cosmos#15448) …
Browse files Browse the repository at this point in the history
…(backport #440) (#441)

* feat: auto-set block timestamp for historical queries (cosmos#15448) (backport #440)

* updates

* updates

* updates

---------

Co-authored-by: Roman <roman@osmosis.team>
  • Loading branch information
mergify[bot] and p0mvn committed May 8, 2023
1 parent 8ee894c commit 9310076
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 31 deletions.
11 changes: 11 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -638,6 +639,16 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e
cacheMS, app.checkState.ctx.BlockHeader(), true, app.logger,
).WithMinGasPrices(app.minGasPrices).WithBlockHeight(height)

if height != lastBlockHeight {
rms, ok := app.cms.(*rootmulti.Store)
if ok {
cInfo, err := rms.GetCommitInfoFromDB(height)
if cInfo != nil && err == nil {
ctx = ctx.WithBlockTime(cInfo.Timestamp)
}
}
}

return ctx, nil
}

Expand Down
1 change: 1 addition & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3059,6 +3059,7 @@ a version/height.
| ----- | ---- | ----- | ----------- |
| `version` | [int64](#int64) | | |
| `store_infos` | [StoreInfo](#cosmos.base.store.v1beta1.StoreInfo) | repeated | |
| `timestamp` | [google.protobuf.Timestamp](#google.protobuf.Timestamp) | | |



Expand Down
6 changes: 4 additions & 2 deletions proto/cosmos/base/store/v1beta1/commit_info.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ syntax = "proto3";
package cosmos.base.store.v1beta1;

import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/cosmos/cosmos-sdk/store/types";

// CommitInfo defines commit information used by the multi-store when committing
// a version/height.
message CommitInfo {
int64 version = 1;
repeated StoreInfo store_infos = 2 [(gogoproto.nullable) = false];
int64 version = 1;
repeated StoreInfo store_infos = 2 [(gogoproto.nullable) = false];
google.protobuf.Timestamp timestamp = 3 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
}

// StoreInfo defines store-specific commit information. It contains a reference
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
99 changes: 79 additions & 20 deletions store/types/commit_info.pb.go

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

0 comments on commit 9310076

Please sign in to comment.