This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime): Polaris Runtime Module (#1215)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ### Summary by CodeRabbit - New Feature: Introduced a new Go package `runtime` that provides a function `ProvidePolarisRuntime` for creating a new Polaris runtime. - Refactor: Updated the `Keeper` struct in the `cosmos/x/evm/keeper` package to include a `chain` field of type `Blockchain` and removed several methods. - New Feature: Added `Precommit` and `PrepareCheckState` functions to the `Keeper` struct in the `cosmos/x/evm/keeper` package. - Refactor: Updated the `AppModule` struct in the `cosmos/x/evm` package to include `PrepareCheckState` and `Precommit` methods. - New Feature: Added a new contract `DeployAndConsumeGas` in the `contracts/scripts` directory that consumes gas in a loop. - Refactor: Updated the `MakeAppConfig` function in the `e2e/testapp` package to include `evmtypes.ModuleName` in the `PrepareCheckStaters` and `Precommiters` lists. - New Feature: Added a safety check on the JSON-RPC config in the `eth/polar/config.go` file. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Cal Bera <[email protected]>
- Loading branch information
Showing
31 changed files
with
880 additions
and
1,826 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// Copyright (c) 2023 Berachain Foundation | ||
// | ||
// Permission is hereby granted, free of charge, to any person | ||
// obtaining a copy of this software and associated documentation | ||
// files (the "Software"), to deal in the Software without | ||
// restriction, including without limitation the rights to use, | ||
// copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the | ||
// Software is furnished to do so, subject to the following | ||
// conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
// OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
pragma solidity ^0.8.17; | ||
|
||
import "../lib/forge-std/src/Script.sol"; | ||
import "../src/testing/SolmateERC20.sol"; | ||
import "../src/testing/ConsumeGas.sol"; | ||
|
||
contract DeployAndConsumeGas is Script { | ||
function run() public { | ||
address dropAddress = address(12); | ||
uint256 quantity = 50000; | ||
|
||
vm.startBroadcast(); | ||
ConsumeGas drop = new ConsumeGas(); | ||
|
||
for (uint256 i = 0; i < 1000; i++) { | ||
drop.consumeGas(300000); | ||
} | ||
|
||
vm.stopBroadcast(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// Copyright (C) 2023, Berachain Foundation. All rights reserved. | ||
// Use of this software is govered by the Business Source License included | ||
// in the LICENSE file of this repository and at www.mariadb.com/bsl11. | ||
// | ||
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY | ||
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER | ||
// VERSIONS OF THE LICENSED WORK. | ||
// | ||
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF | ||
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF | ||
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). | ||
// | ||
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON | ||
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, | ||
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND | ||
// TITLE. | ||
|
||
package runtime | ||
|
||
import ( | ||
"cosmossdk.io/depinject" | ||
"cosmossdk.io/log" | ||
|
||
"pkg.berachain.dev/polaris/cosmos/config" | ||
evmkeeper "pkg.berachain.dev/polaris/cosmos/x/evm/keeper" | ||
ethlog "pkg.berachain.dev/polaris/eth/log" | ||
"pkg.berachain.dev/polaris/eth/polar" | ||
) | ||
|
||
// DepInjectInput is the input for the dep inject framework. | ||
type DepInjectInput struct { | ||
depinject.In | ||
|
||
Logger log.Logger | ||
EVMKeeper *evmkeeper.Keeper | ||
Config func() *config.Config | ||
} | ||
|
||
// DepInjectOutput is the output for the dep inject framework. | ||
type DepInjectOutput struct { | ||
depinject.Out | ||
|
||
Polaris *Polaris | ||
} | ||
|
||
// ProvidePolarisRuntime creates a new Polaris runtime from the provided | ||
// dependencies. | ||
func ProvidePolarisRuntime(input DepInjectInput) DepInjectOutput { | ||
cfg := input.Config() | ||
node, err := polar.NewGethNetworkingStack(&cfg.Node) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
polaris := polar.NewWithNetworkingStack( | ||
&cfg.Polar, input.EVMKeeper.Host, node, ethlog.FuncHandler( | ||
func(r *ethlog.Record) error { | ||
polarisGethLogger := input.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 DepInjectOutput{ | ||
Polaris: &Polaris{ | ||
Polaris: polaris, | ||
EVMKeeper: input.EVMKeeper, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// Copyright (C) 2023, Berachain Foundation. All rights reserved. | ||
// Use of this software is govered by the Business Source License included | ||
// in the LICENSE file of this repository and at www.mariadb.com/bsl11. | ||
// | ||
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY | ||
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER | ||
// VERSIONS OF THE LICENSED WORK. | ||
// | ||
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF | ||
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF | ||
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). | ||
// | ||
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON | ||
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, | ||
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND | ||
// TITLE. | ||
|
||
package runtime | ||
|
||
import ( | ||
"cosmossdk.io/log" | ||
storetypes "cosmossdk.io/store/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/ethereum/go-ethereum/beacon/engine" | ||
"github.com/ethereum/go-ethereum/node" | ||
|
||
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" | ||
coretypes "pkg.berachain.dev/polaris/eth/core/types" | ||
"pkg.berachain.dev/polaris/eth/polar" | ||
) | ||
|
||
type Polaris struct { | ||
*polar.Polaris | ||
|
||
// polaris keepers | ||
EVMKeeper *evmkeeper.Keeper | ||
|
||
// polaris componets | ||
WrappedMiner *miner.Miner | ||
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 | ||
} | ||
|
||
// Init is used to setup the polaris struct. | ||
if err := p.Polaris.Init(); err != nil { | ||
return err | ||
} | ||
|
||
// Setup TxPool Wrapper | ||
p.WrappedTxPool = txpool.New(p.TxPool()) | ||
bApp.SetMempool(p.WrappedTxPool) | ||
|
||
p.WrappedMiner = miner.New(p.Miner()) | ||
bApp.SetPrepareProposal(p.WrappedMiner.PrepareProposal) | ||
|
||
// TODO: deprecate this | ||
p.EVMKeeper.SetBlockchain(p.Blockchain()) | ||
|
||
return nil | ||
} | ||
|
||
func (p *Polaris) Init(clientCtx client.Context, logger log.Logger) error { | ||
// Initialize services. | ||
p.WrappedMiner.Init(libtx.NewSerializer[*engine.ExecutionPayloadEnvelope]( | ||
clientCtx.TxConfig, evmtypes.WrapPayload)) | ||
|
||
p.WrappedTxPool.Init(logger, clientCtx, libtx.NewSerializer[*coretypes.Transaction]( | ||
clientCtx.TxConfig, evmtypes.WrapTx)) | ||
|
||
// Register services with Polaris. | ||
p.RegisterServices(clientCtx, []node.Lifecycle{ | ||
p.WrappedTxPool, | ||
}) | ||
return nil | ||
} | ||
|
||
// 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) | ||
|
||
// Register the services with polaris. | ||
for _, lc := range lcs { | ||
p.RegisterService(lc) | ||
} | ||
|
||
// Start the services. | ||
if err := p.StartServices(); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (p *Polaris) LoadLastState(cms storetypes.CommitMultiStore, appHeight uint64) error { | ||
cmsCtx := sdk.Context{}. | ||
WithMultiStore(cms). | ||
WithGasMeter(storetypes.NewInfiniteGasMeter()). | ||
WithBlockGasMeter(storetypes.NewInfiniteGasMeter()).WithEventManager(sdk.NewEventManager()) | ||
return p.Blockchain().LoadLastState(cmsCtx, appHeight) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.