From 931007612436a93dcee45314c12a637876ec29f5 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 8 May 2023 19:13:21 -0400 Subject: [PATCH] feat: auto-set block timestamp for historical queries (#15448) (backport #440) (#441) * feat: auto-set block timestamp for historical queries (#15448) (backport #440) * updates * updates * updates --------- Co-authored-by: Roman --- baseapp/abci.go | 11 +++ docs/core/proto-docs.md | 1 + .../base/store/v1beta1/commit_info.proto | 6 +- store/rootmulti/store.go | 8 +- store/rootmulti/store_test.go | 10 +- store/types/commit_info.pb.go | 99 +++++++++++++++---- 6 files changed, 104 insertions(+), 31 deletions(-) diff --git a/baseapp/abci.go b/baseapp/abci.go index f6d0d7293704..9d5003b48e9b 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -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" @@ -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 } diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index 6836088f90ee..34aa4253e0ec 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -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) | | | diff --git a/proto/cosmos/base/store/v1beta1/commit_info.proto b/proto/cosmos/base/store/v1beta1/commit_info.proto index 98a33d30e754..c6c6eab6e099 100644 --- a/proto/cosmos/base/store/v1beta1/commit_info.proto +++ b/proto/cosmos/base/store/v1beta1/commit_info.proto @@ -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 diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 233e24033825..86a80f2346c2 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -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 } @@ -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) } @@ -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)) @@ -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) } diff --git a/store/rootmulti/store_test.go b/store/rootmulti/store_test.go index 337906c8581a..bead03663bfc 100644 --- a/store/rootmulti/store_test.go +++ b/store/rootmulti/store_test.go @@ -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)) @@ -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) @@ -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) } @@ -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") @@ -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") diff --git a/store/types/commit_info.pb.go b/store/types/commit_info.pb.go index 988804d2f307..86e10e64db0a 100644 --- a/store/types/commit_info.pb.go +++ b/store/types/commit_info.pb.go @@ -7,15 +7,19 @@ import ( fmt "fmt" _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" + github_com_gogo_protobuf_types "github.com/gogo/protobuf/types" + _ "google.golang.org/protobuf/types/known/timestamppb" io "io" math "math" math_bits "math/bits" + time "time" ) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal var _ = fmt.Errorf var _ = math.Inf +var _ = time.Kitchen // This is a compile-time assertion to ensure that this generated file // is compatible with the proto package it is being compiled against. @@ -28,6 +32,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type CommitInfo struct { Version int64 `protobuf:"varint,1,opt,name=version,proto3" json:"version,omitempty"` StoreInfos []StoreInfo `protobuf:"bytes,2,rep,name=store_infos,json=storeInfos,proto3" json:"store_infos"` + Timestamp time.Time `protobuf:"bytes,3,opt,name=timestamp,proto3,stdtime" json:"timestamp"` } func (m *CommitInfo) Reset() { *m = CommitInfo{} } @@ -77,6 +82,13 @@ func (m *CommitInfo) GetStoreInfos() []StoreInfo { return nil } +func (m *CommitInfo) GetTimestamp() time.Time { + if m != nil { + return m.Timestamp + } + return time.Time{} +} + // StoreInfo defines store-specific commit information. It contains a reference // between a store name and the commit ID. type StoreInfo struct { @@ -195,26 +207,30 @@ func init() { } var fileDescriptor_83f4097f6265b52f = []byte{ - // 301 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xd2, 0x4e, 0xce, 0x2f, 0xce, - 0xcd, 0x2f, 0xd6, 0x4f, 0x4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, 0xc9, 0xe1, 0xf1, 0x9b, 0x10, - 0x17, 0x4b, 0x46, 0x62, 0x71, 0x06, 0xd8, 0x22, 0x9e, 0x20, 0x30, 0xdb, 0x8a, 0x65, 0xc6, 0x02, - 0x79, 0x06, 0x27, 0xa7, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, - 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0xd2, 0x48, - 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x87, 0x46, 0x10, 0x84, 0xd2, 0x2d, - 0x4e, 0xc9, 0x86, 0x46, 0x53, 0x49, 0x65, 0x41, 0x6a, 0x71, 0x12, 0x1b, 0x38, 0xa0, 0x8d, 0x01, - 0x01, 0x00, 0x00, 0xff, 0xff, 0x00, 0xc0, 0xc7, 0x12, 0xc8, 0x01, 0x00, 0x00, + // 354 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0xb1, 0x4e, 0xfb, 0x30, + 0x10, 0xc6, 0xe3, 0x36, 0xfa, 0xff, 0x1b, 0x97, 0xc9, 0x62, 0x08, 0x1d, 0x92, 0xaa, 0x30, 0x44, + 0x42, 0xd8, 0x6a, 0xd9, 0x18, 0x18, 0x02, 0x42, 0xaa, 0xd8, 0x02, 0x13, 0x0b, 0x4a, 0x5a, 0x37, + 0x8d, 0xc0, 0xb9, 0xaa, 0x76, 0x2b, 0xf1, 0x16, 0x1d, 0x19, 0x79, 0x0b, 0x5e, 0xa1, 0x63, 0x47, + 0x26, 0x40, 0xcd, 0x8b, 0xa0, 0x38, 0x71, 0x99, 0xe8, 0x94, 0xbb, 0xf8, 0xbb, 0xfb, 0x7e, 0xfa, + 0x74, 0xf8, 0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, 0xdb, 0xaa, 0x66, 0x87, 0xe3, 0xde, 0x25, + 0x6e, 0x99, 0xb7, 0x3d, 0xf9, 0x10, 0x6c, 0x4f, 0x63, 0x39, 0xd5, 0x46, 0x07, 0x91, 0xae, 0x2f, + 0xec, 0xd7, 0x37, 0xdf, 0x0a, 0xc3, 0xf5, 0xd6, 0x43, 0x9b, 0xad, 0x87, 0xbe, 0xb7, 0x1e, 0x5a, + 0x15, 0x9e, 0xb5, 0x29, 0x3c, 0xeb, 0xa3, 0xf0, 0xac, 0x87, 0x20, 0xcd, 0xd4, 0x74, 0x91, 0xd0, + 0x11, 0x08, 0x56, 0x9f, 0x41, 0xf5, 0x39, 0x93, 0xe3, 0xa7, 0xfa, 0x18, 0xd4, 0xcb, 0x8c, 0xcb, + 0xe4, 0x9f, 0x4e, 0xe5, 0xfc, 0x27, 0x00, 0x00, 0xff, 0xff, 0x46, 0x0e, 0x24, 0xd4, 0x2e, 0x02, + 0x00, 0x00, } func (m *CommitInfo) Marshal() (dAtA []byte, err error) { @@ -237,6 +253,14 @@ func (m *CommitInfo) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + n1, err1 := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.Timestamp, dAtA[i-github_com_gogo_protobuf_types.SizeOfStdTime(m.Timestamp):]) + if err1 != nil { + return 0, err1 + } + i -= n1 + i = encodeVarintCommitInfo(dAtA, i, uint64(n1)) + i-- + dAtA[i] = 0x1a if len(m.StoreInfos) > 0 { for iNdEx := len(m.StoreInfos) - 1; iNdEx >= 0; iNdEx-- { { @@ -360,6 +384,8 @@ func (m *CommitInfo) Size() (n int) { n += 1 + l + sovCommitInfo(uint64(l)) } } + l = github_com_gogo_protobuf_types.SizeOfStdTime(m.Timestamp) + n += 1 + l + sovCommitInfo(uint64(l)) return n } @@ -482,6 +508,39 @@ func (m *CommitInfo) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Timestamp", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowCommitInfo + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthCommitInfo + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthCommitInfo + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := github_com_gogo_protobuf_types.StdTimeUnmarshal(&m.Timestamp, dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipCommitInfo(dAtA[iNdEx:])