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
1 parent
83cd457
commit 9c8d58f
Showing
35 changed files
with
417 additions
and
201 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
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 was deleted.
Oops, something went wrong.
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(), "prepare proposal") | ||
ctx.BlockGasMeter().RefundGas(ctx.BlockGasMeter().GasConsumed(), "prepare 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 { | ||
return &abci.ResponseProcessProposal{ | ||
Status: abci.ResponseProcessProposal_REJECT, | ||
}, fmt.Errorf("failed to decode tx: %w", err) | ||
} | ||
|
||
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) | ||
} |
Oops, something went wrong.