Skip to content

Commit

Permalink
Problem: memiavl is not integrated (crypto-org-chain#989)
Browse files Browse the repository at this point in the history
* Problem: memiavl is not integrated

Solution:
- integrate memiavl feature

* test memiavl in integration test

* Update CHANGELOG.md

Signed-off-by: yihuang <huang@crypto.com>

* Update app/config/toml.go

Signed-off-by: yihuang <huang@crypto.com>

* update memiavl

* update memiavl

* update memiavl

---------

Signed-off-by: yihuang <huang@crypto.com>
  • Loading branch information
yihuang authored Jun 15, 2023
1 parent 8067492 commit 7664c1f
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- [#975](https://github.com/crypto-org-chain/chain-main/pull/975) Integrate local state-sync commands.
- [#988](https://github.com/crypto-org-chain/chain-main/pull/988) Release testnet binaries together.
- [#989](https://github.com/crypto-org-chain/chain-main/pull/989) Integrate memiavl feature.

*Jun 9, 2023*

Expand Down
15 changes: 15 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -140,6 +141,8 @@ import (

// unnamed import of statik for swagger UI support
_ "github.com/crypto-org-chain/chain-main/v4/app/docs/statik"

memiavlrootmulti "github.com/crypto-org-chain/cronos/store/rootmulti"
)

// FIXME remove this line, dummy
Expand Down Expand Up @@ -297,6 +300,7 @@ func New(
legacyAmino := encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry

baseAppOptions = SetupMemIAVL(logger, homePath, appOpts, baseAppOptions)
bApp := baseapp.NewBaseApp(appName, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
Expand Down Expand Up @@ -930,3 +934,14 @@ func StoreKeys() (

return keys, memKeys, tkeys
}

// Close will be called in graceful shutdown in start cmd
func (app *ChainApp) Close() error {
err := app.BaseApp.Close()

if cms, ok := app.CommitMultiStore().(*memiavlrootmulti.Store); ok {
return errors.Join(err, cms.WaitAsyncCommit())
}

return err
}
31 changes: 31 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package config

import "github.com/crypto-org-chain/cronos/memiavl"

const DefaultCacheSize = 1000

type MemIAVLConfig struct {
// Enable defines if the memiavl should be enabled.
Enable bool `mapstructure:"enable"`
// ZeroCopy defines if the memiavl should return slices pointing to mmap-ed buffers directly (zero-copy),
// the zero-copied slices must not be retained beyond current block's execution.
ZeroCopy bool `mapstructure:"zero-copy"`
// AsyncCommitBuffer defines the size of asynchronous commit queue, this greatly improve block catching-up
// performance, -1 means synchronous commit.
AsyncCommitBuffer int `mapstructure:"async-commit-buffer"`
// SnapshotKeepRecent defines what many old snapshots (excluding the latest one) to keep after new snapshots are taken.
SnapshotKeepRecent uint32 `mapstructure:"snapshot-keep-recent"`
// SnapshotInterval defines the block interval the memiavl snapshot is taken, default to 1000.
SnapshotInterval uint32 `mapstructure:"snapshot-interval"`
// CacheSize defines the size of the cache for each memiavl store.
CacheSize int `mapstructure:"cache-size"`
}

func DefaultMemIAVLConfig() MemIAVLConfig {
return MemIAVLConfig{
CacheSize: DefaultCacheSize,
SnapshotInterval: memiavl.DefaultSnapshotInterval,
ZeroCopy: true,
SnapshotKeepRecent: 1,
}
}
30 changes: 30 additions & 0 deletions app/config/toml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

// DefaultConfigTemplate defines the configuration template for the memiavl configuration
const DefaultConfigTemplate = `
###############################################################################
### MemIAVL Configuration ###
###############################################################################
[memiavl]
# Enable defines if the memiavl should be enabled.
enable = {{ .MemIAVL.Enable }}
# ZeroCopy defines if the memiavl should return slices pointing to mmap-ed buffers directly (zero-copy),
# the zero-copied slices must not be retained beyond current block's execution.
zero-copy = {{ .MemIAVL.ZeroCopy }}
# AsyncCommitBuffer defines the size of asynchronous commit queue, this greatly improve block catching-up
# performance, -1 means synchronous commit.
async-commit-buffer = {{ .MemIAVL.AsyncCommitBuffer }}
# SnapshotKeepRecent defines what many old snapshots (excluding the latest one) to keep after new snapshots are taken.
snapshot-keep-recent = {{ .MemIAVL.SnapshotKeepRecent }}
# SnapshotInterval defines the block interval the memiavl snapshot is taken, default to 1000.
snapshot-interval = {{ .MemIAVL.SnapshotInterval }}
# CacheSize defines the size of the cache for each memiavl store, default to 1000.
cache-size = {{ .MemIAVL.CacheSize }}
`
46 changes: 46 additions & 0 deletions app/memiavl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package app

import (
"path/filepath"

"github.com/spf13/cast"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/baseapp"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"

"github.com/crypto-org-chain/cronos/memiavl"
"github.com/crypto-org-chain/cronos/store/rootmulti"
)

const (
FlagMemIAVL = "memiavl.enable"
FlagAsyncCommitBuffer = "memiavl.async-commit-buffer"
FlagZeroCopy = "memiavl.zero-copy"
FlagSnapshotKeepRecent = "memiavl.snapshot-keep-recent"
FlagSnapshotInterval = "memiavl.snapshot-interval"
FlagCacheSize = "memiavl.cache-size"
)

func SetupMemIAVL(logger log.Logger, homePath string, appOpts servertypes.AppOptions, baseAppOptions []func(*baseapp.BaseApp)) []func(*baseapp.BaseApp) {
if cast.ToBool(appOpts.Get(FlagMemIAVL)) {
// cms must be overridden before the other options, because they may use the cms,
// make sure the cms aren't be overridden by the other options later on.
cms := rootmulti.NewStore(filepath.Join(homePath, "data", "memiavl.db"), logger)
cms.SetMemIAVLOptions(memiavl.Options{
AsyncCommitBuffer: cast.ToInt(appOpts.Get(FlagAsyncCommitBuffer)),
ZeroCopy: cast.ToBool(appOpts.Get(FlagZeroCopy)),
SnapshotKeepRecent: cast.ToUint32(appOpts.Get(FlagSnapshotKeepRecent)),
SnapshotInterval: cast.ToUint32(appOpts.Get(FlagSnapshotInterval)),
CacheSize: cast.ToInt(appOpts.Get(FlagCacheSize)),
})
baseAppOptions = append([]func(*baseapp.BaseApp){setCMS(cms)}, baseAppOptions...)
}

return baseAppOptions
}

func setCMS(cms storetypes.CommitMultiStore) func(*baseapp.BaseApp) {
return func(bapp *baseapp.BaseApp) { bapp.SetCMS(cms) }
}
8 changes: 6 additions & 2 deletions cmd/chain-maind/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"

"github.com/crypto-org-chain/chain-main/v4/app"
appcfg "github.com/crypto-org-chain/chain-main/v4/app/config"
"github.com/crypto-org-chain/chain-main/v4/app/params"
"github.com/crypto-org-chain/chain-main/v4/config"
chainmaincli "github.com/crypto-org-chain/chain-main/v4/x/chainmain/client/cli"
Expand Down Expand Up @@ -99,6 +100,8 @@ func initAppConfig() (string, interface{}) {

type CustomAppConfig struct {
serverconfig.Config

MemIAVL appcfg.MemIAVLConfig `mapstructure:"memiavl"`
}

// Optionally allow the chain developer to overwrite the SDK's default
Expand All @@ -108,10 +111,11 @@ func initAppConfig() (string, interface{}) {
srvCfg.GRPCWeb.Address = "127.0.0.1:9091"

customAppConfig := CustomAppConfig{
Config: *srvCfg,
Config: *srvCfg,
MemIAVL: appcfg.DefaultMemIAVLConfig(),
}

return serverconfig.DefaultConfigTemplate, customAppConfig
return serverconfig.DefaultConfigTemplate + appcfg.DefaultConfigTemplate, customAppConfig
}

func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require (
github.com/cosmos/cosmos-proto v1.0.0-alpha8
github.com/cosmos/cosmos-sdk v0.46.13
github.com/cosmos/ibc-go/v5 v5.2.1
github.com/crypto-org-chain/cronos/memiavl v0.0.3-0.20230615113127-926000fa6493
github.com/crypto-org-chain/cronos/store v0.0.3-0.20230615113127-926000fa6493
// v1.0.8
github.com/crypto-org-chain/cronos/versiondb v0.0.0-20230529083827-565fa417b0f8
github.com/gogo/protobuf v1.3.3
Expand Down Expand Up @@ -72,7 +74,6 @@ require (
github.com/cosmos/iavl v0.19.6 // indirect
github.com/cosmos/ledger-cosmos-go v0.12.2 // indirect
github.com/creachadair/taskgroup v0.3.2 // indirect
github.com/crypto-org-chain/cronos/memiavl v0.0.2 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,10 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/crypto-org-chain/cometbft-db v0.0.0-20230412133340-ac70df4b45f6 h1:d4h4Ki1UE/LF6CKwYEm3OZ+HIBCrzSmOokG1vce0O98=
github.com/crypto-org-chain/cometbft-db v0.0.0-20230412133340-ac70df4b45f6/go.mod h1:hF5aclS++7WrW8USOA3zPeKI0CuzwUD2TPYug25ANlQ=
github.com/crypto-org-chain/cronos/memiavl v0.0.2 h1:SmIS7OaH+sUGLiPzIvZt6zlROXlFOKh1mnDdtVcji7c=
github.com/crypto-org-chain/cronos/memiavl v0.0.2/go.mod h1:2/31Co9jqQYJJkB8Z0bXnbVCtkBQH5EOumRjRuUriiA=
github.com/crypto-org-chain/cronos/memiavl v0.0.3-0.20230615113127-926000fa6493 h1:5P0qarea/g/3CwEct5OGTUcS4gx2EOSPVrTl3pVTxLo=
github.com/crypto-org-chain/cronos/memiavl v0.0.3-0.20230615113127-926000fa6493/go.mod h1:CQjV+A6D3OA/8zjgFZgKymKaWJcS9pATjrEaYNl0iZ8=
github.com/crypto-org-chain/cronos/store v0.0.3-0.20230615113127-926000fa6493 h1:cTNQTL1RwXw/Rp++p+C+cHDfvujfe4btchGQBj8Tp/U=
github.com/crypto-org-chain/cronos/store v0.0.3-0.20230615113127-926000fa6493/go.mod h1:NBMo3NxuIslHFDCskFYqOTjk81tW3tR+ecfFDickQF4=
github.com/crypto-org-chain/cronos/versiondb v0.0.0-20230529083827-565fa417b0f8 h1:HiSwdL2dzYnqwcG+cU3Phrm7FWag5yknViZnnZBgqQ0=
github.com/crypto-org-chain/cronos/versiondb v0.0.0-20230529083827-565fa417b0f8/go.mod h1:VJuoTEh4AmvQv1+rxCZutb/Z0OXvS1PdL9zUU+U2+zs=
github.com/crypto-org-chain/tm-db v0.6.8-0.20230424032152-87c7e7f4fb61 h1:Y5OuzOkZtjCfO53Jgzl+H8re6pOU4X205a1VMkDcUdk=
Expand Down
7 changes: 5 additions & 2 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ schema = 3
version = "v0.3.2"
hash = "sha256-Y261IO/d9xjV0UScqHvo31broxvnKn4IQQC9Mu6jNkE="
[mod."github.com/crypto-org-chain/cronos/memiavl"]
version = "v0.0.2"
hash = "sha256-Ua+eNcUTJUYiFDwwJKdNaO8VHlK+X/kzDN3BMVztB2E="
version = "v0.0.3-0.20230615113127-926000fa6493"
hash = "sha256-AMHRImoY5o1zC2G3z422klV+o2K04y3xd2Xkp7Fk1OI="
[mod."github.com/crypto-org-chain/cronos/store"]
version = "v0.0.3-0.20230615113127-926000fa6493"
hash = "sha256-vPCGp6taCOQCPhcuWZt4k1WUBinkcdGf6YmFj/RDfZA="
[mod."github.com/crypto-org-chain/cronos/versiondb"]
version = "v0.0.0-20230529083827-565fa417b0f8"
hash = "sha256-jTWVxOHchsC1e98wldJp6pZe7+lH2wiUUhhUnvJA8O4="
Expand Down
9 changes: 9 additions & 0 deletions integration_tests/configs/cosmovisor.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local config = import 'default.jsonnet';

config {
chaintest+: {
validators: [super.validators[0] {
'app-config':: super['app-config'],
}] + super.validators[1:],
},
}
14 changes: 13 additions & 1 deletion integration_tests/configs/default.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@ local validator = {

{
chaintest: {
validators: [validator { commission_rate: '0.000000000000000000' }, validator],
validators: [validator {
commission_rate: '0.000000000000000000',
'app-config': {
memiavl: {
enable: true,
'zero-copy': true,
'snapshot-interval': 5,
},
store: {
streamers: ['versiondb'],
},
},
}, validator],
accounts: default.accounts + default.reserves + default.signers + [
{
name: 'msigner1',
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def cosmovisor_cluster(worker_index, pytestconfig, tmp_path_factory):
data = tmp_path_factory.mktemp("data")
init_cosmovisor(data)
yield from cluster_fixture(
Path(__file__).parent / "configs/default.jsonnet",
Path(__file__).parent / "configs/cosmovisor.jsonnet",
worker_index,
data,
post_init=post_init,
Expand Down

0 comments on commit 7664c1f

Please sign in to comment.