Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Apr 16, 2021
1 parent 5a13c3f commit e993e09
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion x/wasm/client/codec/marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (

var _ codec.Marshaler = (*ProtoCodec)(nil)

// ProtoCodec that omits empty values
// ProtoCodec that omits empty values.
// This Marshaler can be used globally when setting up the client context or individually
// for each command via `clientCtx.WithJSONMarshaler(myMarshaler)`.
type ProtoCodec struct {
codec.Marshaler
interfaceRegistry types.InterfaceRegistry
Expand Down
19 changes: 12 additions & 7 deletions x/wasm/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ func NewContractInfo(codeID uint64, creator, admin sdk.AccAddress, label string,
}
}

// validatable is an optional
// validatable is an optional interface that can be implemented by an ContractInfoExtension to enable validation
type validatable interface {
ValidateBasic() error
}

// ValidateBasic does syntax checks on the data. If an extension is set and has the `ValidateBasic() error` method, then
// the method is called as well. It is recommend to implement `ValidateBasic` so that the data is verified in the setter
// but also in the genesis import process.
func (c *ContractInfo) ValidateBasic() error {
if c.CodeID == 0 {
return sdkerrors.Wrap(ErrEmpty, "code id")
Expand All @@ -93,27 +96,29 @@ func (c *ContractInfo) ValidateBasic() error {
if c.Extension == nil {
return nil
}

e, ok := c.Extension.GetCachedValue().(validatable)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "extension: %T", c.Extension)
return nil
}
if err := e.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "extension")
}
return nil
}

// SetExtension set new extension data
// SetExtension set new extension data. Calls `ValidateBasic() error` on non nil values when method is implemented by
// the extension.
func (c *ContractInfo) SetExtension(ext ContractInfoExtension) error {
if ext == nil {
c.Extension = nil
return nil
}
if e, ok := ext.(validatable); ok {
if err := e.ValidateBasic(); err != nil {
return err
}
}
if ext == nil {
c.Extension = nil
return nil
}
any, err := codectypes.NewAnyWithValue(ext)
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrPackAny, err.Error())
Expand Down
8 changes: 8 additions & 0 deletions x/wasm/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func TestContractInfoValidateBasic(t *testing.T) {
},
expError: true,
},
"not validatable extension": {
srcMutator: func(c *ContractInfo) {
// any protobuf type with ValidateBasic method
any, err := codectypes.NewAnyWithValue(&govtypes.Proposal{})
require.NoError(t, err)
c.Extension = any
},
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
Expand Down

0 comments on commit e993e09

Please sign in to comment.