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

temp(proposal): Include cosmos txs in block proposal #1469

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions cosmos/runtime/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"sync"
"time"

"github.com/cosmos/gogoproto/proto"
mempool "github.com/cosmos/cosmos-sdk/types/mempool"

"github.com/berachain/polaris/eth"
"github.com/berachain/polaris/eth/core"
Expand All @@ -48,8 +48,10 @@
bc core.Blockchain

valTxSelector baseapp.TxSelector
txVerifier baseapp.ProposalTxVerifier
serializer EnvelopeSerializer
allowedValMsgs map[string]sdk.Msg
cmdsMempool mempool.Mempool
currentPayload *miner.Payload

blockBuilderMu *sync.RWMutex
Expand All @@ -58,15 +60,18 @@
// New produces a cosmos miner from a geth miner.
func New(
miner eth.Miner, app TxDecoder, allowedValMsgs map[string]sdk.Msg,
bc core.Blockchain, blockBuilderMu *sync.RWMutex,
bc core.Blockchain, blockBuilderMu *sync.RWMutex, cmdsMempool mempool.Mempool,
txVerifier baseapp.ProposalTxVerifier,
) *Miner {
return &Miner{
miner: miner,
app: app,
bc: bc,
allowedValMsgs: allowedValMsgs,
valTxSelector: baseapp.NewDefaultTxSelector(),
txVerifier: txVerifier,
blockBuilderMu: blockBuilderMu,
cmdsMempool: cmdsMempool,
}
}

Expand Down Expand Up @@ -135,7 +140,7 @@
if m.currentPayload == nil {
return nil, 0
}
envelope := m.currentPayload.ResolveFull()
envelope := m.currentPayload.ResolveEmpty()
payload := envelope.ExecutionPayload

// Record metadata about the payload
Expand Down Expand Up @@ -168,28 +173,30 @@
}
blockGasRemaining := uint64(b.MaxGas) - ethGasUsed

for _, txBz := range txs {
tx, err := m.app.TxDecode(txBz)
if err != nil {
continue
}
iterator := m.cmdsMempool.Select(ctx, txs)
for iterator != nil {
memTx := iterator.Tx()

includeTx := true
for _, msg := range tx.GetMsgs() {
if _, ok := m.allowedValMsgs[proto.MessageName(msg)]; !ok {
includeTx = false
break
// NOTE: Since transaction verification was already executed in CheckTx,
// which calls mempool.Insert, in theory everything in the pool should be
// valid. But some mempool implementations may insert invalid txs, so we
// check again.
txBz, err := m.txVerifier.PrepareProposalVerifyTx(memTx)
if err != nil {
err := m.cmdsMempool.Remove(memTx)

Check failure on line 186 in cosmos/runtime/miner/miner.go

View workflow job for this annotation

GitHub Actions / ci (lint, polaris-linux-latest, 1.21.6)

shadow: declaration of "err" shadows declaration at line 184 (govet)
if err != nil && !errors.Is(err, mempool.ErrTxNotFound) {
return nil, err
}
}

if includeTx {
} else {
stop := m.valTxSelector.SelectTxForProposal(
ctx, uint64(maxTxBytes), blockGasRemaining, tx, txBz,
ctx, uint64(maxTxBytes), blockGasRemaining, memTx, txBz,
)
if stop {
break
}
}

iterator = iterator.Next()
}
return m.valTxSelector.SelectedTxs(ctx), nil
}
5 changes: 4 additions & 1 deletion cosmos/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (

cometabci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/mempool"
Expand Down Expand Up @@ -70,6 +71,7 @@ type CosmosApp interface {
CommitMultiStore() storetypes.CommitMultiStore
PreBlocker(sdk.Context, *cometabci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error)
BeginBlocker(ctx sdk.Context) (sdk.BeginBlock, error)
baseapp.ProposalTxVerifier
}

// Polaris is a struct that wraps the Polaris struct from the polar package.
Expand Down Expand Up @@ -143,7 +145,8 @@ func (p *Polaris) Build(
// Wrap the geth miner and txpool with the cosmos miner and txpool.
p.WrappedMiner = miner.New(
p.ExecutionLayer.Backend().Miner(), app, allowedValMsgs,
p.Backend().Blockchain(), &p.blockBuilderMu,
p.Backend().Blockchain(), &p.blockBuilderMu, p.WrappedTxPool,
app,
)
p.WrappedBlockchain = chain.New(
p.ExecutionLayer.Backend().Blockchain(), app,
Expand Down
10 changes: 7 additions & 3 deletions cosmos/runtime/txpool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type GethTxPool interface {
type Mempool struct {
eth.TxPool
lifetime int64
cmdsPool mempool.Mempool
chain core.ChainReader
handler Lifecycle
crc CometRemoteCache
Expand All @@ -84,6 +85,7 @@ func New(
crc: newCometRemoteCache(),
blockBuilderMu: blockBuilderMu,
priceLimit: priceLimit,
cmdsPool: mempool.DefaultPriorityMempool(),
}
}

Expand Down Expand Up @@ -117,7 +119,7 @@ func (m *Mempool) Insert(ctx context.Context, sdkTx sdk.Tx) error {
wet, ok := utils.GetAs[*types.WrappedEthereumTransaction](msgs[0])
if !ok {
// We have to return nil for non-ethereum transactions as to not fail check-tx.
return nil
return m.cmdsPool.Insert(ctx, sdkTx)
}

// Add the eth tx to the Geth txpool.
Expand Down Expand Up @@ -161,7 +163,7 @@ func (m *Mempool) Remove(tx sdk.Tx) error {
if len(msgs) == 1 {
env, ok := utils.GetAs[*types.WrappedPayloadEnvelope](msgs[0])
if !ok {
return nil
goto remove
}

// Unwrap the payload to unpack the individual eth transactions to remove from the txpool.
Expand All @@ -175,6 +177,8 @@ func (m *Mempool) Remove(tx sdk.Tx) error {
// Remove the eth tx from comet seen tx cache.
m.crc.DropRemoteTx(txHash)
}
return nil
}
return nil
remove:
return m.cmdsPool.Remove(tx)
}
Loading