Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: iavl v1 #499

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
78bc47f
iavl v1
czarcas7ic Dec 21, 2023
f0de908
attempt further fixes
czarcas7ic Dec 21, 2023
f7dc910
further fixes (prune)
czarcas7ic Dec 21, 2023
8756631
change prune logic
czarcas7ic Dec 21, 2023
b748a97
dont add this extra logic
czarcas7ic Dec 22, 2023
3217b74
add logs
czarcas7ic Dec 22, 2023
20e0872
more logs
czarcas7ic Dec 22, 2023
a35f7b8
fix silly go mod issue
czarcas7ic Dec 22, 2023
9752a38
update iavl to not block on orphan prune
czarcas7ic Dec 22, 2023
3448975
change back go version
czarcas7ic Dec 22, 2023
cb8c8ed
fix go version issue
czarcas7ic Dec 22, 2023
d395698
fix
czarcas7ic Dec 22, 2023
cbc721b
iavl print
czarcas7ic Dec 22, 2023
88d22f8
iavl tag
czarcas7ic Dec 22, 2023
13c06a8
iavl update concurrency
czarcas7ic Dec 23, 2023
8470c78
Revert "iavl update concurrency"
czarcas7ic Dec 23, 2023
4e9a64f
iavl dev change
czarcas7ic Dec 23, 2023
9fa6d6b
iavl dev tune
czarcas7ic Dec 23, 2023
6b0a664
fix stacking
czarcas7ic Dec 23, 2023
60445fc
nil pointer
czarcas7ic Dec 23, 2023
b7662f3
update iavl version
czarcas7ic Dec 27, 2023
749888f
pull back commit in v1
czarcas7ic Dec 27, 2023
bc2a82b
include 3 commits
czarcas7ic Dec 27, 2023
4b8471e
go mod tidy
czarcas7ic Dec 27, 2023
0a503e9
upgrade go 1.19 to 1.20
czarcas7ic Dec 28, 2023
0bd1948
go mod tidy
czarcas7ic Dec 28, 2023
79ad698
CI
czarcas7ic Dec 28, 2023
b3794d9
labler
czarcas7ic Dec 28, 2023
57a0288
comment out test, we dont do reverse index
czarcas7ic Dec 28, 2023
fc12616
tests
czarcas7ic Dec 28, 2023
ecf4bef
query output
czarcas7ic Dec 28, 2023
eb2fc59
text output
czarcas7ic Dec 28, 2023
bd6c893
fix deterministic test
czarcas7ic Dec 28, 2023
9d1cbc8
remove tests
czarcas7ic Dec 28, 2023
46b778b
set labeler to version instead of main
czarcas7ic Dec 28, 2023
7665e0b
remove sonar
czarcas7ic Dec 28, 2023
5540749
remove expedited prop logic from e2e
czarcas7ic Dec 29, 2023
1afc709
fix operations test
czarcas7ic Dec 29, 2023
d6eb2d8
run CI on push to our fork
czarcas7ic Dec 29, 2023
743e42a
Merge branch 'osmo-v22/v0.47.5' into adam/osmo/v0.47.5-iavl-v1
czarcas7ic Jan 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ func SetIAVLDisableFastNode(disable bool) func(*BaseApp) {
return func(bapp *BaseApp) { bapp.cms.SetIAVLDisableFastNode(disable) }
}

// SetIAVLLazyLoading enables/disables lazy loading of the IAVL store.
func SetIAVLLazyLoading(lazyLoading bool) func(*BaseApp) {
return func(bapp *BaseApp) { bapp.cms.SetLazyLoading(lazyLoading) }
}

