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.
- Loading branch information
Showing
32 changed files
with
431 additions
and
113 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,115 @@ | ||
// 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 chain | ||
|
||
import ( | ||
"fmt" | ||
|
||
storetypes "cosmossdk.io/store/types" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/ethereum/go-ethereum/beacon/engine" | ||
|
||
evmtypes "pkg.berachain.dev/polaris/cosmos/x/evm/types" | ||
"pkg.berachain.dev/polaris/eth/core/types" | ||
) | ||
|
||
func (wbc *WrappedBlockchain) ProcessProposal( | ||
ctx sdk.Context, req *abci.RequestProcessProposal, | ||
) (*abci.ResponseProcessProposal, error) { | ||
var ( | ||
err error | ||
) | ||
|
||
// We have to run the PreBlocker && BeginBlocker to get the chain into the state | ||
// it'll be in when the EVM transaction actually runs. | ||
if _, err = wbc.app.PreBlocker(ctx, &abci.RequestFinalizeBlock{ | ||
Txs: req.Txs, | ||
Time: req.Time, | ||
Misbehavior: req.Misbehavior, | ||
Height: req.Height, | ||
NextValidatorsHash: req.NextValidatorsHash, | ||
ProposerAddress: req.ProposerAddress, | ||
}); err != nil { | ||
return &abci.ResponseProcessProposal{ | ||
Status: abci.ResponseProcessProposal_REJECT, | ||
}, err | ||
} else if _, err = wbc.app.BeginBlocker(ctx); err != nil { | ||
return &abci.ResponseProcessProposal{ | ||
Status: abci.ResponseProcessProposal_REJECT, | ||
}, err | ||
} | ||
|
||
ctx.GasMeter().RefundGas(ctx.GasMeter().GasConsumed(), "process proposal") | ||
ctx.BlockGasMeter().RefundGas(ctx.BlockGasMeter().GasConsumed(), "process proposal") | ||
ctx = ctx.WithKVGasConfig(storetypes.GasConfig{}). | ||
WithTransientKVGasConfig(storetypes.GasConfig{}). | ||
WithGasMeter(storetypes.NewInfiniteGasMeter()) | ||
|
||
// Pull an execution payload out of the proposal. | ||
var envelope *engine.ExecutionPayloadEnvelope | ||
for _, tx := range req.Txs { | ||
var sdkTx sdk.Tx | ||
sdkTx, err = wbc.app.TxDecode(tx) | ||
if err != nil { | ||
// should have been verified in prepare proposal, we | ||
// ignore it for now (i.e VE extensions will fail decode). | ||
continue | ||
} | ||
|
||
protoEnvelope := sdkTx.GetMsgs()[0] | ||
if env, ok := protoEnvelope.(*evmtypes.WrappedPayloadEnvelope); ok { | ||
envelope = env.UnwrapPayload() | ||
break | ||
} | ||
} | ||
|
||
// If the proposal doesn't contain an ethereum envelope, we should reject it. | ||
if envelope == nil { | ||
return &abci.ResponseProcessProposal{ | ||
Status: abci.ResponseProcessProposal_REJECT, | ||
}, fmt.Errorf("failed to find envelope in proposal") | ||
} | ||
|
||
// Convert it to a block. | ||
var block *types.Block | ||
if block, err = engine.ExecutableDataToBlock(*envelope.ExecutionPayload, nil, nil); err != nil { | ||
ctx.Logger().Error("failed to build evm block", "err", err) | ||
return &abci.ResponseProcessProposal{ | ||
Status: abci.ResponseProcessProposal_REJECT, | ||
}, err | ||
} | ||
|
||
// Insert the block into the chain. | ||
if err = wbc.InsertBlockWithoutSetHead(ctx, block); err != nil { | ||
ctx.Logger().Error("failed to insert block", "err", err) | ||
return &abci.ResponseProcessProposal{ | ||
Status: abci.ResponseProcessProposal_REJECT, | ||
}, err | ||
} | ||
|
||
return &abci.ResponseProcessProposal{ | ||
Status: abci.ResponseProcessProposal_ACCEPT, | ||
}, nil | ||
} |
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,69 @@ | ||
// 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 chain | ||
|
||
import ( | ||
"context" | ||
|
||
"pkg.berachain.dev/polaris/eth/core" | ||
"pkg.berachain.dev/polaris/eth/core/types" | ||
) | ||
|
||
// WrappedBlockchain is a struct that wraps the core blockchain with additional | ||
// application context. | ||
type WrappedBlockchain struct { | ||
core.Blockchain // chain is the core blockchain. | ||
app App // App is the application context. | ||
|
||
} | ||
|
||
// New creates a new instance of WrappedBlockchain with the provided core blockchain | ||
// and application context. | ||
func New(chain core.Blockchain, app App) *WrappedBlockchain { | ||
return &WrappedBlockchain{Blockchain: chain, app: app} | ||
} | ||
|
||
// WriteGenesisState writes the genesis state to the blockchain. | ||
// It uses the provided context as the application context. | ||
func (wbc *WrappedBlockchain) WriteGenesisState( | ||
ctx context.Context, genState *core.Genesis, | ||
) error { | ||
wbc.PreparePlugins(ctx) | ||
return wbc.WriteGenesisBlock(genState.ToBlock()) | ||
} | ||
|
||
// InsertBlockWithoutSetHead inserts a block into the blockchain and sets | ||
// it as the head. It uses the provided context as the application context. | ||
func (wbc *WrappedBlockchain) InsertBlockAndSetHead( | ||
ctx context.Context, block *types.Block, | ||
) error { | ||
wbc.PreparePlugins(ctx) | ||
return wbc.Blockchain.InsertBlockAndSetHead(block) | ||
} | ||
|
||
// InsertBlockWithoutSetHead inserts a block into the blockchain without setting it | ||
// as the head. It uses the provided context as the application context. | ||
func (wbc *WrappedBlockchain) InsertBlockWithoutSetHead( | ||
ctx context.Context, block *types.Block, | ||
) error { | ||
wbc.PreparePlugins(ctx) | ||
return wbc.Blockchain.InsertBlockWithoutSetHead(block) | ||
} |
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,33 @@ | ||
// 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 chain | ||
|
||
import ( | ||
abci "github.com/cometbft/cometbft/abci/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
type App interface { | ||
BeginBlocker(sdk.Context) (sdk.BeginBlock, error) | ||
PreBlocker(sdk.Context, *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) | ||
TxDecode(txBz []byte) (sdk.Tx, error) | ||
} |
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,87 @@ | ||
// 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 miner implements the Ethereum miner. | ||
package miner | ||
|
||
import ( | ||
storetypes "cosmossdk.io/store/types" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
// emptyHash is a common.Hash initialized to all zeros. | ||
var emptyHash = common.Hash{} | ||
|
||
// PrepareProposal implements baseapp.PrepareProposal. | ||
func (m *Miner) PrepareProposal( | ||
ctx sdk.Context, req *abci.RequestPrepareProposal, | ||
) (*abci.ResponsePrepareProposal, error) { | ||
var ( | ||
payloadEnvelopeBz []byte | ||
err error | ||
valTxs [][]byte | ||
ethGasUsed uint64 | ||
) | ||
|
||
// We have to run the PreBlocker && BeginBlocker to get the chain into the state | ||
// it'll be in when the EVM transaction actually runs. | ||
if _, err = m.app.PreBlocker(ctx, nil); err != nil { | ||
return nil, err | ||
} else if _, err = m.app.BeginBlocker(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
ctx.GasMeter().RefundGas(ctx.GasMeter().GasConsumed(), "prepare proposal") | ||
ctx.BlockGasMeter().RefundGas(ctx.BlockGasMeter().GasConsumed(), "prepare proposal") | ||
ctx = ctx.WithKVGasConfig(storetypes.GasConfig{}). | ||
WithTransientKVGasConfig(storetypes.GasConfig{}). | ||
WithGasMeter(storetypes.NewInfiniteGasMeter()) | ||
|
||
// We have to prime the state plugin. | ||
if err = m.keeper.SetLatestQueryContext(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Trigger the geth miner to build a block. | ||
if payloadEnvelopeBz, ethGasUsed, err = m.buildBlock(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Process the validator messages. | ||
if valTxs, err = m.processValidatorMsgs(ctx, req.MaxTxBytes, ethGasUsed, req.Txs); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Combine the payload envelope with the validator transactions. | ||
allTxs := [][]byte{payloadEnvelopeBz} | ||
|
||
// If there are validator transactions, append them to the allTxs slice. | ||
if len(valTxs) > 0 { | ||
allTxs = append(allTxs, valTxs...) | ||
} | ||
|
||
// Return the payload and validator transactions as a transaction in the proposal. | ||
return &abci.ResponsePrepareProposal{Txs: allTxs}, err | ||
} |
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,54 @@ | ||
// 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 miner | ||
|
||
import ( | ||
"context" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/ethereum/go-ethereum/beacon/engine" | ||
|
||
evmkeeper "pkg.berachain.dev/polaris/cosmos/x/evm/keeper" | ||
) | ||
|
||
// EnvelopeSerializer is used to convert an envelope into a byte slice that represents | ||
// a cosmos sdk.Tx. | ||
type ( | ||
EnvelopeSerializer interface { | ||
ToSdkTxBytes(*engine.ExecutionPayloadEnvelope, uint64) ([]byte, error) | ||
} | ||
|
||
App interface { | ||
BeginBlocker(sdk.Context) (sdk.BeginBlock, error) | ||
PreBlocker(sdk.Context, *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) | ||
TxDecode(txBytes []byte) (sdk.Tx, error) | ||
} | ||
|
||
// EVMKeeper is an interface that defines the methods needed for the EVM setup. | ||
EVMKeeper interface { | ||
// Setup initializes the EVM keeper. | ||
Setup(evmkeeper.WrappedBlockchain) error | ||
SetLatestQueryContext(context.Context) error | ||
} | ||
) |
Oops, something went wrong.