Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: default prepare proposal logic is missing #1439

Merged
6 changes: 3 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,15 @@ func New(
}

baseAppOptions = memiavlstore.SetupMemIAVL(logger, homePath, appOpts, false, false, baseAppOptions)
mempool := mempool.NoOpMempool{}

blockProposalHandler := NewProposalHandler(encodingConfig.TxConfig.TxDecoder(), identity)
blockProposalHandler := NewProposalHandler(encodingConfig.TxConfig.TxDecoder(), identity, mempool)

// NOTE we use custom transaction decoder that supports the sdk.Tx interface instead of sdk.StdTx
// Setup Mempool and Proposal Handlers
baseAppOptions = append(baseAppOptions, func(app *baseapp.BaseApp) {
mempool := mempool.NoOpMempool{}
app.SetMempool(mempool)
app.SetPrepareProposal(blockProposalHandler.PrepareProposalHandler())
app.SetPrepareProposal(blockProposalHandler.PrepareProposalHandler(app))
app.SetProcessProposal(blockProposalHandler.ProcessProposalHandler())
})
bApp := baseapp.NewBaseApp(Name, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
Expand Down
60 changes: 38 additions & 22 deletions app/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,47 @@ 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/types/mempool"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
)

type BlockList struct {
Addresses []string `mapstructure:"addresses"`
}

type txSelector struct {
baseapp.TxSelector
handler *ProposalHandler
}

func (ts *txSelector) SelectTxForProposal(maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte) bool {
tx, err := ts.handler.TxDecoder(txBz)
if err != nil {
return false
}
if err = ts.handler.ValidateTransaction(tx); err != nil {
return false
}
return ts.TxSelector.SelectTxForProposal(maxTxBytes, maxBlockGas, memTx, txBz)
}

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

blocklist map[string]struct{}
lastBlockList []byte
mempool mempool.Mempool
}

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

Expand Down Expand Up @@ -77,12 +98,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 @@ -96,24 +112,24 @@ func (h *ProposalHandler) ValidateTransaction(txBz []byte) error {
return nil
}

func (h *ProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHandler {
return func(ctx sdk.Context, req abci.RequestPrepareProposal) abci.ResponsePrepareProposal {
txs := make([][]byte, 0, len(req.Txs))
for _, txBz := range req.Txs {
if err := h.ValidateTransaction(txBz); err != nil {
continue
}
txs = append(txs, txBz)
}

return abci.ResponsePrepareProposal{Txs: txs}
}
func (h *ProposalHandler) PrepareProposalHandler(txVerifier baseapp.ProposalTxVerifier) sdk.PrepareProposalHandler {
defaultHandler := baseapp.NewDefaultProposalHandler(h.mempool, txVerifier)
yihuang marked this conversation as resolved.
Show resolved Hide resolved
defaultHandler.SetTxSelector(&txSelector{
TxSelector: baseapp.NewDefaultTxSelector(),
handler: h,
})
return defaultHandler.PrepareProposalHandler()
}

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