From 73de69de01a496624ba5bb63067406a8d88663f4 Mon Sep 17 00:00:00 2001 From: Joe <85871158+Josumner@users.noreply.github.com> Date: Mon, 12 Jul 2021 10:47:35 +0100 Subject: [PATCH 1/2] Diff denom keeper tests (#96) * feat: add Multiple denomination drip test * feat: test different denomination add fund * style: improve clarity of naming * fix: change drip test values --- x/airdrop/keeper/keeper_test.go | 84 ++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/x/airdrop/keeper/keeper_test.go b/x/airdrop/keeper/keeper_test.go index 94aa2b7d79..37e9f51a5c 100644 --- a/x/airdrop/keeper/keeper_test.go +++ b/x/airdrop/keeper/keeper_test.go @@ -17,6 +17,8 @@ var ( feeCollectorAddress = authtypes.NewModuleAddress(authtypes.FeeCollectorName) addr1 = sdk.AccAddress([]byte("addr1_______________")) addr2 = sdk.AccAddress([]byte("addr2_______________")) + addr3 = sdk.AccAddress([]byte("addr3_______________")) + addr4 = sdk.AccAddress([]byte("addr4_______________")) ) type KeeperTestSuite struct { @@ -34,9 +36,10 @@ func (s *KeeperTestSuite) SetupTest() { Height: 10, }) - s.app.AirdropKeeper.SetParams(s.ctx, types.NewParams(addr1.String(), addr2.String())) + s.app.AirdropKeeper.SetParams(s.ctx, types.NewParams(addr1.String(), addr2.String(), addr3.String(), addr4.String())) } + func (s *KeeperTestSuite) TestAddNewFund() { amount := sdk.NewInt64Coin(sdk.DefaultBondDenom, 4000) s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr1, amount)) // ensure the account is funded @@ -164,6 +167,85 @@ func (s *KeeperTestSuite) TestFeeDrip() { s.Require().Equal(feeCollectorBalance, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(4040))) } +func (s *KeeperTestSuite) TestAddNewFundWithDiffDenom() { + amount := sdk.NewInt64Coin("denom", 4000) + s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr1, amount)) // ensure the account is funded + + addrBalance := s.app.BankKeeper.GetBalance(s.ctx, addr1, "denom") + moduleBalance := s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, "denom") + + // sanity check + s.Require().Equal(addrBalance, sdk.NewCoin("denom", sdk.NewInt(4000))) + s.Require().Equal(moduleBalance, sdk.NewCoin("denom", sdk.NewInt(0))) + + fund := types.Fund{ + Amount: &amount, + DripAmount: sdk.NewInt(40), + } + s.Require().NoError(s.app.AirdropKeeper.AddFund(s.ctx, addr1, fund)) + + addrBalance = s.app.BankKeeper.GetBalance(s.ctx, addr1, "denom") + moduleBalance = s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, "denom") + + s.Require().Equal(addrBalance, sdk.NewCoin("denom", sdk.NewInt(0))) + s.Require().Equal(moduleBalance, sdk.NewCoin("denom", sdk.NewInt(4000))) +} + +func (s *KeeperTestSuite) TestMultiDenomFeeDrip() { + amountStake := sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000) + amountDenom := sdk.NewInt64Coin("denom", 1000) + fund1 := types.Fund{ + Amount: &amountStake, + DripAmount: sdk.NewInt(500)} + + fund2 := types.Fund{ + Amount: &amountStake, + DripAmount: sdk.NewInt(800), + } + + fund3 := types.Fund{ + Amount: &amountDenom, + DripAmount: sdk.NewInt(100)} + + fund4 := types.Fund{ + Amount: &amountDenom, + DripAmount: sdk.NewInt(300), + } + s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr1, amountStake)) + s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr2, amountStake)) + s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr3, amountDenom)) + s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr4, amountDenom)) + + s.Require().NoError(s.app.AirdropKeeper.AddFund(s.ctx, addr1, fund1)) + s.Require().NoError(s.app.AirdropKeeper.AddFund(s.ctx, addr2, fund2)) + s.Require().NoError(s.app.AirdropKeeper.AddFund(s.ctx, addr3, fund3)) + s.Require().NoError(s.app.AirdropKeeper.AddFund(s.ctx, addr4, fund4)) // check the balances + + moduleBalanceStake := s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, sdk.DefaultBondDenom) + moduleBalanceDenom := s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, "denom") + feeCollectorBalanceStake := s.app.BankKeeper.GetBalance(s.ctx, feeCollectorAddress, sdk.DefaultBondDenom) + feeCollectorBalanceDenom := s.app.BankKeeper.GetBalance(s.ctx, feeCollectorAddress, "denom") + + s.Require().Equal(moduleBalanceStake, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(2000))) + s.Require().Equal(moduleBalanceDenom, sdk.NewCoin("denom", sdk.NewInt(2000))) + s.Require().Equal(feeCollectorBalanceStake, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(0))) + s.Require().Equal(feeCollectorBalanceDenom, sdk.NewCoin("denom", sdk.NewInt(0))) + + _, err := s.app.AirdropKeeper.DripAllFunds(s.ctx) // test case - drip the funds + s.Require().NoError(err) // check that the fees have been transferred + + moduleBalanceStake = s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, sdk.DefaultBondDenom) + moduleBalanceDenom = s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, "denom") + feeCollectorBalanceStake = s.app.BankKeeper.GetBalance(s.ctx, feeCollectorAddress, sdk.DefaultBondDenom) + feeCollectorBalanceDenom = s.app.BankKeeper.GetBalance(s.ctx, feeCollectorAddress, "denom") + + s.Require().Equal(feeCollectorBalanceStake, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1300))) // 800 drip fund 1, 500 drip fund 2 + s.Require().Equal(feeCollectorBalanceDenom, sdk.NewCoin("denom", sdk.NewInt(400))) // 100 drip fund 3, 300 drip fund 4 + s.Require().Equal(moduleBalanceStake, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(700))) // remainder 500 from fund 1, remainder 200 from fund 2 + s.Require().Equal(moduleBalanceDenom, sdk.NewCoin("denom", sdk.NewInt(1600))) // remainder 900 from fund 3, remainder 700 from fund 4 + +} + func TestKeeperTestSuite(t *testing.T) { suite.Run(t, new(KeeperTestSuite)) } From 9b3f86e3f412934888f6a31474dab74618c00e48 Mon Sep 17 00:00:00 2001 From: Joe <85871158+Josumner@users.noreply.github.com> Date: Wed, 14 Jul 2021 16:10:29 +0100 Subject: [PATCH 2/2] test: intial GenesisState Validate test (#99) * test: intial GenesisState Validate test * Fix: fix basic first tests * Test: test Validate and NewGenesisState * Fix: let AllowList be empty * Tidy: goimports genesis_test.go * Refactor: increase test coverage * Refactor: remove unnecessary code and rename vars --- x/airdrop/keeper/params.go | 2 +- x/airdrop/types/genesis_test.go | 63 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 x/airdrop/types/genesis_test.go diff --git a/x/airdrop/keeper/params.go b/x/airdrop/keeper/params.go index c748d41074..0ccf156afd 100644 --- a/x/airdrop/keeper/params.go +++ b/x/airdrop/keeper/params.go @@ -7,7 +7,7 @@ import ( func (k Keeper) GetWhiteListClients(ctx sdk.Context) []string { var res []string - k.paramSpace.Get(ctx, types.KeyWhiteList, &res) + k.paramSpace.GetIfExists(ctx, types.KeyWhiteList, &res) return res } diff --git a/x/airdrop/types/genesis_test.go b/x/airdrop/types/genesis_test.go new file mode 100644 index 0000000000..901770b719 --- /dev/null +++ b/x/airdrop/types/genesis_test.go @@ -0,0 +1,63 @@ +package types_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/x/airdrop/types" + "github.com/stretchr/testify/require" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var ( + addr = sdk.AccAddress([]byte("addr________________")) + invalidAddr = sdk.AccAddress([]byte("\n\n\n\n\taddr________________\t\n\n\n\n\n")) +) + +func TestNewGenesisState(t *testing.T) { + p := types.NewParams() + expectedFunds := []types.ActiveFund{ + { + Sender: addr.String(), + Fund: &types.Fund{ + Amount: &sdk.Coin{Denom: "test", Amount: sdk.NewInt(10)}, + DripAmount: sdk.NewInt(1), + }, + }, + } + expectedState := &types.GenesisState{ + Params: p, + Funds: expectedFunds, + } + require.Equal(t, expectedState.GetFunds(), expectedFunds) + require.Equal(t, expectedState.Params, p) +} + +func TestValidateGenesisState(t *testing.T) { + p1 := types.Params{ + AllowList: []string{ + addr.String(), // valid address + }, + } + p2 := types.Params{ + AllowList: []string{ + invalidAddr.String(), // invalid address + }, + } + funds := []types.ActiveFund{ + { + Sender: addr.String(), + Fund: &types.Fund{ + Amount: &sdk.Coin{ + Denom: "test", + Amount: sdk.NewInt(10), + }, + DripAmount: sdk.NewInt(1), + }, + }, + } + gen1 := types.NewGenesisState(p1, funds) + gen2 := types.NewGenesisState(p2, funds) + require.NoError(t, gen1.Validate()) + require.Error(t, gen2.Validate()) +} \ No newline at end of file