Skip to content

Commit

Permalink
[EVM] Add transient receipts with eventual flush to store (sei-protoc…
Browse files Browse the repository at this point in the history
…ol#1742)

* add transient receipts

* add state store to keeper

* avoid using ctx evm store

* goimports

* Merge conflicts

* Update cosmos

* Add backend

* add close function

* add debug lines

* include prefix on write

* Fix initialization logic

* rename close to handle close

* remove transient deletion

* Update seidb version

* update sei-cosmos

* add in-memory state store for tests

* Set KeepLastVersion to false

* fix mocking of receipts

* flush after receipts exist

* add transient->pebble->legacy fallback

* make error name consistent

* avoid reading from transient store

* use latest version

* fix goimports

* add test coverage for test state store

* tick sei-cosmos

---------

Co-authored-by: yzang2019 <[email protected]>
  • Loading branch information
stevenlanders and yzang2019 authored Jun 27, 2024
1 parent 6e9257d commit d25456d
Show file tree
Hide file tree
Showing 26 changed files with 682 additions and 63 deletions.
92 changes: 68 additions & 24 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
"sync"
"time"

"github.com/cosmos/cosmos-sdk/server"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/sei-protocol/sei-db/ss"

"github.com/ethereum/go-ethereum/ethclient"
ethrpc "github.com/ethereum/go-ethereum/rpc"
Expand All @@ -26,14 +28,6 @@ import (

storetypes "github.com/cosmos/cosmos-sdk/store/types"

"github.com/sei-protocol/sei-chain/aclmapping"
aclutils "github.com/sei-protocol/sei-chain/aclmapping/utils"
appparams "github.com/sei-protocol/sei-chain/app/params"
"github.com/sei-protocol/sei-chain/app/upgrades"
v0upgrade "github.com/sei-protocol/sei-chain/app/upgrades/v0"
"github.com/sei-protocol/sei-chain/utils"
"github.com/sei-protocol/sei-chain/wasmbinding"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -47,27 +41,26 @@ import (
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkacltypes "github.com/cosmos/cosmos-sdk/types/accesscontrol"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/authz"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
aclmodule "github.com/cosmos/cosmos-sdk/x/accesscontrol"
aclclient "github.com/cosmos/cosmos-sdk/x/accesscontrol/client"
aclconstants "github.com/cosmos/cosmos-sdk/x/accesscontrol/constants"
aclkeeper "github.com/cosmos/cosmos-sdk/x/accesscontrol/keeper"
acltypes "github.com/cosmos/cosmos-sdk/x/accesscontrol/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/cosmos/cosmos-sdk/x/authz"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -118,18 +111,25 @@ import (
ibcporttypes "github.com/cosmos/ibc-go/v3/modules/core/05-port/types"
ibchost "github.com/cosmos/ibc-go/v3/modules/core/24-host"
ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper"
"github.com/sei-protocol/sei-chain/x/mint"
mintclient "github.com/sei-protocol/sei-chain/x/mint/client/cli"
mintkeeper "github.com/sei-protocol/sei-chain/x/mint/keeper"
minttypes "github.com/sei-protocol/sei-chain/x/mint/types"

"github.com/sei-protocol/sei-chain/aclmapping"
aclutils "github.com/sei-protocol/sei-chain/aclmapping/utils"
appparams "github.com/sei-protocol/sei-chain/app/params"
"github.com/sei-protocol/sei-chain/app/upgrades"
v0upgrade "github.com/sei-protocol/sei-chain/app/upgrades/v0"
"github.com/sei-protocol/sei-chain/utils"
"github.com/sei-protocol/sei-chain/wasmbinding"
"github.com/sei-protocol/sei-chain/x/evm"
evmante "github.com/sei-protocol/sei-chain/x/evm/ante"
"github.com/sei-protocol/sei-chain/x/evm/blocktest"
evmkeeper "github.com/sei-protocol/sei-chain/x/evm/keeper"
"github.com/sei-protocol/sei-chain/x/evm/querier"
"github.com/sei-protocol/sei-chain/x/evm/replay"
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
"github.com/sei-protocol/sei-chain/x/mint"
mintclient "github.com/sei-protocol/sei-chain/x/mint/client/cli"
mintkeeper "github.com/sei-protocol/sei-chain/x/mint/keeper"
minttypes "github.com/sei-protocol/sei-chain/x/mint/types"
seidb "github.com/sei-protocol/sei-db/ss/types"
"github.com/spf13/cast"
abci "github.com/tendermint/tendermint/abci/types"
tmcfg "github.com/tendermint/tendermint/config"
Expand Down Expand Up @@ -166,6 +166,8 @@ import (

// unnamed import of statik for openapi/swagger UI support
_ "github.com/sei-protocol/sei-chain/docs/swagger"

ssconfig "github.com/sei-protocol/sei-db/config"
)

// this line is used by starport scaffolding # stargate/wasm/app/enabledProposals
Expand Down Expand Up @@ -264,7 +266,8 @@ var (
// EmptyAclmOpts defines a type alias for a list of wasm options.
EmptyACLOpts []aclkeeper.Option
// EnableOCC allows tests to override default OCC enablement behavior
EnableOCC = true
EnableOCC = true
EmptyAppOptions []AppOption
)

var (
Expand Down Expand Up @@ -382,8 +385,12 @@ type App struct {
encodingConfig appparams.EncodingConfig
evmRPCConfig evmrpc.Config
lightInvarianceConfig LightInvarianceConfig

receiptStore seidb.StateStore
}

type AppOption func(*App)

// New returns a reference to an initialized blockchain app
func New(
logger log.Logger,
Expand All @@ -400,13 +407,15 @@ func New(
appOpts servertypes.AppOptions,
wasmOpts []wasm.Option,
aclOpts []aclkeeper.Option,
appOptions []AppOption,
baseAppOptions ...func(*baseapp.BaseApp),
) *App {
appCodec := encodingConfig.Marshaler
cdc := encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry

bAppOptions := SetupSeiDB(logger, homePath, appOpts, baseAppOptions)

bApp := baseapp.NewBaseApp(AppName, logger, db, encodingConfig.TxConfig.TxDecoder(), tmConfig, appOpts, bAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
Expand All @@ -423,7 +432,7 @@ func New(
tokenfactorytypes.StoreKey,
// this line is used by starport scaffolding # stargate/app/storeKey
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey, evmtypes.TransientStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey, dexmoduletypes.MemStoreKey, banktypes.DeferredCacheStoreKey, evmtypes.MemStoreKey, oracletypes.MemStoreKey)

app := &App{
Expand All @@ -440,6 +449,11 @@ func New(
metricCounter: &map[string]float32{},
encodingConfig: encodingConfig,
}

for _, option := range appOptions {
option(app)
}

app.ParamsKeeper = initParamsKeeper(appCodec, cdc, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey])

// set the BaseApp's parameter store
Expand Down Expand Up @@ -596,9 +610,26 @@ func New(
wasmOpts...,
)

receiptStorePath := filepath.Join(homePath, "data", "receipt.db")
ssConfig := ssconfig.DefaultStateStoreConfig()
ssConfig.DedicatedChangelog = true
ssConfig.KeepRecent = cast.ToInt(appOpts.Get(server.FlagMinRetainBlocks))
ssConfig.DBDirectory = receiptStorePath
ssConfig.KeepLastVersion = false
if app.receiptStore == nil {
app.receiptStore, err = ss.NewStateStore(logger, receiptStorePath, ssConfig)
if err != nil {
panic(fmt.Sprintf("error while creating receipt store: %s", err))
}
}
app.EvmKeeper = *evmkeeper.NewKeeper(keys[evmtypes.StoreKey], memKeys[evmtypes.MemStoreKey],
app.GetSubspace(evmtypes.ModuleName), app.BankKeeper, &app.AccountKeeper, &app.StakingKeeper,
app.TransferKeeper, wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper), &app.WasmKeeper)
tkeys[evmtypes.TransientStoreKey], app.GetSubspace(evmtypes.ModuleName), app.receiptStore, app.BankKeeper,
&app.AccountKeeper, &app.StakingKeeper, app.TransferKeeper,
wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper), &app.WasmKeeper)

bApp.SetPreCommitHandler(app.HandlePreCommit)
bApp.SetCloseHandler(app.HandleClose)

app.evmRPCConfig, err = evmrpc.ReadConfig(appOpts)
if err != nil {
panic(fmt.Sprintf("error reading EVM config due to %s", err))
Expand Down Expand Up @@ -970,6 +1001,19 @@ func New(
return app
}

// HandlePreCommit happens right before the block is committed
func (app *App) HandlePreCommit(ctx sdk.Context) error {
return app.EvmKeeper.FlushTransientReceipts(ctx)
}

// Close closes all items that needs closing (called by baseapp)
func (app *App) HandleClose() error {
if app.receiptStore != nil {
return app.receiptStore.Close()
}
return nil
}

// Add (or remove) keepers when they are introduced / removed in different versions
func (app *App) SetStoreUpgradeHandlers() {
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
Expand Down
17 changes: 17 additions & 0 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ func Setup(isCheckTx bool, enableEVMCustomPrecompiles bool, baseAppOptions ...fu
db := dbm.NewMemDB()
encodingConfig := MakeEncodingConfig()
cdc := encodingConfig.Marshaler

options := []AppOption{
func(app *App) {
app.receiptStore = NewInMemoryStateStore()
},
}

res = New(
log.NewNopLogger(),
db,
Expand All @@ -192,6 +199,7 @@ func Setup(isCheckTx bool, enableEVMCustomPrecompiles bool, baseAppOptions ...fu
TestAppOpts{},
EmptyWasmOpts,
EmptyACLOpts,
options,
baseAppOptions...,
)
if !isCheckTx {
Expand Down Expand Up @@ -220,6 +228,13 @@ func SetupWithSc(isCheckTx bool, enableEVMCustomPrecompiles bool, baseAppOptions
db := dbm.NewMemDB()
encodingConfig := MakeEncodingConfig()
cdc := encodingConfig.Marshaler

options := []AppOption{
func(app *App) {
app.receiptStore = NewInMemoryStateStore()
},
}

res = New(
log.NewNopLogger(),
db,
Expand All @@ -235,6 +250,7 @@ func SetupWithSc(isCheckTx bool, enableEVMCustomPrecompiles bool, baseAppOptions
TestAppOpts{true},
EmptyWasmOpts,
EmptyACLOpts,
options,
baseAppOptions...,
)
if !isCheckTx {
Expand Down Expand Up @@ -285,6 +301,7 @@ func SetupTestingAppWithLevelDb(isCheckTx bool, enableEVMCustomPrecompiles bool)
TestAppOpts{},
EmptyWasmOpts,
EmptyACLOpts,
nil,
)
if !isCheckTx {
genesisState := NewDefaultGenesisState(cdc)
Expand Down
Loading

0 comments on commit d25456d

Please sign in to comment.