Skip to content

Commit

Permalink
fix!: Use MessageDescriptor instead of FullName for SignerFuncs
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica committed Mar 25, 2024
1 parent d1c3547 commit 6e9f9dd
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions x/tx/signing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type Context struct {
typeResolver protoregistry.MessageTypeResolver
addressCodec address.Codec
validatorAddressCodec address.Codec
getSignersFuncs map[protoreflect.FullName]GetSignersFunc
customGetSignerFuncs map[protoreflect.FullName]GetSignersFunc
getSignersFuncs map[protoreflect.MessageDescriptor]GetSignersFunc
customGetSignerFuncs map[protoreflect.MessageDescriptor]GetSignersFunc
maxRecursionDepth int
}

Expand All @@ -50,7 +50,7 @@ type Options struct {
ValidatorAddressCodec address.Codec

// CustomGetSigners is a map of message types to custom GetSignersFuncs.
CustomGetSigners map[protoreflect.FullName]GetSignersFunc
CustomGetSigners map[protoreflect.MessageDescriptor]GetSignersFunc

// MaxRecursionDepth is the maximum depth of nested messages that will be traversed
MaxRecursionDepth int
Expand All @@ -62,11 +62,11 @@ type Options struct {
// NOTE: if a custom signers function is defined, the message type used to
// define this function MUST be the concrete type passed to GetSigners,
// otherwise a runtime type error will occur.
func (o *Options) DefineCustomGetSigners(typeName protoreflect.FullName, f GetSignersFunc) {
func (o *Options) DefineCustomGetSigners(typeDesc protoreflect.MessageDescriptor, f GetSignersFunc) {
if o.CustomGetSigners == nil {
o.CustomGetSigners = map[protoreflect.FullName]GetSignersFunc{}
o.CustomGetSigners = map[protoreflect.MessageDescriptor]GetSignersFunc{}
}
o.CustomGetSigners[typeName] = f
o.CustomGetSigners[typeDesc] = f
}

// ProtoFileResolver is a protodesc.Resolver that also allows iterating over all
Expand Down Expand Up @@ -100,7 +100,7 @@ func NewContext(options Options) (*Context, error) {
options.MaxRecursionDepth = 32
}

customGetSignerFuncs := map[protoreflect.FullName]GetSignersFunc{}
customGetSignerFuncs := map[protoreflect.MessageDescriptor]GetSignersFunc{}
for k := range options.CustomGetSigners {
customGetSignerFuncs[k] = options.CustomGetSigners[k]
}
Expand All @@ -110,7 +110,7 @@ func NewContext(options Options) (*Context, error) {
typeResolver: protoTypes,
addressCodec: options.AddressCodec,
validatorAddressCodec: options.ValidatorAddressCodec,
getSignersFuncs: map[protoreflect.FullName]GetSignersFunc{},
getSignersFuncs: map[protoreflect.MessageDescriptor]GetSignersFunc{},
customGetSignerFuncs: customGetSignerFuncs,
maxRecursionDepth: options.MaxRecursionDepth,
}
Expand Down Expand Up @@ -156,7 +156,7 @@ func (c *Context) Validate() error {

for j := 0; j < sd.Methods().Len(); j++ {
md := sd.Methods().Get(j).Input()
_, hasCustomSigner := c.customGetSignerFuncs[md.FullName()]
_, hasCustomSigner := c.customGetSignerFuncs[md]
if _, err := getSignersFieldNames(md); err == nil && hasCustomSigner {
errs = append(errs, fmt.Errorf("a custom signer function as been defined for message %s which already has a signer field defined with (cosmos.msg.v1.signer)", md.FullName()))
continue
Expand Down Expand Up @@ -330,18 +330,18 @@ func (c *Context) getAddressCodec(field protoreflect.FieldDescriptor) address.Co
}

func (c *Context) getGetSignersFn(messageDescriptor protoreflect.MessageDescriptor) (GetSignersFunc, error) {
f, ok := c.customGetSignerFuncs[messageDescriptor.FullName()]
f, ok := c.customGetSignerFuncs[messageDescriptor]
if ok {
return f, nil
}
f, ok = c.getSignersFuncs[messageDescriptor.FullName()]
f, ok = c.getSignersFuncs[messageDescriptor]
if !ok {
var err error
f, err = c.makeGetSignersFunc(messageDescriptor)
if err != nil {
return nil, err
}
c.getSignersFuncs[messageDescriptor.FullName()] = f
c.getSignersFuncs[messageDescriptor] = f
}

return f, nil
Expand Down

0 comments on commit 6e9f9dd

Please sign in to comment.