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

feat(server/v2/cometbft): add checktx handler #21985

Merged
merged 6 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading