diff --git a/CHANGELOG.md b/CHANGELOG.md index b5adc9e76..2a4e297e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### Improvements + +- [#961](https://github.com/cosmos/iavl/pull/961) Add new `GetLatestVersion` API to get the latest version. + ## v1.2.0 May 13, 2024 ### Improvements diff --git a/mutable_tree.go b/mutable_tree.go index e71253852..350e39b93 100644 --- a/mutable_tree.go +++ b/mutable_tree.go @@ -73,6 +73,11 @@ func (tree *MutableTree) IsEmpty() bool { return tree.ImmutableTree.Size() == 0 } +// GetLatestVersion returns the latest version of the tree. +func (tree *MutableTree) GetLatestVersion() (int64, error) { + return tree.ndb.getLatestVersion() +} + // VersionExists returns whether or not a version exists. func (tree *MutableTree) VersionExists(version int64) bool { legacyLatestVersion, err := tree.ndb.getLegacyLatestVersion() diff --git a/mutable_tree_test.go b/mutable_tree_test.go index 6e58843ff..1f28c572d 100644 --- a/mutable_tree_test.go +++ b/mutable_tree_test.go @@ -347,11 +347,15 @@ func prepareTree(t *testing.T) *MutableTree { return newTree } -func TestMutableTree_VersionExists(t *testing.T) { +func TestMutableTree_Version(t *testing.T) { tree := prepareTree(t) require.True(t, tree.VersionExists(1)) require.True(t, tree.VersionExists(2)) require.False(t, tree.VersionExists(3)) + + v, err := tree.GetLatestVersion() + require.NoError(t, err) + require.Equal(t, int64(2), v) } func checkGetVersioned(t *testing.T, tree *MutableTree, version int64, key, value []byte) {