From ef305e2489882cf749887c338abd84c917c5d1b9 Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Wed, 24 May 2023 17:34:34 -0600 Subject: [PATCH 1/2] fix: fix blockchain stop to produce blocks --- baseapp/baseapp.go | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 179584b5a9..f940d33d88 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -882,10 +882,13 @@ func createEvents(events sdk.Events, msg sdk.Msg) sdk.Events { // PrepareProposal state internally will be discarded. will be // returned if the transaction cannot be encoded. will be returned if // the transaction is valid, otherwise will be returned. -func (app *BaseApp) PrepareProposalVerifyTx(tx sdk.Tx) ([]byte, error) { - bz, err := app.txEncoder(tx) - if err != nil { - return nil, err +func (app *BaseApp) PrepareProposalVerifyTx(tx sdk.Tx, bz []byte) ([]byte, error) { + var err error + if bz == nil { + bz, err = app.txEncoder(tx) + if err != nil { + return nil, err + } } _, _, _, _, err = app.runTx(runTxPrepareProposal, bz) //nolint:dogsled @@ -920,7 +923,7 @@ type ( // that any custom ABCI PrepareProposal and ProcessProposal handler can use // to verify a transaction. ProposalTxVerifier interface { - PrepareProposalVerifyTx(tx sdk.Tx) ([]byte, error) + PrepareProposalVerifyTx(tx sdk.Tx, txBz []byte) ([]byte, error) ProcessProposalVerifyTx(txBz []byte) (sdk.Tx, error) } @@ -961,17 +964,33 @@ func NewDefaultProposalHandler(mp mempool.Mempool, txVerifier ProposalTxVerifier // FIFO order. func (h DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHandler { return func(ctx sdk.Context, req abci.RequestPrepareProposal) abci.ResponsePrepareProposal { + var ( + selectedTxs [][]byte + totalTxBytes int64 + ) + // If the mempool is nil or a no-op mempool, we simply return the transactions // requested from CometBFT, which, by default, should be in FIFO order. _, isNoOp := h.mempool.(mempool.NoOpMempool) if h.mempool == nil || isNoOp { - return abci.ResponsePrepareProposal{Txs: req.Txs} - } + for _, txBz := range req.Txs { + bz, err := h.txVerifier.PrepareProposalVerifyTx(nil, txBz) + if err != nil { + continue + } else { + txSize := int64(len(bz)) + if totalTxBytes += txSize; totalTxBytes <= req.MaxTxBytes { + selectedTxs = append(selectedTxs, bz) + } else { + // We've reached capacity per req.MaxTxBytes so we cannot select any + // more transactions. + break + } + } - var ( - selectedTxs [][]byte - totalTxBytes int64 - ) + } + return abci.ResponsePrepareProposal{Txs: selectedTxs} + } iterator := h.mempool.Select(ctx, req.Txs) @@ -982,7 +1001,7 @@ func (h DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHand // 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. - bz, err := h.txVerifier.PrepareProposalVerifyTx(memTx) + bz, err := h.txVerifier.PrepareProposalVerifyTx(memTx, nil) if err != nil { err := h.mempool.Remove(memTx) if err != nil && !errors.Is(err, mempool.ErrTxNotFound) { From 61dbda4cfdc654305e070ebc23c462749f18c0fe Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Thu, 25 May 2023 11:39:59 +0800 Subject: [PATCH 2/2] refine codes --- baseapp/baseapp.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index f940d33d88..4a2a4dcdf1 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -973,21 +973,11 @@ func (h DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHand // requested from CometBFT, which, by default, should be in FIFO order. _, isNoOp := h.mempool.(mempool.NoOpMempool) if h.mempool == nil || isNoOp { - for _, txBz := range req.Txs { + for _, txBz := range req.Txs { // req.MaxTxBytes has been ensured bz, err := h.txVerifier.PrepareProposalVerifyTx(nil, txBz) - if err != nil { - continue - } else { - txSize := int64(len(bz)) - if totalTxBytes += txSize; totalTxBytes <= req.MaxTxBytes { - selectedTxs = append(selectedTxs, bz) - } else { - // We've reached capacity per req.MaxTxBytes so we cannot select any - // more transactions. - break - } + if err == nil { + selectedTxs = append(selectedTxs, bz) } - } return abci.ResponsePrepareProposal{Txs: selectedTxs} }