Skip to content

Commit

Permalink
Merge branch 'main' into marko/SS_removal
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Dec 2, 2024
2 parents 10192af + c207a45 commit c3359be
Show file tree
Hide file tree
Showing 23 changed files with 424 additions and 49 deletions.
2 changes: 1 addition & 1 deletion runtime/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ require (
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-metrics v0.5.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions runtime/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU=
github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg=
github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down
4 changes: 3 additions & 1 deletion scripts/build/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ LEDGER_ENABLED ?= true
BINDIR ?= $(GOPATH)/bin
BUILDDIR ?= $(CURDIR)/build
SIMAPP = simapp
APPNAME = simd
MOCKS_DIR = $(CURDIR)/tests/mocks
HTTPS_GIT := https://github.com/cosmos/cosmos-sdk.git
DOCKER := $(shell which docker)
Expand Down Expand Up @@ -53,6 +54,7 @@ endif

ifeq (v2,$(findstring v2,$(COSMOS_BUILD_OPTIONS)))
SIMAPP = simapp/v2
APPNAME = simdv2
endif

# DB backend selection
Expand Down Expand Up @@ -86,7 +88,7 @@ build_tags_comma_sep := $(subst $(whitespace),$(comma),$(build_tags))
# process linker flags

ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=sim \
-X github.com/cosmos/cosmos-sdk/version.AppName=simd \
-X github.com/cosmos/cosmos-sdk/version.AppName=$(APPNAME) \
-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags_comma_sep)"
Expand Down
2 changes: 1 addition & 1 deletion server/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ require (
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions server/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU=
github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg=
github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand Down
4 changes: 2 additions & 2 deletions server/v2/testdata/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ minimum-gas-prices = '0stake'
app-db-backend = 'goleveldb'

[store.options]
# State storage database type. Currently we support: "sqlite", "pebble" and "rocksdb"
ss-type = 'sqlite'
# State storage database type. Currently we support: "pebble" and "rocksdb"
ss-type = 'pebble'
# State commitment database type. Currently we support: "iavl" and "iavl-v2"
sc-type = 'iavl'

Expand Down
2 changes: 2 additions & 0 deletions simapp/v2/simdv2/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/rpc"
sdktelemetry "github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
Expand Down Expand Up @@ -65,6 +66,7 @@ func InitRootCmd[T transaction.Tx](
txCommand(),
keys.Commands(),
offchain.OffChain(),
version.NewVersionCommand(),
)

// build CLI skeleton for initial config parsing or a client application invocation
Expand Down
7 changes: 7 additions & 0 deletions store/v2/commitment/iavl/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func (t *IavlTree) WorkingHash() []byte {

// LoadVersion loads the state at the given version.
func (t *IavlTree) LoadVersion(version uint64) error {
_, err := t.tree.LoadVersion(int64(version))
return err
}

// LoadVersionForOverwriting loads the state at the given version.
// Any versions greater than targetVersion will be deleted.
func (t *IavlTree) LoadVersionForOverwriting(version uint64) error {
return t.tree.LoadVersionForOverwriting(int64(version))
}

Expand Down
7 changes: 6 additions & 1 deletion store/v2/commitment/iavl/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import (

func TestCommitterSuite(t *testing.T) {
s := &commitment.CommitStoreTestSuite{
NewStore: func(db corestore.KVStoreWithBatch, storeKeys, oldStoreKeys []string, logger corelog.Logger) (*commitment.CommitStore, error) {
NewStore: func(
db corestore.KVStoreWithBatch,
_ string,
storeKeys, oldStoreKeys []string,
logger corelog.Logger,
) (*commitment.CommitStore, error) {
multiTrees := make(map[string]commitment.Tree)
cfg := DefaultConfig()
mountTreeFn := func(storeKey string) (commitment.Tree, error) {
Expand Down
155 changes: 155 additions & 0 deletions store/v2/commitment/iavlv2/tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package iavlv2

import (
"errors"
"fmt"

"github.com/cosmos/iavl/v2"
ics23 "github.com/cosmos/ics23/go"

corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/v2"
"cosmossdk.io/store/v2/commitment"
)

var (
_ commitment.Tree = (*Tree)(nil)
_ commitment.Reader = (*Tree)(nil)
_ store.PausablePruner = (*Tree)(nil)
)

type Tree struct {
tree *iavl.Tree
}

func NewTree(treeOptions iavl.TreeOptions, dbOptions iavl.SqliteDbOptions, pool *iavl.NodePool) (*Tree, error) {
sql, err := iavl.NewSqliteDb(pool, dbOptions)
if err != nil {
return nil, err
}
tree := iavl.NewTree(sql, pool, treeOptions)
return &Tree{tree: tree}, nil
}

func (t *Tree) Set(key, value []byte) error {
_, err := t.tree.Set(key, value)
return err
}

func (t *Tree) Remove(key []byte) error {
_, _, err := t.tree.Remove(key)
return err
}

func (t *Tree) GetLatestVersion() (uint64, error) {
return uint64(t.tree.Version()), nil
}

func (t *Tree) Hash() []byte {
return t.tree.Hash()
}

func (t *Tree) Version() uint64 {
return uint64(t.tree.Version())
}

func (t *Tree) LoadVersion(version uint64) error {
if err := isHighBitSet(version); err != nil {
return err
}

if version == 0 {
return nil
}
return t.tree.LoadVersion(int64(version))
}

func (t *Tree) LoadVersionForOverwriting(version uint64) error {
return t.LoadVersion(version) // TODO: implement overwriting
}

func (t *Tree) Commit() ([]byte, uint64, error) {
h, v, err := t.tree.SaveVersion()
return h, uint64(v), err
}

func (t *Tree) SetInitialVersion(version uint64) error {
if err := isHighBitSet(version); err != nil {
return err
}
t.tree.SetShouldCheckpoint()
return t.tree.SetInitialVersion(int64(version))
}

func (t *Tree) GetProof(version uint64, key []byte) (*ics23.CommitmentProof, error) {
if err := isHighBitSet(version); err != nil {
return nil, err
}
return t.tree.GetProof(int64(version), key)
}

func (t *Tree) Get(version uint64, key []byte) ([]byte, error) {
if err := isHighBitSet(version); err != nil {
return nil, err
}
if int64(version) != t.tree.Version() {
cloned, err := t.tree.ReadonlyClone()
if err != nil {
return nil, err
}
if err = cloned.LoadVersion(int64(version)); err != nil {
return nil, err
}
return cloned.Get(key)
} else {
return t.tree.Get(key)
}
}

func (t *Tree) Iterator(version uint64, start, end []byte, ascending bool) (corestore.Iterator, error) {
if err := isHighBitSet(version); err != nil {
return nil, err
}
if int64(version) != t.tree.Version() {
return nil, fmt.Errorf("loading past version not yet supported")
}
if ascending {
return t.tree.Iterator(start, end, false)
} else {
return t.tree.ReverseIterator(start, end)
}
}

func (t *Tree) Export(version uint64) (commitment.Exporter, error) {
return nil, errors.New("snapshot import/export not yet supported")
}

func (t *Tree) Import(version uint64) (commitment.Importer, error) {
return nil, errors.New("snapshot import/export not yet supported")
}

func (t *Tree) Close() error {
return t.tree.Close()
}

func (t *Tree) Prune(version uint64) error {
if err := isHighBitSet(version); err != nil {
return err
}

return t.tree.DeleteVersionsTo(int64(version))
}

// PausePruning is unnecessary in IAVL v2 due to the advanced pruning mechanism
func (t *Tree) PausePruning(bool) {}

func (t *Tree) WorkingHash() []byte {
return t.tree.Hash()
}

func isHighBitSet(version uint64) error {
if version&(1<<63) != 0 {
return fmt.Errorf("%d too large; uint64 with the highest bit set are not supported", version)
}
return nil
}
46 changes: 46 additions & 0 deletions store/v2/commitment/iavlv2/tree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package iavlv2

import (
"fmt"
"testing"

"github.com/cosmos/iavl/v2"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

corelog "cosmossdk.io/core/log"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/v2/commitment"
)

func TestCommitterSuite(t *testing.T) {
nodePool := iavl.NewNodePool()
s := &commitment.CommitStoreTestSuite{
TreeType: "iavlv2",
NewStore: func(
db corestore.KVStoreWithBatch,
dbDir string,
storeKeys, oldStoreKeys []string,
logger corelog.Logger,
) (*commitment.CommitStore, error) {
multiTrees := make(map[string]commitment.Tree)
mountTreeFn := func(storeKey string) (commitment.Tree, error) {
path := fmt.Sprintf("%s/%s", dbDir, storeKey)
tree, err := NewTree(iavl.DefaultTreeOptions(), iavl.SqliteDbOptions{Path: path}, nodePool)
require.NoError(t, err)
return tree, nil
}
for _, storeKey := range storeKeys {
multiTrees[storeKey], _ = mountTreeFn(storeKey)
}
oldTrees := make(map[string]commitment.Tree)
for _, storeKey := range oldStoreKeys {
oldTrees[storeKey], _ = mountTreeFn(storeKey)
}

return commitment.NewCommitStore(multiTrees, oldTrees, db, logger)
},
}

suite.Run(t, s)
}
4 changes: 4 additions & 0 deletions store/v2/commitment/mem/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (t *Tree) LoadVersion(version uint64) error {
return nil
}

func (t *Tree) LoadVersionForOverwriting(version uint64) error {
return nil
}

func (t *Tree) Commit() ([]byte, uint64, error) {
return nil, 0, nil
}
Expand Down
25 changes: 20 additions & 5 deletions store/v2/commitment/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@ func (c *CommitStore) LoadVersion(targetVersion uint64) error {
for storeKey := range c.multiTrees {
storeKeys = append(storeKeys, storeKey)
}
return c.loadVersion(targetVersion, storeKeys)
return c.loadVersion(targetVersion, storeKeys, false)
}

func (c *CommitStore) LoadVersionForOverwriting(targetVersion uint64) error {
storeKeys := make([]string, 0, len(c.multiTrees))
for storeKey := range c.multiTrees {
storeKeys = append(storeKeys, storeKey)
}

return c.loadVersion(targetVersion, storeKeys, true)
}

// LoadVersionAndUpgrade implements store.UpgradeableStore.
Expand Down Expand Up @@ -133,10 +142,10 @@ func (c *CommitStore) LoadVersionAndUpgrade(targetVersion uint64, upgrades *core
return err
}

return c.loadVersion(targetVersion, newStoreKeys)
return c.loadVersion(targetVersion, newStoreKeys, true)
}

func (c *CommitStore) loadVersion(targetVersion uint64, storeKeys []string) error {
func (c *CommitStore) loadVersion(targetVersion uint64, storeKeys []string, overrideAfter bool) error {
// Rollback the metadata to the target version.
latestVersion, err := c.GetLatestVersion()
if err != nil {
Expand All @@ -154,8 +163,14 @@ func (c *CommitStore) loadVersion(targetVersion uint64, storeKeys []string) erro
}

for _, storeKey := range storeKeys {
if err := c.multiTrees[storeKey].LoadVersion(targetVersion); err != nil {
return err
if overrideAfter {
if err := c.multiTrees[storeKey].LoadVersionForOverwriting(targetVersion); err != nil {
return err
}
} else {
if err := c.multiTrees[storeKey].LoadVersion(targetVersion); err != nil {
return err
}
}
}

Expand Down
Loading

0 comments on commit c3359be

Please sign in to comment.