// SetInterBlockCache provides a BaseApp option function that sets the
// inter-block cache.
func SetInterBlockCache(cache sdk.MultiStorePersistentCache) func(*BaseApp) {
Expand Down
16 changes: 11 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ require (
github.com/cometbft/cometbft-db v0.7.0
github.com/confio/ics23/go v0.9.0
github.com/cosmos/btcutil v1.0.5
github.com/cosmos/cosmos-db v1.0.0
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk/db v1.0.0-beta.1.0.20220726092710-f848e4300a8a
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogogateway v1.2.0
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/iavl v0.20.0
github.com/cosmos/iavl v1.0.0
github.com/cosmos/ics23/go v0.10.0
github.com/cosmos/ledger-cosmos-go v0.12.1
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
Expand All @@ -48,14 +50,14 @@ require (
github.com/prometheus/common v0.42.0
github.com/rakyll/statik v0.1.7
github.com/rs/zerolog v1.30.0
github.com/spf13/cast v1.5.0
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.4
github.com/tendermint/go-amino v0.16.0
github.com/tidwall/btree v1.6.0
golang.org/x/crypto v0.11.0
golang.org/x/crypto v0.12.0
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb
google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529
google.golang.org/grpc v1.56.2
Expand All @@ -74,6 +76,8 @@ require (
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect
github.com/DataDog/zstd v1.4.5 // indirect
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/aws/aws-sdk-go v1.44.203 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
Expand All @@ -83,6 +87,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.10.0 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v0.0.0-20220817183557-09c6e030a677 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/coinbase/rosetta-sdk-go/types v1.0.0 // indirect
github.com/cosmos/rosetta-sdk-go v0.10.0 // indirect
Expand All @@ -96,6 +101,7 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/emicklei/dot v1.4.2 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getsentry/sentry-go v0.23.0 // indirect
Expand Down Expand Up @@ -132,6 +138,7 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.7.15 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
Expand All @@ -141,7 +148,6 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/gomega v1.20.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
Expand All @@ -165,7 +171,7 @@ require (
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.126.0 // indirect
Expand Down
140 changes: 129 additions & 11 deletions go.sum

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) {
mempool.SenderNonceMaxTxOpt(cast.ToInt(appOpts.Get(FlagMempoolMaxTxs))),
),
),
baseapp.SetIAVLLazyLoading(cast.ToBool(appOpts.Get(FlagIAVLLazyLoading))),
baseapp.SetChainID(chainID),
}
}
Expand Down
27 changes: 12 additions & 15 deletions store/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"cosmossdk.io/log"
dbm "github.com/cometbft/cometbft-db"
"github.com/cosmos/iavl"
"github.com/stretchr/testify/require"
Expand All @@ -12,15 +13,15 @@ import (
"github.com/cosmos/cosmos-sdk/store/cachekv"
iavlstore "github.com/cosmos/cosmos-sdk/store/iavl"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/store/wrapper"
)

func TestGetOrSetStoreCache(t *testing.T) {
db := dbm.NewMemDB()
db := wrapper.NewCosmosDB(dbm.NewMemDB())
mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)

sKey := types.NewKVStoreKey("test")
tree, err := iavl.NewMutableTree(db, 100, false)
require.NoError(t, err)
tree := iavl.NewMutableTree(db, 100, false, log.NewNopLogger())
store := iavlstore.UnsafeNewStore(tree)
store2 := mngr.GetStoreCache(sKey, store)

Expand All @@ -29,12 +30,11 @@ func TestGetOrSetStoreCache(t *testing.T) {
}

func TestUnwrap(t *testing.T) {
db := dbm.NewMemDB()
db := wrapper.NewCosmosDB(dbm.NewMemDB())
mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)

sKey := types.NewKVStoreKey("test")
tree, err := iavl.NewMutableTree(db, 100, false)
require.NoError(t, err)
tree := iavl.NewMutableTree(db, 100, false, log.NewNopLogger())
store := iavlstore.UnsafeNewStore(tree)
_ = mngr.GetStoreCache(sKey, store)

Expand All @@ -43,12 +43,11 @@ func TestUnwrap(t *testing.T) {
}

func TestStoreCache(t *testing.T) {
db := dbm.NewMemDB()
db := wrapper.NewCosmosDB(dbm.NewMemDB())
mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)

sKey := types.NewKVStoreKey("test")
tree, err := iavl.NewMutableTree(db, 100, false)
require.NoError(t, err)
tree := iavl.NewMutableTree(db, 100, false, log.NewNopLogger())
store := iavlstore.UnsafeNewStore(tree)
kvStore := mngr.GetStoreCache(sKey, store)

Expand All @@ -70,12 +69,11 @@ func TestStoreCache(t *testing.T) {
}

func TestReset(t *testing.T) {
db := dbm.NewMemDB()
db := wrapper.NewCosmosDB(dbm.NewMemDB())
mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)

sKey := types.NewKVStoreKey("test")
tree, err := iavl.NewMutableTree(db, 100, false)
require.NoError(t, err)
tree := iavl.NewMutableTree(db, 100, false, log.NewNopLogger())
store := iavlstore.UnsafeNewStore(tree)
store2 := mngr.GetStoreCache(sKey, store)

Expand All @@ -91,12 +89,11 @@ func TestReset(t *testing.T) {
}

func TestCacheWrap(t *testing.T) {
db := dbm.NewMemDB()
db := wrapper.NewCosmosDB(dbm.NewMemDB())
mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)

sKey := types.NewKVStoreKey("test")
tree, err := iavl.NewMutableTree(db, 100, false)
require.NoError(t, err)
tree := iavl.NewMutableTree(db, 100, false, log.NewNopLogger())
store := iavlstore.UnsafeNewStore(tree)

cacheWrapper := mngr.GetStoreCache(sKey, store).CacheWrap()
Expand Down
59 changes: 21 additions & 38 deletions store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"io"
"time"

clog "cosmossdk.io/log"
dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
Expand All @@ -17,6 +18,7 @@
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
"github.com/cosmos/cosmos-sdk/store/tracekv"
"github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/store/wrapper"
"github.com/cosmos/cosmos-sdk/telemetry"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/kv"
Expand All @@ -43,19 +45,16 @@
// LoadStore returns an IAVL Store as a CommitKVStore. Internally, it will load the
// store's version (id) from the provided DB. An error is returned if the version
// fails to load, or if called with a positive version on an empty tree.
func LoadStore(db dbm.DB, logger log.Logger, key types.StoreKey, id types.CommitID, lazyLoading bool, cacheSize int, disableFastNode bool) (types.CommitKVStore, error) {
return LoadStoreWithInitialVersion(db, logger, key, id, lazyLoading, 0, cacheSize, disableFastNode)
func LoadStore(db dbm.DB, logger log.Logger, key types.StoreKey, id types.CommitID, cacheSize int, disableFastNode bool) (types.CommitKVStore, error) {
return LoadStoreWithInitialVersion(db, logger, key, id, 0, cacheSize, disableFastNode)
}

// LoadStoreWithInitialVersion returns an IAVL Store as a CommitKVStore setting its initialVersion
// to the one given. Internally, it will load the store's version (id) from the
// provided DB. An error is returned if the version fails to load, or if called with a positive
// version on an empty tree.
func LoadStoreWithInitialVersion(db dbm.DB, logger log.Logger, key types.StoreKey, id types.CommitID, lazyLoading bool, initialVersion uint64, cacheSize int, disableFastNode bool) (types.CommitKVStore, error) {
tree, err := iavl.NewMutableTreeWithOpts(db, cacheSize, &iavl.Options{InitialVersion: initialVersion}, disableFastNode)
if err != nil {
return nil, err
}
func LoadStoreWithInitialVersion(db dbm.DB, logger log.Logger, key types.StoreKey, id types.CommitID, initialVersion uint64, cacheSize int, disableFastNode bool) (types.CommitKVStore, error) {
tree := iavl.NewMutableTree(wrapper.NewCosmosDB(db), cacheSize, disableFastNode, clog.NewNopLogger(), iavl.InitialVersionOption(initialVersion))

isUpgradeable, err := tree.IsUpgradeable()
if err != nil {
Expand All @@ -68,16 +67,10 @@
"store_key", key.String(),
"version", initialVersion,
"commit", fmt.Sprintf("%X", id),
"is_lazy", lazyLoading,
)
}

if lazyLoading {
_, err = tree.LazyLoadVersion(id.Version)
} else {
_, err = tree.LoadVersion(id.Version)
}

_, err = tree.LoadVersion(id.Version)
if err != nil {
return nil, err
}
Expand All @@ -87,8 +80,7 @@
}

return &Store{
tree: tree,
logger: logger,
tree: &mutableTree{tree},

Check failure on line 83 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use &(mutableTree literal) (value of type *mutableTree) as Tree value in struct literal: *mutableTree does not implement Tree (missing method DeleteVersionsTo) (typecheck)
}, nil
}

Expand All @@ -100,7 +92,7 @@
// passed into iavl.MutableTree
func UnsafeNewStore(tree *iavl.MutableTree) *Store {
return &Store{
tree: tree,
tree: &mutableTree{tree},

Check failure on line 95 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use &(mutableTree literal) (value of type *mutableTree) as Tree value in struct literal: *mutableTree does not implement Tree (missing method DeleteVersionsTo) (typecheck)
}
}

Expand All @@ -120,7 +112,7 @@
}

