-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
refactor(x/bank): swap sendrestriction check in InputOutputCoins #21976
Changes from all commits
323cd78
88dbd51
1ea05fe
a0518d0
a95e85c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,14 +148,15 @@ func (k BaseSendKeeper) InputOutputCoins(ctx context.Context, input types.Input, | |
return err | ||
} | ||
|
||
err = k.subUnlockedCoins(ctx, inAddress, input.Coins) | ||
if err != nil { | ||
return err | ||
// ensure all coins can be sent | ||
type toSend struct { | ||
AddressStr string | ||
Address []byte | ||
Coins sdk.Coins | ||
} | ||
|
||
var outAddress sdk.AccAddress | ||
sending := make([]toSend, 0) | ||
for _, out := range outputs { | ||
outAddress, err = k.ak.AddressCodec().StringToBytes(out.Address) | ||
outAddress, err := k.ak.AddressCodec().StringToBytes(out.Address) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -165,13 +166,25 @@ func (k BaseSendKeeper) InputOutputCoins(ctx context.Context, input types.Input, | |
return err | ||
} | ||
|
||
if err := k.addCoins(ctx, outAddress, out.Coins); err != nil { | ||
sending = append(sending, toSend{ | ||
Address: outAddress, | ||
AddressStr: out.Address, | ||
Coins: out.Coins, | ||
}) | ||
} | ||
|
||
if err := k.subUnlockedCoins(ctx, inAddress, input.Coins); err != nil { | ||
return err | ||
} | ||
Comment on lines
+176
to
+178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure transactional integrity when subtracting coins By moving |
||
|
||
for _, out := range sending { | ||
if err := k.addCoins(ctx, out.Address, out.Coins); err != nil { | ||
return err | ||
} | ||
|
||
if err := k.EventService.EventManager(ctx).EmitKV( | ||
types.EventTypeTransfer, | ||
event.NewAttribute(types.AttributeKeyRecipient, out.Address), | ||
event.NewAttribute(types.AttributeKeyRecipient, out.AddressStr), | ||
event.NewAttribute(types.AttributeKeySender, input.Address), | ||
event.NewAttribute(sdk.AttributeKeyAmount, out.Coins.String()), | ||
); err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Optimize 'sending' slice allocation
To improve performance by reducing memory allocations, consider preallocating the
sending
slice with the capacity oflen(outputs)
.You can update the code as follows:
📝 Committable suggestion