Skip to content

Commit 04b4b26

Browse files
AdityaSripalmergify-bot
authored and
mergify-bot
committed
Create test chain with multiple validators (#942)
* testing: adding multiple sender accounts for testing puproses * fix genesis setup (#936) * Update testing/chain.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * refactor: code hygiene * Update testing/chain.go Co-authored-by: Aditya <adityasripal@gmail.com> * multi validator commit taken from @Saione * add function to pass custom valset * add changelog Co-authored-by: Sean King <sean@seking.dev> Co-authored-by: Sean King <seantking@users.noreply.github.com> Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> (cherry picked from commit 98f4d3a) # Conflicts: # testing/chain.go
1 parent 8c17224 commit 04b4b26

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
6363

6464
### Improvements
6565

66+
* (testing) [\#942](https://github.com/cosmos/ibc-go/pull/942) `NewTestChain` will create 4 validators in validator set by default. A new constructor function `NewTestChainWithValSet` is provided for test writers who want custom control over the validator set of test chains.
6667
* (testing) [\#904](https://github.com/cosmos/ibc-go/pull/904) Add `ParsePacketFromEvents` function to the testing package. Useful when sending/relaying packets via the testing package.
6768
* (testing) [\#893](https://github.com/cosmos/ibc-go/pull/893) Support custom private keys for testing.
6869
* (testing) [\#810](https://github.com/cosmos/ibc-go/pull/810) Additional testing function added to `Endpoint` type called `RecvPacketWithResult`. Performs the same functionality as the existing `RecvPacket` function but also returns the message result. `path.RelayPacket` no longer uses the provided acknowledgement argument and instead obtains the acknowledgement via MsgRecvPacket events.

testing/chain.go

+46-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ type TestChain struct {
6161
SenderAccount authtypes.AccountI
6262
}
6363

64-
// NewTestChain initializes a new TestChain instance with a single validator set using a
65-
// generated secp256k1 Tendermint private key. It also creates a sender BaseAccount to be used for
66-
// delivering transactions.
64+
// NewTestChainWithValSet initializes a new TestChain instance with the given validator set
65+
// and signer array. It also initializes 10 Sender accounts with a balance of 10000000000000000000 coins of
66+
// bond denom to use for tests.
6767
//
6868
// The first block height is committed to state in order to allow for client creations on
6969
// counterparty chains. The TestChain will return with a block height starting at 2.
@@ -73,6 +73,7 @@ type TestChain struct {
7373
//
7474
// NOTE: to use a custom sender privkey and account for testing purposes, replace and modify this
7575
// constructor function.
76+
<<<<<<< HEAD
7677
func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
7778
// generate validator private/public key
7879
privVal := mock.NewPV()
@@ -89,6 +90,16 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
8990
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
9091
amount, ok := sdk.NewIntFromString("10000000000000000000")
9192
require.True(t, ok)
93+
=======
94+
//
95+
// CONTRACT: Validator and signer array must be provided in the order expected by Tendermint.
96+
// i.e. sorted first by power and then lexicographically by address.
97+
func NewTestChainWithValSet(t *testing.T, coord *Coordinator, chainID string, valSet *tmtypes.ValidatorSet, signers []tmtypes.PrivValidator) *TestChain {
98+
99+
genAccs := []authtypes.GenesisAccount{}
100+
genBals := []banktypes.Balance{}
101+
senderAccs := []SenderAccount{}
102+
>>>>>>> 98f4d3a (Create test chain with multiple validators (#942))
92103

93104
balance := banktypes.Balance{
94105
Address: acc.GetAddress().String(),
@@ -127,6 +138,38 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
127138
return chain
128139
}
129140

141+
// NewTestChain initializes a new test chain with a default of 4 validators
142+
// Use this function if the tests do not need custom control over the validator set
143+
func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
144+
// generate validators private/public key
145+
var (
146+
validatorsPerChain = 4
147+
validators []*tmtypes.Validator
148+
signersByAddress = make(map[string]tmtypes.PrivValidator, validatorsPerChain)
149+
)
150+
151+
for i := 0; i < validatorsPerChain; i++ {
152+
privVal := mock.NewPV()
153+
pubKey, err := privVal.GetPubKey()
154+
require.NoError(t, err)
155+
validators = append(validators, tmtypes.NewValidator(pubKey, 1))
156+
signersByAddress[pubKey.Address().String()] = privVal
157+
}
158+
159+
// construct validator set;
160+
// Note that the validators are sorted by voting power
161+
// or, if equal, by address lexical order
162+
valSet := tmtypes.NewValidatorSet(validators)
163+
164+
// create signers indexed by the valSet validators's order
165+
signers := []tmtypes.PrivValidator{}
166+
for _, val := range valSet.Validators {
167+
signers = append(signers, signersByAddress[val.PubKey.Address().String()])
168+
}
169+
170+
return NewTestChainWithValSet(t, coord, chainID, valSet, signers)
171+
}
172+
130173
// GetContext returns the current context for the application.
131174
func (chain *TestChain) GetContext() sdk.Context {
132175
return chain.App.GetBaseApp().NewContext(false, chain.CurrentHeader)

0 commit comments

Comments
 (0)