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: return error instead of panic for behaviors triggered by client #1395

Merged
merged 13 commits into from
May 24, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/fswap) [\#1372](https://github.com/Finschia/finschia-sdk/pull/1372) support message based proposals
* (x/fswap) [\#1387](https://github.com/Finschia/finschia-sdk/pull/1387) add new Swap query to get a single swap
* (x/fswap) [\#1382](https://github.com/Finschia/finschia-sdk/pull/1382) add validation & unit tests in fswap module
* (x/fbridge) [\#1395](https://github.com/Finschia/finschia-sdk/pull/1395) Return error instead of panic for insufficient balance during bridge transfer

### Bug Fixes
* chore(deps) [\#1141](https://github.com/Finschia/finschia-sdk/pull/1141) Bump github.com/cosmos/ledger-cosmos-go from 0.12.2 to 0.13.2 to fix ledger signing issue
Expand Down
2 changes: 1 addition & 1 deletion x/fbridge/keeper/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (k Keeper) handleBridgeTransfer(ctx sdk.Context, sender sdk.AccAddress, amo
}

if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, token); err != nil {
panic(err)
return 0, err
}

if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, token); err != nil {
jaeseung-bae marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
19 changes: 15 additions & 4 deletions x/fbridge/keeper/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"encoding/binary"
"errors"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -18,10 +19,9 @@ func TestHandleBridgeTransfer(t *testing.T) {
amt := sdk.NewInt(1000000)
denom := "stake"
token := sdk.Coins{sdk.Coin{Denom: denom, Amount: amt}}

bankKeeper.EXPECT().IsSendEnabledCoins(ctx, token).Return(nil)
bankKeeper.EXPECT().SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, token).Return(nil)
bankKeeper.EXPECT().BurnCoins(ctx, types.ModuleName, token).Return(nil)
bankKeeper.EXPECT().IsSendEnabledCoins(ctx, token).Return(nil).Times(1)
bankKeeper.EXPECT().SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, token).Return(nil).Times(1)
bankKeeper.EXPECT().BurnCoins(ctx, types.ModuleName, token).Return(nil).Times(1)

k := NewKeeper(encCfg.Codec, key, memKey, authKeeper, bankKeeper, types.DefaultAuthority().String())
params := types.DefaultParams()
Expand All @@ -41,6 +41,17 @@ func TestHandleBridgeTransfer(t *testing.T) {
h, err := k.GetSeqToBlocknum(ctx, handledSeq)
require.NoError(t, err)
require.Equal(t, uint64(ctx.BlockHeight()), h)

// test error cases
bankKeeper.EXPECT().IsSendEnabledCoins(ctx, token).Return(nil).Times(1)
bankKeeper.EXPECT().SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, token).Return(errors.New("insufficient funds")).Times(1)
_, err = k.handleBridgeTransfer(ctx, sender, amt)
require.Error(t, err)

bankKeeper.EXPECT().IsSendEnabledCoins(ctx, token).Return(nil).Times(1)
bankKeeper.EXPECT().SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, token).Return(nil).Times(1)
bankKeeper.EXPECT().BurnCoins(ctx, types.ModuleName, token).Return(errors.New("failed to burn coins")).Times(1)
require.Panics(t, func() { _, _ = k.handleBridgeTransfer(ctx, sender, amt) }, "cannot burn coins after a successful send to a module account: failed to burn coins")
}

func TestIsValidEthereumAddress(t *testing.T) {
Expand Down
Loading