Skip to content

Commit

Permalink
test: more cli validation and tests (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency authored Apr 19, 2024
1 parent 39214eb commit 4c2db02
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 10 deletions.
8 changes: 4 additions & 4 deletions interchaintest/helpers/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"github.com/strangelove-ventures/interchaintest/v8/ibc"
)

func ManifestUpdateParams(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, poaAdmin ibc.Wallet, addressPairs string, automaticInflation bool, coinInflationPerYear sdk.Coin, flags ...string) (sdk.TxResponse, error) {
txCmd := []string{"tx", "manifest", "update-params", addressPairs, fmt.Sprintf("%v", automaticInflation), coinInflationPerYear.String()}
func ManifestUpdateParams(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, poaAdmin ibc.Wallet, addressPairs string, automaticInflation string, coinInflationPerYear string, flags ...string) (sdk.TxResponse, error) {
txCmd := []string{"tx", "manifest", "update-params", addressPairs, automaticInflation, coinInflationPerYear}
fmt.Println("ManifestUpdateParams", txCmd)
cmd := TxCommandBuilder(ctx, chain, txCmd, poaAdmin.KeyName(), flags...)
return ExecuteTransaction(ctx, chain, cmd)
}

func ManifestStakeholderPayout(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, poaAdmin ibc.Wallet, coinAmount sdk.Coin, flags ...string) (sdk.TxResponse, error) {
txCmd := []string{"tx", "manifest", "stakeholder-payout", coinAmount.String()}
func ManifestStakeholderPayout(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, poaAdmin ibc.Wallet, coinAmount string, flags ...string) (sdk.TxResponse, error) {
txCmd := []string{"tx", "manifest", "stakeholder-payout", coinAmount}
fmt.Println("ManifestStakeholderPayout", txCmd)
cmd := TxCommandBuilder(ctx, chain, txCmd, poaAdmin.KeyName(), flags...)
return ExecuteTransaction(ctx, chain, cmd)
Expand Down
85 changes: 79 additions & 6 deletions interchaintest/mainfest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestManifestModule(t *testing.T) {

t.Run("fail; Perform a manual distribution payout from the PoA admin (fails due to auto inflation being on)", func(t *testing.T) {
c := sdk.NewCoin(Denom, sdkmath.NewInt(9999999999))
txRes, _ := helpers.ManifestStakeholderPayout(t, ctx, appChain, poaAdmin, c)
txRes, _ := helpers.ManifestStakeholderPayout(t, ctx, appChain, poaAdmin, c.String())
require.EqualValues(t, 0, txRes.Code)

// ensure the new balance is not > c.Amount (a manual payout)
Expand All @@ -108,8 +108,8 @@ func TestManifestModule(t *testing.T) {
txRes, _ := helpers.ManifestUpdateParams(
t, ctx, appChain, poaAdmin,
fmt.Sprintf("%s:1_000_000,%s:99_000_000", uaddr, addr2),
false,
sdk.NewCoin(Denom, sdkmath.NewIntFromUint64(p.Inflation.YearlyAmount)), // it's off, this just matches genesis
"false",
sdk.NewCoin(Denom, sdkmath.NewIntFromUint64(p.Inflation.YearlyAmount)).String(), // it's off, this just matches genesis
)
require.EqualValues(t, 0, txRes.Code)

Expand All @@ -125,7 +125,7 @@ func TestManifestModule(t *testing.T) {
beforeBal2, _ := appChain.GetBalance(ctx, addr2, Denom)

c := sdk.NewCoin(Denom, sdkmath.NewInt(100_000000))
txRes, _ := helpers.ManifestStakeholderPayout(t, ctx, appChain, poaAdmin, c)
txRes, _ := helpers.ManifestStakeholderPayout(t, ctx, appChain, poaAdmin, c.String())
require.EqualValues(t, 0, txRes.Code)

user1bal, err := appChain.GetBalance(ctx, uaddr, Denom)
Expand All @@ -138,17 +138,90 @@ func TestManifestModule(t *testing.T) {

})

t.Run("fail: invalid payout coin", func(t *testing.T) {
_, err := helpers.ManifestStakeholderPayout(t, ctx, appChain, poaAdmin, "foobar")
require.Error(t, err)
require.ErrorContains(t, err, "invalid decimal coin expression")
})

t.Run("fail: invalid stakeholder addr", func(t *testing.T) {
_, err := helpers.ManifestUpdateParams(
t, ctx, appChain, poaAdmin,
fmt.Sprintf("%s:1_000_000,%s:99_000_000", uaddr, "foobar"),
false,
sdk.NewCoin(Denom, sdkmath.NewIntFromUint64(p.Inflation.YearlyAmount)), // it's off, this just matches genesis
"false",
sdk.NewCoin(Denom, sdkmath.NewIntFromUint64(p.Inflation.YearlyAmount)).String(), // it's off, this just matches genesis
)
require.Error(t, err)
require.ErrorContains(t, err, "invalid address")
})

t.Run("fail: invalid stakeholder percentage (>100%)", func(t *testing.T) {
_, err := helpers.ManifestUpdateParams(
t, ctx, appChain, poaAdmin,
fmt.Sprintf("%s:2_000_000,%s:99_000_000", uaddr, addr2),
"false",
sdk.NewCoin(Denom, sdkmath.NewIntFromUint64(p.Inflation.YearlyAmount)).String(), // it's off, this just matches genesis
)
require.Error(t, err)
require.ErrorContains(t, err, "stakeholders should add up to")
})

t.Run("fail: invalid stakeholder percentage (<100%)", func(t *testing.T) {
_, err := helpers.ManifestUpdateParams(
t, ctx, appChain, poaAdmin,
fmt.Sprintf("%s:1_000_000,%s:98_000_000", uaddr, addr2),
"false",
sdk.NewCoin(Denom, sdkmath.NewIntFromUint64(p.Inflation.YearlyAmount)).String(), // it's off, this just matches genesis
)
require.Error(t, err)
require.ErrorContains(t, err, "stakeholders should add up to")
})

t.Run("fail: invalid stakeholder", func(t *testing.T) {
_, err := helpers.ManifestUpdateParams(
t, ctx, appChain, poaAdmin,
"foobar",
"false",
sdk.NewCoin(Denom, sdkmath.NewIntFromUint64(p.Inflation.YearlyAmount)).String(), // it's off, this just matches genesis
)
require.Error(t, err)
require.ErrorContains(t, err, "invalid stakeholder")
})

t.Run("fail: invalid percentage", func(t *testing.T) {
_, err := helpers.ManifestUpdateParams(
t, ctx, appChain, poaAdmin,
fmt.Sprintf("%s:foobar", uaddr),
"false",
sdk.NewCoin(Denom, sdkmath.NewIntFromUint64(p.Inflation.YearlyAmount)).String(), // it's off, this just matches genesis
)
require.Error(t, err)
require.ErrorContains(t, err, "invalid percentage")
})

t.Run("fail: invalid automatic inflation", func(t *testing.T) {
_, err := helpers.ManifestUpdateParams(
t, ctx, appChain, poaAdmin,
fmt.Sprintf("%s:1_000_000,%s:99_000_000", uaddr, addr2),
"foobar",
sdk.NewCoin(Denom, sdkmath.NewIntFromUint64(p.Inflation.YearlyAmount)).String(), // it's off, this just matches genesis
)
require.Error(t, err)
require.ErrorContains(t, err, "invalid syntax")
require.ErrorContains(t, err, "strconv.ParseBool")
})

t.Run("fail: invalid inflation coin", func(t *testing.T) {
_, err := helpers.ManifestUpdateParams(
t, ctx, appChain, poaAdmin,
fmt.Sprintf("%s:1_000_000,%s:99_000_000", uaddr, addr2),
"false",
"foobar",
)
require.Error(t, err)
require.ErrorContains(t, err, "invalid decimal coin expression")
})

t.Cleanup(func() {
_ = ic.Close()
})
Expand Down
11 changes: 11 additions & 0 deletions x/manifest/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ func TestUpdateParams(t *testing.T) {
}, false, 0, "umfx"),
success: false,
},
{
desc: "invalid stakeholder address",
sender: authority.String(),
p: types.NewParams([]*types.StakeHolders{
{
Address: "invalid",
Percentage: 100_000_000,
},
}, false, 0, "umfx"),
success: false,
},
{
desc: "duplicate address",
sender: authority.String(),
Expand Down
5 changes: 5 additions & 0 deletions x/manifest/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
"encoding/json"
fmt "fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
Expand Down Expand Up @@ -62,6 +64,9 @@ func (p Params) Validate() error {

seen := make(map[string]struct{})
for _, sh := range p.StakeHolders {
if _, err := sdk.AccAddressFromBech32(sh.Address); err != nil {
return fmt.Errorf("invalid address: %s", sh.Address)
}
if _, ok := seen[sh.Address]; ok {
return fmt.Errorf("duplicate address: %s", sh.Address)
}
Expand Down

0 comments on commit 4c2db02

Please sign in to comment.