Skip to content

Commit

Permalink
Determinise validator creation
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Mar 11, 2024
1 parent 73247ef commit 34bce60
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/consumer-democracy/proposals_whitelisting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestDemocracyGovernanceWhitelistingKeys(t *testing.T) {
_, valUpdates, _, err := testutil.CreateValidators(4)
_, valUpdates, _, err := testutil.CreateValidators(4, "consumer")
require.NoError(t, err)
ibctesting.DefaultTestingAppInit = icstestingutils.DemocracyConsumerAppIniter(valUpdates)
chain := ibctesting.NewTestChain(t, ibctesting.NewCoordinator(t, 0), "test")
Expand Down
20 changes: 15 additions & 5 deletions tests/mbt/driver/mbt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,28 @@ func RunItfTrace(t *testing.T, path string) {
// generate keys that can be assigned on consumers, according to the ConsumerAddresses in the trace
consumerAddressesExpr := params["ConsumerAddresses"].Value.(itf.ListExprType)

consumerValidators, _, consumerPrivVals, err := integration.CreateValidators(len(consumerAddressesExpr))
consumerValidators, _, consumerPrivVals, err := integration.CreateValidators(len(consumerAddressesExpr), "consumer")
require.NoError(t, err, "Error creating consumer signers")

// sort the keys of consumer priv vals for determinism
// this is necessary because the order of the keys in the map is not deterministic
// and the model and the system need to have the same order
consumerPrivValKeys := make([]string, 0, len(consumerPrivVals))
for key := range consumerPrivVals {
consumerPrivValKeys = append(consumerPrivValKeys, key)
}
// sort the keys
sort.Strings(consumerPrivValKeys)

consumerAddrNamesToVals := make(map[string]*cmttypes.Validator, len(consumerAddressesExpr))
consumerAddrNamesToPrivVals := make(map[string]cmttypes.PrivValidator, len(consumerAddressesExpr))
realAddrsToModelConsAddrs := make(map[string]string, len(consumerAddressesExpr))
i := 0
for address, privVal := range consumerPrivVals {
for i := 0; i < len(consumerAddressesExpr); i++ {
address := consumerPrivValKeys[i]
privVal := consumerPrivVals[address]
consumerAddrNamesToPrivVals[consumerAddressesExpr[i].Value.(string)] = privVal
consumerAddrNamesToVals[consumerAddressesExpr[i].Value.(string)] = consumerValidators.Validators[i]
realAddrsToModelConsAddrs[address] = consumerAddressesExpr[i].Value.(string)
i++
}

// create params struct
Expand Down Expand Up @@ -168,7 +178,7 @@ func RunItfTrace(t *testing.T, path string) {
valNames[i] = val.Value.(string)
}

valSet, addressMap, signers, err := CreateValSet(initialValSet)
valSet, addressMap, signers, err := CreateValSet(initialValSet, "provider")
require.NoError(t, err, "Error creating validator set")

// get the set of signers for consumers: the validator signers, plus signers for the assignable addresses
Expand Down
7 changes: 5 additions & 2 deletions tests/mbt/driver/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"log"
"sort"
"testing"
"time"

Expand Down Expand Up @@ -57,9 +58,9 @@ var (
// - a validator set
// - a map from node names to validator objects and
// - a map from validator addresses to private validators (signers)
func CreateValSet(initialValidatorSet map[string]int64) (*cmttypes.ValidatorSet, map[string]*cmttypes.Validator, map[string]cmttypes.PrivValidator, error) {
func CreateValSet(initialValidatorSet map[string]int64, chainId string) (*cmttypes.ValidatorSet, map[string]*cmttypes.Validator, map[string]cmttypes.PrivValidator, error) {
// create a valSet and signers, but the voting powers will not yet be right
valSet, _, signers, err := integration.CreateValidators(len(initialValidatorSet))
valSet, _, signers, err := integration.CreateValidators(len(initialValidatorSet), chainId)
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -72,6 +73,8 @@ func CreateValSet(initialValidatorSet map[string]int64) (*cmttypes.ValidatorSet,
for valName := range initialValidatorSet {
valNames = append(valNames, valName)
}
// sort the names so that the order is deterministic
sort.Strings(valNames)

// assign the validators from the created valSet to valNames in the chosen order
for i, valName := range valNames {
Expand Down
2 changes: 1 addition & 1 deletion testutil/ibc_testing/generic_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func AddDemocracyConsumer[T testutil.DemocConsumerApp](
s.T().Helper()

// generate validators private/public key
valSet, valUpdates, signers, err := testutil.CreateValidators(4)
valSet, valUpdates, signers, err := testutil.CreateValidators(4, "test")
require.NoError(s.T(), err)

ibctesting.DefaultTestingAppInit = appIniter(valUpdates)
Expand Down
13 changes: 11 additions & 2 deletions testutil/integration/validators.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package integration

import (
"fmt"

"github.com/cosmos/cosmos-sdk/testutil/mock"

"github.com/cometbft/cometbft/abci/types"
tmencoding "github.com/cometbft/cometbft/crypto/encoding"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
)

func CreateValidators(n int) (
// CreateValidators creates a set of validators for testing purposes
// and returns the validator set, validator updates, and a map of signers by address
// n is the number of validators to create
// chainId is the chain id to use for the private keys. it is only important to get different private keys
// for different chains, so it is fine to not have this chainId match the chainId in the context where the
// validators will be used, but the same string shouldn't be repeated across different invocations of this in the same scope.
func CreateValidators(n int, chainId string) (
*tmtypes.ValidatorSet, []types.ValidatorUpdate, map[string]tmtypes.PrivValidator, error,
) {
// generate validators private/public key
Expand All @@ -17,7 +26,7 @@ func CreateValidators(n int) (
signersByAddress = make(map[string]tmtypes.PrivValidator, n)
)
for i := 0; i < n; i++ {
privVal := mock.NewPV()
privVal := mock.PV{PrivKey: ed25519.GenPrivKeyFromSecret([]byte(chainId + fmt.Sprint(i)))}
pubKey, err := privVal.GetPubKey()
if err != nil {
return nil, nil, nil, err
Expand Down

0 comments on commit 34bce60

Please sign in to comment.