Skip to content

Commit

Permalink
(test-diff pass) refactor diff core to use common test val utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Nov 2, 2022
1 parent f1cfae5 commit fdc95bd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
39 changes: 7 additions & 32 deletions tests/difference/core/driver/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/ibc-go/v3/testing/mock"

ibctesting "github.com/cosmos/ibc-go/v3/testing"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
simapp "github.com/cosmos/interchain-security/testutil/simapp"

cryptoEd25519 "crypto/ed25519"

cosmosEd25519 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
testcrypto "github.com/cosmos/interchain-security/testutil/crypto"

clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
Expand Down Expand Up @@ -60,24 +55,6 @@ type Builder struct {
initState InitState
}

type ValidatorKeyData struct {
signer mock.PV
address sdk.ValAddress
pubKey crypto.PubKey
}

func (b *Builder) getValidatorKeyData(seedIx int) ValidatorKeyData {
ret := ValidatorKeyData{}
ret.signer = b.getValidatorPK(seedIx)
pubKey, err := ret.signer.GetPubKey()
require.NoError(b.suite.T(), err)
ret.pubKey = pubKey
addr, err := sdk.ValAddressFromHex(ret.pubKey.Address().String())
require.NoError(b.suite.T(), err)
ret.address = addr
return ret
}

func (b *Builder) ctx(chain string) sdk.Context {
return b.chain(chain).GetContext()
}
Expand Down Expand Up @@ -135,10 +112,8 @@ func (b *Builder) consAddr(i int64) sdk.ConsAddress {
}

// getValidatorPK returns the validator private key using the given seed index
func (b *Builder) getValidatorPK(seedIx int) mock.PV {
seed := []byte(b.initState.PKSeeds[seedIx])
//lint:ignore SA1019 We don't care because this is only a test.
return mock.PV{PrivKey: &cosmosEd25519.PrivKey{Key: cryptoEd25519.NewKeyFromSeed(seed)}}
func (b *Builder) getValidatorPK(seedIx int) testcrypto.Validator {
return testcrypto.NewValidatorFromBytesSeed([]byte(b.initState.PKSeeds[seedIx]))
}

func (b *Builder) getAppBytesAndSenders(chainID string, app ibctesting.TestingApp, genesis map[string]json.RawMessage,
Expand Down Expand Up @@ -335,10 +310,10 @@ func (b *Builder) createValidators() (*tmtypes.ValidatorSet, map[string]tmtypes.
continue
}

validatorKeyData := b.getValidatorKeyData(i)
signers[validatorKeyData.pubKey.Address().String()] = validatorKeyData.signer
addresses = append(addresses, validatorKeyData.address)
validators = append(validators, tmtypes.NewValidator(validatorKeyData.pubKey, int64(power)))
testVal := b.getValidatorPK(i)
signers[testVal.SDKValAddressString()] = testVal
addresses = append(addresses, testVal.SDKValAddress())
validators = append(validators, testVal.TMValidator(int64(power)))
}

return tmtypes.NewValidatorSet(validators), signers, addresses
Expand Down
18 changes: 18 additions & 0 deletions testutil/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (

sdkcryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdkcryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"

tmcrypto "github.com/tendermint/tendermint/crypto"
tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"
)

type Validator struct {
Expand All @@ -33,6 +35,10 @@ func NewValidatorFromIntSeed(i int) Validator {
return NewValidatorFromBytesSeed(seed)
}

func (v *Validator) TMValidator(power int64) *tmtypes.Validator {
return tmtypes.NewValidator(v.TMCryptoPubKey(), power)
}

func (v *Validator) TMProtoCryptoPublicKey() tmprotocrypto.PublicKey {
ret, err := sdkcryptocodec.ToTmProtoPublicKey(v.SDKPubKey())
if err != nil {
Expand All @@ -57,3 +63,15 @@ func (v *Validator) SDKPubKey() sdkcryptotypes.PubKey {
}
return ret
}

func (v *Validator) SDKValAddressString() string {
return v.TMCryptoPubKey().Address().String()
}

func (v *Validator) SDKValAddress() sdktypes.ValAddress {
ret, err := sdktypes.ValAddressFromHex(v.SDKValAddressString())
if err != nil {
panic(err)
}
return ret
}

0 comments on commit fdc95bd

Please sign in to comment.