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>
This commit is contained in:
dauTT 2020-09-07 23:44:23 +02:00 committed by GitHub
parent 2539f6e488
commit d61fa43108
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 109 additions and 10 deletions

View File

@ -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)

View File

@ -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{})

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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
}

View File

@ -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)
}