-
Notifications
You must be signed in to change notification settings - Fork 116
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
Changes from 8 commits
fb0cef1
966d729
301c651
b34f693
da27d30
dc89038
63df384
eca1733
c3240d6
fc2637e
662661a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ( | ||
|
@@ -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, | ||
) | ||
|
||
|
@@ -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 | ||
|
@@ -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}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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}) | ||
} | ||
|
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" | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer a separate function |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -177,16 +174,6 @@ func (params *InMemKeeperParams) RegisterSdkCryptoCodecInterfaces() { | |
params.Cdc = codec.NewProtoCodec(ir) | ||
} | ||
|
||
type PrivateKey struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are several instances of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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())) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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, | ||
}, | ||
}, | ||
|
@@ -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) | ||
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if |
||
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the first arg maybe should be |
||
require.NoError(t, err) | ||
consumerKeeper.SetCCValidator(ctx, ccVal) | ||
|
||
|
There was a problem hiding this comment.
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