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 (backport #1395) #1400

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/fbridge) [\#1369](https://github.com/Finschia/finschia-sdk/pull/1369) Add the event of `SetBridgeStatus`
* (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/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 behaviors triggered by client
* (x/fswap) [\#1396](https://github.com/Finschia/finschia-sdk/pull/1396) refactor to use snake_case in proto
* (x/fswap) [\#1391](https://github.com/Finschia/finschia-sdk/pull/1391) add cli_test for fswap module

Expand Down
8 changes: 6 additions & 2 deletions x/fbridge/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
proposals := k.GetRoleProposals(ctx)
for _, proposal := range proposals {
if ctx.BlockTime().After(proposal.ExpiredAt) {
k.deleteRoleProposal(ctx, proposal.Id)
if err := k.deleteRoleProposal(ctx, proposal.Id); err != nil {
panic(err)

Check warning on line 17 in x/fbridge/keeper/abci.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/abci.go#L16-L17

Added lines #L16 - L17 were not covered by tests
}
}
}
}
Expand All @@ -36,7 +38,9 @@
panic(err)
}

k.deleteRoleProposal(ctx, proposal.Id)
if err := k.deleteRoleProposal(ctx, proposal.Id); err != nil {
panic(err)

Check warning on line 42 in x/fbridge/keeper/abci.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/abci.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions x/fbridge/keeper/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import (
"encoding/binary"
"fmt"
"time"

sdk "github.com/Finschia/finschia-sdk/types"
Expand Down Expand Up @@ -62,7 +61,7 @@
func (k Keeper) updateRole(ctx sdk.Context, role types.Role, addr sdk.AccAddress) error {
previousRole := k.GetRole(ctx, addr)
if previousRole == role {
return sdkerrors.ErrInvalidRequest.Wrap("target already has same role")
return nil
}

roleMeta := k.GetRoleMetadata(ctx)
Expand Down Expand Up @@ -173,12 +172,14 @@
return proposal, true
}

func (k Keeper) deleteRoleProposal(ctx sdk.Context, id uint64) {
func (k Keeper) deleteRoleProposal(ctx sdk.Context, id uint64) error {

Check warning on line 175 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L175

Added line #L175 was not covered by tests
store := ctx.KVStore(k.storeKey)
if _, found := k.GetRoleProposal(ctx, id); !found {
panic(fmt.Sprintf("role proposal #%d not found", id))
return sdkerrors.ErrNotFound.Wrapf("role proposal #%d not found", id)

Check warning on line 178 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L178

Added line #L178 was not covered by tests
}

store.Delete(types.ProposalKey(id))
return nil

Check warning on line 182 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L182

Added line #L182 was not covered by tests
}

// IterateProposals iterates over the all the role proposals and performs a callback function
Expand Down
5 changes: 4 additions & 1 deletion x/fbridge/keeper/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestAssignRole(t *testing.T) {

// 4. Guardian assigns an address to a same role
err = k.updateRole(ctx, types.RoleOperator, addrs[1])
require.Error(t, err, "role must not be updated to the same role")
require.NoError(t, err)
}

func TestBridgeHaltAndResume(t *testing.T) {
Expand Down Expand Up @@ -86,4 +86,7 @@ func TestBridgeHaltAndResume(t *testing.T) {
require.NoError(t, err)
require.Equal(t, types.StatusActive, k.GetBridgeStatus(ctx), "bridge status must be active (2/3)")
require.Equal(t, types.BridgeStatusMetadata{Active: 2, Inactive: 1}, k.GetBridgeStatusMetadata(ctx))

err = k.updateBridgeSwitch(ctx, addrs[0], 3)
require.Error(t, err, "invalid bridge status must be rejected")
}
4 changes: 2 additions & 2 deletions x/fbridge/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

for _, pair := range gs.Roles {
if err := k.setRole(ctx, pair.Role, sdk.MustAccAddressFromBech32(pair.Address)); err != nil {
panic(err)
return err

Check warning on line 22 in x/fbridge/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/genesis.go#L22

Added line #L22 was not covered by tests
}
}

for _, sw := range gs.BridgeSwitches {
if err := k.setBridgeSwitch(ctx, sdk.MustAccAddressFromBech32(sw.Guardian), sw.Status); err != nil {
panic(err)
return err

Check warning on line 28 in x/fbridge/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/genesis.go#L28

Added line #L28 was not covered by tests
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/fbridge/keeper/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ 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 {
panic(fmt.Errorf("cannot burn coins after a successful send to a module account: %v", err))
panic(fmt.Errorf("cannot burn coins after a successful send to a module account: %s", err))
}

seq := k.GetNextSequence(ctx)
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