return &Store{
tree: &immutableTree{iTree},

Check failure on line 115 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use &(immutableTree literal) (value of type *immutableTree) as Tree value in struct literal: *immutableTree does not implement Tree (missing method Get) (typecheck)
}, nil
}

Expand All @@ -142,10 +134,7 @@

// LastCommitID implements Committer.
func (st *Store) LastCommitID() types.CommitID {
hash, err := st.tree.Hash()
if err != nil {
panic(err)
}
hash := st.tree.Hash()

return types.CommitID{
Version: st.tree.Version(),
Expand All @@ -172,7 +161,7 @@

// GetAllVersions returns all versions in the iavl tree
func (st *Store) GetAllVersions() []int {
return st.tree.AvailableVersions()
return st.tree.(*mutableTree).AvailableVersions()

Check failure on line 164 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

impossible type assertion: st.tree.(*mutableTree)
}

// Implements Store.
Expand Down Expand Up @@ -226,22 +215,17 @@
st.tree.Remove(key)
}

// DeleteVersions deletes a series of versions from the MutableTree. An error
// DeleteVersionsTo deletes versions upto the given version from the MutableTree. An error
// is returned if any single version is invalid or the delete fails. All writes
// happen in a single batch with a single commit.
func (st *Store) DeleteVersions(versions ...int64) error {
return st.tree.DeleteVersions(versions...)
func (st *Store) DeleteVersionsTo(version int64) error {
return st.tree.DeleteVersionsTo(version)
}

// LoadVersionForOverwriting attempts to load a tree at a previously committed
// version, or the latest version below it. Any versions greater than targetVersion will be deleted.
func (st *Store) LoadVersionForOverwriting(targetVersion int64) (int64, error) {
return st.tree.LoadVersionForOverwriting(targetVersion)
}

// LazyLoadVersionForOverwriting is the lazy version of LoadVersionForOverwriting.
func (st *Store) LazyLoadVersionForOverwriting(targetVersion int64) (int64, error) {
return st.tree.LazyLoadVersionForOverwriting(targetVersion)
return targetVersion, st.tree.LoadVersionForOverwriting(targetVersion)
}

// Implements types.KVStore.
Expand Down Expand Up @@ -274,20 +258,20 @@
if err != nil {
return nil, fmt.Errorf("iavl export failed for version %v: %w", version, err)
}
tree, ok := istore.tree.(*immutableTree)

Check failure on line 261 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

impossible type assertion: istore.tree.(*immutableTree)
if !ok || tree == nil {
return nil, fmt.Errorf("iavl export failed: unable to fetch tree for version %v", version)
}
return tree.Export()

Check failure on line 265 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

tree.Export undefined (type *immutableTree has no field or method Export) (typecheck)
}

// Import imports an IAVL tree at the given version, returning an iavl.Importer for importing.
func (st *Store) Import(version int64) (*iavl.Importer, error) {
tree, ok := st.tree.(*iavl.MutableTree)
tree, ok := st.tree.(*mutableTree)

Check failure on line 270 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

impossible type assertion: st.tree.(*mutableTree)
if !ok {
return nil, errors.New("iavl import failed: unable to find mutable tree")
}
return tree.Import(version)

Check failure on line 274 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

tree.Import undefined (type *mutableTree has no field or method Import) (typecheck)
}

// Handle gatest the latest height, if height is 0
Expand Down Expand Up @@ -390,25 +374,24 @@
// appropriate merkle.Proof. Since this must be called after querying for the value, this function should never error
// Thus, it will panic on error rather than returning it
func getProofFromTree(tree *iavl.MutableTree, key []byte, exists bool) *tmcrypto.ProofOps {
var (
commitmentProof *ics23.CommitmentProof
err error
)
var commitmentProof *ics23.CommitmentProof

if exists {
// value was found
commitmentProof, err = tree.GetMembershipProof(key)
tempCommitmentProof, err := tree.GetMembershipProof(key)
if err != nil {
// sanity check: If value was found, membership proof must be creatable
panic(fmt.Sprintf("unexpected value for empty proof: %s", err.Error()))
}
commitmentProof = wrapper.ConvertCommitmentProof(tempCommitmentProof)
} else {
// value wasn't found
commitmentProof, err = tree.GetNonMembershipProof(key)
tempCommitmentProof, err := tree.GetNonMembershipProof(key)
if err != nil {
// sanity check: If value wasn't found, nonmembership proof must be creatable
panic(fmt.Sprintf("unexpected error for nonexistence proof: %s", err.Error()))
}
commitmentProof = wrapper.ConvertCommitmentProof(tempCommitmentProof)
}

op := types.NewIavlCommitmentOp(key, commitmentProof)
Expand Down
Loading
Loading