From 951a148ac7775cd3e36b9370464fa6b05471e342 Mon Sep 17 00:00:00 2001 From: Naga Tulasi Gandham <40757909+NagaTulasi@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:24:44 +0530 Subject: [PATCH] (chore) update tests in modules/light-clients/07-tendermint/ tests to expect specific error (#7665) * fix: codec test * chore: make lint-fix --------- Co-authored-by: DimitrisJim --- .../07-tendermint/header_test.go | 24 +++--- .../07-tendermint/misbehaviour_handle_test.go | 73 ++++++++++--------- .../07-tendermint/misbehaviour_test.go | 43 ++++++----- .../07-tendermint/proposal_handle_test.go | 13 ++-- .../07-tendermint/update_test.go | 40 +++++----- 5 files changed, 103 insertions(+), 90 deletions(-) diff --git a/modules/light-clients/07-tendermint/header_test.go b/modules/light-clients/07-tendermint/header_test.go index 1c5d1544a8f..fcdb3403984 100644 --- a/modules/light-clients/07-tendermint/header_test.go +++ b/modules/light-clients/07-tendermint/header_test.go @@ -1,6 +1,7 @@ package tendermint_test import ( + "errors" "time" cmtprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto" @@ -25,37 +26,37 @@ func (suite *TendermintTestSuite) TestHeaderValidateBasic() { testCases := []struct { name string malleate func() - expPass bool + expErr error }{ - {"valid header", func() {}, true}, + {"valid header", func() {}, nil}, {"header is nil", func() { header.Header = nil - }, false}, + }, errors.New("tendermint header cannot be nil")}, {"signed header is nil", func() { header.SignedHeader = nil - }, false}, + }, errors.New("tendermint signed header cannot be nil")}, {"SignedHeaderFromProto failed", func() { header.SignedHeader.Commit.Height = -1 - }, false}, + }, errors.New("header is not a tendermint header")}, {"signed header failed tendermint ValidateBasic", func() { header = suite.chainA.LatestCommittedHeader header.SignedHeader.Commit = nil - }, false}, + }, errors.New("header failed basic validation")}, {"trusted height is equal to header height", func() { var ok bool header.TrustedHeight, ok = header.GetHeight().(clienttypes.Height) suite.Require().True(ok) - }, false}, + }, errors.New("invalid header height")}, {"validator set nil", func() { header.ValidatorSet = nil - }, false}, + }, errors.New("invalid client header")}, {"ValidatorSetFromProto failed", func() { header.ValidatorSet.Validators[0].PubKey = cmtprotocrypto.PublicKey{} - }, false}, + }, errors.New("validator set is not tendermint validator set")}, {"header validator hash does not equal hash of validator set", func() { // use chainB's randomly generated validator set header.ValidatorSet = suite.chainB.LatestCommittedHeader.ValidatorSet - }, false}, + }, errors.New("validator set does not match hash")}, } suite.Require().Equal(exported.Tendermint, suite.header.ClientType()) @@ -72,10 +73,11 @@ func (suite *TendermintTestSuite) TestHeaderValidateBasic() { err := header.ValidateBasic() - if tc.expPass { + if tc.expErr == nil { suite.Require().NoError(err) } else { suite.Require().Error(err) + suite.Require().ErrorContains(err, tc.expErr.Error()) } }) } diff --git a/modules/light-clients/07-tendermint/misbehaviour_handle_test.go b/modules/light-clients/07-tendermint/misbehaviour_handle_test.go index 9f81e0d9343..ce49cb1ab89 100644 --- a/modules/light-clients/07-tendermint/misbehaviour_handle_test.go +++ b/modules/light-clients/07-tendermint/misbehaviour_handle_test.go @@ -1,6 +1,7 @@ package tendermint_test import ( + "errors" "fmt" "strings" "time" @@ -35,7 +36,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { testCases := []struct { name string malleate func() - expPass bool + expErr error }{ { "valid fork misbehaviour", func() { @@ -56,7 +57,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } }, - true, + nil, }, { "valid time misbehaviour", func() { @@ -71,7 +72,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } }, - true, + nil, }, { "valid time misbehaviour, header 1 time strictly less than header 2 time", func() { @@ -86,7 +87,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Hour), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } }, - true, + nil, }, { "valid misbehavior at height greater than last consensusState", func() { @@ -100,7 +101,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } - }, true, + }, nil, }, { "valid misbehaviour with different trusted heights", func() { @@ -124,7 +125,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight2, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals2, suite.chainB.Signers), } }, - true, + nil, }, { "valid misbehaviour at a previous revision", func() { @@ -149,7 +150,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { err = path.EndpointB.UpgradeChain() suite.Require().NoError(err) }, - true, + nil, }, { "valid misbehaviour at a future revision", func() { @@ -169,7 +170,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header2: suite.chainB.CreateTMClientHeader(futureRevision, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } }, - true, + nil, }, { "valid misbehaviour with trusted heights at a previous revision", func() { @@ -191,7 +192,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } }, - true, + nil, }, { "consensus state's valset hash different from misbehaviour should still pass", func() { @@ -216,7 +217,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), bothValSet, suite.chainB.NextVals, trustedVals, bothSigners), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, bothValSet, suite.chainB.NextVals, trustedVals, bothSigners), } - }, true, + }, nil, }, { "invalid misbehaviour: misbehaviour from different chain", func() { @@ -236,7 +237,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header1: suite.chainB.CreateTMClientHeader("evmos", int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader("evmos", int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } - }, false, + }, errors.New("invalid light client misbehaviour"), }, { "misbehaviour trusted validators does not match validator hash in trusted consensus state", func() { @@ -253,7 +254,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, altValSet, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, altValSet, suite.chainB.Signers), } - }, false, + }, errors.New("invalid validator set"), }, { "trusted consensus state does not exist", func() { @@ -267,12 +268,12 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight.Increment().(clienttypes.Height), suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } - }, false, + }, errors.New("consensus state not found"), }, { "invalid tendermint misbehaviour", func() { misbehaviour = &solomachine.Misbehaviour{} - }, false, + }, errors.New("invalid client type"), }, { "trusting period expired", func() { @@ -294,7 +295,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } - }, false, + }, errors.New("time since latest trusted state has passed the trusting period"), }, { "header 1 valset has too much change", func() { @@ -314,7 +315,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), altValSet, suite.chainB.NextVals, trustedVals, altSigners), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } - }, false, + }, errors.New("validator set in header has too much change from trusted validator set"), }, { "header 2 valset has too much change", func() { @@ -334,7 +335,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, altValSet, suite.chainB.NextVals, trustedVals, altSigners), } - }, false, + }, errors.New("validator set in header has too much change from trusted validator set"), }, { "both header 1 and header 2 valsets have too much change", func() { @@ -354,7 +355,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), altValSet, suite.chainB.NextVals, trustedVals, altSigners), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, altValSet, suite.chainB.NextVals, trustedVals, altSigners), } - }, false, + }, errors.New("validator set in header has too much change from trusted validator set"), }, } @@ -375,10 +376,11 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { err = lightClientModule.VerifyClientMessage(suite.chainA.GetContext(), path.EndpointA.ClientID, misbehaviour) - if tc.expPass { + if tc.expErr == nil { suite.Require().NoError(err) } else { suite.Require().Error(err) + suite.Require().ErrorContains(err, tc.expErr.Error()) } }) } @@ -411,7 +413,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { testCases := []struct { name string malleate func() - expPass bool + expErr error }{ { "valid fork misbehaviour", func() { @@ -432,7 +434,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } }, - true, + nil, }, { "valid time misbehaviour", func() { @@ -447,7 +449,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } }, - true, + nil, }, { "valid time misbehaviour, header 1 time strictly less than header 2 time", func() { @@ -462,7 +464,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Hour), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } }, - true, + nil, }, { "valid misbehavior at height greater than last consensusState", func() { @@ -476,7 +478,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } - }, true, + }, nil, }, { "valid misbehaviour with different trusted heights", func() { @@ -500,7 +502,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight2, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals2, suite.chainB.Signers), } }, - true, + nil, }, { "consensus state's valset hash different from misbehaviour should still pass", func() { @@ -525,7 +527,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), bothValSet, suite.chainB.NextVals, trustedVals, bothSigners), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, bothValSet, suite.chainB.NextVals, trustedVals, bothSigners), } - }, true, + }, nil, }, { "invalid misbehaviour: misbehaviour from different chain", func() { @@ -545,7 +547,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header1: suite.chainB.CreateTMClientHeader("evmos", int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader("evmos", int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } - }, false, + }, errors.New("validator set in header has too much change from trusted validator set"), }, { "misbehaviour trusted validators does not match validator hash in trusted consensus state", func() { @@ -562,7 +564,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, altValSet, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, altValSet, suite.chainB.Signers), } - }, false, + }, errors.New("invalid validator set"), }, { "trusted consensus state does not exist", func() { @@ -576,12 +578,12 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight.Increment().(clienttypes.Height), suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } - }, false, + }, errors.New("consensus state not found"), }, { "invalid tendermint misbehaviour", func() { misbehaviour = &solomachine.Misbehaviour{} - }, false, + }, errors.New("nvalid client type"), }, { "trusting period expired", func() { @@ -603,7 +605,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } - }, false, + }, errors.New("time since latest trusted state has passed the trusting period"), }, { "header 1 valset has too much change", func() { @@ -623,7 +625,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), altValSet, suite.chainB.NextVals, trustedVals, altSigners), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), } - }, false, + }, errors.New("validator set in header has too much change from trusted validator set"), }, { "header 2 valset has too much change", func() { @@ -643,7 +645,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, altValSet, suite.chainB.NextVals, trustedVals, altSigners), } - }, false, + }, errors.New("validator set in header has too much change from trusted validator set"), }, { "both header 1 and header 2 valsets have too much change", func() { @@ -663,7 +665,7 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), altValSet, suite.chainB.NextVals, trustedVals, altSigners), Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, altValSet, suite.chainB.NextVals, trustedVals, altSigners), } - }, false, + }, errors.New("validator set in header has too much change from trusted validator set"), }, } @@ -684,10 +686,11 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { err = lightClientModule.VerifyClientMessage(suite.chainA.GetContext(), path.EndpointA.ClientID, misbehaviour) - if tc.expPass { + if tc.expErr == nil { suite.Require().NoError(err) } else { suite.Require().Error(err) + suite.Require().ErrorContains(err, tc.expErr.Error()) } }) } diff --git a/modules/light-clients/07-tendermint/misbehaviour_test.go b/modules/light-clients/07-tendermint/misbehaviour_test.go index 9fc589dc081..fe5efaea67f 100644 --- a/modules/light-clients/07-tendermint/misbehaviour_test.go +++ b/modules/light-clients/07-tendermint/misbehaviour_test.go @@ -1,8 +1,11 @@ package tendermint_test import ( + "errors" "time" + errorsmod "cosmossdk.io/errors" + "github.com/cometbft/cometbft/crypto/tmhash" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" cmttypes "github.com/cometbft/cometbft/types" @@ -48,7 +51,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { name string misbehaviour *ibctm.Misbehaviour malleateMisbehaviour func(misbehaviour *ibctm.Misbehaviour) error - expPass bool + expErr error }{ { "valid fork misbehaviour, two headers at same height have different time", @@ -58,7 +61,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { ClientId: clientID, }, func(misbehaviour *ibctm.Misbehaviour) error { return nil }, - true, + nil, }, { "valid time misbehaviour, both headers at different heights are at same time", @@ -68,19 +71,19 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { ClientId: clientID, }, func(misbehaviour *ibctm.Misbehaviour) error { return nil }, - true, + nil, }, { "misbehaviour Header1 is nil", ibctm.NewMisbehaviour(clientID, nil, suite.header), func(m *ibctm.Misbehaviour) error { return nil }, - false, + errorsmod.Wrap(ibctm.ErrInvalidHeader, "misbehaviour Header1 cannot be nil"), }, { "misbehaviour Header2 is nil", ibctm.NewMisbehaviour(clientID, suite.header, nil), func(m *ibctm.Misbehaviour) error { return nil }, - false, + errorsmod.Wrap(ibctm.ErrInvalidHeader, "misbehaviour Header2 cannot be nil"), }, { "valid misbehaviour with different trusted headers", @@ -90,7 +93,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { ClientId: clientID, }, func(misbehaviour *ibctm.Misbehaviour) error { return nil }, - true, + nil, }, { "trusted height is 0 in Header1", @@ -100,7 +103,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { ClientId: clientID, }, func(misbehaviour *ibctm.Misbehaviour) error { return nil }, - false, + errorsmod.Wrap(ibctm.ErrInvalidHeaderHeight, "misbehaviour Header1 cannot have zero revision height"), }, { "trusted height is 0 in Header2", @@ -110,7 +113,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { ClientId: clientID, }, func(misbehaviour *ibctm.Misbehaviour) error { return nil }, - false, + errorsmod.Wrap(ibctm.ErrInvalidHeaderHeight, "misbehaviour Header2 cannot have zero revision height"), }, { "trusted valset is nil in Header1", @@ -120,7 +123,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { ClientId: clientID, }, func(misbehaviour *ibctm.Misbehaviour) error { return nil }, - false, + errorsmod.Wrap(ibctm.ErrInvalidValidatorSet, "trusted validator set in Header1 cannot be empty"), }, { "trusted valset is nil in Header2", @@ -130,7 +133,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { ClientId: clientID, }, func(misbehaviour *ibctm.Misbehaviour) error { return nil }, - false, + errorsmod.Wrap(ibctm.ErrInvalidValidatorSet, "trusted validator set in Header2 cannot be empty"), }, { "invalid client ID ", @@ -140,7 +143,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { ClientId: "GAIA", }, func(misbehaviour *ibctm.Misbehaviour) error { return nil }, - false, + errors.New("identifier GAIA has invalid length"), }, { "chainIDs do not match", @@ -150,7 +153,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { ClientId: clientID, }, func(misbehaviour *ibctm.Misbehaviour) error { return nil }, - false, + errorsmod.Wrap(clienttypes.ErrInvalidMisbehaviour, "headers must have identical chainIDs"), }, { "header2 height is greater", @@ -160,7 +163,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { ClientId: clientID, }, func(misbehaviour *ibctm.Misbehaviour) error { return nil }, - false, + errors.New("Header1 height is less than Header2 height"), }, { "header 1 doesn't have 2/3 majority", @@ -181,7 +184,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { misbehaviour.Header1.Commit = extCommit.ToCommit().ToProto() return err }, - false, + errors.New("validator set did not commit to header"), }, { "header 2 doesn't have 2/3 majority", @@ -202,7 +205,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { misbehaviour.Header2.Commit = extCommit.ToCommit().ToProto() return err }, - false, + errors.New("validator set did not commit to header"), }, { "validators sign off on wrong commit", @@ -216,7 +219,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { misbehaviour.Header2.Commit.BlockID = tmBlockID.ToProto() return nil }, - false, + errors.New("header 2 failed validation"), }, } @@ -226,11 +229,13 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() { suite.Run(tc.name, func() { err := tc.malleateMisbehaviour(tc.misbehaviour) suite.Require().NoError(err) + err = tc.misbehaviour.ValidateBasic() - if tc.expPass { - suite.Require().NoError(tc.misbehaviour.ValidateBasic(), "valid test case %d failed: %s", i, tc.name) + if tc.expErr == nil { + suite.Require().NoError(err, "valid test case %d failed: %s", i, tc.name) } else { - suite.Require().Error(tc.misbehaviour.ValidateBasic(), "invalid test case %d passed: %s", i, tc.name) + suite.Require().Error(err, "invalid test case %d passed: %s", i, tc.name) + suite.Require().ErrorContains(err, tc.expErr.Error()) } }) } diff --git a/modules/light-clients/07-tendermint/proposal_handle_test.go b/modules/light-clients/07-tendermint/proposal_handle_test.go index 703651ca265..e114dc3f50e 100644 --- a/modules/light-clients/07-tendermint/proposal_handle_test.go +++ b/modules/light-clients/07-tendermint/proposal_handle_test.go @@ -70,17 +70,17 @@ func (suite *TendermintTestSuite) TestCheckSubstituteAndUpdateState() { testCases := []struct { name string FreezeClient bool - expPass bool + expError error }{ { name: "PASS: update checks are deprecated, client is not frozen", FreezeClient: false, - expPass: true, + expError: nil, }, { name: "PASS: update checks are deprecated, client is frozen", FreezeClient: true, - expPass: true, + expError: nil, }, } @@ -138,7 +138,7 @@ func (suite *TendermintTestSuite) TestCheckSubstituteAndUpdateState() { err := subjectClientState.CheckSubstituteAndUpdateState(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), subjectClientStore, substituteClientStore, substituteClientState) - if tc.expPass { + if tc.expError == nil { suite.Require().NoError(err) updatedClient, ok := subjectPath.EndpointA.GetClientState().(*ibctm.ClientState) @@ -165,6 +165,7 @@ func (suite *TendermintTestSuite) TestCheckSubstituteAndUpdateState() { suite.Require().Equal(time.Hour*24*7, updatedClient.TrustingPeriod) } else { suite.Require().Error(err) + suite.Require().ErrorContains(err, tc.expError.Error()) } }) } @@ -179,7 +180,7 @@ func (suite *TendermintTestSuite) TestIsMatchingClientState() { testCases := []struct { name string malleate func() - expPass bool + isMatch bool }{ { "matching clients", func() { @@ -235,7 +236,7 @@ func (suite *TendermintTestSuite) TestIsMatchingClientState() { tc.malleate() - suite.Require().Equal(tc.expPass, ibctm.IsMatchingClientState(*subjectClientState, *substituteClientState)) + suite.Require().Equal(tc.isMatch, ibctm.IsMatchingClientState(*subjectClientState, *substituteClientState)) }) } } diff --git a/modules/light-clients/07-tendermint/update_test.go b/modules/light-clients/07-tendermint/update_test.go index ab4deca0f86..05ed8cfb2d2 100644 --- a/modules/light-clients/07-tendermint/update_test.go +++ b/modules/light-clients/07-tendermint/update_test.go @@ -1,6 +1,7 @@ package tendermint_test import ( + "errors" "time" storetypes "cosmossdk.io/store/types" @@ -39,12 +40,12 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { testCases := []struct { name string malleate func() - expPass bool + expErr error }{ { name: "success", malleate: func() {}, - expPass: true, + expErr: nil, }, { name: "successful verify header for header with a previous height", @@ -64,7 +65,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { err = path.EndpointA.UpdateClient() suite.Require().NoError(err) }, - expPass: true, + expErr: nil, }, { name: "successful verify header: header with future height and different validator set", @@ -82,7 +83,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+5, trustedHeight, suite.chainB.ProposedHeader.Time, bothValSet, suite.chainB.NextVals, trustedVals, bothSigners) }, - expPass: true, + expErr: nil, }, { name: "successful verify header: header with next height and different validator set", @@ -100,7 +101,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight, suite.chainB.ProposedHeader.Time, bothValSet, suite.chainB.NextVals, trustedVals, bothSigners) }, - expPass: true, + expErr: nil, }, { name: "unsuccessful updates, passed in incorrect trusted validators for given consensus state", @@ -115,7 +116,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time, bothValSet, bothValSet, bothValSet, bothSigners) }, - expPass: false, + expErr: errors.New("invalid validator set"), }, { name: "unsuccessful verify header with next height: update header mismatches nextValSetHash", @@ -129,7 +130,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { // this will err as altValSet.Hash() != consState.NextValidatorsHash header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time, altValSet, altValSet, trustedVals, altSigners) }, - expPass: false, + expErr: errors.New("failed to verify header"), }, { name: "unsuccessful update with future height: too much change in validator set", @@ -142,7 +143,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time, altValSet, altValSet, trustedVals, altSigners) }, - expPass: false, + expErr: errors.New("failed to verify header: cant trust new val set"), }, { name: "unsuccessful verify header: header height revision and trusted height revision mismatch", @@ -154,7 +155,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { header = suite.chainB.CreateTMClientHeader(chainIDRevision1, 3, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers) }, - expPass: false, + expErr: errors.New("invalid client header"), }, { name: "unsuccessful verify header: header height < consensus height", @@ -170,7 +171,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { // Make new header at height less than latest client state header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(heightMinus1.RevisionHeight), trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers) }, - expPass: false, + expErr: errors.New("invalid client header"), }, { name: "unsuccessful verify header: header basic validation failed", @@ -178,7 +179,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { // cause header to fail validatebasic by changing commit height to mismatch header height header.SignedHeader.Commit.Height = revisionHeight - 1 }, - expPass: false, + expErr: errors.New("header and commit height mismatch"), }, { name: "unsuccessful verify header: header timestamp is not past last client timestamp", @@ -191,7 +192,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time.Add(-time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers) }, - expPass: false, + expErr: errors.New("failed to verify header"), }, { name: "unsuccessful verify header: header with incorrect header chain-id", @@ -204,7 +205,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { header = suite.chainB.CreateTMClientHeader(chainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers) }, - expPass: false, + expErr: errors.New("header height revision 0 does not match trusted header revision 1"), }, { name: "unsuccessful update: trusting period has passed since last client timestamp", @@ -219,7 +220,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { suite.chainB.ExpireClient(ibctesting.TrustingPeriod) }, - expPass: false, + expErr: errors.New("failed to verify header"), }, { name: "unsuccessful update for a previous revision", @@ -237,7 +238,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { err = path.EndpointB.UpgradeChain() suite.Require().NoError(err) }, - expPass: false, + expErr: errors.New("failed to verify header"), }, { name: "successful update with identical header to a previous update", @@ -255,7 +256,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { err = path.EndpointA.UpdateClient() suite.Require().NoError(err) }, - expPass: true, + expErr: nil, }, { @@ -269,7 +270,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID+"-1", suite.chainB.ProposedHeader.Height+5, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers) }, - expPass: false, + expErr: errors.New("failed to verify header"), }, { @@ -287,7 +288,7 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers) }, - expPass: false, + expErr: errors.New("header height revision 2 does not match trusted header revision 1"), }, } @@ -314,10 +315,11 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { err = lightClientModule.VerifyClientMessage(suite.chainA.GetContext(), path.EndpointA.ClientID, header) - if tc.expPass { + if tc.expErr == nil { suite.Require().NoError(err, tc.name) } else { suite.Require().Error(err) + suite.Require().ErrorContains(err, tc.expErr.Error()) } }) }