laconicd-deprecated/app/middleware/reject.go

68 lines
1.8 KiB
Go
Raw Normal View History

package middleware
import (
context "context"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx"
evmtypes "github.com/tharsis/ethermint/x/evm/types"
)
2022-04-28 07:43:39 +00:00
// RejectMessagesMiddleware prevents invalid msg types from being executed
type RejectMessagesMiddleware struct {
next tx.Handler
}
2022-04-28 07:43:39 +00:00
func NewRejectMessagesMiddleware(txh tx.Handler) tx.Handler {
return RejectMessagesMiddleware{
next: txh,
}
}
// Middleware rejects messages that requires ethereum-specific authentication.
// For example `MsgEthereumTx` requires fee to be deducted in the antehandler in
// order to perform the refund.
// CheckTx implements tx.Handler
2022-04-28 07:43:39 +00:00
func (rmd RejectMessagesMiddleware) CheckTx(ctx context.Context, req tx.Request, checkReq tx.RequestCheckTx) (tx.Response, tx.ResponseCheckTx, error) {
if _, err := reject(req); err != nil {
return tx.Response{}, tx.ResponseCheckTx{}, err
}
2022-04-23 14:37:00 +00:00
return rmd.next.CheckTx(ctx, req, checkReq)
}
// DeliverTx implements tx.Handler
2022-04-28 07:43:39 +00:00
func (rmd RejectMessagesMiddleware) DeliverTx(ctx context.Context, req tx.Request) (tx.Response, error) {
if _, err := reject(req); err != nil {
return tx.Response{}, err
2022-04-23 14:37:00 +00:00
}
return rmd.next.DeliverTx(ctx, req)
}
// SimulateTx implements tx.Handler
2022-04-28 07:43:39 +00:00
func (rmd RejectMessagesMiddleware) SimulateTx(ctx context.Context, req tx.Request) (tx.Response, error) {
if _, err := reject(req); err != nil {
return tx.Response{}, err
}
return rmd.next.SimulateTx(ctx, req)
}
func reject(req tx.Request) (tx.Response, error) {
2022-04-23 14:37:00 +00:00
reqTx := req.Tx
for _, msg := range reqTx.GetMsgs() {
if _, ok := msg.(*evmtypes.MsgEthereumTx); ok {
return tx.Response{}, sdkerrors.Wrapf(
sdkerrors.ErrInvalidType,
"MsgEthereumTx needs to be contained within a tx with 'ExtensionOptionsEthereumTx' option",
)
}
}
2022-04-28 07:43:39 +00:00
return tx.Response{}, nil
}
2022-04-28 07:43:39 +00:00
var _ tx.Handler = RejectMessagesMiddleware{}