Skip to content

Commit 34b9d82

Browse files
committed
This allows to setup a TestChain with more than one validators.
* Patch cosmos#918: - [x] Run a TestChain with more than one genesis accounts - [x] Run a TestChain with more than one validators - [x] Run multiple validators with same voting power - [ ] Update validator set of TestChains (elegant solution is still WIP) * Easy fix for the valset update is available in the issue
1 parent 12a60b1 commit 34b9d82

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

testing/app.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,21 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
9595
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
9696
genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis)
9797

98+
// compute bonded amount
99+
totalBondAmt := bondAmt.Mul(sdk.NewInt(int64(len(delegations))))
100+
98101
totalSupply := sdk.NewCoins()
99102
for _, b := range balances {
100103
// add genesis acc tokens and delegated tokens to total supply
101-
totalSupply = totalSupply.Add(b.Coins.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))...)
104+
totalSupply = totalSupply.Add(b.Coins...)
102105
}
106+
// add bonded amountto total supply
107+
totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, totalBondAmt))
103108

104109
// add bonded amount to bonded pool module account
105110
balances = append(balances, banktypes.Balance{
106111
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
107-
Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)},
112+
Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, totalBondAmt)},
108113
})
109114

110115
// update total supply

testing/chain.go

+25-9
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,31 @@ type TestChain struct {
6969
// Time management is handled by the Coordinator in order to ensure synchrony between chains.
7070
// Each update of any chain increments the block header time for all chains by 5 seconds.
7171
func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
72-
// generate validator private/public key
73-
privVal := mock.NewPV()
74-
pubKey, err := privVal.GetPubKey()
75-
require.NoError(t, err)
76-
77-
// create validator set with single validator
78-
validator := tmtypes.NewValidator(pubKey, 1)
79-
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})
80-
signers := []tmtypes.PrivValidator{privVal}
72+
73+
// generate validators private/public key
74+
var (
75+
validators []*tmtypes.Validator
76+
signersByAddress = make(map[string]tmtypes.PrivValidator, ValidatorsPerChain)
77+
)
78+
79+
for i := 0; i < ValidatorsPerChain; i++ {
80+
privVal := mock.NewPV()
81+
pubKey, err := privVal.GetPubKey()
82+
require.NoError(t, err)
83+
validators = append(validators, tmtypes.NewValidator(pubKey, 1))
84+
signersByAddress[pubKey.Address().String()] = privVal
85+
}
86+
87+
// construct validator set;
88+
// Note that the validators are sorted by voting power
89+
// or, if equal, by address lexical order
90+
valSet := tmtypes.NewValidatorSet(validators)
91+
92+
// create signers indexed by the valSet validators's order
93+
signers := []tmtypes.PrivValidator{}
94+
for _, val := range valSet.Validators {
95+
signers = append(signers, signersByAddress[val.PubKey.Address().String()])
96+
}
8197

8298
// generate genesis account
8399
senderPrivKey := secp256k1.GenPrivKey()

testing/chain_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ func TestCreateSortedSignerArray(t *testing.T) {
4545
actual = ibctesting.CreateSortedSignerArray(privVal2, privVal1, validator2, validator1)
4646
require.Equal(t, expected, actual)
4747
}
48+
49+
func TestValidatorsPerChain(t *testing.T) {
50+
ibctesting.ValidatorsPerChain = 2
51+
52+
coordinator := ibctesting.NewCoordinator(t, 2)
53+
chainA := coordinator.GetChain(ibctesting.GetChainID(1))
54+
chainB := coordinator.GetChain(ibctesting.GetChainID(2))
55+
require.Len(t, chainA.Vals.Validators, 2)
56+
require.Len(t, chainB.Vals.Validators, 2)
57+
}

testing/simapp/test_helpers.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ func SignAndDeliver(
263263
require.Nil(t, res)
264264
}
265265

266-
app.EndBlock(abci.RequestEndBlock{})
266+
valUpdates := app.EndBlock(abci.RequestEndBlock{})
267+
for _, v := range valUpdates.ValidatorUpdates {
268+
fmt.Printf("%#v\n", v.String())
269+
}
270+
267271
app.Commit()
268272

269273
return gInfo, res, err

testing/values.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ var (
5757
MockFailPacketData = mock.MockFailPacketData
5858
MockRecvCanaryCapabilityName = mock.MockRecvCanaryCapabilityName
5959

60-
prefix = commitmenttypes.NewMerklePrefix([]byte("ibc"))
60+
prefix = commitmenttypes.NewMerklePrefix([]byte("ibc"))
61+
ValidatorsPerChain = 1
6162
)
6263

6364
func GetMockRecvCanaryCapabilityName(packet channeltypes.Packet) string {

0 commit comments

Comments
 (0)