Skip to content

Commit

Permalink
fix: revert "do not fail fast" in bank's sends
Browse files Browse the repository at this point in the history
since it should be handled by SendRestrictionFn by not returning error when necessary
  • Loading branch information
kakysha committed Jun 12, 2024
1 parent 818721f commit b2463e2
Showing 1 changed file with 12 additions and 62 deletions.
74 changes: 12 additions & 62 deletions x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,45 +157,19 @@ func (k BaseSendKeeper) InputOutputCoins(ctx context.Context, input types.Input,
return err
}

outAddresses := make([][]sdk.AccAddress, len(outputs))
isSuccess := false

for i, out := range outputs {
var (
outAddress, newOutAddress sdk.AccAddress
)
outAddress, err = k.ak.AddressCodec().StringToBytes(out.Address)
for _, out := range outputs {
outAddress, err := k.ak.AddressCodec().StringToBytes(out.Address)
if err != nil {
return err
}
outAddresses[i] = make([]sdk.AccAddress, len(out.Coins)) // out address per denom

for j, coin := range out.Coins {
newOutAddress, err = k.sendRestriction.apply(ctx, inAddress, outAddress, coin)
for _, coin := range out.Coins {
newOutAddress, err := k.sendRestriction.apply(ctx, inAddress, outAddress, coin)
if err != nil {
continue
}

outAddresses[i][j] = newOutAddress

isSuccess = true
}
}

if !isSuccess {
return err // returning last err from sendRestrictionFn (does it matter which one?)
}

for i, out := range outputs {
for j, coin := range out.Coins {
// skip restricted coin
if outAddresses[i][j] == nil {
continue
return err
}

toAddr := outAddresses[i][j]

err := k.sendCoin(ctx, inAddress, toAddr, coin)
err = k.sendCoin(ctx, inAddress, newOutAddress, coin)
if err != nil {
return err
}
Expand All @@ -220,45 +194,21 @@ func (k BaseSendKeeper) SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccA
return err
}

var (
err error
isSuccess bool // if at least one send succeedes, we proceed
toAddresses = make([]sdk.AccAddress, len(amt)) // out address per denom
)
// bech32 encoding is expensive! Only do it once for fromAddr
fromAddrString := fromAddr.String()

for i, coin := range amt {
toAddr, err = k.sendRestriction.apply(ctx, fromAddr, toAddr, coin)

for _, coin := range amt {
newToAddr, err := k.sendRestriction.apply(ctx, fromAddr, toAddr, coin)
if err != nil {
continue
}

toAddresses[i] = toAddr

isSuccess = true
}

if !isSuccess {
return err // returning last err from sendRestrictionFn (does it matter which one?)
}

for i, coin := range amt {
// skip restricted coin
if toAddresses[i] == nil {
continue
return err
}

toAddr = toAddresses[i]

err := k.sendCoin(ctx, fromAddr, toAddr, coin)
err = k.sendCoin(ctx, fromAddr, newToAddr, coin)
if err != nil {
return err
}
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
// bech32 encoding is expensive! Only do it once for fromAddr
fromAddrString := fromAddr.String()
sdkCtx.EventManager().EmitEvent(sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(types.AttributeKeySender, fromAddrString),
Expand Down

0 comments on commit b2463e2

Please sign in to comment.