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: add validation with context information #316

Merged
merged 2 commits into from
Oct 18, 2023
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
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this a hardfork?

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
Loading