Skip to content

Commit

Permalink
remove unused CoinFromRequestKey method; add tests for DenomFromReque…
Browse files Browse the repository at this point in the history
…stKey; handle case where key is invalid and contains no denom
  • Loading branch information
Joe Bowman committed Mar 10, 2023
1 parent 6707f34 commit a0fc86f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 20 deletions.
13 changes: 4 additions & 9 deletions utils/coins.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

// coinFromRequestKey parses
func CoinFromRequestKey(query []byte, accAddr sdk.AccAddress) (sdk.Coin, error) {
denom, err := DenomFromRequestKey(query, accAddr)
if err != nil {
return sdk.Coin{}, err
}
return sdk.NewCoin(denom, sdk.ZeroInt()), nil
}

func DenomFromRequestKey(query []byte, accAddr sdk.AccAddress) (string, error) {
balancesStore := query[1:]
gotAccAddress, denom, err := banktypes.AddressAndDenomFromBalancesStore(balancesStore)
if err != nil {
return "", err
}

if len(denom) == 0 {
return "", fmt.Errorf("key contained no denom")
}

if !gotAccAddress.Equals(accAddr) {
return "", fmt.Errorf("account mismatch; expected %s, got %s", accAddr.String(), gotAccAddress.String())
}
Expand Down
74 changes: 74 additions & 0 deletions utils/coins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package utils_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/require"

utils "github.com/ingenuity-build/quicksilver/utils"
)

func TestDenomFromRequestKey(t *testing.T) {
cases := []struct {
name string
fn func() (sdk.AccAddress, string, []byte)
err string
}{
{
"valid",
func() (sdk.AccAddress, string, []byte) {
accAddr := utils.GenerateAccAddressForTest()
prefix := banktypes.CreateAccountBalancesPrefix(accAddr.Bytes())
key := append(prefix, []byte("denom")...)
return accAddr, "denom", key
},
"",
},
{
"invalid - address mismatch",
func() (sdk.AccAddress, string, []byte) {
keyAddr, err := utils.AccAddressFromBech32("cosmos135rd8ft0dyq8fv3w3hhmaa55qu3pe668j99qh67mg747ew4ad03qsgq8vh", "cosmos")
require.NoError(t, err)
checkAddr, err := utils.AccAddressFromBech32("cosmos1ent5eg0xn3pskf3fhdw8mky88ry7t4kx628ru3pzp4nqjp6eufusphlldy", "cosmos")
require.NoError(t, err)
prefix := banktypes.CreateAccountBalancesPrefix(keyAddr.Bytes())
key := append(prefix, []byte("denom")...)
return checkAddr, "denom", key
},
"account mismatch; expected cosmos135rd8ft0dyq8fv3w3hhmaa55qu3pe668j99qh67mg747ew4ad03qsgq8vh, got cosmos1ent5eg0xn3pskf3fhdw8mky88ry7t4kx628ru3pzp4nqjp6eufusphlldy",
},
{
"invalid - empty address",
func() (sdk.AccAddress, string, []byte) {
accAddr := sdk.AccAddress{}
prefix := banktypes.CreateAccountBalancesPrefix(accAddr.Bytes())
key := append(prefix, []byte("denom")...)
return accAddr, "denom", key
},
"invalid key",
},
{
"invalid - empty denom",
func() (sdk.AccAddress, string, []byte) {
accAddr := utils.GenerateAccAddressForTest()
prefix := banktypes.CreateAccountBalancesPrefix(accAddr.Bytes())
key := append(prefix, []byte("")...)
return accAddr, "", key
},
"key contained no denom",
},
}

for _, c := range cases {
address, expectedDenom, key := c.fn()
actualDenom, error := utils.DenomFromRequestKey(key, address)
if len(c.err) == 0 {
require.NoError(t, error)
require.Equal(t, expectedDenom, actualDenom)
} else {
require.Errorf(t, error, c.err)
}
}
}
12 changes: 1 addition & 11 deletions x/interchainstaking/keeper/callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ import (
icstypes "github.com/ingenuity-build/quicksilver/x/interchainstaking/types"
)

func TestCoinFromRequestKey(t *testing.T) {
accAddr := utils.GenerateAccAddressForTest()
prefix := banktypes.CreateAccountBalancesPrefix(accAddr.Bytes())
query := append(prefix, []byte("denom")...)

coin, err := utils.CoinFromRequestKey(query, accAddr)
require.NoError(t, err)
require.Equal(t, "denom", coin.Denom)
}

// ValSetCallback

func (s *KeeperTestSuite) TestHandleValsetCallback() {
Expand Down Expand Up @@ -681,7 +671,7 @@ func (s *KeeperTestSuite) TestHandleValidatorCallbackJailedWithSlashing() {
// s.Require().NoError(err)

// expected := test.expected(zone.Validators)
// fmt.Println(app.InterchainstakingKeeper.GetAllDelegations(ctx, &zone))
// 9(app.InterchainstakingKeeper.GetAllDelegations(ctx, &zone))
// _, found = app.InterchainstakingKeeper.GetDelegation(ctx, &zone, expected.DelegationAddress, expected.ValidatorAddress)
// s.Require().True(found)
// })
Expand Down

0 comments on commit a0fc86f

Please sign in to comment.