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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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

### Bug Fixes
Expand Down
16 changes: 12 additions & 4 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 @@ -32,11 +34,17 @@
}

if types.CheckTrustLevelThreshold(k.GetRoleMetadata(ctx).Guardian, voteYes, guardianTrustLevel) || proposal.Proposer == k.GetAuthority() {
if err := k.updateRole(ctx, proposal.Role, sdk.MustAccAddressFromBech32(proposal.Target)); err != nil {
0Tech marked this conversation as resolved.
Show resolved Hide resolved
panic(err)
target := sdk.MustAccAddressFromBech32(proposal.Target)
if proposal.Role != k.GetRole(ctx, target) {
err := k.updateRole(ctx, proposal.Role, target)
if err != nil {
panic(err)

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L37 - L41 were not covered by tests
}
}

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

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

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/abci.go#L45-L46

Added lines #L45 - L46 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 {
0Tech marked this conversation as resolved.
Show resolved Hide resolved
return sdkerrors.ErrInvalidRequest.Wrap("target already has same role")
return nil
}

roleMeta := k.GetRoleMetadata(ctx)
Expand Down Expand Up @@ -175,12 +174,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 177 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L177

Added line #L177 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 180 in x/fbridge/keeper/auth.go

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L180

Added line #L180 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

x/fbridge/keeper/auth.go#L184

Added line #L184 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) {

0Tech marked this conversation as resolved.
Show resolved Hide resolved
// 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 {
jaeseung-bae marked this conversation as resolved.
Show resolved Hide resolved
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