Skip to content

Commit

Permalink
feat(server/v2/cometbft): add checktx handler (#21985)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Oct 3, 2024
1 parent 72f7716 commit a4a0107
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
47 changes: 26 additions & 21 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Consensus[T transaction.Tx] struct {
processProposalHandler handlers.ProcessHandler[T]
verifyVoteExt handlers.VerifyVoteExtensionhandler
extendVote handlers.ExtendVoteHandler
checkTxHandler handlers.CheckTxHandler[T]

addrPeerFilter types.PeerFilter // filter peers by address and port
idPeerFilter types.PeerFilter // filter peers by node ID
Expand Down Expand Up @@ -136,31 +137,35 @@ func (c *Consensus[T]) CheckTx(ctx context.Context, req *abciproto.CheckTxReques
return nil, err
}

resp, err := c.app.ValidateTx(ctx, decodedTx)
// we do not want to return a cometbft error, but a check tx response with the error
if err != nil && !errors.Is(err, resp.Error) {
return nil, err
}
if c.checkTxHandler == nil {
resp, err := c.app.ValidateTx(ctx, decodedTx)
// we do not want to return a cometbft error, but a check tx response with the error
if err != nil && !errors.Is(err, resp.Error) {
return nil, err
}

events, err := intoABCIEvents(resp.Events, c.indexedEvents)
if err != nil {
return nil, err
}
events, err := intoABCIEvents(resp.Events, c.indexedEvents)
if err != nil {
return nil, err
}

cometResp := &abciproto.CheckTxResponse{
Code: 0,
GasWanted: uint64ToInt64(resp.GasWanted),
GasUsed: uint64ToInt64(resp.GasUsed),
Events: events,
}
if resp.Error != nil {
space, code, log := errorsmod.ABCIInfo(resp.Error, c.cfg.AppTomlConfig.Trace)
cometResp.Code = code
cometResp.Codespace = space
cometResp.Log = log
cometResp := &abciproto.CheckTxResponse{
Code: 0,
GasWanted: uint64ToInt64(resp.GasWanted),
GasUsed: uint64ToInt64(resp.GasUsed),
Events: events,
}
if resp.Error != nil {
space, code, log := errorsmod.ABCIInfo(resp.Error, c.cfg.AppTomlConfig.Trace)
cometResp.Code = code
cometResp.Codespace = space
cometResp.Log = log
}

return cometResp, nil
}

return cometResp, nil
return c.checkTxHandler(c.app.ValidateTx)
}

// Info implements types.Application.
Expand Down
4 changes: 4 additions & 0 deletions server/v2/cometbft/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"

"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
)
Expand All @@ -27,4 +28,7 @@ type (
// It takes a context, a store reader map, and a request to extend a vote.
// It returns a response to extend the vote and an error if any.
ExtendVoteHandler func(context.Context, store.ReaderMap, *abci.ExtendVoteRequest) (*abci.ExtendVoteResponse, error)

// CheckTxHandler is a function type that handles the execution of a transaction.
CheckTxHandler[T transaction.Tx] func(func(ctx context.Context, tx T) (server.TxResult, error)) (*abci.CheckTxResponse, error)
)
2 changes: 2 additions & 0 deletions server/v2/cometbft/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type keyGenF = func() (cmtcrypto.PrivKey, error)
type ServerOptions[T transaction.Tx] struct {
PrepareProposalHandler handlers.PrepareHandler[T]
ProcessProposalHandler handlers.ProcessHandler[T]
CheckTxHandler handlers.CheckTxHandler[T]
VerifyVoteExtensionHandler handlers.VerifyVoteExtensionhandler
ExtendVoteHandler handlers.ExtendVoteHandler
KeygenF keyGenF
Expand All @@ -35,6 +36,7 @@ func DefaultServerOptions[T transaction.Tx]() ServerOptions[T] {
return ServerOptions[T]{
PrepareProposalHandler: handlers.NoOpPrepareProposal[T](),
ProcessProposalHandler: handlers.NoOpProcessProposal[T](),
CheckTxHandler: nil,
VerifyVoteExtensionHandler: handlers.NoOpVerifyVoteExtensionHandler(),
ExtendVoteHandler: handlers.NoOpExtendVote(),
Mempool: func(cfg map[string]any) mempool.Mempool[T] { return mempool.NoOpMempool[T]{} },
Expand Down
1 change: 1 addition & 0 deletions server/v2/cometbft/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (s *CometBFTServer[T]) Init(appI serverv2.AppI[T], cfg map[string]any, logg
)
consensus.prepareProposalHandler = s.serverOptions.PrepareProposalHandler
consensus.processProposalHandler = s.serverOptions.ProcessProposalHandler
consensus.checkTxHandler = s.serverOptions.CheckTxHandler
consensus.verifyVoteExt = s.serverOptions.VerifyVoteExtensionHandler
consensus.extendVote = s.serverOptions.ExtendVoteHandler
consensus.addrPeerFilter = s.serverOptions.AddrPeerFilter
Expand Down

0 comments on commit a4a0107

Please sign in to comment.