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

fix(x/tx): concurrent map writes when calling GetSigners #21073

Merged
merged 9 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions x/tx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Since v0.13.0, x/tx follows Cosmos SDK semver: https://github.com/cosmos/cosmos-

## [Unreleased]

### Improvements

* [#21073](https://github.com/cosmos/cosmos-sdk/pull/21073) Add write-only mutex in Context to protect `getSignersFuncs` map from concurrent writes.

## [v0.13.3](https://github.com/cosmos/cosmos-sdk/releases/tag/x/tx/v0.13.3) - 2024-04-22

### Improvements
Expand Down
14 changes: 11 additions & 3 deletions x/tx/signing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func NewContext(options Options) (*Context, error) {
maxRecursionDepth: options.MaxRecursionDepth,
}

if err := c.Validate(); err != nil {
return nil, err
}

return c, nil
}

Expand Down Expand Up @@ -161,7 +165,7 @@ func (c *Context) Validate() error {
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
}
_, err := c.getGetSignersFn(md)
_, err := c.getGetSignersFn(md, true)
if err != nil {
errs = append(errs, err)
}
Expand Down Expand Up @@ -329,13 +333,17 @@ func (c *Context) getAddressCodec(field protoreflect.FieldDescriptor) address.Co
return addrCdc
}

func (c *Context) getGetSignersFn(messageDescriptor protoreflect.MessageDescriptor) (GetSignersFunc, error) {
func (c *Context) getGetSignersFn(messageDescriptor protoreflect.MessageDescriptor, add bool) (GetSignersFunc, error) {
f, ok := c.customGetSignerFuncs[messageDescriptor.FullName()]
if ok {
return f, nil
}
f, ok = c.getSignersFuncs[messageDescriptor.FullName()]
if !ok {
if !add {
return nil, fmt.Errorf("no GetSignersFunc found for message %s; have you called Validate()?", messageDescriptor.FullName())
}

var err error
f, err = c.makeGetSignersFunc(messageDescriptor)
Copy link
Member

Choose a reason for hiding this comment

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

lgtm because makeGetSignersFunc is stateless right? so worst case we get multiple blocked writes which will quickly resolve

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeap, before the worst case would bring the node down, now the worst case just creates some tiny delays

if err != nil {
Expand All @@ -349,7 +357,7 @@ func (c *Context) getGetSignersFn(messageDescriptor protoreflect.MessageDescript

// GetSigners returns the signers for a given message.
func (c *Context) GetSigners(msg proto.Message) ([][]byte, error) {
f, err := c.getGetSignersFn(msg.ProtoReflect().Descriptor())
f, err := c.getGetSignersFn(msg.ProtoReflect().Descriptor(), false)
if err != nil {
return nil, err
}
Expand Down
Loading