From 52dcee60c9d1c5df6002aa2c06a5cee47b7e8f9d Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Thu, 25 Apr 2024 09:32:30 +0300 Subject: [PATCH 1/5] tmp: e2e test requirements --- tests/reward/cli_test.go | 14 +++ tests/reward/grpc.go | 1 + tests/reward/suite.go | 204 ++++++++++++++++++++++++++++++++ testutil/network/network.go | 130 +++++++++++++++++++- testutil/simapp/test_helpers.go | 16 +++ 5 files changed, 362 insertions(+), 3 deletions(-) create mode 100644 tests/reward/cli_test.go create mode 100644 tests/reward/grpc.go create mode 100644 tests/reward/suite.go diff --git a/tests/reward/cli_test.go b/tests/reward/cli_test.go new file mode 100644 index 00000000..497bbde6 --- /dev/null +++ b/tests/reward/cli_test.go @@ -0,0 +1,14 @@ +package client + +import ( + "testing" + + "github.com/sge-network/sge/testutil/network" + "github.com/stretchr/testify/suite" +) + +func TestE2ETestSuite(t *testing.T) { + cfg := network.DefaultConfig() + cfg.NumValidators = 1 + suite.Run(t, NewE2ETestSuite(cfg)) +} diff --git a/tests/reward/grpc.go b/tests/reward/grpc.go new file mode 100644 index 00000000..da13c8ef --- /dev/null +++ b/tests/reward/grpc.go @@ -0,0 +1 @@ +package client diff --git a/tests/reward/suite.go b/tests/reward/suite.go new file mode 100644 index 00000000..5ff511fd --- /dev/null +++ b/tests/reward/suite.go @@ -0,0 +1,204 @@ +package client + +import ( + "fmt" + "time" + + sdkmath "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/client/flags" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/gogoproto/proto" + "github.com/golang-jwt/jwt" + "github.com/google/uuid" + "github.com/spf13/cast" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + + "github.com/sge-network/sge/testutil/network" + "github.com/sge-network/sge/testutil/simapp" + "github.com/sge-network/sge/x/reward/client/cli" + "github.com/sge-network/sge/x/reward/types" +) + +var ( + genesis types.GenesisState + promoterUID = uuid.NewString() + campaignStartDate = uint64(time.Now().Unix()) + campaignEndDate = uint64(time.Now().Add(5 * time.Minute).Unix()) +) + +type E2ETestSuite struct { + suite.Suite + + cfg network.Config + network *network.Network +} + +func NewE2ETestSuite(cfg network.Config) *E2ETestSuite { + return &E2ETestSuite{cfg: cfg} +} + +func (s *E2ETestSuite) SetupSuite() { + s.T().Log("setting up e2e test suite") + + genesisState := s.cfg.GenesisState + s.Require().NoError(s.cfg.Codec.UnmarshalJSON(genesisState[types.ModuleName], &genesis)) + + // promoterAddr = simapp.TestParamUsers["user1"].Address.String() + + // genesis.PromoterList = []types.Promoter{ + // { + // Creator: promoterAddr, + // UID: uuid.NewString(), + // Addresses: []string{promoterAddr}, + // Conf: types.PromoterConf{ + // CategoryCap: []types.CategoryCap{{ + // Category: types.RewardCategory_REWARD_CATEGORY_SIGNUP, + // CapPerAcc: 1, + // }}, + // }, + // }, + // } + + // genesis.PromoterByAddressList = []types.PromoterByAddress{ + // { + // PromoterUID: genesis.PromoterList[0].UID, + // Address: promoterAddr, + // }, + // } + + // genesis.CampaignList = []types.Campaign{ + // { + // Creator: promoterAddr, + // UID: uuid.NewString(), + // Promoter: promoterAddr, + // StartTS: campaignStartDate, + // EndTS: campaignEndDate, + // RewardCategory: types.RewardCategory_REWARD_CATEGORY_SIGNUP, + // RewardType: types.RewardType_REWARD_TYPE_SIGNUP, + // RewardAmountType: types.RewardAmountType_REWARD_AMOUNT_TYPE_FIXED, + // RewardAmount: &types.RewardAmount{ + // SubaccountAmount: sdkmath.NewInt(100), + // }, + // Pool: , + // }, + // } + + genesisBz, err := s.cfg.Codec.MarshalJSON(&genesis) + s.Require().NoError(err) + genesisState[types.ModuleName] = genesisBz + s.cfg.GenesisState = genesisState + + s.network = network.New(s.T(), s.cfg) + s.Require().NoError(s.network.WaitForNextBlock()) +} + +// func (s *E2ETestSuite) TearDownSuite() { +// s.T().Log("tearing down e2e test suite") +// s.network.Cleanup() +// } + +func (s *E2ETestSuite) TestNewCampaignTxCmd() { + val := s.network.Validators[0] + + { + clientCtx := val.ClientCtx + + ticket, err := simapp.CreateJwtTicket(jwt.MapClaims{ + "exp": time.Now().Add(time.Minute * 5).Unix(), + "iat": time.Now().Unix(), + "uid": promoterUID, + "conf": types.PromoterConf{ + CategoryCap: []types.CategoryCap{{ + Category: types.RewardCategory_REWARD_CATEGORY_SIGNUP, + CapPerAcc: 1, + }}, + }, + }) + require.Nil(s.T(), err) + + args := []string{ + ticket, + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdkmath.NewInt(10))).String()), + } + bz, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdCreatePromoter(), args) + s.Require().NoError(err) + respType := sdk.TxResponse{} + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), &respType), bz.String()) + s.Require().Equal(uint32(0), respType.Code) + } + + testCases := []struct { + name string + uid string + totalFunds sdkmath.Int + ticketClaims jwt.MapClaims + args []string + expectErr bool + expectedCode uint32 + respType proto.Message + }{ + { + "valid transaction", + uuid.NewString(), + sdkmath.NewInt(100), + jwt.MapClaims{ + "exp": time.Now().Add(time.Minute * 5).Unix(), + "iat": time.Now().Unix(), + "promoter": val.Address, + "start_ts": cast.ToString(campaignStartDate), + "end_ts": cast.ToString(campaignEndDate), + "category": types.RewardCategory_REWARD_CATEGORY_SIGNUP, + "reward_type": types.RewardType_REWARD_TYPE_SIGNUP, + "reward_amount_type": types.RewardAmountType_REWARD_AMOUNT_TYPE_FIXED, + "reward_amount": types.RewardAmount{ + SubaccountAmount: sdkmath.NewInt(100), + UnlockPeriod: uint64(1000), + }, + "is_active": true, + "meta": "sample signup campaign", + "cap_count": 1, + }, + []string{ + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdkmath.NewInt(10))).String()), + }, + false, 0, &sdk.TxResponse{}, + }, + } + + for _, tc := range testCases { + tc := tc + + s.Require().NoError(s.network.WaitForNextBlock()) + s.Run(tc.name, func() { + clientCtx := val.ClientCtx + + ticket, err := simapp.CreateJwtTicket(tc.ticketClaims) + require.Nil(s.T(), err) + + tc.args = append([]string{ + tc.uid, + tc.totalFunds.String(), + ticket, + }, tc.args...) + bz, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdCreateCampaign(), tc.args) + + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), tc.respType), bz.String()) + txResp := tc.respType.(*sdk.TxResponse) + s.Require().Equal(tc.expectedCode, txResp.Code) + } + }) + } +} diff --git a/testutil/network/network.go b/testutil/network/network.go index 291f0317..3524a46c 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -16,8 +16,12 @@ import ( simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/sge-network/sge/app" + "github.com/sge-network/sge/app/params" + "github.com/sge-network/sge/testutil/simapp" + ovmtypes "github.com/sge-network/sge/x/ovm/types" ) type ( @@ -39,6 +43,39 @@ func New(t *testing.T, configs ...network.Config) *network.Network { } else { cfg = configs[0] } + // baseDir := t.TempDir() + + // nodeDirName := fmt.Sprintf("node%d", 0) + // node0Dir := filepath.Join(baseDir, nodeDirName, "simcli") + + // buf := bufio.NewReader(os.Stdin) + // kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, node0Dir, buf, cfg.Codec, cfg.KeyringOptions...) + // if err != nil { + // panic(err) + // } + + // keyringAlgos, _ := kb.SupportedAlgorithms() + // algo, err := keyring.NewSigningAlgoFromString(cfg.SigningAlgo, keyringAlgos) + // if err != nil { + // panic(err) + // } + + // addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, "", true, algo) + // if err != nil { + // panic(err) + // } + + // info := map[string]string{"secret": secret} + // infoBz, err := json.Marshal(info) + // if err != nil { + // panic(err) + // } + + // // save private key seed words + // err = simapp.WriteKeyringFile(fmt.Sprintf("%v.json", "key_seed"), node0Dir, infoBz) + // if err != nil { + // panic(err) + // } net, err := network.New(t, t.TempDir(), cfg) if err != nil { @@ -56,6 +93,93 @@ func DefaultConfig() network.Config { encoding = app.MakeEncodingConfig() chainID = "chain-" + tmrand.NewRand().Str(6) ) + + // simapp.GenerateSimappUsers() + + // Initialize test app by genesis account + // genAccs := simapp.GenerateSimappGenesisAccounts() + + // Create testapp instance + // balances := simapp.GenerateSimappUserBalances() + + defGen := app.ModuleBasics.DefaultGenesis(encoding.Marshaler) + { + // modify the staking denom in the genesis + stakingGenState := defGen[stakingtypes.ModuleName] + var newStakingGenState stakingtypes.GenesisState + + if err := encoding.Marshaler.UnmarshalJSON(stakingGenState, &newStakingGenState); err != nil { + panic(err) + } + + // change to default bond denom + newStakingGenState.Params.BondDenom = params.DefaultBondDenom + + var err error + defGen[stakingtypes.ModuleName], err = encoding.Marshaler.MarshalJSON(&newStakingGenState) + if err != nil { + panic(err) + } + } + // { + // authGenState := defGen[authtypes.ModuleName] + // var newAuthGenState authtypes.GenesisState + + // if err := encoding.Marshaler.UnmarshalJSON(authGenState, &newAuthGenState); err != nil { + // panic(err) + // } + + // for _, v := range genAccs { + // anyVal, err := codectypes.NewAnyWithValue(v) + // if err != nil { + // panic(err) + // } + // newAuthGenState.Accounts = append(newAuthGenState.Accounts, anyVal) + // } + + // var err error + // defGen[authtypes.ModuleName], err = encoding.Marshaler.MarshalJSON(&newAuthGenState) + // if err != nil { + // panic(err) + // } + // } + // { + // bankGenState := defGen[banktypes.ModuleName] + // var newBankGenState banktypes.GenesisState + + // if err := encoding.Marshaler.UnmarshalJSON(bankGenState, &newBankGenState); err != nil { + // panic(err) + // } + + // newBankGenState.Balances = append(newBankGenState.Balances, balances...) + + // // totalSupply := sdk.NewCoins() + // // for _, b := range balances { + // // totalSupply = totalSupply.Add(b.Coins...) + // // } + // // newBankGenState.Supply = totalSupply + + // var err error + // defGen[banktypes.ModuleName], err = encoding.Marshaler.MarshalJSON(&newBankGenState) + // if err != nil { + // panic(err) + // } + + // // stateBytes, err := json.MarshalIndent(genesisState, "", " ") + // // if err != nil { + // // panic(err) + // // } + // } + + { + ovmGenesisState := &ovmtypes.GenesisState{ + KeyVault: ovmtypes.KeyVault{ + PublicKeys: simapp.GenerateOvmPublicKeys(ovmtypes.MinPubKeysCount), + }, + } + defGen[ovmtypes.ModuleName] = encoding.Marshaler.MustMarshalJSON(ovmGenesisState) + } + return network.Config{ Codec: encoding.Marshaler, TxConfig: encoding.TxConfig, @@ -78,12 +202,12 @@ func DefaultConfig() network.Config { baseapp.SetChainID(chainID), ) }, - GenesisState: app.ModuleBasics.DefaultGenesis(encoding.Marshaler), + GenesisState: defGen, TimeoutCommit: 2 * time.Second, ChainID: chainID, NumValidators: 1, - BondDenom: sdk.DefaultBondDenom, - MinGasPrices: fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom), + BondDenom: params.DefaultBondDenom, + MinGasPrices: fmt.Sprintf("0.000006%s", params.DefaultBondDenom), AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction), StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction), BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction), diff --git a/testutil/simapp/test_helpers.go b/testutil/simapp/test_helpers.go index c2a6fd8e..b15bdd86 100644 --- a/testutil/simapp/test_helpers.go +++ b/testutil/simapp/test_helpers.go @@ -7,6 +7,8 @@ import ( "encoding/hex" "fmt" "math" + "os" + "path/filepath" "github.com/golang-jwt/jwt" "github.com/spf13/cast" @@ -111,3 +113,17 @@ func RandomString(length int) string { str := base64.RawURLEncoding.EncodeToString(buff) return str[:length] // strip 1 extra character we get from odd length results } + +func WriteKeyringFile(name string, dir string, contents []byte) error { + file := filepath.Join(dir, name) + + if err := os.MkdirAll(dir, 0o755); err != nil { + return fmt.Errorf("could not create directory %q: %w", dir, err) + } + + if err := os.WriteFile(file, contents, 0o644); err != nil { //nolint: gosec + return err + } + + return nil +} From 1750b25056e10e4392b2b78f4076a6ef3ad267fa Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Thu, 25 Apr 2024 22:06:23 +0300 Subject: [PATCH 2/5] test: add balance check to e2e --- tests/reward/suite.go | 85 ++++++++++++++++++++++++++++++++++--------- tests/sdk/balance.go | 31 ++++++++++++++++ 2 files changed, 98 insertions(+), 18 deletions(-) create mode 100644 tests/sdk/balance.go diff --git a/tests/reward/suite.go b/tests/reward/suite.go index 5ff511fd..7e9c2d49 100644 --- a/tests/reward/suite.go +++ b/tests/reward/suite.go @@ -11,10 +11,10 @@ import ( "github.com/cosmos/gogoproto/proto" "github.com/golang-jwt/jwt" "github.com/google/uuid" - "github.com/spf13/cast" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + sdkcli "github.com/sge-network/sge/tests/sdk" "github.com/sge-network/sge/testutil/network" "github.com/sge-network/sge/testutil/simapp" "github.com/sge-network/sge/x/reward/client/cli" @@ -23,9 +23,11 @@ import ( var ( genesis types.GenesisState + fees = int64(10) promoterUID = uuid.NewString() campaignStartDate = uint64(time.Now().Unix()) - campaignEndDate = uint64(time.Now().Add(5 * time.Minute).Unix()) + campaignEndDate = time.Now().Add(time.Minute * time.Duration(120)).Unix() + campaignFunds = sdkmath.NewInt(100000000) ) type E2ETestSuite struct { @@ -101,10 +103,8 @@ func (s *E2ETestSuite) SetupSuite() { func (s *E2ETestSuite) TestNewCampaignTxCmd() { val := s.network.Validators[0] - + clientCtx := val.ClientCtx { - clientCtx := val.ClientCtx - ticket, err := simapp.CreateJwtTicket(jwt.MapClaims{ "exp": time.Now().Add(time.Minute * 5).Unix(), "iat": time.Now().Unix(), @@ -132,26 +132,62 @@ func (s *E2ETestSuite) TestNewCampaignTxCmd() { s.Require().Equal(uint32(0), respType.Code) } + valBalance := sdkcli.GetSGEBalance(clientCtx, val.Address.String()) + s.Require().Equal(sdkmath.NewInt(400000000), valBalance) + testCases := []struct { - name string - uid string - totalFunds sdkmath.Int - ticketClaims jwt.MapClaims - args []string - expectErr bool - expectedCode uint32 - respType proto.Message + name string + uid string + totalFunds sdkmath.Int + ticketClaims jwt.MapClaims + args []string + expectErr bool + expectedCode uint32 + expectedErrMsg string + respType proto.Message }{ + { + "not enough balance to charge pool", + uuid.NewString(), + sdkmath.NewInt(4000000000), + jwt.MapClaims{ + "exp": time.Now().Add(time.Minute * 5).Unix(), + "iat": time.Now().Unix(), + "promoter": val.Address, + "start_ts": campaignStartDate, + "end_ts": campaignEndDate, + "category": types.RewardCategory_REWARD_CATEGORY_SIGNUP, + "reward_type": types.RewardType_REWARD_TYPE_SIGNUP, + "reward_amount_type": types.RewardAmountType_REWARD_AMOUNT_TYPE_FIXED, + "reward_amount": types.RewardAmount{ + SubaccountAmount: sdkmath.NewInt(100), + UnlockPeriod: uint64(1000), + }, + "is_active": true, + "meta": "sample signup campaign", + "cap_count": 1, + }, + []string{ + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdkmath.NewInt(10))).String()), + }, + false, + types.ErrInFundingCampaignPool.ABCICode(), + "", + &sdk.TxResponse{}, + }, { "valid transaction", uuid.NewString(), - sdkmath.NewInt(100), + campaignFunds, jwt.MapClaims{ "exp": time.Now().Add(time.Minute * 5).Unix(), "iat": time.Now().Unix(), "promoter": val.Address, - "start_ts": cast.ToString(campaignStartDate), - "end_ts": cast.ToString(campaignEndDate), + "start_ts": campaignStartDate, + "end_ts": campaignEndDate, "category": types.RewardCategory_REWARD_CATEGORY_SIGNUP, "reward_type": types.RewardType_REWARD_TYPE_SIGNUP, "reward_amount_type": types.RewardAmountType_REWARD_AMOUNT_TYPE_FIXED, @@ -169,7 +205,10 @@ func (s *E2ETestSuite) TestNewCampaignTxCmd() { fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdkmath.NewInt(10))).String()), }, - false, 0, &sdk.TxResponse{}, + false, + 0, + "", + &sdk.TxResponse{}, }, } @@ -196,9 +235,19 @@ func (s *E2ETestSuite) TestNewCampaignTxCmd() { s.Require().NoError(err) s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), tc.respType), bz.String()) - txResp := tc.respType.(*sdk.TxResponse) + + txResp, err := clitestutil.GetTxResponse(s.network, clientCtx, tc.respType.(*sdk.TxResponse).TxHash) + s.Require().NoError(err) s.Require().Equal(tc.expectedCode, txResp.Code) + if tc.expectedErrMsg != "" { + s.Require().Contains(txResp.RawLog, tc.expectedErrMsg) + } } }) } + + s.Require().NoError(s.network.WaitForNextBlock()) + expectedBalance := valBalance.Sub(campaignFunds).Sub(sdk.NewInt(fees * 3)) + valBalance = sdkcli.GetSGEBalance(clientCtx, val.Address.String()) + s.Require().Equal(expectedBalance, valBalance) } diff --git a/tests/sdk/balance.go b/tests/sdk/balance.go new file mode 100644 index 00000000..4efc8463 --- /dev/null +++ b/tests/sdk/balance.go @@ -0,0 +1,31 @@ +package sdk + +import ( + "fmt" + + sdkmath "cosmossdk.io/math" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + sdk "github.com/cosmos/cosmos-sdk/types" + bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/cli" +) + +func GetSGEBalance(clientCtx client.Context, address string) sdkmath.Int { + args := []string{ + address, + fmt.Sprintf("--%s=json", flags.FlagOutput), + fmt.Sprintf("--%s=usge", bankcli.FlagDenom), + } + bz, err := clitestutil.ExecTestCLICmd(clientCtx, bankcli.GetBalancesCmd(), args) + if err != nil { + panic(err) + } + respType := sdk.Coin{} + err = clientCtx.Codec.UnmarshalJSON(bz.Bytes(), &respType) + if err != nil { + panic(err) + } + + return respType.Amount +} From a85f03499bd3079312930f8ae9a23a96f8d34f33 Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Fri, 26 Apr 2024 16:04:30 +0300 Subject: [PATCH 3/5] test: e2e grant signup reward and balances --- tests/reward/suite.go | 122 ++++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 47 deletions(-) diff --git a/tests/reward/suite.go b/tests/reward/suite.go index 7e9c2d49..ae2c1cf4 100644 --- a/tests/reward/suite.go +++ b/tests/reward/suite.go @@ -17,8 +17,11 @@ import ( sdkcli "github.com/sge-network/sge/tests/sdk" "github.com/sge-network/sge/testutil/network" "github.com/sge-network/sge/testutil/simapp" + sgetypes "github.com/sge-network/sge/types" "github.com/sge-network/sge/x/reward/client/cli" "github.com/sge-network/sge/x/reward/types" + subaccountcli "github.com/sge-network/sge/x/subaccount/client/cli" + subaccounttypes "github.com/sge-network/sge/x/subaccount/types" ) var ( @@ -47,46 +50,6 @@ func (s *E2ETestSuite) SetupSuite() { genesisState := s.cfg.GenesisState s.Require().NoError(s.cfg.Codec.UnmarshalJSON(genesisState[types.ModuleName], &genesis)) - // promoterAddr = simapp.TestParamUsers["user1"].Address.String() - - // genesis.PromoterList = []types.Promoter{ - // { - // Creator: promoterAddr, - // UID: uuid.NewString(), - // Addresses: []string{promoterAddr}, - // Conf: types.PromoterConf{ - // CategoryCap: []types.CategoryCap{{ - // Category: types.RewardCategory_REWARD_CATEGORY_SIGNUP, - // CapPerAcc: 1, - // }}, - // }, - // }, - // } - - // genesis.PromoterByAddressList = []types.PromoterByAddress{ - // { - // PromoterUID: genesis.PromoterList[0].UID, - // Address: promoterAddr, - // }, - // } - - // genesis.CampaignList = []types.Campaign{ - // { - // Creator: promoterAddr, - // UID: uuid.NewString(), - // Promoter: promoterAddr, - // StartTS: campaignStartDate, - // EndTS: campaignEndDate, - // RewardCategory: types.RewardCategory_REWARD_CATEGORY_SIGNUP, - // RewardType: types.RewardType_REWARD_TYPE_SIGNUP, - // RewardAmountType: types.RewardAmountType_REWARD_AMOUNT_TYPE_FIXED, - // RewardAmount: &types.RewardAmount{ - // SubaccountAmount: sdkmath.NewInt(100), - // }, - // Pool: , - // }, - // } - genesisBz, err := s.cfg.Codec.MarshalJSON(&genesis) s.Require().NoError(err) genesisState[types.ModuleName] = genesisBz @@ -96,13 +59,10 @@ func (s *E2ETestSuite) SetupSuite() { s.Require().NoError(s.network.WaitForNextBlock()) } -// func (s *E2ETestSuite) TearDownSuite() { -// s.T().Log("tearing down e2e test suite") -// s.network.Cleanup() -// } - func (s *E2ETestSuite) TestNewCampaignTxCmd() { + s.T().Log("==== new campaign create command test started") val := s.network.Validators[0] + clientCtx := val.ClientCtx { ticket, err := simapp.CreateJwtTicket(jwt.MapClaims{ @@ -135,6 +95,7 @@ func (s *E2ETestSuite) TestNewCampaignTxCmd() { valBalance := sdkcli.GetSGEBalance(clientCtx, val.Address.String()) s.Require().Equal(sdkmath.NewInt(400000000), valBalance) + campaignUID := uuid.NewString() testCases := []struct { name string uid string @@ -148,7 +109,7 @@ func (s *E2ETestSuite) TestNewCampaignTxCmd() { }{ { "not enough balance to charge pool", - uuid.NewString(), + campaignUID, sdkmath.NewInt(4000000000), jwt.MapClaims{ "exp": time.Now().Add(time.Minute * 5).Unix(), @@ -180,7 +141,7 @@ func (s *E2ETestSuite) TestNewCampaignTxCmd() { }, { "valid transaction", - uuid.NewString(), + campaignUID, campaignFunds, jwt.MapClaims{ "exp": time.Now().Add(time.Minute * 5).Unix(), @@ -231,6 +192,7 @@ func (s *E2ETestSuite) TestNewCampaignTxCmd() { if tc.expectErr { s.Require().Error(err) + s.T().Logf("error captured: %s", tc.name) } else { s.Require().NoError(err) @@ -242,6 +204,7 @@ func (s *E2ETestSuite) TestNewCampaignTxCmd() { if tc.expectedErrMsg != "" { s.Require().Contains(txResp.RawLog, tc.expectedErrMsg) } + s.T().Logf("==== success campaign create test case finished: %s", tc.name) } }) } @@ -250,4 +213,69 @@ func (s *E2ETestSuite) TestNewCampaignTxCmd() { expectedBalance := valBalance.Sub(campaignFunds).Sub(sdk.NewInt(fees * 3)) valBalance = sdkcli.GetSGEBalance(clientCtx, val.Address.String()) s.Require().Equal(expectedBalance, valBalance) + s.T().Logf("==== bank balance checked after creating the campaign: %s", valBalance) + + { + ticket, err := simapp.CreateJwtTicket(jwt.MapClaims{ + "exp": time.Now().Add(time.Minute * 5).Unix(), + "iat": time.Now().Unix(), + "common": types.RewardPayloadCommon{ + Receiver: val.Address.String(), + SourceUID: "", + Meta: "signup reward campaign", + KycData: &sgetypes.KycDataPayload{ + Ignore: false, + Approved: true, + ID: val.Address.String(), + }, + }, + }) + require.Nil(s.T(), err) + + rewardUID := uuid.NewString() + args := []string{ + rewardUID, + campaignUID, + ticket, + fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdkmath.NewInt(10))).String()), + } + bz, err := clitestutil.ExecTestCLICmd(clientCtx, cli.CmdGrantReward(), args) + s.Require().NoError(err) + respType := sdk.TxResponse{} + + s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bz.Bytes(), &respType), bz.String()) + + txResp, err := clitestutil.GetTxResponse(s.network, clientCtx, respType.TxHash) + s.Require().NoError(err) + s.Require().Equal(uint32(0), txResp.Code) + s.T().Logf("==== success reward grant test case finished: %s", rewardUID) + } + + valBalance = sdkcli.GetSGEBalance(clientCtx, val.Address.String()) + expectedBalance = expectedBalance.Sub(sdkmath.NewInt(10)) + s.Require().Equal(expectedBalance, valBalance) + s.T().Logf("==== bank balance checked after creating the campaign: %s", valBalance) + + args := []string{ + val.Address.String(), + fmt.Sprintf("--%s=json", flags.FlagOutput), + } + bz, err := clitestutil.ExecTestCLICmd(clientCtx, subaccountcli.QuerySubaccount(), args) + if err != nil { + panic(err) + } + respType := subaccounttypes.QuerySubaccountResponse{} + err = clientCtx.Codec.UnmarshalJSON(bz.Bytes(), &respType) + if err != nil { + panic(err) + } + + subaccountBalance := sdkcli.GetSGEBalance(clientCtx, respType.Address) + s.Require().Equal(sdkmath.NewInt(100), subaccountBalance) + s.T().Logf("==== bank balance checked after creating the campaign: %s", subaccountBalance) + + s.T().Log("==== new campaign create command test passed successfully") } From d6f4170f7510b2e7a7546017360b31b3c43f33ab Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Tue, 30 Apr 2024 11:39:05 +0300 Subject: [PATCH 4/5] test: fix minter provision after network test integration --- x/mint/client/cli/query_minter_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/mint/client/cli/query_minter_test.go b/x/mint/client/cli/query_minter_test.go index 2da04737..841da800 100644 --- a/x/mint/client/cli/query_minter_test.go +++ b/x/mint/client/cli/query_minter_test.go @@ -131,7 +131,7 @@ func TestQueryPhaseProvision(t *testing.T) { } else { require.NoError(t, err) phaseProvision := strings.Replace(string(res.Bytes()), "\n", "", 1) - require.Equal(t, "0.000000000000000000", phaseProvision) + require.Equal(t, "57446808.510638297750000000", phaseProvision) } }) } From 9353a1b0a743dfc47a6b43923960384e5a540327 Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Tue, 30 Apr 2024 11:42:44 +0300 Subject: [PATCH 5/5] test: rearange e2e test dirs --- tests/{ => e2e}/reward/cli_test.go | 8 +++++--- tests/{ => e2e}/reward/grpc.go | 0 tests/{ => e2e}/reward/suite.go | 2 +- tests/{ => e2e}/sdk/balance.go | 0 4 files changed, 6 insertions(+), 4 deletions(-) rename tests/{ => e2e}/reward/cli_test.go (63%) rename tests/{ => e2e}/reward/grpc.go (100%) rename tests/{ => e2e}/reward/suite.go (99%) rename tests/{ => e2e}/sdk/balance.go (100%) diff --git a/tests/reward/cli_test.go b/tests/e2e/reward/cli_test.go similarity index 63% rename from tests/reward/cli_test.go rename to tests/e2e/reward/cli_test.go index 497bbde6..06d65961 100644 --- a/tests/reward/cli_test.go +++ b/tests/e2e/reward/cli_test.go @@ -1,14 +1,16 @@ -package client +package client_test import ( "testing" - "github.com/sge-network/sge/testutil/network" "github.com/stretchr/testify/suite" + + client "github.com/sge-network/sge/tests/e2e/reward" + "github.com/sge-network/sge/testutil/network" ) func TestE2ETestSuite(t *testing.T) { cfg := network.DefaultConfig() cfg.NumValidators = 1 - suite.Run(t, NewE2ETestSuite(cfg)) + suite.Run(t, client.NewE2ETestSuite(cfg)) } diff --git a/tests/reward/grpc.go b/tests/e2e/reward/grpc.go similarity index 100% rename from tests/reward/grpc.go rename to tests/e2e/reward/grpc.go diff --git a/tests/reward/suite.go b/tests/e2e/reward/suite.go similarity index 99% rename from tests/reward/suite.go rename to tests/e2e/reward/suite.go index ae2c1cf4..31057b0c 100644 --- a/tests/reward/suite.go +++ b/tests/e2e/reward/suite.go @@ -14,7 +14,7 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - sdkcli "github.com/sge-network/sge/tests/sdk" + sdkcli "github.com/sge-network/sge/tests/e2e/sdk" "github.com/sge-network/sge/testutil/network" "github.com/sge-network/sge/testutil/simapp" sgetypes "github.com/sge-network/sge/types" diff --git a/tests/sdk/balance.go b/tests/e2e/sdk/balance.go similarity index 100% rename from tests/sdk/balance.go rename to tests/e2e/sdk/balance.go