Skip to content

Commit

Permalink
Problem: default prepare proposal logic is missing
Browse files Browse the repository at this point in the history
Solution:
- keep the default prepare proposal logics: check the block size limit and
  gas limit
  • Loading branch information
yihuang committed May 13, 2024
1 parent 57b190e commit 98d0131
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions app/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"filippo.io/age"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
)
Expand All @@ -18,17 +19,20 @@ type BlockList struct {
}

type ProposalHandler struct {
TxDecoder sdk.TxDecoder
Identity age.Identity
TxDecoder sdk.TxDecoder
Identity age.Identity
TxSelector baseapp.TxSelector

blocklist map[string]struct{}
lastBlockList []byte
}

func NewProposalHandler(txDecoder sdk.TxDecoder, identity age.Identity) *ProposalHandler {
return &ProposalHandler{
TxDecoder: txDecoder,
Identity: identity,
blocklist: make(map[string]struct{}),
TxDecoder: txDecoder,
Identity: identity,
blocklist: make(map[string]struct{}),
TxSelector: baseapp.NewDefaultTxSelector(),
}
}

Expand Down Expand Up @@ -77,12 +81,7 @@ func (h *ProposalHandler) SetBlockList(blob []byte) error {
return nil
}

func (h *ProposalHandler) ValidateTransaction(txBz []byte) error {
tx, err := h.TxDecoder(txBz)
if err != nil {
return err
}

func (h *ProposalHandler) ValidateTransaction(tx sdk.Tx) error {
sigTx, ok := tx.(signing.SigVerifiableTx)
if !ok {
return fmt.Errorf("tx of type %T does not implement SigVerifiableTx", tx)
Expand All @@ -98,22 +97,42 @@ func (h *ProposalHandler) ValidateTransaction(txBz []byte) error {

func (h *ProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHandler {
return func(ctx sdk.Context, req abci.RequestPrepareProposal) abci.ResponsePrepareProposal {
txs := make([][]byte, 0, len(req.Txs))
var maxBlockGas uint64
if b := ctx.ConsensusParams().Block; b != nil {
maxBlockGas = uint64(b.MaxGas)
}

defer h.TxSelector.Clear()

for _, txBz := range req.Txs {
if err := h.ValidateTransaction(txBz); err != nil {
memTx, err := h.TxDecoder(txBz)
if err != nil {
continue
}
txs = append(txs, txBz)

if err := h.ValidateTransaction(memTx); err != nil {
continue
}

stop := h.TxSelector.SelectTxForProposal(uint64(req.MaxTxBytes), maxBlockGas, memTx, txBz)
if stop {
break
}
}

return abci.ResponsePrepareProposal{Txs: txs}
return abci.ResponsePrepareProposal{Txs: h.TxSelector.SelectedTxs()}
}
}

func (h *ProposalHandler) ProcessProposalHandler() sdk.ProcessProposalHandler {
return func(ctx sdk.Context, req abci.RequestProcessProposal) abci.ResponseProcessProposal {
for _, txBz := range req.Txs {
if err := h.ValidateTransaction(txBz); err != nil {
memTx, err := h.TxDecoder(txBz)
if err != nil {
continue
}

if err := h.ValidateTransaction(memTx); err != nil {
return abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}
}
}
Expand Down

0 comments on commit 98d0131

Please sign in to comment.