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
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 @@
"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 @@
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

Check warning on line 43 in app/proposal.go

View check run for this annotation

Codecov / codecov/patch

app/proposal.go#L38-L43

Added lines #L38 - L43 were not covered by tests
}
}

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

Check warning on line 48 in app/proposal.go

View check run for this annotation

Codecov / codecov/patch

app/proposal.go#L47-L48

Added lines #L47 - L48 were not covered by tests
}

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

Check warning on line 53 in app/proposal.go

View check run for this annotation

Codecov / codecov/patch

app/proposal.go#L53

Added line #L53 was not covered by tests
}

type ProposalHandler struct {
TxDecoder sdk.TxDecoder
Identity age.Identity
Expand Down Expand Up @@ -77,12 +113,7 @@
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 {

Check warning on line 116 in app/proposal.go

View check run for this annotation

Codecov / codecov/patch

app/proposal.go#L116

Added line #L116 was not covered by tests
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 @@
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}

Check warning on line 135 in app/proposal.go

View check run for this annotation

Codecov / codecov/patch

app/proposal.go#L133-L135

Added lines #L133 - L135 were not covered by tests
}

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

Check warning on line 138 in app/proposal.go

View check run for this annotation

Codecov / codecov/patch

app/proposal.go#L138

Added line #L138 was not covered by tests
return abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}
}
}
Expand Down
Loading