From 8a57482d720e3da8b461fd8bc710108927e85205 Mon Sep 17 00:00:00 2001 From: Cool Developer Date: Thu, 17 Oct 2024 10:05:07 -0400 Subject: [PATCH 1/2] add new api LatestVersion --- baseapp/baseapp.go | 2 +- store/iavl/store.go | 5 +++++ store/mem/store.go | 2 ++ store/rootmulti/dbadapter.go | 4 ++++ store/rootmulti/store.go | 6 +++++- store/transient/store.go | 5 +++++ store/types/store.go | 1 + 7 files changed, 23 insertions(+), 2 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 2ef933c205c3..4d51fea669b7 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/iavl/store.go b/store/iavl/store.go index ae6b0b56ff70..42f8fed7c910 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 e1f2998ce201..feff6ee27192 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 c0954e8532ab..5228859c2752 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 d23bcc57b090..486fffebeba9 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 b3d1fd4bb0fd..c309c03044ec 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 de77e2c548aa..8aab76a170b0 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 From 5c729a6008af678adb0d2d3de8344624e8456d98 Mon Sep 17 00:00:00 2001 From: Cool Developer Date: Thu, 17 Oct 2024 10:08:04 -0400 Subject: [PATCH 2/2] changelog --- store/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/store/CHANGELOG.md b/store/CHANGELOG.md index 6cf411505792..12e6c613d2ca 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.