Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

chore(app): Setup app better. #1224

Merged
merged 20 commits into from
Oct 13, 2023
83 changes: 0 additions & 83 deletions cosmos/runtime/depinject.go

This file was deleted.

47 changes: 35 additions & 12 deletions cosmos/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ import (
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/node"

"pkg.berachain.dev/polaris/cosmos/config"
libtx "pkg.berachain.dev/polaris/cosmos/lib/tx"
"pkg.berachain.dev/polaris/cosmos/miner"
"pkg.berachain.dev/polaris/cosmos/txpool"
evmkeeper "pkg.berachain.dev/polaris/cosmos/x/evm/keeper"
enginep "pkg.berachain.dev/polaris/cosmos/x/evm/plugins/engine"
evmtypes "pkg.berachain.dev/polaris/cosmos/x/evm/types"
"pkg.berachain.dev/polaris/eth/core"
coretypes "pkg.berachain.dev/polaris/eth/core/types"
ethlog "pkg.berachain.dev/polaris/eth/log"
"pkg.berachain.dev/polaris/eth/polar"
)

Expand All @@ -52,12 +54,39 @@ type Polaris struct {
WrappedTxPool *txpool.Mempool
}

func (p *Polaris) Setup(bApp *baseapp.BaseApp) error {
// SetupPrecompiles is used to setup the precompile contracts post depinject.
if err := p.EVMKeeper.SetupPrecompiles(); err != nil {
return err
// ProvidePolarisRuntime creates a new Polaris runtime from the provided
// dependencies.
func New(cfg *config.Config, logger log.Logger, host core.PolarisHostChain) *Polaris {
node, err := polar.NewGethNetworkingStack(&cfg.Node)
if err != nil {
panic(err)
}

polaris := polar.NewWithNetworkingStack(
&cfg.Polar, host, node, ethlog.FuncHandler(
func(r *ethlog.Record) error {
polarisGethLogger := logger.With("module", "polaris-geth")
switch r.Lvl { //nolint:nolintlint,exhaustive // linter is bugged.
case ethlog.LvlTrace:
case ethlog.LvlDebug:
polarisGethLogger.Debug(r.Msg, r.Ctx...)
case ethlog.LvlInfo:
polarisGethLogger.Info(r.Msg, r.Ctx...)
case ethlog.LvlWarn:
case ethlog.LvlCrit:
case ethlog.LvlError:
polarisGethLogger.Error(r.Msg, r.Ctx...)
}
return nil
}),
)

return &Polaris{
Polaris: polaris,
}
}

func (p *Polaris) Setup(bApp *baseapp.BaseApp) error {
// Init is used to setup the polaris struct.
if err := p.Polaris.Init(); err != nil {
return err
Expand All @@ -70,9 +99,6 @@ func (p *Polaris) Setup(bApp *baseapp.BaseApp) error {
p.WrappedMiner = miner.New(p.Miner())
bApp.SetPrepareProposal(p.WrappedMiner.PrepareProposal)

// TODO: deprecate this
p.EVMKeeper.SetBlockchain(p.Blockchain())

return nil
}

Expand All @@ -93,10 +119,7 @@ func (p *Polaris) Init(clientCtx client.Context, logger log.Logger) error {

// Register Services allows for the application to register lifecycles with the evm
// networking stack.
func (p *Polaris) RegisterServices(clientContext client.Context, lcs []node.Lifecycle) {
// TODO: probably get rid of engine plugin or something and handle rpc methods better.
p.EVMKeeper.Host.GetEnginePlugin().(enginep.Plugin).Start(clientContext)

func (p *Polaris) RegisterServices(_ client.Context, lcs []node.Lifecycle) {
// Register the services with polaris.
for _, lc := range lcs {
p.RegisterService(lc)
Expand Down
33 changes: 24 additions & 9 deletions e2e/testapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import (
evmconfig "pkg.berachain.dev/polaris/cosmos/config"
signinglib "pkg.berachain.dev/polaris/cosmos/lib/signing"
polarruntime "pkg.berachain.dev/polaris/cosmos/runtime"
evmkeeper "pkg.berachain.dev/polaris/cosmos/x/evm/keeper"
enginep "pkg.berachain.dev/polaris/cosmos/x/evm/plugins/engine"
)

// DefaultNodeHome default home directories for the application daemon.
Expand All @@ -72,6 +74,7 @@ var (
// capabilities aren't needed for testing.
type SimApp struct {
*runtime.App
*polarruntime.Polaris
legacyAmino *codec.LegacyAmino
appCodec codec.Codec
txConfig client.TxConfig
Expand All @@ -92,7 +95,7 @@ type SimApp struct {

// polaris contains all the required components for the
// polaris evm.
polaris *polarruntime.Polaris
EVMKeeper *evmkeeper.Keeper
}

//nolint:gochecknoinits // from sdk.
Expand All @@ -118,15 +121,14 @@ func NewPolarisApp(
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {
var (
app = &SimApp{polaris: &polarruntime.Polaris{}}
app = &SimApp{}
appBuilder *runtime.AppBuilder
// merge the AppConfig and other configuration in one config
appConfig = depinject.Configs(
MakeAppConfig(bech32Prefix),
depinject.Provide(
signinglib.ProvideNoopGetSigners[*evmv1alpha1.WrappedEthereumTransaction],
signinglib.ProvideNoopGetSigners[*evmv1alpha1.WrappedPayloadEnvelope],
polarruntime.ProvidePolarisRuntime,
),
depinject.Supply(
// supply the application options
Expand Down Expand Up @@ -178,20 +180,30 @@ func NewPolarisApp(
&app.UpgradeKeeper,
&app.EvidenceKeeper,
&app.ConsensusParamsKeeper,
&app.polaris,
&app.polaris.EVMKeeper,
&app.EVMKeeper,
); err != nil {
panic(err)
}

// Build the app using the app builder.
app.App = appBuilder.Build(db, traceStore, baseAppOptions...)
app.Polaris = polarruntime.New(
evmconfig.MustReadConfigFromAppOpts(appOpts), app.Logger(), app.EVMKeeper.Host,
)

// SetupPrecompiles is used to setup the precompile contracts post depinject.
if err := app.EVMKeeper.SetupPrecompiles(); err != nil {
panic(err)
}

// Initialize Polaris Runtime.
if err := app.polaris.Setup(app.BaseApp); err != nil {
if err := app.Polaris.Setup(app.BaseApp); err != nil {
panic(err)
}

// TODO: deprecate this
app.EVMKeeper.SetBlockchain(app.Polaris.Blockchain())

// Set the ante handler to nil, since it is not needed.
app.SetAnteHandler(nil)

Expand All @@ -212,7 +224,7 @@ func NewPolarisApp(
}

// Load the last state of the polaris evm.
if err := app.polaris.LoadLastState(
if err := app.Polaris.LoadLastState(
app.CommitMultiStore(), uint64(app.LastBlockHeight()),
); err != nil {
panic(err)
Expand Down Expand Up @@ -259,14 +271,17 @@ func (app *SimApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APICon
panic(err)
}

if err := app.polaris.Init(apiSvr.ClientCtx, app.Logger()); err != nil {
// TODO: probably get rid of engine plugin or something and handle rpc methods better.
app.EVMKeeper.Host.GetEnginePlugin().(enginep.Plugin).Start(apiSvr.ClientCtx)

if err := app.Polaris.Init(apiSvr.ClientCtx, app.Logger()); err != nil {
panic(err)
}
}

// Close shuts down the application.
func (app *SimApp) Close() error {
if pl := app.polaris; pl != nil {
if pl := app.Polaris; pl != nil {
return pl.Close()
}
return app.BaseApp.Close()
Expand Down
2 changes: 0 additions & 2 deletions e2e/testapp/polard/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import (
evmv1alpha1 "pkg.berachain.dev/polaris/cosmos/api/polaris/evm/v1alpha1"
evmconfig "pkg.berachain.dev/polaris/cosmos/config"
signinglib "pkg.berachain.dev/polaris/cosmos/lib/signing"
polarruntime "pkg.berachain.dev/polaris/cosmos/runtime"
testapp "pkg.berachain.dev/polaris/e2e/testapp"
)

Expand All @@ -91,7 +90,6 @@ func NewRootCmd() *cobra.Command {
simtestutil.NewAppOptionsWithFlagHome(tempDir()),
),
depinject.Provide(
polarruntime.ProvidePolarisRuntime,
signinglib.ProvideNoopGetSigners[*evmv1alpha1.WrappedEthereumTransaction],
signinglib.ProvideNoopGetSigners[*evmv1alpha1.WrappedPayloadEnvelope]),
),
Expand Down
Loading