From a514883e2f4101b2c8009dfdc421abbb5f324dcd Mon Sep 17 00:00:00 2001 From: cool-developer <51834436+cool-develope@users.noreply.github.com> Date: Tue, 9 Jul 2024 08:07:57 -0400 Subject: [PATCH] chore: new `GetLatestVersion` api (#961) --- CHANGELOG.md | 4 ++++ mutable_tree.go | 5 +++++ mutable_tree_test.go | 6 +++++- 3 files changed, 14 insertions(+), 1 deletion(-) 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) {