Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed May 17, 2024
1 parent 2faec0d commit d01914e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
14 changes: 10 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,21 @@ func New(
}

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

blockProposalHandler := NewProposalHandler(encodingConfig.TxConfig.TxDecoder(), identity, mempool)
blockProposalHandler := NewProposalHandler(encodingConfig.TxConfig.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(app))

// Re-use the default prepare proposal handler, extend the transaction validation logic
defaultProposalHandler := baseapp.NewDefaultProposalHandler(mempool, app)
defaultProposalHandler.SetTxSelector(NewExtTxSelector(baseapp.NewDefaultTxSelector(), 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
46 changes: 24 additions & 22 deletions app/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,41 @@ import (
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
var _ baseapp.TxSelector = &ExtTxSelector{}

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

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

func (ts *ExtTxSelector) SelectTxForProposal(maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte) bool {
if err := ts.ValidateTx(memTx); err != nil {
return false
}
return ts.TxSelector.SelectTxForProposal(maxTxBytes, maxBlockGas, memTx, txBz)
return ts.Parent.SelectTxForProposal(maxTxBytes, maxBlockGas, memTx, txBz)
}

func (ts *ExtTxSelector) SelectedTxs() [][]byte {
return ts.Parent.SelectedTxs()
}

func (ts *ExtTxSelector) Clear() {
ts.Parent.Clear()
}

type ProposalHandler struct {
Expand All @@ -41,15 +54,13 @@ type ProposalHandler struct {

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

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

Expand Down Expand Up @@ -112,15 +123,6 @@ func (h *ProposalHandler) ValidateTransaction(tx sdk.Tx) error {
return nil
}

func (h *ProposalHandler) PrepareProposalHandler(txVerifier baseapp.ProposalTxVerifier) sdk.PrepareProposalHandler {
defaultHandler := baseapp.NewDefaultProposalHandler(h.mempool, txVerifier)
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 {
Expand Down

0 comments on commit d01914e

Please sign in to comment.