diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 2ef933c205..4d51fea669 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -397,7 +397,7 @@ func (app *BaseApp) LastCommitID() storetypes.CommitID { // LastBlockHeight returns the last committed block height. func (app *BaseApp) LastBlockHeight() int64 { - return app.cms.LastCommitID().Version + return app.cms.LatestVersion() } // ChainID returns the chainID of the app. diff --git a/store/CHANGELOG.md b/store/CHANGELOG.md index 6cf4115057..12e6c613d2 100644 --- a/store/CHANGELOG.md +++ b/store/CHANGELOG.md @@ -25,6 +25,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Improvements + +* (store) [#22305](https://github.com/cosmos/cosmos-sdk/pull/22305) Add `LatestVersion` to the `Committer` interface to get the latest version of the store. + ### Bug Fixes * (store) [#20425](https://github.com/cosmos/cosmos-sdk/pull/20425) Fix nil pointer panic when query historical state where a new store don't exist. diff --git a/store/iavl/store.go b/store/iavl/store.go index ae6b0b56ff..42f8fed7c9 100644 --- a/store/iavl/store.go +++ b/store/iavl/store.go @@ -146,6 +146,11 @@ func (st *Store) LastCommitID() types.CommitID { } } +// LatestVersion implements Committer. +func (st *Store) LatestVersion() int64 { + return st.tree.Version() +} + // PausePruning implements CommitKVStore interface. func (st *Store) PausePruning(pause bool) { if pause { diff --git a/store/mem/store.go b/store/mem/store.go index e1f2998ce2..feff6ee271 100644 --- a/store/mem/store.go +++ b/store/mem/store.go @@ -59,4 +59,6 @@ func (s *Store) GetPruning() pruningtypes.PruningOptions { func (s Store) LastCommitID() (id types.CommitID) { return } +func (s Store) LatestVersion() (version int64) { return } + func (s Store) WorkingHash() (hash []byte) { return } diff --git a/store/rootmulti/dbadapter.go b/store/rootmulti/dbadapter.go index c0954e8532..5228859c27 100644 --- a/store/rootmulti/dbadapter.go +++ b/store/rootmulti/dbadapter.go @@ -36,6 +36,10 @@ func (cdsa commitDBStoreAdapter) LastCommitID() types.CommitID { } } +func (cdsa commitDBStoreAdapter) LatestVersion() int64 { + return -1 +} + func (cdsa commitDBStoreAdapter) WorkingHash() []byte { return commithash } diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index d23bcc57b0..486fffebeb 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -434,7 +434,11 @@ func (rs *Store) PopStateCache() []*types.StoreKVPair { // LatestVersion returns the latest version in the store func (rs *Store) LatestVersion() int64 { - return rs.LastCommitID().Version + if rs.lastCommitInfo == nil { + return GetLatestVersion(rs.db) + } + + return rs.lastCommitInfo.Version } // LastCommitID implements Committer/CommitStore. diff --git a/store/transient/store.go b/store/transient/store.go index b3d1fd4bb0..c309c03044 100644 --- a/store/transient/store.go +++ b/store/transient/store.go @@ -42,6 +42,11 @@ func (ts *Store) LastCommitID() types.CommitID { return types.CommitID{} } +// LatestVersion implements Committer +func (ts *Store) LatestVersion() int64 { + return 0 +} + func (ts *Store) WorkingHash() []byte { return []byte{} } diff --git a/store/types/store.go b/store/types/store.go index de77e2c548..8aab76a170 100644 --- a/store/types/store.go +++ b/store/types/store.go @@ -21,6 +21,7 @@ type Store interface { type Committer interface { Commit() CommitID LastCommitID() CommitID + LatestVersion() int64 // WorkingHash returns the hash of the KVStore's state before commit. WorkingHash() []byte