Skip to content

Commit

Permalink
Problem: default prepare proposal logic is missing (#1439)
Browse files Browse the repository at this point in the history
* Problem: default prepare proposal logic is missing

Solution:
- keep the default prepare proposal logics: check the block size limit and
  gas limit

* make use of default

* cleanup

* cleanup

* cleanup

* add back decoder

* cleanup

* changelog

* ignore gas wanted in prepare proposal handler

---------

Co-authored-by: mmsqe <[email protected]>
  • Loading branch information
yihuang and mmsqe authored May 20, 2024
1 parent 5728a85 commit b6b7200
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
* (e2ee)[#1415](https://github.com/crypto-org-chain/cronos/pull/1415) Add batch keys query for e2ee module.
* (e2ee)[#1421](https://github.com/crypto-org-chain/cronos/pull/1421) Validate e2ee key when register.

### Bug Fixes

* [#1439](https://github.com/crypto-org-chain/cronos/pull/1439) Add back default prepare proposal logic.

*May 3, 2024*

## v1.2.2
Expand Down
16 changes: 14 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ func New(
appCodec := encodingConfig.Codec
cdc := encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry
txDecoder := encodingConfig.TxConfig.TxDecoder()

var identity age.Identity
{
Expand All @@ -423,14 +424,25 @@ func New(

baseAppOptions = memiavlstore.SetupMemIAVL(logger, homePath, appOpts, false, false, baseAppOptions)

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

// 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())

// Re-use the default prepare proposal handler, extend the transaction validation logic
defaultProposalHandler := baseapp.NewDefaultProposalHandler(mempool, app)
defaultProposalHandler.SetTxSelector(NewExtTxSelector(
baseapp.NewDefaultTxSelector(),
txDecoder,
blockProposalHandler.ValidateTransaction,
))
app.SetPrepareProposal(defaultProposalHandler.PrepareProposalHandler())

// The default process proposal handler do nothing when the mempool is noop,
// so we just implement a new one.
app.SetProcessProposal(blockProposalHandler.ProcessProposalHandler())
})
bApp := baseapp.NewBaseApp(Name, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
Expand Down
64 changes: 43 additions & 21 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 @@ -17,6 +18,41 @@ type BlockList struct {
Addresses []string `mapstructure:"addresses"`
}

var _ baseapp.TxSelector = &ExtTxSelector{}

// ExtTxSelector extends a baseapp.TxSelector with extra tx validation method
type ExtTxSelector struct {
baseapp.TxSelector
TxDecoder sdk.TxDecoder
ValidateTx func(sdk.Tx) error
}

func NewExtTxSelector(parent baseapp.TxSelector, txDecoder sdk.TxDecoder, validateTx func(sdk.Tx) error) *ExtTxSelector {
return &ExtTxSelector{
TxSelector: parent,
TxDecoder: txDecoder,
ValidateTx: validateTx,
}
}

func (ts *ExtTxSelector) SelectTxForProposal(maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte) bool {
var err error
if memTx == nil {
memTx, err = ts.TxDecoder(txBz)
if err != nil {
return false
}
}

if err := ts.ValidateTx(memTx); err != nil {
return false
}

// don't pass `memTx` to parent selector so it don't check tx gas wanted against block gas limit,
// it conflicts with the max-tx-gas-wanted logic.
return ts.TxSelector.SelectTxForProposal(maxTxBytes, maxBlockGas, nil, txBz)
}

type ProposalHandler struct {
TxDecoder sdk.TxDecoder
Identity age.Identity
Expand Down Expand Up @@ -77,12 +113,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 +127,15 @@ 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) 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 {
return abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}
}

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

0 comments on commit b6b7200

Please sign in to comment.