Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new cryptoId testutil across repo #473

Closed
wants to merge 11 commits into from
14 changes: 5 additions & 9 deletions tests/e2e/channel_init.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package e2e

import (
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"

tmtypes "github.com/tendermint/tendermint/types"

channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"

"github.com/cosmos/interchain-security/testutil/crypto"
ccv "github.com/cosmos/interchain-security/x/ccv/types"

abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -47,18 +45,16 @@ func (suite *CCVTestSuite) TestConsumerGenesis() {

origTime := suite.consumerChain.GetContext().BlockTime()

pk1, err := cryptocodec.ToTmProtoPublicKey(ed25519.GenPrivKey().PubKey())
suite.Require().NoError(err)
pk2, err := cryptocodec.ToTmProtoPublicKey(ed25519.GenPrivKey().PubKey())
suite.Require().NoError(err)
cId1 := crypto.NewCryptoIdentityFromRandSeed()
cId2 := crypto.NewCryptoIdentityFromRandSeed()
pd := ccv.NewValidatorSetChangePacketData(
[]abci.ValidatorUpdate{
{
PubKey: pk1,
PubKey: cId1.TMProtoCryptoPublicKey(),
Power: 30,
},
{
PubKey: pk2,
PubKey: cId2.TMProtoCryptoPublicKey(),
Power: 20,
},
},
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/normal_operations.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package e2e

import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/interchain-security/testutil/crypto"
consumertypes "github.com/cosmos/interchain-security/x/ccv/consumer/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
Expand All @@ -20,8 +20,8 @@ func (k CCVTestSuite) TestHistoricalInfo() {
// and then call track historical info in the next block
createVal := func(k CCVTestSuite) {
// add new validator to consumer states
pk := ed25519.GenPrivKey().PubKey()
cVal, err := consumertypes.NewCCValidator(pk.Address(), int64(1), pk)
cId := crypto.NewCryptoIdentityFromRandSeed()
cVal, err := consumertypes.NewCCValidator(cId.SDKValAddress(), int64(1), cId.SDKPubKey())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1st arg I'm not sure about. I think it might need to be cId.ABCIAddressBytes()
3rd arg is fine

k.Require().NoError(err)

consumerKeeper.SetCCValidator(k.consumerChain.GetContext(), cVal)
Expand Down
25 changes: 13 additions & 12 deletions tests/e2e/slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/interchain-security/testutil/crypto"
ccv "github.com/cosmos/interchain-security/x/ccv/types"

clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
)

const (
Expand Down Expand Up @@ -270,8 +270,9 @@ func (suite *CCVTestSuite) TestHandleSlashPacketErrors() {
suite.Require().Error(err, "slash with height mapping to zero")

// construct slashing packet with non existing validator
cryptoId := crypto.NewCryptoIdentityFromRandSeed()
slashingPkt := ccv.NewSlashPacketData(
abci.Validator{Address: ed25519.GenPrivKey().PubKey().Address(),
abci.Validator{Address: cryptoId.ABCIAddressBytes(),
Power: int64(0)}, uint64(0), stakingtypes.Downtime,
)

Expand Down Expand Up @@ -534,8 +535,7 @@ func (suite *CCVTestSuite) TestValidatorDoubleSigning() {

// create a validator pubkey and address
// note that the validator wont't necessarily be in valset to due the TM delay
pubkey := ed25519.GenPrivKey().PubKey()
consAddr := sdk.ConsAddress(pubkey.Address())
cryptoId := crypto.NewCryptoIdentityFromRandSeed()

// set an arbitrary infraction height
infractionHeight := ctx.BlockHeight() - 1
Expand All @@ -546,22 +546,23 @@ func (suite *CCVTestSuite) TestValidatorDoubleSigning() {
Height: infractionHeight,
Power: power,
Time: time.Now().UTC(),
ConsensusAddress: consAddr.String(),
ConsensusAddress: cryptoId.SDKConsAddress().String(),
}

// add validator signing-info to the store
suite.consumerApp.GetE2eSlashingKeeper().SetValidatorSigningInfo(ctx, consAddr, slashingtypes.ValidatorSigningInfo{
Address: consAddr.String(),
Tombstoned: false,
})
suite.consumerApp.GetE2eSlashingKeeper().SetValidatorSigningInfo(
ctx, cryptoId.SDKConsAddress(), slashingtypes.ValidatorSigningInfo{
Address: cryptoId.SDKConsAddress().String(),
Tombstoned: false,
})

// save next sequence before sending a slash packet
seq, ok := suite.consumerApp.GetIBCKeeper().ChannelKeeper.GetNextSequenceSend(ctx, ccv.ConsumerPortID, channelID)
suite.Require().True(ok)

// construct slash packet data and get the expcted commit hash
packetData := ccv.NewSlashPacketData(
abci.Validator{Address: consAddr.Bytes(), Power: power},
abci.Validator{Address: cryptoId.ABCIAddressBytes(), Power: power},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thinkkkk it's ok 👍 . It's also confusing for me tbh.

// get VSC ID mapping to the infraction height with the TM delay substracted
suite.consumerApp.GetConsumerKeeper().GetHeightValsetUpdateID(ctx, uint64(infractionHeight-sdk.ValidatorUpdateDelay)),
stakingtypes.DoubleSign,
Expand Down Expand Up @@ -603,9 +604,9 @@ func (suite *CCVTestSuite) TestSendSlashPacket() {
infraction := stakingtypes.Downtime
for j := 0; j < 2; j++ {
for i := 0; i < 4; i++ {
addr := ed25519.GenPrivKey().PubKey().Address()
cryptoId := crypto.NewCryptoIdentityFromRandSeed()
val := abci.Validator{
Address: addr}
Address: cryptoId.ABCIAddressBytes()}
consumerKeeper.SendSlashPacket(ctx, val, 0, infraction)
slashedVals = append(slashedVals, slashedVal{validator: val, infraction: infraction})
}
Expand Down
14 changes: 11 additions & 3 deletions testutil/crypto/crypto.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package crypto

import (
"crypto/rand"

"encoding/binary"

ibcmock "github.com/cosmos/ibc-go/v3/testing/mock"
Expand Down Expand Up @@ -31,10 +33,16 @@ func NewCryptoIdentityFromBytesSeed(seed []byte) CryptoIdentity {
return CryptoIdentity{PV: privKey}
}

func NewCryptoIdentityFromIntSeed(i int) CryptoIdentity {
iUint64 := uint64(i)
func NewCryptoIdentityFromRandSeed() CryptoIdentity {
b := make([]byte, 8)
_, _ = rand.Read(b)
seed := binary.BigEndian.Uint64(b)
shaspitz marked this conversation as resolved.
Show resolved Hide resolved
return NewCryptoIdentityFromIntSeed(seed)
shaspitz marked this conversation as resolved.
Show resolved Hide resolved
}

func NewCryptoIdentityFromIntSeed(i uint64) CryptoIdentity {
seed := []byte("AAAAAAAAabcdefghijklmnopqrstuvwx") // 8+24 bytes
binary.LittleEndian.PutUint64(seed[:8], iUint64)
binary.LittleEndian.PutUint64(seed[:8], i)
return NewCryptoIdentityFromBytesSeed(seed)
}
Comment on lines +43 to 47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a separate function FromUint64Seed if it's necessary. I used int as the arg to begin with because it's the easiest type to use when writing tests (in loops and such).


Expand Down
13 changes: 0 additions & 13 deletions testutil/keeper/unit_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ import (
"github.com/cosmos/interchain-security/x/ccv/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types"
Expand Down Expand Up @@ -177,16 +174,6 @@ func (params *InMemKeeperParams) RegisterSdkCryptoCodecInterfaces() {
params.Cdc = codec.NewProtoCodec(ir)
}

type PrivateKey struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removal of old helper here

PrivKey cryptotypes.PrivKey
}

// Generates a public key for unit tests (abiding by tricky interface implementations from tm/sdk)
func GenPubKey() (crypto.PubKey, error) {
privKey := PrivateKey{ed25519.GenPrivKey()}
return cryptocodec.ToTmPubKeyInterface(privKey.PrivKey.PubKey())
}

func GetClientState(chainID string) *ibctmtypes.ClientState {
return ibctmtypes.NewClientState(chainID, ibctmtypes.DefaultTrustLevel, 0, 0,
time.Second*10, clienttypes.Height{}, commitmenttypes.GetSDKSpecs(),
Expand Down
17 changes: 7 additions & 10 deletions x/ccv/consumer/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
consumertypes "github.com/cosmos/interchain-security/x/ccv/consumer/types"

ccvcrypto "github.com/cosmos/interchain-security/testutil/crypto"
testutil "github.com/cosmos/interchain-security/testutil/keeper"
)

Expand All @@ -32,8 +31,8 @@ func TestInitGenesis(t *testing.T) {
clientID := "tendermint-07"

// generate validator public key
pubKey, err := testutil.GenPubKey()
require.NoError(t, err)
cryptoId := ccvcrypto.NewCryptoIdentityFromRandSeed()
pubKey := cryptoId.TMCryptoPubKey()

// create validator set with single validator
validator := tmtypes.NewValidator(pubKey, 1)
Expand Down Expand Up @@ -171,10 +170,8 @@ func TestExportGenesis(t *testing.T) {
)

// create a single validator
pubKey := ed25519.GenPrivKey().PubKey()
tmPK, err := cryptocodec.ToTmPubKeyInterface(pubKey)
require.NoError(t, err)
validator := tmtypes.NewValidator(tmPK, 1)
cId := ccvcrypto.NewCryptoIdentityFromRandSeed()
validator := tmtypes.NewValidator(cId.TMCryptoPubKey(), 1)

// create consensus state using a single validator
consensusState := testutil.GetConsensusState(clientID, time.Time{}, validator)
Expand All @@ -188,7 +185,7 @@ func TestExportGenesis(t *testing.T) {
name: "export a new chain",
malleate: func(ctx sdk.Context, ck consumerkeeper.Keeper, mocks testutil.MockedKeepers) {
// populate the states used by a new consumer chain
cVal, err := consumertypes.NewCCValidator(validator.Address.Bytes(), 1, pubKey)
cVal, err := consumertypes.NewCCValidator(validator.Address.Bytes(), 1, cId.SDKPubKey())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several instances of validator.Address.Bytes() I think they could be cId.ABCIAddressBytes()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if it's true it might not be worth changing them.

require.NoError(t, err)
ck.SetCCValidator(ctx, cVal)
ck.SetProviderClientID(ctx, clientID)
Expand All @@ -211,7 +208,7 @@ func TestExportGenesis(t *testing.T) {
name: "export a chain that has an established CCV channel",
malleate: func(ctx sdk.Context, ck consumerkeeper.Keeper, mocks testutil.MockedKeepers) {
// populate the states used by a running chain
cVal, err := consumertypes.NewCCValidator(validator.Address.Bytes(), 1, pubKey)
cVal, err := consumertypes.NewCCValidator(validator.Address.Bytes(), 1, cId.SDKPubKey())
require.NoError(t, err)
ck.SetCCValidator(ctx, cVal)
ck.SetOutstandingDowntime(ctx, sdk.ConsAddress(validator.Address.Bytes()))
Expand Down
25 changes: 10 additions & 15 deletions x/ccv/consumer/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package keeper_test
import (
"testing"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
conntypes "github.com/cosmos/ibc-go/v3/modules/core/03-connection/types"
"github.com/cosmos/interchain-security/testutil/crypto"
testkeeper "github.com/cosmos/interchain-security/testutil/keeper"
"github.com/cosmos/interchain-security/x/ccv/consumer/types"
ccv "github.com/cosmos/interchain-security/x/ccv/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
)

// TestProviderClientID tests getter and setter functionality for the client ID stored on consumer keeper
Expand Down Expand Up @@ -46,19 +44,17 @@ func TestProviderChannel(t *testing.T) {

// TestPendingChanges tests getter, setter, and delete functionality for pending VSCs on a consumer chain
func TestPendingChanges(t *testing.T) {
pk1, err := cryptocodec.ToTmProtoPublicKey(ed25519.GenPrivKey().PubKey())
require.NoError(t, err)
pk2, err := cryptocodec.ToTmProtoPublicKey(ed25519.GenPrivKey().PubKey())
require.NoError(t, err)
cId1 := crypto.NewCryptoIdentityFromRandSeed()
cId2 := crypto.NewCryptoIdentityFromRandSeed()

pd := ccv.NewValidatorSetChangePacketData(
[]abci.ValidatorUpdate{
{
PubKey: pk1,
PubKey: cId1.TMProtoCryptoPublicKey(),
Power: 30,
},
{
PubKey: pk2,
PubKey: cId2.TMProtoCryptoPublicKey(),
Power: 20,
},
},
Expand All @@ -69,7 +65,7 @@ func TestPendingChanges(t *testing.T) {
consumerKeeper, ctx, ctrl, _ := testkeeper.GetConsumerKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()

err = consumerKeeper.SetPendingChanges(ctx, pd)
err := consumerKeeper.SetPendingChanges(ctx, pd)
require.NoError(t, err)
gotPd, ok := consumerKeeper.GetPendingChanges(ctx)
require.True(t, ok)
Expand Down Expand Up @@ -120,15 +116,14 @@ func TestCrossChainValidator(t *testing.T) {
consumerKeeper, ctx, ctrl, _ := testkeeper.GetConsumerKeeperAndCtx(t, keeperParams)
defer ctrl.Finish()

cId := crypto.NewCryptoIdentityFromRandSeed()

// should return false
_, found := consumerKeeper.GetCCValidator(ctx, ed25519.GenPrivKey().PubKey().Address())
_, found := consumerKeeper.GetCCValidator(ctx, cId.SDKValAddress())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if cId.SDKValAddress() is right here.

require.False(t, found)

// Obtain derived private key
privKey := ed25519.GenPrivKey()

// Set cross chain validator
ccVal, err := types.NewCCValidator(privKey.PubKey().Address(), 1000, privKey.PubKey())
ccVal, err := types.NewCCValidator(cId.SDKValAddress(), 1000, cId.SDKPubKey())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the first arg maybe should be cId.ABCIAddressBytes()

require.NoError(t, err)
consumerKeeper.SetCCValidator(ctx, ccVal)

Expand Down
26 changes: 11 additions & 15 deletions x/ccv/consumer/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"testing"
"time"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
"github.com/cosmos/interchain-security/testutil/crypto"
testkeeper "github.com/cosmos/interchain-security/testutil/keeper"
consumertypes "github.com/cosmos/interchain-security/x/ccv/consumer/types"
"github.com/cosmos/interchain-security/x/ccv/types"
Expand All @@ -27,31 +26,28 @@ func TestOnRecvVSCPacket(t *testing.T) {
consumerCCVChannelID := "consumerCCVChannelID"
providerCCVChannelID := "providerCCVChannelID"

pk1, err := cryptocodec.ToTmProtoPublicKey(ed25519.GenPrivKey().PubKey())
require.NoError(t, err)
pk2, err := cryptocodec.ToTmProtoPublicKey(ed25519.GenPrivKey().PubKey())
require.NoError(t, err)
pk3, err := cryptocodec.ToTmProtoPublicKey(ed25519.GenPrivKey().PubKey())
require.NoError(t, err)
cId1 := crypto.NewCryptoIdentityFromRandSeed()
cId2 := crypto.NewCryptoIdentityFromRandSeed()
cId3 := crypto.NewCryptoIdentityFromRandSeed()

changes1 := []abci.ValidatorUpdate{
{
PubKey: pk1,
PubKey: cId1.TMProtoCryptoPublicKey(),
Power: 30,
},
{
PubKey: pk2,
PubKey: cId2.TMProtoCryptoPublicKey(),
Power: 20,
},
}

changes2 := []abci.ValidatorUpdate{
{
PubKey: pk2,
PubKey: cId2.TMProtoCryptoPublicKey(),
Power: 40,
},
{
PubKey: pk3,
PubKey: cId3.TMProtoCryptoPublicKey(),
Power: 10,
},
}
Expand Down Expand Up @@ -95,15 +91,15 @@ func TestOnRecvVSCPacket(t *testing.T) {
types.ValidatorSetChangePacketData{ValidatorUpdates: changes2},
types.ValidatorSetChangePacketData{ValidatorUpdates: []abci.ValidatorUpdate{
{
PubKey: pk1,
PubKey: cId1.TMProtoCryptoPublicKey(),
Power: 30,
},
{
PubKey: pk2,
PubKey: cId2.TMProtoCryptoPublicKey(),
Power: 40,
},
{
PubKey: pk3,
PubKey: cId3.TMProtoCryptoPublicKey(),
Power: 10,
},
}},
Expand Down
Loading