Skip to content

Commit

Permalink
feat: add validation with context information (#316)
Browse files Browse the repository at this point in the history
* feat: add validation with context information

* update comments
  • Loading branch information
forcodedancing authored Oct 18, 2023
1 parent 7b897e5 commit 3803e78
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
20 changes: 20 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,23 @@ func validateBasicTxMsgs(msgs []sdk.Msg) error {
return nil
}

// validateRuntimeTxMsgs executes basic runtime validator calls for messages.
func validateRuntimeTxMsgs(ctx sdk.Context, msgs []sdk.Msg) error {
if len(msgs) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "must contain at least one message")
}

for _, msg := range msgs {
if runtimeMsg, ok := msg.(sdk.MsgWithRuntimeValidation); ok {
if err := runtimeMsg.ValidateRuntime(ctx); err != nil {
return err
}
}
}

return nil
}

// Returns the application's deliverState if app is in runTxModeDeliver,
// prepareProposalState if app is in runTxPrepareProposal, processProposalState
// if app is in runTxProcessProposal, and checkState otherwise.
Expand Down Expand Up @@ -790,6 +807,9 @@ func (app *BaseApp) runTxOnContext(ctx sdk.Context, mode runTxMode, txBytes []by
if err := validateBasicTxMsgs(msgs); err != nil {
return sdk.GasInfo{}, nil, nil, 0, err
}
if err := validateRuntimeTxMsgs(ctx, msgs); err != nil {
return sdk.GasInfo{}, nil, nil, 0, err
}

if app.anteHandler != nil {
var (
Expand Down
6 changes: 6 additions & 0 deletions baseapp/msg_service_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ func (msr *MsgServiceRouter) RegisterService(sd *grpc.ServiceDesc, handler inter
return nil, err
}

if runtimeReq, ok := req.(sdk.MsgWithRuntimeValidation); ok {
if err := runtimeReq.ValidateRuntime(ctx); err != nil {
return nil, err
}
}

if msr.circuitBreaker != nil {
msgURL := sdk.MsgTypeURL(req)

Expand Down
11 changes: 11 additions & 0 deletions types/tx_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ type (
GetSigners() []AccAddress
}

// MsgWithRuntimeValidation defines the interface a transaction message which wants to enable runtime validation.
// Contract: enable runtime validation after Pampas upgrade.
MsgWithRuntimeValidation interface {
Msg

// ValidateRuntime does a simple validation, different from ValidateBasic the context information
// will be used to facilitate validation for hardfork.
// Contract: only used for simple validation which needs hardfork logic.
ValidateRuntime(ctx Context) error
}

// Fee defines an interface for an application application-defined concrete
// transaction type to be able to set and return the transaction fee.
Fee interface {
Expand Down
6 changes: 6 additions & 0 deletions x/gov/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func (k Keeper) SubmitProposal(ctx sdk.Context, messages []sdk.Msg, metadata, ti
return v1.Proposal{}, sdkerrors.Wrap(types.ErrInvalidProposalMsg, err.Error())
}

if runtimeMsg, ok := msg.(sdk.MsgWithRuntimeValidation); ok {
if err := runtimeMsg.ValidateRuntime(ctx); err != nil {
return v1.Proposal{}, sdkerrors.Wrap(types.ErrInvalidProposalMsg, err.Error())
}
}

signers := msg.GetSigners()
if len(signers) != 1 {
return v1.Proposal{}, types.ErrInvalidSigner
Expand Down

0 comments on commit 3803e78

Please sign in to comment.