Skip to content

Commit

Permalink
Problem: memiavl is not integrated with state machine
Browse files Browse the repository at this point in the history
Solution:
- implement custom rootmulti store and kvstore wrappers

add flag --memiavl to enable memiavl

kvstore use change set api
  • Loading branch information
yihuang committed Apr 4, 2023
1 parent bc8b57f commit ab0b541
Show file tree
Hide file tree
Showing 12 changed files with 1,110 additions and 13 deletions.
8 changes: 7 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import (

// this line is used by starport scaffolding # stargate/app/moduleImport

"github.com/crypto-org-chain/cronos/store/rootmulti"
"github.com/crypto-org-chain/cronos/v2/x/cronos"
cronosclient "github.com/crypto-org-chain/cronos/v2/x/cronos/client"
cronoskeeper "github.com/crypto-org-chain/cronos/v2/x/cronos/keeper"
Expand All @@ -149,7 +150,7 @@ const (
// NOTE: In the SDK, the default value is 255.
AddrLen = 20

FileStreamerDirectory = "file_streamer"
FlagMemIAVL = "memiavl"
)

// this line is used by starport scaffolding # stargate/wasm/app/enabledProposals
Expand Down Expand Up @@ -343,6 +344,11 @@ func New(
interfaceRegistry := encodingConfig.InterfaceRegistry

bApp := baseapp.NewBaseApp(Name, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)

if cast.ToBool(appOpts.Get(FlagMemIAVL)) {
bApp.SetCMS(rootmulti.NewStore(filepath.Join(homePath, "memiavl.db"), logger))
}

bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
bApp.SetInterfaceRegistry(interfaceRegistry)
Expand Down
1 change: 1 addition & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ buildGoApplication rec {
"!/client/"
"!/versiondb/"
"!/memiavl/"
"!/store/"
"!go.mod"
"!go.sum"
"!gomod2nix.toml"
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/cosmos/cosmos-sdk v0.46.11
github.com/cosmos/gogoproto v1.4.2
github.com/cosmos/ibc-go/v5 v5.2.0
github.com/crypto-org-chain/cronos/store v0.0.0
github.com/crypto-org-chain/cronos/versiondb v0.0.0
github.com/ethereum/go-ethereum v1.10.26
github.com/evmos/ethermint v0.6.1-0.20221101220534-a8ea4eceb6d9
Expand Down Expand Up @@ -208,8 +209,9 @@ replace (
github.com/cometbft/cometbft-db => github.com/crypto-org-chain/cometbft-db v0.0.0-20230306031617-b3e4fd3331c4
// Ref: https://forum.cosmos.network/t/ibc-security-advisory-dragonberry/7702
github.com/confio/ics23/go => github.com/confio/ics23/go v0.9.0
github.com/cosmos/cosmos-sdk => github.com/cosmos/cosmos-sdk v0.46.11
github.com/cosmos/cosmos-sdk => github.com/cosmos/cosmos-sdk v0.46.12-0.20230403092311-152465bd0ca7
github.com/crypto-org-chain/cronos/memiavl => ./memiavl
github.com/crypto-org-chain/cronos/store => ./store
github.com/crypto-org-chain/cronos/versiondb => ./versiondb
github.com/ethereum/go-ethereum => github.com/crypto-org-chain/go-ethereum v1.10.19-deepcopy-jumptable
github.com/evmos/ethermint => github.com/crypto-org-chain/ethermint v0.20.8-cronos.0.20230315032257-cb741e1d8196
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ github.com/cosmos/btcutil v1.0.5 h1:t+ZFcX77LpKtDBhjucvnOH8C2l2ioGsBNEQ3jef8xFk=
github.com/cosmos/btcutil v1.0.5/go.mod h1:IyB7iuqZMJlthe2tkIFL33xPyzbFYP0XVdS8P5lUPis=
github.com/cosmos/cosmos-proto v1.0.0-beta.1 h1:iDL5qh++NoXxG8hSy93FdYJut4XfgbShIocllGaXx/0=
github.com/cosmos/cosmos-proto v1.0.0-beta.1/go.mod h1:8k2GNZghi5sDRFw/scPL8gMSowT1vDA+5ouxL8GjaUE=
github.com/cosmos/cosmos-sdk v0.46.11 h1:csMJQHtcb/EIu2EJmeg/2NaGkGw3ZScFwa4CRpCCw18=
github.com/cosmos/cosmos-sdk v0.46.11/go.mod h1:bG4AkW9bqc8ycrryyKGQEl3YV9BY2wr6HggGq8kvcgM=
github.com/cosmos/cosmos-sdk v0.46.12-0.20230403092311-152465bd0ca7 h1:NhJVy00Q2PDTjxufX7f8Byy/qkS2Kl9YezOYosdXMhI=
github.com/cosmos/cosmos-sdk v0.46.12-0.20230403092311-152465bd0ca7/go.mod h1:bG4AkW9bqc8ycrryyKGQEl3YV9BY2wr6HggGq8kvcgM=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
Expand Down
4 changes: 2 additions & 2 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ schema = 3
version = "v1.0.0-beta.1"
hash = "sha256-oATkuj+fM5eBn+ywO+w/tL0AFSIEkx0J3Yz+VhVe0QA="
[mod."github.com/cosmos/cosmos-sdk"]
version = "v0.46.11"
hash = "sha256-RpjzVDZw5n0y4kobBCy2d1oPmDCgL5k47UaZMI67sqU="
version = "v0.46.12-0.20230403092311-152465bd0ca7"
hash = "sha256-pxjj8RzbtYoJ3mRqSze33piQBZHVCB4Qk0iOgqtbdHw="
replaced = "github.com/cosmos/cosmos-sdk"
[mod."github.com/cosmos/go-bip39"]
version = "v1.0.0"
Expand Down
1 change: 1 addition & 0 deletions integration_tests/configs/default.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
staked: '1000000000000000000stake',
mnemonic: '${VALIDATOR1_MNEMONIC}',
'app-config': {
memiavl: true,
store: {
streamers: ['file', 'versiondb'],
},
Expand Down
2 changes: 1 addition & 1 deletion memiavl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/ledgerwatch/erigon-lib v0.0.0-20230210071639-db0e7ed11263
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.1
github.com/tendermint/tm-db v0.6.7
github.com/tidwall/btree v1.5.0
Expand Down Expand Up @@ -34,7 +35,6 @@ require (
github.com/linxGnu/grocksdb v1.7.10 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
Expand Down
46 changes: 40 additions & 6 deletions memiavl/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"crypto/sha256"
"errors"
"math"
"os"

dbm "github.com/tendermint/tm-db"
"github.com/cosmos/iavl"
dbm "github.com/tendermint/tm-db"
)

var emptyHash = sha256.New().Sum(nil)

// verify change sets by replay them to rebuild iavl tree and verify the root hashes
type Tree struct {
snapshot *Snapshot
initialVersion uint32

version uint32
// root node of empty tree is represented as `nil`
root Node

initialVersion uint32
}

// NewEmptyTree creates an empty tree at an arbitrary version.
Expand Down Expand Up @@ -50,9 +52,30 @@ func NewFromSnapshot(snapshot *Snapshot) *Tree {
return NewEmptyTree(int64(snapshot.Version()))
}
return &Tree{
version: snapshot.Version(),
root: snapshot.RootNode(),
snapshot: snapshot,
version: snapshot.Version(),
root: snapshot.RootNode(),
}
}

// Load try to load an existing memiavl tree at the directory,
// if the directory don't exists, creates an empty tree with the provided initial version.
func Load(dir string, initialVersion int64) (*Tree, error) {
snapshot, err := OpenSnapshot(dir)
if err != nil {
if os.IsNotExist(err) {
return NewWithInitialVersion(initialVersion), nil
}
return nil, err
}
return NewFromSnapshot(snapshot), nil
}

func (t *Tree) SetInitialVersion(version int64) {
if version >= int64(math.MaxUint32) {
panic("version overflows uint32")
}
t.initialVersion = uint32(version)
}

// ApplyChangeSet apply the change set of a whole version, and update hashes.
Expand Down Expand Up @@ -80,7 +103,11 @@ func (t *Tree) remove(key []byte) {
func (t *Tree) saveVersion(updateHash bool) ([]byte, int64, error) {
var hash []byte
if updateHash {
hash = t.root.Hash()
if t.root == nil {
hash = emptyHash
} else {
hash = t.root.Hash()
}
}

if t.version >= uint32(math.MaxUint32) {
Expand Down Expand Up @@ -118,6 +145,13 @@ func (t *Tree) Get(key []byte) []byte {
return t.root.Get(key)
}

func (t *Tree) Has(key []byte) bool {
if t.Get(key) == nil {
return false
}
return true
}

func (t *Tree) Iterator(start, end []byte, ascending bool) dbm.Iterator {
return NewIterator(start, end, ascending, t.root)
}
76 changes: 76 additions & 0 deletions store/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module github.com/crypto-org-chain/cronos/store

go 1.20

require (
github.com/cosmos/cosmos-sdk v0.46.11
github.com/cosmos/iavl v0.19.5
github.com/crypto-org-chain/cronos/memiavl v0.0.1
github.com/gogo/protobuf v1.3.2
github.com/pkg/errors v0.9.1
github.com/tendermint/tendermint v0.34.27
github.com/tendermint/tm-db v0.6.7
)

require (
cosmossdk.io/errors v1.0.0-beta.7 // indirect
github.com/VictoriaMetrics/metrics v1.23.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/ledgerwatch/erigon-lib v0.0.0-20230210071639-db0e7ed11263 // indirect
github.com/ledgerwatch/log/v3 v3.7.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tidwall/btree v1.5.0 // indirect
github.com/torquem-ch/mdbx-go v0.27.5 // indirect
github.com/valyala/fastrand v1.1.0 // indirect
github.com/valyala/histogram v1.2.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/exp v0.0.0-20230206171751-46f607a40771 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57 // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.2-0.20220831092852-f930b1dc76e8 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace (
github.com/crypto-org-chain/cronos/memiavl => ../memiavl
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
// use cometbft
github.com/tendermint/tendermint => github.com/cometbft/cometbft v0.34.27
)
Loading

0 comments on commit ab0b541

Please sign in to comment.