From 028a34448eea816079acb422770e4c4cc9b9f5f4 Mon Sep 17 00:00:00 2001 From: Sean King Date: Wed, 16 Feb 2022 16:41:17 +0100 Subject: [PATCH 1/7] testing: adding multiple sender accounts for testing puproses --- testing/chain.go | 66 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/testing/chain.go b/testing/chain.go index 8dfe4b8fb27..de408dafb84 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -36,6 +36,11 @@ import ( "github.com/cosmos/ibc-go/v3/testing/simapp" ) +type SenderAccount struct { + SenderPrivKey cryptotypes.PrivKey + SenderAccount authtypes.AccountI +} + // TestChain is a testing struct that wraps a simapp with the last TM Header, the current ABCI // header and the validators of the TestChain. It also contains a field called ChainID. This // is the clientID that *other* chains use to refer to this TestChain. The SenderAccount @@ -59,6 +64,8 @@ type TestChain struct { // autogenerated sender private key SenderPrivKey cryptotypes.PrivKey SenderAccount authtypes.AccountI + + SenderAccounts []SenderAccount } // NewTestChain initializes a new TestChain instance with a single validator set using a @@ -84,18 +91,34 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain { valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator}) signers := []tmtypes.PrivValidator{privVal} - // generate genesis account - senderPrivKey := secp256k1.GenPrivKey() - acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0) - amount, ok := sdk.NewIntFromString("10000000000000000000") - require.True(t, ok) + genAccs := []authtypes.GenesisAccount{} + genBals := []banktypes.Balance{} + senderAccs := []SenderAccount{} + + // generate genesis accounts + for i := 0; i < 9; i++ { + senderPrivKey := secp256k1.GenPrivKey() + acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), uint64(i)) + amount, ok := sdk.NewIntFromString("10000000000000000000") + require.True(t, ok) + + balance := banktypes.Balance{ + Address: acc.GetAddress().String(), + Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amount)), + } + + genAccs = append(genAccs, acc) + genBals = append(genBals, balance) + + senderAcc := SenderAccount{ + SenderAccount: acc, + SenderPrivKey: senderPrivKey, + } - balance := banktypes.Balance{ - Address: acc.GetAddress().String(), - Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amount)), + senderAccs = append(senderAccs, senderAcc) } - app := SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, chainID, balance) + app := SetupWithGenesisValSet(t, valSet, genAccs, chainID, genBals...) // create current header and call begin block header := tmproto.Header{ @@ -108,18 +131,19 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain { // create an account to send transactions from chain := &TestChain{ - t: t, - Coordinator: coord, - ChainID: chainID, - App: app, - CurrentHeader: header, - QueryServer: app.GetIBCKeeper(), - TxConfig: txConfig, - Codec: app.AppCodec(), - Vals: valSet, - Signers: signers, - SenderPrivKey: senderPrivKey, - SenderAccount: acc, + t: t, + Coordinator: coord, + ChainID: chainID, + App: app, + CurrentHeader: header, + QueryServer: app.GetIBCKeeper(), + TxConfig: txConfig, + Codec: app.AppCodec(), + Vals: valSet, + Signers: signers, + SenderPrivKey: senderAccs[0].SenderPrivKey, + SenderAccount: senderAccs[0].SenderAccount, + SenderAccounts: senderAccs, } coord.CommitBlock(chain) From 8ba3325f4f2850649840b048bb32ac2bad7f08bf Mon Sep 17 00:00:00 2001 From: Aditya Date: Wed, 16 Feb 2022 17:39:50 +0100 Subject: [PATCH 2/7] fix genesis setup (#936) --- testing/app.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/testing/app.go b/testing/app.go index 487e0569ba2..3c0ba9b6f06 100644 --- a/testing/app.go +++ b/testing/app.go @@ -91,22 +91,23 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) } - // set validators and delegations - stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) - genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis) - totalSupply := sdk.NewCoins() - for _, b := range balances { - // add genesis acc tokens and delegated tokens to total supply - totalSupply = totalSupply.Add(b.Coins.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))...) - } // add bonded amount to bonded pool module account balances = append(balances, banktypes.Balance{ Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(), - Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)}, + Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt.Mul(sdk.NewInt(int64(len(valSet.Validators)))))}, }) + for _, b := range balances { + // add genesis acc tokens and delegated tokens to total supply + totalSupply = totalSupply.Add(b.Coins...) + } + + // set validators and delegations + stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) + genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis) + // update total supply bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}) genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) From d6dd06f7aea3d535c64a45ab93be25812ff7c591 Mon Sep 17 00:00:00 2001 From: Sean King Date: Wed, 16 Feb 2022 17:41:32 +0100 Subject: [PATCH 3/7] Update testing/chain.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> --- testing/chain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/chain.go b/testing/chain.go index de408dafb84..66c3b3a4bda 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -98,7 +98,7 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain { // generate genesis accounts for i := 0; i < 9; i++ { senderPrivKey := secp256k1.GenPrivKey() - acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), uint64(i)) + acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), 0) amount, ok := sdk.NewIntFromString("10000000000000000000") require.True(t, ok) From fd60d98c71bc94a6f257bc1b29526979944f94ff Mon Sep 17 00:00:00 2001 From: Sean King Date: Wed, 16 Feb 2022 17:47:11 +0100 Subject: [PATCH 4/7] refactor: code hygiene --- testing/chain.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/testing/chain.go b/testing/chain.go index 66c3b3a4bda..3d7c8a4a22c 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -36,6 +36,10 @@ import ( "github.com/cosmos/ibc-go/v3/testing/simapp" ) +const ( + MAX_ACCOUNTS = 9 +) + type SenderAccount struct { SenderPrivKey cryptotypes.PrivKey SenderAccount authtypes.AccountI @@ -96,7 +100,7 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain { senderAccs := []SenderAccount{} // generate genesis accounts - for i := 0; i < 9; i++ { + for i := 0; i < MAX_ACCOUNTS; i++ { senderPrivKey := secp256k1.GenPrivKey() acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), 0) amount, ok := sdk.NewIntFromString("10000000000000000000") From 0d8159512421a048dd9596fc14c4e15552be5914 Mon Sep 17 00:00:00 2001 From: Sean King Date: Wed, 16 Feb 2022 17:52:29 +0100 Subject: [PATCH 5/7] Update testing/chain.go Co-authored-by: Aditya --- testing/chain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/chain.go b/testing/chain.go index 3d7c8a4a22c..344915b5a15 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -37,7 +37,7 @@ import ( ) const ( - MAX_ACCOUNTS = 9 + MAX_ACCOUNTS = 10 ) type SenderAccount struct { From e39e6d02829dc0399b05cdd0ea4b0d919955c443 Mon Sep 17 00:00:00 2001 From: Sean King Date: Thu, 17 Feb 2022 16:06:02 +0100 Subject: [PATCH 6/7] fix: setting totalySupply to empty --- testing/app.go | 5 ----- testing/chain.go | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/testing/app.go b/testing/app.go index 3c0ba9b6f06..320bb0f00a7 100644 --- a/testing/app.go +++ b/testing/app.go @@ -99,11 +99,6 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt.Mul(sdk.NewInt(int64(len(valSet.Validators)))))}, }) - for _, b := range balances { - // add genesis acc tokens and delegated tokens to total supply - totalSupply = totalSupply.Add(b.Coins...) - } - // set validators and delegations stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis) diff --git a/testing/chain.go b/testing/chain.go index 344915b5a15..abfa02610d2 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -36,7 +36,7 @@ import ( "github.com/cosmos/ibc-go/v3/testing/simapp" ) -const ( +var ( MAX_ACCOUNTS = 10 ) From ac2e438a97981e3731da9741eb4725d44c0ed96d Mon Sep 17 00:00:00 2001 From: Sean King Date: Thu, 17 Feb 2022 16:15:31 +0100 Subject: [PATCH 7/7] nit: CamelCase not UPPERCASE --- testing/chain.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/chain.go b/testing/chain.go index abfa02610d2..86717501c6a 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -37,7 +37,7 @@ import ( ) var ( - MAX_ACCOUNTS = 10 + MaxAccounts = 10 ) type SenderAccount struct { @@ -100,7 +100,7 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain { senderAccs := []SenderAccount{} // generate genesis accounts - for i := 0; i < MAX_ACCOUNTS; i++ { + for i := 0; i < MaxAccounts; i++ { senderPrivKey := secp256k1.GenPrivKey() acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), 0) amount, ok := sdk.NewIntFromString("10000000000000000000")