Skip to content

Commit

Permalink
Create test chain with multiple validators (#942)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AdityaSripal authored and mergify-bot committed Feb 21, 2022
1 parent 8c17224 commit 04b4b26
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (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.
* (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.
* (testing) [\#893](https://github.com/cosmos/ibc-go/pull/893) Support custom private keys for testing.
* (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.
Expand Down
49 changes: 46 additions & 3 deletions testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ type TestChain struct {
SenderAccount authtypes.AccountI
}

// NewTestChain initializes a new TestChain instance with a single validator set using a
// generated secp256k1 Tendermint private key. It also creates a sender BaseAccount to be used for
// delivering transactions.
// NewTestChainWithValSet initializes a new TestChain instance with the given validator set
// and signer array. It also initializes 10 Sender accounts with a balance of 10000000000000000000 coins of
// bond denom to use for tests.
//
// The first block height is committed to state in order to allow for client creations on
// counterparty chains. The TestChain will return with a block height starting at 2.
Expand All @@ -73,6 +73,7 @@ type TestChain struct {
//
// NOTE: to use a custom sender privkey and account for testing purposes, replace and modify this
// constructor function.
<<<<<<< HEAD
func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
// generate validator private/public key
privVal := mock.NewPV()
Expand All @@ -89,6 +90,16 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
amount, ok := sdk.NewIntFromString("10000000000000000000")
require.True(t, ok)
=======
//
// CONTRACT: Validator and signer array must be provided in the order expected by Tendermint.
// i.e. sorted first by power and then lexicographically by address.
func NewTestChainWithValSet(t *testing.T, coord *Coordinator, chainID string, valSet *tmtypes.ValidatorSet, signers []tmtypes.PrivValidator) *TestChain {

genAccs := []authtypes.GenesisAccount{}
genBals := []banktypes.Balance{}
senderAccs := []SenderAccount{}
>>>>>>> 98f4d3a (Create test chain with multiple validators (#942))

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

// NewTestChain initializes a new test chain with a default of 4 validators
// Use this function if the tests do not need custom control over the validator set
func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
// generate validators private/public key
var (
validatorsPerChain = 4
validators []*tmtypes.Validator
signersByAddress = make(map[string]tmtypes.PrivValidator, validatorsPerChain)
)

for i := 0; i < validatorsPerChain; i++ {
privVal := mock.NewPV()
pubKey, err := privVal.GetPubKey()
require.NoError(t, err)
validators = append(validators, tmtypes.NewValidator(pubKey, 1))
signersByAddress[pubKey.Address().String()] = privVal
}

// construct validator set;
// Note that the validators are sorted by voting power
// or, if equal, by address lexical order
valSet := tmtypes.NewValidatorSet(validators)

// create signers indexed by the valSet validators's order
signers := []tmtypes.PrivValidator{}
for _, val := range valSet.Validators {
signers = append(signers, signersByAddress[val.PubKey.Address().String()])
}

return NewTestChainWithValSet(t, coord, chainID, valSet, signers)
}

// GetContext returns the current context for the application.
func (chain *TestChain) GetContext() sdk.Context {
return chain.App.GetBaseApp().NewContext(false, chain.CurrentHeader)
Expand Down

0 comments on commit 04b4b26

Please sign in to comment.