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

Commit

Permalink
feat(miner): Allow allowlisted sdk.Msgs (mainly for validator confi…
Browse files Browse the repository at this point in the history
…guration) (#1269)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced validator transactions and their inclusion in the proposal.
- Added a new map to determine which messages can be submitted by
external users.
- **Refactor**
- Adjusted function parameters and return values to accommodate the new
validator transactions feature.
- **Tests**
	- Updated test function calls to match the new function signatures.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Devon Bear <[email protected]>
  • Loading branch information
itsdevbear authored Nov 6, 2023
1 parent 594360d commit 061492a
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 22 deletions.
1 change: 1 addition & 0 deletions cosmos/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
cosmossdk.io/log v1.2.1
cosmossdk.io/math v1.1.3-rc.1
cosmossdk.io/store v1.0.0
cosmossdk.io/x/evidence v0.0.0-20230818115413-c402c51a1508
cosmossdk.io/x/tx v0.12.0
github.com/btcsuite/btcd v0.23.2
github.com/btcsuite/btcd/btcutil v1.1.3
Expand Down
1 change: 1 addition & 0 deletions cosmos/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ 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 h1:6tnPgTpTSIskaTmw/4s5C9FARdgFflycIc9OX8i1tOI=
cosmossdk.io/store v1.0.0/go.mod h1:ABMprwjvx6IpMp8l06TwuMrj6694/QP5NIW+X6jaTYc=
cosmossdk.io/x/evidence v0.0.0-20230818115413-c402c51a1508 h1:R9H1lDpcPSkrLOnt6IDE38o0Wp8xE/+BAxocb0oyX4I=
cosmossdk.io/x/tx v0.12.0 h1:Ry2btjQdrfrje9qZ3iZeZSmDArjgxUJMMcLMrX4wj5U=
cosmossdk.io/x/tx v0.12.0/go.mod h1:qTth2coAGkwCwOCjqQ8EAQg+9udXNRzcnSbMgGKGEI0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down
24 changes: 18 additions & 6 deletions cosmos/runtime/miner/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (m *Miner) PrepareProposal(
var (
payloadEnvelopeBz []byte
err error
valTxs [][]byte
ethGasUsed uint64
)

// We have to run the PreBlocker && BeginBlocker to get the chain into the state
Expand All @@ -65,18 +67,28 @@ func (m *Miner) PrepareProposal(
WithGasMeter(storetypes.NewInfiniteGasMeter())

// We have to prime the state plugin.
// NOTE: if buildBlock below fails, we will have a bad context set on the state plugin.
// In practice this is not a big deal, but this is something we need to address as part
// of better context management practices across Polaris.
if err = m.keeper.SetLatestQueryContext(ctx); err != nil {
return nil, err
}

// Trigger the geth miner to build a block.
if payloadEnvelopeBz, err = m.buildBlock(ctx); err != nil {
if payloadEnvelopeBz, ethGasUsed, err = m.buildBlock(ctx); err != nil {
return nil, err
}

// Return the payload as a transaction in the proposal.
return &abci.ResponsePrepareProposal{Txs: [][]byte{payloadEnvelopeBz}}, 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
}
1 change: 1 addition & 0 deletions cosmos/runtime/miner/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type (
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.
Expand Down
68 changes: 55 additions & 13 deletions cosmos/runtime/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,37 @@ package miner
import (
"context"

"github.com/cosmos/gogoproto/proto"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/miner"

"pkg.berachain.dev/polaris/eth"
"pkg.berachain.dev/polaris/eth/core"
"pkg.berachain.dev/polaris/eth/core/types"
)

// Miner implements the baseapp.TxSelector interface.
type Miner struct {
eth.Miner
app App
chain core.Blockchain
keeper EVMKeeper
valTxSelector baseapp.TxSelector
serializer EnvelopeSerializer
allowedValMsgs map[string]sdk.Msg
currentPayload *miner.Payload
}

// New produces a cosmos miner from a geth miner.
func New(gm eth.Miner, app App, keeper EVMKeeper, chain core.Blockchain) *Miner {
func New(gm eth.Miner, app App, keeper EVMKeeper, allowedValMsgs map[string]sdk.Msg) *Miner {
return &Miner{
Miner: gm,
keeper: keeper,
app: app,
chain: chain,
Miner: gm,
keeper: keeper,
app: app,
allowedValMsgs: allowedValMsgs,
valTxSelector: baseapp.NewDefaultTxSelector(),
}
}

Expand All @@ -61,12 +65,13 @@ func (m *Miner) Init(serializer EnvelopeSerializer) {

// buildBlock builds and submits a payload, it also waits for the txs
// to resolve from the underying worker.
func (m *Miner) buildBlock(ctx sdk.Context) ([]byte, error) {
func (m *Miner) buildBlock(ctx sdk.Context) ([]byte, uint64, error) {
defer m.clearPayload()
if err := m.submitPayloadForBuilding(ctx); err != nil {
return nil, err
return nil, 0, err
}
return m.resolveEnvelope(), nil
env, gasUsed := m.resolveEnvelope()
return env, gasUsed, nil
}

// submitPayloadForBuilding submits a payload for building.
Expand Down Expand Up @@ -99,19 +104,56 @@ func (m *Miner) constructPayloadArgs(ctx sdk.Context) *miner.BuildPayloadArgs {
}

// resolveEnvelope resolves the payload.
func (m *Miner) resolveEnvelope() []byte {
func (m *Miner) resolveEnvelope() ([]byte, uint64) {
if m.currentPayload == nil {
return nil
return nil, 0
}
envelope := m.currentPayload.ResolveFull()
bz, err := m.serializer.ToSdkTxBytes(envelope, envelope.ExecutionPayload.GasLimit)
if err != nil {
panic(err)
}
return bz
return bz, envelope.ExecutionPayload.GasUsed
}

// clearPayload clears the payload.
func (m *Miner) clearPayload() {
m.currentPayload = nil
}

// processValidatorMsgs processes the validator messages.
func (m *Miner) processValidatorMsgs(
ctx sdk.Context, maxTxBytes int64, ethGasUsed uint64, txs [][]byte,
) ([][]byte, error) { //nolint:unparam // should be handled better.
var maxBlockGas uint64
if b := ctx.ConsensusParams().Block; b != nil {
maxBlockGas = uint64(b.MaxGas)
}

blockGasRemaining := maxBlockGas - ethGasUsed

for _, txBz := range txs {
tx, err := m.app.TxDecode(txBz)
if err != nil {
continue
}

includeTx := true
for _, msg := range tx.GetMsgs() {
if _, ok := m.allowedValMsgs[proto.MessageName(msg)]; !ok {
includeTx = false
break
}
}

if includeTx {
stop := m.valTxSelector.SelectTxForProposal(
ctx, uint64(maxTxBytes), blockGasRemaining, tx, txBz,
)
if stop {
break
}
}
}
return m.valTxSelector.SelectedTxs(ctx), nil
}
78 changes: 78 additions & 0 deletions cosmos/runtime/miner/msgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 (
evidence "cosmossdk.io/x/evidence/types"

sdk "github.com/cosmos/cosmos-sdk/types"
crisis "github.com/cosmos/cosmos-sdk/x/crisis/types"
gov "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govbeta "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
slashing "github.com/cosmos/cosmos-sdk/x/slashing/types"
staking "github.com/cosmos/cosmos-sdk/x/staking/types"
)

var (
// DefaultAllowedMsgs are messages that can be submitted by external users.
DefaultAllowedMsgs = map[string]sdk.Msg{
// crisis
"cosmos.crisis.v1beta1.MsgVerifyInvariant": &crisis.MsgVerifyInvariant{},
"cosmos.crisis.v1beta1.MsgVerifyInvariantResponse": nil,

// evidence
"cosmos.evidence.v1beta1.Equivocation": nil,
"cosmos.evidence.v1beta1.MsgSubmitEvidence": &evidence.MsgSubmitEvidence{},
"cosmos.evidence.v1beta1.MsgSubmitEvidenceResponse": nil,

// gov
"cosmos.gov.v1.MsgDeposit": &gov.MsgDeposit{},
"cosmos.gov.v1.MsgDepositResponse": nil,
"cosmos.gov.v1.MsgVote": &gov.MsgVote{},
"cosmos.gov.v1.MsgVoteResponse": nil,
"cosmos.gov.v1.MsgVoteWeighted": &gov.MsgVoteWeighted{},
"cosmos.gov.v1.MsgVoteWeightedResponse": nil,
"cosmos.gov.v1beta1.MsgDeposit": &govbeta.MsgDeposit{},
"cosmos.gov.v1beta1.MsgDepositResponse": nil,
"cosmos.gov.v1beta1.MsgVote": &govbeta.MsgVote{},
"cosmos.gov.v1beta1.MsgVoteResponse": nil,
"cosmos.gov.v1beta1.MsgVoteWeighted": &govbeta.MsgVoteWeighted{},
"cosmos.gov.v1beta1.MsgVoteWeightedResponse": nil,
"cosmos.gov.v1beta1.TextProposal": nil,

// slashing
"cosmos.slashing.v1beta1.MsgUnjail": &slashing.MsgUnjail{},
"cosmos.slashing.v1beta1.MsgUnjailResponse": nil,

// staking
"cosmos.staking.v1beta1.MsgCreateValidator": &staking.MsgCreateValidator{},
"cosmos.staking.v1beta1.MsgCreateValidatorResponse": nil,
"cosmos.staking.v1beta1.MsgEditValidator": &staking.MsgEditValidator{},
"cosmos.staking.v1beta1.MsgEditValidatorResponse": nil,

// tx
"cosmos.tx.v1beta1.Tx": nil,

// upgrade
"cosmos.upgrade.v1beta1.CancelSoftwareUpgradeProposal": nil,
"cosmos.upgrade.v1beta1.SoftwareUpgradeProposal": nil,
}
)
4 changes: 2 additions & 2 deletions cosmos/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ func New(
// Build is a function that sets up the Polaris struct.
// It takes a BaseApp and an EVMKeeper as arguments.
// It returns an error if the setup fails.
func (p *Polaris) Build(app CosmosApp, ek EVMKeeper) error {
func (p *Polaris) Build(app CosmosApp, ek EVMKeeper, allowedValMsgs map[string]sdk.Msg) error {
// Wrap the geth miner and txpool with the cosmos miner and txpool.
p.WrappedTxPool = txpool.New(p.Blockchain(), p.TxPool())
p.WrappedMiner = miner.New(p.Miner(), app, ek, p.Blockchain())
p.WrappedMiner = miner.New(p.Miner(), app, ek, allowedValMsgs)
p.WrappedBlockchain = chain.New(p.Blockchain(), app)

app.SetMempool(p.WrappedTxPool)
Expand Down
3 changes: 2 additions & 1 deletion e2e/testapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
ethcryptocodec "pkg.berachain.dev/polaris/cosmos/crypto/codec"
signinglib "pkg.berachain.dev/polaris/cosmos/lib/signing"
polarruntime "pkg.berachain.dev/polaris/cosmos/runtime"
"pkg.berachain.dev/polaris/cosmos/runtime/miner"
evmkeeper "pkg.berachain.dev/polaris/cosmos/x/evm/keeper"
)

Expand Down Expand Up @@ -191,7 +192,7 @@ func NewPolarisApp(
)

// Setup Polaris Runtime.
if err := app.Polaris.Build(app, app.EVMKeeper); err != nil {
if err := app.Polaris.Build(app, app.EVMKeeper, miner.DefaultAllowedMsgs); err != nil {
panic(err)
}

Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3
github.com/CloudyKit/jet/v6 v6.2.0 h1:EpcZ6SR9n28BUGtNJSvlBqf90IpjeFr36Tizxhn/oME=
github.com/CloudyKit/jet/v6 v6.2.0/go.mod h1:d3ypHeIRNo2+XyqnGA8s+aphtcVpjP5hPwP/Lzo7Ro4=
github.com/DataDog/datadog-go v3.2.0+incompatible h1:qSG2N4FghB1He/r2mFrWKCaL7dXCilEuNEeAn20fdD4=
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/GaijinEntertainment/go-exhaustruct/v2 v2.3.0 h1:+r1rSv4gvYn0wmRjC8X7IAzX8QezqtFV9m0MUHFJgts=
github.com/GaijinEntertainment/go-exhaustruct/v2 v2.3.0/go.mod h1:b3g59n2Y+T5xmcxJL+UEG2f8cQploZm1mR/v6BW0mU0=
github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM=
Expand Down

0 comments on commit 061492a

Please sign in to comment.