From 640f89adecef4a13f9611048124381fb28f6767e Mon Sep 17 00:00:00 2001 From: "Felix C. Morency" <1102868+fmorency@users.noreply.github.com> Date: Fri, 19 Apr 2024 11:25:56 -0400 Subject: [PATCH] test: more cli validation and tests --- interchaintest/helpers/manifest.go | 8 +-- interchaintest/mainfest_test.go | 85 ++++++++++++++++++++++++++-- x/manifest/keeper/msg_server_test.go | 11 ++++ x/manifest/types/params.go | 5 ++ 4 files changed, 99 insertions(+), 10 deletions(-) diff --git a/interchaintest/helpers/manifest.go b/interchaintest/helpers/manifest.go index 4ba3892..1ed3274 100644 --- a/interchaintest/helpers/manifest.go +++ b/interchaintest/helpers/manifest.go @@ -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) diff --git a/interchaintest/mainfest_test.go b/interchaintest/mainfest_test.go index 3f09faa..2c3e2b0 100644 --- a/interchaintest/mainfest_test.go +++ b/interchaintest/mainfest_test.go @@ -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) @@ -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) @@ -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) @@ -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() }) diff --git a/x/manifest/keeper/msg_server_test.go b/x/manifest/keeper/msg_server_test.go index f71e988..bdaa2eb 100644 --- a/x/manifest/keeper/msg_server_test.go +++ b/x/manifest/keeper/msg_server_test.go @@ -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(), diff --git a/x/manifest/types/params.go b/x/manifest/types/params.go index aa359b6..5b9673e 100644 --- a/x/manifest/types/params.go +++ b/x/manifest/types/params.go @@ -3,6 +3,8 @@ package types import ( "encoding/json" fmt "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" ) const ( @@ -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) }