From ff5e5ff20df9791df703c6b5ff627ed02599a5f5 Mon Sep 17 00:00:00 2001 From: colin axner <25233464+colin-axner@users.noreply.github.com> Date: Wed, 18 Sep 2024 12:44:52 +0200 Subject: [PATCH 1/3] refactor: remove internal dep on historical info in testing package --- .../07-tendermint/misbehaviour_handle_test.go | 116 +++++++++--------- .../07-tendermint/update_test.go | 70 +++++------ testing/chain.go | 77 ++++-------- 3 files changed, 119 insertions(+), 144 deletions(-) diff --git a/modules/light-clients/07-tendermint/misbehaviour_handle_test.go b/modules/light-clients/07-tendermint/misbehaviour_handle_test.go index 23e7342cca8..9f81e0d9343 100644 --- a/modules/light-clients/07-tendermint/misbehaviour_handle_test.go +++ b/modules/light-clients/07-tendermint/misbehaviour_handle_test.go @@ -42,8 +42,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -63,8 +63,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) misbehaviour = &ibctm.Misbehaviour{ Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+3, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), @@ -78,8 +78,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) misbehaviour = &ibctm.Misbehaviour{ Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+3, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), @@ -93,8 +93,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) misbehaviour = &ibctm.Misbehaviour{ 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), @@ -107,8 +107,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight1, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals1, err := suite.chainB.GetTrustedValidators(int64(trustedHeight1.RevisionHeight)) - suite.Require().NoError(err) + trustedVals1, ok := suite.chainB.TrustedValidators[trustedHeight1.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -116,8 +116,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight2, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals2, err := suite.chainB.GetTrustedValidators(int64(trustedHeight2.RevisionHeight)) - suite.Require().NoError(err) + trustedVals2, ok := suite.chainB.TrustedValidators[trustedHeight2.RevisionHeight] + suite.Require().True(ok) misbehaviour = &ibctm.Misbehaviour{ Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight1, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals1, suite.chainB.Signers), @@ -131,8 +131,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -156,8 +156,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) height, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) @@ -176,8 +176,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) // increment revision of chainID err = path.EndpointB.UpgradeChain() @@ -198,8 +198,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -223,8 +223,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -260,8 +260,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) misbehaviour = &ibctm.Misbehaviour{ 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), @@ -279,8 +279,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -301,8 +301,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -321,8 +321,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -341,8 +341,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -418,8 +418,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -439,8 +439,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) misbehaviour = &ibctm.Misbehaviour{ Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+3, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), @@ -454,8 +454,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) misbehaviour = &ibctm.Misbehaviour{ Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+3, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), @@ -469,8 +469,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) misbehaviour = &ibctm.Misbehaviour{ 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), @@ -483,8 +483,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight1, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals1, err := suite.chainB.GetTrustedValidators(int64(trustedHeight1.RevisionHeight)) - suite.Require().NoError(err) + trustedVals1, ok := suite.chainB.TrustedValidators[trustedHeight1.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -492,8 +492,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight2, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals2, err := suite.chainB.GetTrustedValidators(int64(trustedHeight2.RevisionHeight)) - suite.Require().NoError(err) + trustedVals2, ok := suite.chainB.TrustedValidators[trustedHeight2.RevisionHeight] + suite.Require().True(ok) misbehaviour = &ibctm.Misbehaviour{ Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height, trustedHeight1, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals1, suite.chainB.Signers), @@ -507,8 +507,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -532,8 +532,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -569,8 +569,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) misbehaviour = &ibctm.Misbehaviour{ 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), @@ -588,8 +588,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -610,8 +610,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -630,8 +630,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) @@ -650,8 +650,8 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviourNonRevisionChainID() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) err = path.EndpointA.UpdateClient() suite.Require().NoError(err) diff --git a/modules/light-clients/07-tendermint/update_test.go b/modules/light-clients/07-tendermint/update_test.go index aa53c5544cd..ab4deca0f86 100644 --- a/modules/light-clients/07-tendermint/update_test.go +++ b/modules/light-clients/07-tendermint/update_test.go @@ -52,8 +52,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) // passing the ProposedHeader.Height as the block height as it will become a previous height once we commit N blocks 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) @@ -72,8 +72,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) // Create bothValSet with both suite validator and altVal bothValSet := cmttypes.NewValidatorSet(append(suite.chainB.Vals.Validators, altVal)) @@ -90,8 +90,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) // Create bothValSet with both suite validator and altVal bothValSet := cmttypes.NewValidatorSet(append(suite.chainB.Vals.Validators, altVal)) @@ -123,8 +123,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) // 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) @@ -137,8 +137,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time, altValSet, altValSet, trustedVals, altSigners) }, @@ -149,8 +149,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { malleate: func() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) header = suite.chainB.CreateTMClientHeader(chainIDRevision1, 3, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers) }, @@ -162,8 +162,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) heightMinus1 := clienttypes.NewHeight(trustedHeight.RevisionNumber, trustedHeight.RevisionHeight-1) @@ -186,8 +186,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) 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) }, @@ -199,8 +199,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) 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) }, @@ -212,8 +212,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight] + suite.Require().True(ok) header = suite.chainA.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+1, trustedHeight, suite.chainB.ProposedHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers) @@ -227,8 +227,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) // passing the ProposedHeader.Height as the block height as it will become an update to previous revision once we upgrade the client 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) @@ -245,8 +245,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) // passing the ProposedHeader.Height as the block height as it will become a previous height once we commit N blocks 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) @@ -264,8 +264,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) 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) }, @@ -278,8 +278,8 @@ func (suite *TendermintTestSuite) TestVerifyHeader() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) // increment the revision of the chain err = path.EndpointB.UpgradeChain() @@ -757,10 +757,10 @@ func (suite *TendermintTestSuite) TestCheckForMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) - err = path.EndpointA.UpdateClient() + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) height, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) @@ -778,8 +778,8 @@ func (suite *TendermintTestSuite) TestCheckForMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) clientMessage = &ibctm.Misbehaviour{ Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+3, trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), @@ -866,8 +866,8 @@ func (suite *TendermintTestSuite) TestCheckForMisbehaviour() { trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height) suite.Require().True(ok) - trustedVals, err := suite.chainB.GetTrustedValidators(int64(trustedHeight.RevisionHeight) + 1) - suite.Require().NoError(err) + trustedVals, ok := suite.chainB.TrustedValidators[trustedHeight.RevisionHeight+1] + suite.Require().True(ok) clientMessage = &ibctm.Misbehaviour{ Header2: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.ProposedHeader.Height+3, trustedHeight, suite.chainB.ProposedHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers), diff --git a/testing/chain.go b/testing/chain.go index 8a8dbabba0f..6dcf82d2f98 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -17,8 +17,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - "github.com/cosmos/cosmos-sdk/x/staking/testutil" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" abci "github.com/cometbft/cometbft/abci/types" "github.com/cometbft/cometbft/crypto/tmhash" @@ -73,6 +71,10 @@ type TestChain struct { // the new PrivValidator entry. Signers map[string]cmttypes.PrivValidator + //TrustedValidators is a mapping used to obtain the validator set from which we can prove a header update. + // It maps from a header height to the next validator set associated with that header. + TrustedValidators map[uint64]*cmttypes.ValidatorSet + // autogenerated sender private key SenderPrivKey cryptotypes.PrivKey SenderAccount sdk.AccountI @@ -145,19 +147,20 @@ func NewTestChainWithValSet(tb testing.TB, coord *Coordinator, chainID string, v // create an account to send transactions from chain := &TestChain{ - TB: tb, - Coordinator: coord, - ChainID: chainID, - App: app, - ProposedHeader: header, - TxConfig: txConfig, - Codec: app.AppCodec(), - Vals: valSet, - NextVals: valSet, - Signers: signers, - SenderPrivKey: senderAccs[0].SenderPrivKey, - SenderAccount: senderAccs[0].SenderAccount, - SenderAccounts: senderAccs, + TB: tb, + Coordinator: coord, + ChainID: chainID, + App: app, + ProposedHeader: header, + TxConfig: txConfig, + Codec: app.AppCodec(), + Vals: valSet, + NextVals: valSet, + Signers: signers, + TrustedValidators: make(map[uint64]*cmttypes.ValidatorSet, 0), + SenderPrivKey: senderAccs[0].SenderPrivKey, + SenderAccount: senderAccs[0].SenderAccount, + SenderAccounts: senderAccs, } // commit genesis block @@ -309,6 +312,11 @@ func (chain *TestChain) commitBlock(res *abci.ResponseFinalizeBlock) { // set the last header to the current header // use nil trusted fields chain.LatestCommittedHeader = chain.CurrentTMClientHeader() + // set the trusted validator set to the next validator set + // The latest trusted validator set is the next validator set + // associated with the header being committed in storage. This will + // allow for header updates to be proved against these validators. + chain.TrustedValidators[uint64(chain.ProposedHeader.Height)] = chain.NextVals // val set changes returned from previous block get applied to the next validators // of this block. See tendermint spec for details. @@ -400,39 +408,6 @@ func (chain *TestChain) GetConsensusState(clientID string, height exported.Heigh return chain.App.GetIBCKeeper().ClientKeeper.GetClientConsensusState(chain.GetContext(), clientID, height) } -// GetTrustedValidators will return the trusted validator set of the chain for the given trusted height. Otherwise -// it will return an error. -func (chain *TestChain) GetTrustedValidators(trustedHeight int64) (*cmttypes.ValidatorSet, error) { - // historical information does not store the validator set which committed the header at - // height h. During BeginBlock, it stores the last updated validator set. This is equivalent to - // the next validator set at height h. This is because cometbft processes the validator set - // as follows: - // - // valSetChanges := endBlock() - // chain.Vals = chain.NextVals - // chain.NextVals = applyValSetChanges(chain.NextVals, valSetChanges) - // - // At height h, the validators in the historical information are the: - // validators used to sign height h + 1 (next validator set) - // - // Since we want to return the trusted validator set, which is the next validator set - // for height h, we can simply query using the trusted height. - histInfo, err := chain.App.GetStakingKeeper().GetHistoricalInfo(chain.GetContext(), trustedHeight) - if err != nil { - return nil, err - } - - valSet := stakingtypes.Validators{ - Validators: histInfo.Valset, - } - - tmValidators, err := testutil.ToCmtValidators(valSet, sdk.DefaultPowerReduction) - if err != nil { - return nil, err - } - return cmttypes.NewValidatorSet(tmValidators), nil -} - // GetAcknowledgement retrieves an acknowledgement for the provided packet. If the // acknowledgement does not exist then testing will fail. func (chain *TestChain) GetAcknowledgement(packet channeltypes.Packet) []byte { @@ -593,9 +568,9 @@ func (chain *TestChain) IBCClientHeader(header *ibctm.Header, trustedHeight clie return nil, errorsmod.Wrap(ibctm.ErrInvalidHeaderHeight, "trustedHeight must be a non-zero height") } - cmtTrustedVals, err := chain.GetTrustedValidators(int64(trustedHeight.RevisionHeight)) - if err != nil { - return nil, err + cmtTrustedVals, ok := chain.TrustedValidators[trustedHeight.RevisionHeight] + if !ok { + return nil, fmt.Errorf("unable to find trusted validators at height %d", trustedHeight.RevisionHeight) } trustedVals, err := cmtTrustedVals.ToProto() From e7527b91b17b1902821b3fc6c3877c4789860db5 Mon Sep 17 00:00:00 2001 From: colin axner <25233464+colin-axner@users.noreply.github.com> Date: Wed, 18 Sep 2024 12:49:41 +0200 Subject: [PATCH 2/3] lint --- testing/chain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/chain.go b/testing/chain.go index 6dcf82d2f98..e7b254db2ee 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -71,7 +71,7 @@ type TestChain struct { // the new PrivValidator entry. Signers map[string]cmttypes.PrivValidator - //TrustedValidators is a mapping used to obtain the validator set from which we can prove a header update. + // TrustedValidators is a mapping used to obtain the validator set from which we can prove a header update. // It maps from a header height to the next validator set associated with that header. TrustedValidators map[uint64]*cmttypes.ValidatorSet From f668894780c02afd5d4dd84af294f8bcdd4cbf70 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Thu, 19 Sep 2024 12:04:09 +0200 Subject: [PATCH 3/3] chore: add changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dc9c417934..bce1f292dc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,7 +51,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (core/24-host) [\#7239](https://github.com/cosmos/ibc-go/pull/7239) Removed function `ChannelCapabilityPath` * (apps/27-interchain-accounts) [\#7239](https://github.com/cosmos/ibc-go/pull/7239) The following functions have been removed: `AuthenticateCapability`, `ClaimCapability` * (apps/transfer) [\#7239](https://github.com/cosmos/ibc-go/pull/7239) The following functions have been removed: `BindPort`, `AuthenticateCapability`, `ClaimCapability` -* (capability) [\#7279](https://github.com/cosmos/ibc-go/pull/7279) The module `capability` has been removed +* (capability) [\#7279](https://github.com/cosmos/ibc-go/pull/7279) The module `capability` has been removed. +* (testing) [\#7305](https://github.com/cosmos/ibc-go/pull/7305) Added `TrustedValidators` map to `TestChain`. This removes the dependency on the `x/staking` module for retrieving trusted validator sets at a given height, and removes the `GetTrustedValidators` method from the `TestChain` struct. ### State Machine Breaking