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

Commit

Permalink
feat(abci): add a custom process proposal (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Nov 3, 2023
1 parent 3f15a11 commit cd12ec4
Show file tree
Hide file tree
Showing 31 changed files with 426 additions and 182 deletions.
2 changes: 1 addition & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ packages:
recursive: True
with-expecter: true
all: True
pkg.berachain.dev/polaris/cosmos/txpool:
pkg.berachain.dev/polaris/cosmos/runtime/txpool:
config:
recursive: True
with-expecter: true
Expand Down
1 change: 0 additions & 1 deletion cosmos/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
cosmossdk.io/log v1.2.1
cosmossdk.io/math v1.1.3-rc.1
cosmossdk.io/store v1.0.0-rc.0
cosmossdk.io/x/evidence v0.0.0-20230818115413-c402c51a1508
cosmossdk.io/x/tx v0.11.0
github.com/btcsuite/btcd v0.23.2
github.com/btcsuite/btcd/btcutil v1.1.3
Expand Down
1 change: 0 additions & 1 deletion cosmos/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ cosmossdk.io/math v1.1.3-rc.1 h1:NebCNWDqb1MJRNfvxr4YY7d8FSYgkuB3L75K6xvM+Zo=
cosmossdk.io/math v1.1.3-rc.1/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0-rc.0 h1:9DwOjuUYxDtYxn/REkTxGQAmxlIGfRroB35MQ8TrxF4=
cosmossdk.io/store v1.0.0-rc.0/go.mod h1:FtBDOJmwtOZfmKKF65bKZbTYgS3bDNjjo3nP76dAegk=
cosmossdk.io/x/evidence v0.0.0-20230818115413-c402c51a1508 h1:R9H1lDpcPSkrLOnt6IDE38o0Wp8xE/+BAxocb0oyX4I=
cosmossdk.io/x/tx v0.11.0 h1:Ak2LIC06bXqPbpMIEorkQbwVddRvRys1sL3Cjm+KPfs=
cosmossdk.io/x/tx v0.11.0/go.mod h1:tzuC7JlfGivYuIO32JbvvY3Ft9s6FK1+r0/nGHiHwtM=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down
78 changes: 0 additions & 78 deletions cosmos/miner/msgs.go

This file was deleted.

2 changes: 1 addition & 1 deletion cosmos/runtime/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"

"pkg.berachain.dev/polaris/cosmos/txpool"
"pkg.berachain.dev/polaris/cosmos/runtime/txpool"
)

// NewAnteHandler creates a new instance of AnteHandler with EjectOnRecheckTxDecorator.
Expand Down
115 changes: 115 additions & 0 deletions cosmos/runtime/chain/abci.go
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
}
69 changes: 69 additions & 0 deletions cosmos/runtime/chain/chain.go
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)
}
33 changes: 33 additions & 0 deletions cosmos/runtime/chain/interfaces.go
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)
}
Loading

0 comments on commit cd12ec4

Please sign in to comment.