From d61fa431085694d6a31a8821c0a496baf4942fa0 Mon Sep 17 00:00:00 2001 From: dauTT Date: Mon, 7 Sep 2020 23:44:23 +0200 Subject: [PATCH] x/ibc: mock own privValidator rather than relying on the one from tendermint (#7241) * 1) Create PrivValidator mock in x/ibc/testing/mock/privval.go 2) Adjust tests accordingly * Add tests Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- x/ibc/02-client/keeper/client_test.go | 3 +- x/ibc/02-client/keeper/keeper_test.go | 5 +- x/ibc/02-client/types/genesis_test.go | 3 +- .../types/misbehaviour_handle_test.go | 3 +- .../07-tendermint/types/misbehaviour_test.go | 3 +- x/ibc/07-tendermint/types/tendermint_test.go | 3 +- x/ibc/07-tendermint/types/update_test.go | 3 +- x/ibc/ibc_test.go | 3 +- x/ibc/testing/chain.go | 2 +- x/ibc/testing/mock/privval.go | 47 +++++++++++++++++++ x/ibc/testing/mock/privval_test.go | 44 +++++++++++++++++ 11 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 x/ibc/testing/mock/privval.go create mode 100644 x/ibc/testing/mock/privval_test.go diff --git a/x/ibc/02-client/keeper/client_test.go b/x/ibc/02-client/keeper/client_test.go index c3c4ce8374..c5ec2c51ee 100644 --- a/x/ibc/02-client/keeper/client_test.go +++ b/x/ibc/02-client/keeper/client_test.go @@ -11,6 +11,7 @@ import ( localhosttypes "github.com/cosmos/cosmos-sdk/x/ibc/09-localhost/types" commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" "github.com/cosmos/cosmos-sdk/x/ibc/exported" + ibctestingmock "github.com/cosmos/cosmos-sdk/x/ibc/testing/mock" ) const ( @@ -249,7 +250,7 @@ func (suite *KeeperTestSuite) TestUpdateClientLocalhost() { } func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() { - altPrivVal := tmtypes.NewMockPV() + altPrivVal := ibctestingmock.NewPV() altPubKey, err := altPrivVal.GetPubKey() suite.Require().NoError(err) altVal := tmtypes.NewValidator(altPubKey, 4) diff --git a/x/ibc/02-client/keeper/keeper_test.go b/x/ibc/02-client/keeper/keeper_test.go index a5cb4b481e..098acb5844 100644 --- a/x/ibc/02-client/keeper/keeper_test.go +++ b/x/ibc/02-client/keeper/keeper_test.go @@ -21,6 +21,7 @@ import ( commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" "github.com/cosmos/cosmos-sdk/x/ibc/exported" ibctesting "github.com/cosmos/cosmos-sdk/x/ibc/testing" + ibctestingmock "github.com/cosmos/cosmos-sdk/x/ibc/testing/mock" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -77,7 +78,7 @@ func (suite *KeeperTestSuite) SetupTest() { suite.cdc = app.AppCodec() suite.ctx = app.BaseApp.NewContext(isCheckTx, tmproto.Header{Height: height, ChainID: testClientID, Time: now2}) suite.keeper = &app.IBCKeeper.ClientKeeper - suite.privVal = tmtypes.NewMockPV() + suite.privVal = ibctestingmock.NewPV() pubKey, err := suite.privVal.GetPubKey() suite.Require().NoError(err) @@ -90,7 +91,7 @@ func (suite *KeeperTestSuite) SetupTest() { var validators stakingtypes.Validators for i := 1; i < 11; i++ { - privVal := tmtypes.NewMockPV() + privVal := ibctestingmock.NewPV() pk, err := privVal.GetPubKey() suite.Require().NoError(err) val := stakingtypes.NewValidator(sdk.ValAddress(pk.Address()), pk, stakingtypes.Description{}) diff --git a/x/ibc/02-client/types/genesis_test.go b/x/ibc/02-client/types/genesis_test.go index c4d84bd008..194ecd78ec 100644 --- a/x/ibc/02-client/types/genesis_test.go +++ b/x/ibc/02-client/types/genesis_test.go @@ -13,6 +13,7 @@ import ( commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" "github.com/cosmos/cosmos-sdk/x/ibc/exported" ibctesting "github.com/cosmos/cosmos-sdk/x/ibc/testing" + ibctestingmock "github.com/cosmos/cosmos-sdk/x/ibc/testing/mock" ) const ( @@ -25,7 +26,7 @@ const ( var clientHeight = types.NewHeight(0, 10) func TestValidateGenesis(t *testing.T) { - privVal := tmtypes.NewMockPV() + privVal := ibctestingmock.NewPV() pubKey, err := privVal.GetPubKey() require.NoError(t, err) diff --git a/x/ibc/07-tendermint/types/misbehaviour_handle_test.go b/x/ibc/07-tendermint/types/misbehaviour_handle_test.go index ca891dcfbf..0fab5a8819 100644 --- a/x/ibc/07-tendermint/types/misbehaviour_handle_test.go +++ b/x/ibc/07-tendermint/types/misbehaviour_handle_test.go @@ -12,10 +12,11 @@ import ( "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" "github.com/cosmos/cosmos-sdk/x/ibc/exported" + ibctestingmock "github.com/cosmos/cosmos-sdk/x/ibc/testing/mock" ) func (suite *TendermintTestSuite) TestCheckMisbehaviourAndUpdateState() { - altPrivVal := tmtypes.NewMockPV() + altPrivVal := ibctestingmock.NewPV() altPubKey, err := altPrivVal.GetPubKey() suite.Require().NoError(err) diff --git a/x/ibc/07-tendermint/types/misbehaviour_test.go b/x/ibc/07-tendermint/types/misbehaviour_test.go index 36434bc874..e3ae74184b 100644 --- a/x/ibc/07-tendermint/types/misbehaviour_test.go +++ b/x/ibc/07-tendermint/types/misbehaviour_test.go @@ -9,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" "github.com/cosmos/cosmos-sdk/x/ibc/exported" + ibctestingmock "github.com/cosmos/cosmos-sdk/x/ibc/testing/mock" ) func (suite *TendermintTestSuite) TestMisbehaviour() { @@ -28,7 +29,7 @@ func (suite *TendermintTestSuite) TestMisbehaviour() { } func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { - altPrivVal := tmtypes.NewMockPV() + altPrivVal := ibctestingmock.NewPV() altPubKey, err := altPrivVal.GetPubKey() suite.Require().NoError(err) diff --git a/x/ibc/07-tendermint/types/tendermint_test.go b/x/ibc/07-tendermint/types/tendermint_test.go index e974de008e..9f12c504b7 100644 --- a/x/ibc/07-tendermint/types/tendermint_test.go +++ b/x/ibc/07-tendermint/types/tendermint_test.go @@ -15,6 +15,7 @@ import ( clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types" ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" ibctesting "github.com/cosmos/cosmos-sdk/x/ibc/testing" + ibctestingmock "github.com/cosmos/cosmos-sdk/x/ibc/testing/mock" ) const ( @@ -66,7 +67,7 @@ func (suite *TendermintTestSuite) SetupTest() { // Header time is intended to be time for any new header used for updates suite.headerTime = time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC) - suite.privVal = tmtypes.NewMockPV() + suite.privVal = ibctestingmock.NewPV() pubKey, err := suite.privVal.GetPubKey() suite.Require().NoError(err) diff --git a/x/ibc/07-tendermint/types/update_test.go b/x/ibc/07-tendermint/types/update_test.go index a30dea0e9f..8c64d35173 100644 --- a/x/ibc/07-tendermint/types/update_test.go +++ b/x/ibc/07-tendermint/types/update_test.go @@ -8,6 +8,7 @@ import ( clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types" types "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" + ibctestingmock "github.com/cosmos/cosmos-sdk/x/ibc/testing/mock" ) func (suite *TendermintTestSuite) TestCheckHeaderAndUpdateState() { @@ -19,7 +20,7 @@ func (suite *TendermintTestSuite) TestCheckHeaderAndUpdateState() { ) // Setup different validators and signers for testing different types of updates - altPrivVal := tmtypes.NewMockPV() + altPrivVal := ibctestingmock.NewPV() altPubKey, err := altPrivVal.GetPubKey() suite.Require().NoError(err) diff --git a/x/ibc/ibc_test.go b/x/ibc/ibc_test.go index c0cff249cd..3206fd6a53 100644 --- a/x/ibc/ibc_test.go +++ b/x/ibc/ibc_test.go @@ -13,6 +13,7 @@ import ( clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types" channeltypes "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/types" ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" + ibctestingmock "github.com/cosmos/cosmos-sdk/x/ibc/testing/mock" ) const ( @@ -53,7 +54,7 @@ func (suite *IBCTestSuite) SetupTest() { isCheckTx := false suite.app = simapp.Setup(isCheckTx) - privVal := tmtypes.NewMockPV() + privVal := ibctestingmock.NewPV() pubKey, err := privVal.GetPubKey() suite.Require().NoError(err) diff --git a/x/ibc/testing/chain.go b/x/ibc/testing/chain.go index 652d7d7b57..c63eb75cb8 100644 --- a/x/ibc/testing/chain.go +++ b/x/ibc/testing/chain.go @@ -105,7 +105,7 @@ type TestChain struct { // Each update of any chain increments the block header time for all chains by 5 seconds. func NewTestChain(t *testing.T, chainID string) *TestChain { // generate validator private/public key - privVal := tmtypes.NewMockPV() + privVal := mock.NewPV() pubKey, err := privVal.GetPubKey() require.NoError(t, err) diff --git a/x/ibc/testing/mock/privval.go b/x/ibc/testing/mock/privval.go new file mode 100644 index 0000000000..22aa04c541 --- /dev/null +++ b/x/ibc/testing/mock/privval.go @@ -0,0 +1,47 @@ +package mock + +import ( + "github.com/tendermint/tendermint/crypto" + "github.com/tendermint/tendermint/crypto/ed25519" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + tmtypes "github.com/tendermint/tendermint/types" +) + +var _ tmtypes.PrivValidator = PV{} + +// MockPV implements PrivValidator without any safety or persistence. +// Only use it for testing. +type PV struct { + PrivKey crypto.PrivKey +} + +func NewPV() PV { + return PV{ed25519.GenPrivKey()} +} + +// GetPubKey implements PrivValidator interface +func (pv PV) GetPubKey() (crypto.PubKey, error) { + return pv.PrivKey.PubKey(), nil +} + +// SignVote implements PrivValidator interface +func (pv PV) SignVote(chainID string, vote *tmproto.Vote) error { + signBytes := tmtypes.VoteSignBytes(chainID, vote) + sig, err := pv.PrivKey.Sign(signBytes) + if err != nil { + return err + } + vote.Signature = sig + return nil +} + +// SignProposal implements PrivValidator interface +func (pv PV) SignProposal(chainID string, proposal *tmproto.Proposal) error { + signBytes := tmtypes.ProposalSignBytes(chainID, proposal) + sig, err := pv.PrivKey.Sign(signBytes) + if err != nil { + return err + } + proposal.Signature = sig + return nil +} diff --git a/x/ibc/testing/mock/privval_test.go b/x/ibc/testing/mock/privval_test.go new file mode 100644 index 0000000000..b9f0487a36 --- /dev/null +++ b/x/ibc/testing/mock/privval_test.go @@ -0,0 +1,44 @@ +package mock_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + tmtypes "github.com/tendermint/tendermint/types" + + "github.com/cosmos/cosmos-sdk/x/ibc/testing/mock" +) + +const chainID = "testChain" + +func TestGetPubKey(t *testing.T) { + pv := mock.NewPV() + pk, err := pv.GetPubKey() + require.NoError(t, err) + require.Equal(t, "ed25519", pk.Type()) +} + +func TestSignVote(t *testing.T) { + pv := mock.NewPV() + pk, _ := pv.GetPubKey() + + vote := &tmproto.Vote{Height: 2} + pv.SignVote(chainID, vote) + + msg := tmtypes.VoteSignBytes(chainID, vote) + ok := pk.VerifySignature(msg, vote.Signature) + require.True(t, ok) +} + +func TestSignProposal(t *testing.T) { + pv := mock.NewPV() + pk, _ := pv.GetPubKey() + + proposal := &tmproto.Proposal{Round: 2} + pv.SignProposal(chainID, proposal) + + msg := tmtypes.ProposalSignBytes(chainID, proposal) + ok := pk.VerifySignature(msg, proposal.Signature) + require.True(t, ok) +}