Skip to content
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

Add revision number tests for 07-tendermint #1302

Merged
merged 18 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion modules/light-clients/07-tendermint/types/misbehaviour_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package types

import (
"bytes"
"fmt"
"strings"
"time"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -112,8 +114,16 @@ func checkMisbehaviourHeader(
chainID := clientState.GetChainID()
// If chainID is in revision format, then set revision number of chainID with the revision number
// of the misbehaviour header
if clienttypes.IsRevisionFormat(chainID) {
switch {
case chainID == header.Header.ChainID: // use chainID
case clienttypes.IsRevisionFormat(chainID) && clienttypes.IsRevisionFormat(header.Header.ChainID):
chainID, _ = clienttypes.SetRevisionNumber(chainID, header.GetHeight().GetRevisionNumber())
case clienttypes.IsRevisionFormat(chainID) && !clienttypes.IsRevisionFormat(header.Header.ChainID):
// past misbehaviour
chainID = strings.TrimSuffix(chainID, fmt.Sprintf("-%d", clienttypes.ParseChainID(chainID)))
case !clienttypes.IsRevisionFormat(chainID) && clienttypes.IsRevisionFormat(header.Header.ChainID):
// future misbehaviour
chainID = fmt.Sprintf("%s-%d", chainID, header.GetHeight().GetRevisionNumber())
}

// - ValidatorSet must have TrustLevel similarity with trusted FromValidatorSet
Expand Down
107 changes: 60 additions & 47 deletions modules/light-clients/07-tendermint/types/misbehaviour_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,54 +119,68 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() {
},
true,
},
/*
{

"valid misbehaviour at a previous revision",
types.NewClientState(chainIDRevision1, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, clienttypes.NewHeight(1, 1), commitmenttypes.GetSDKSpecs(), upgradePath, false, false),
types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), bothValsHash),
heightMinus1,
types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), suite.valsHash),
heightMinus3,
&types.Misbehaviour{
Header1: suite.chainA.CreateTMClientHeader(chainIDRevision0, int64(height.RevisionHeight+1), heightMinus1, suite.now, bothValSet, bothValSet, bothValSet, bothSigners),
Header2: suite.chainA.CreateTMClientHeader(chainIDRevision0, int64(height.RevisionHeight+1), heightMinus3, suite.now.Add(time.Minute), bothValSet, bothValSet, suite.valSet, bothSigners),
ClientId: chainID,
},
suite.now,
true,
{

"valid misbehaviour at a previous revision", func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)

trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)

err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)

height := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)

misbehaviour = &types.Misbehaviour{
Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.CurrentHeader.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.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers),
}

// increment the revision of the chain
err = path.EndpointB.UpgradeChain()
suite.Require().NoError(err)
},
{
"valid misbehaviour at a future revision",
types.NewClientState(chainIDRevision0, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs(), upgradePath, false, false),
types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), bothValsHash),
heightMinus1,
types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), suite.valsHash),
heightMinus3,
&types.Misbehaviour{
Header1: suite.chainA.CreateTMClientHeader(chainIDRevision0, 3, heightMinus1, suite.now, bothValSet, bothValSet, bothValSet, bothSigners),
Header2: suite.chainA.CreateTMClientHeader(chainIDRevision0, 3, heightMinus3, suite.now.Add(time.Minute), bothValSet, bothValSet, suite.valSet, bothSigners),
ClientId: chainID,
},
suite.now,
true,
true,
},
{
"valid misbehaviour at a future revision", func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)

trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)

height := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)

futureRevision := suite.chainB.ChainID + "-1"

misbehaviour = &types.Misbehaviour{
Header1: suite.chainB.CreateTMClientHeader(futureRevision, int64(height.RevisionHeight), trustedHeight, suite.chainB.CurrentHeader.Time.Add(time.Minute), suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers),
Header2: suite.chainB.CreateTMClientHeader(futureRevision, int64(height.RevisionHeight), trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers),
}
},
{
"valid misbehaviour with trusted heights at a previous revision",
types.NewClientState(chainIDRevision1, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, clienttypes.NewHeight(1, 1), commitmenttypes.GetSDKSpecs(), upgradePath, false, false),
types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), bothValsHash),
heightMinus1,
types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), suite.valsHash),
heightMinus3,
&types.Misbehaviour{
Header1: suite.chainA.CreateTMClientHeader(chainIDRevision1, 1, heightMinus1, suite.now, bothValSet, bothValSet, bothValSet, bothSigners),
Header2: suite.chainA.CreateTMClientHeader(chainIDRevision1, 1, heightMinus3, suite.now.Add(time.Minute), bothValSet, bothValSet, suite.valSet, bothSigners),
ClientId: chainID,
},
suite.now,
true,
true,
},
{
"valid misbehaviour with trusted heights at a previous revision", func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work on these tests!

trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)

trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)

// increment the revision of the chain
err = path.EndpointB.UpgradeChain()
suite.Require().NoError(err)

height := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)

misbehaviour = &types.Misbehaviour{
Header1: suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, int64(height.RevisionHeight), trustedHeight, suite.chainB.CurrentHeader.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.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers),
}
},
*/
true,
},
{
"consensus state's valset hash different from misbehaviour should still pass", func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)
Expand Down Expand Up @@ -360,10 +374,9 @@ func (suite *TendermintTestSuite) TestVerifyMisbehaviour() {
err := path.EndpointA.CreateClient()
suite.Require().NoError(err)

clientState := path.EndpointA.GetClientState()

tc.malleate()

clientState := path.EndpointA.GetClientState()
clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID)

err = clientState.VerifyClientMessage(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), clientStore, misbehaviour)
Expand Down
117 changes: 60 additions & 57 deletions modules/light-clients/07-tendermint/types/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,67 +208,70 @@ func (suite *TendermintTestSuite) TestVerifyHeader() {
},
expPass: false,
},
// TODO: add revision tests after helper function to upgrade chain/client
/*
{
name: "successful update for a previous revision",
setup: func(suite *TendermintTestSuite) {
clientState = types.NewClientState(chainIDRevision1, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs(), upgradePath, false, false)
consensusState = types.NewConsensusState(suite.clientTime, commitmenttypes.NewMerkleRoot(suite.header.Header.GetAppHash()), suite.valsHash)
consStateHeight = heightMinus3
newHeader = suite.chainA.CreateTMClientHeader(chainIDRevision0, int64(height.RevisionHeight), heightMinus3, suite.headerTime, bothValSet, bothValSet, suite.valSet, bothSigners)
currentTime = suite.now
},
expPass: true,
},
{
name: "successful update with identical header to a previous update",
setup: func(suite *TendermintTestSuite) {
clientState = types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, heightPlus1, commitmenttypes.GetSDKSpecs(), upgradePath, false, false)
consensusState = types.NewConsensusState(suite.clientTime, commitmenttypes.NewMerkleRoot(suite.header.Header.GetAppHash()), suite.valsHash)
newHeader = suite.chainA.CreateTMClientHeader(chainID, int64(heightPlus1.RevisionHeight), height, suite.headerTime, suite.valSet, suite.valSet, suite.valSet, suite.signers)
currentTime = suite.now
ctx := suite.chainA.GetContext().WithBlockTime(currentTime)
// Store the header's consensus state in client store before UpdateClient call
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(ctx, clientID, heightPlus1, newHeader.ConsensusState())
},
expFrozen: false,
expPass: true,
{
name: "successful update for a previous revision",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)

trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)

// passing the CurrentHeader.Height as the block height as it will become a previous height once we commit N blocks
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)

// increment the revision of the chain
err = path.EndpointB.UpgradeChain()
suite.Require().NoError(err)
},
{
name: "unsuccessful update to a future revision",
setup: func(suite *TendermintTestSuite) {
clientState = types.NewClientState(chainIDRevision0, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs(), upgradePath, false, false)
consensusState = types.NewConsensusState(suite.clientTime, commitmenttypes.NewMerkleRoot(suite.header.Header.GetAppHash()), suite.valsHash)
newHeader = suite.chainA.CreateTMClientHeader(chainIDRevision1, 1, height, suite.headerTime, suite.valSet, suite.valSet, suite.valSet, suite.signers)
currentTime = suite.now
},
expPass: false,
expPass: true,
},
{
name: "successful update with identical header to a previous update",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)

trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)

// passing the CurrentHeader.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.CurrentHeader.Height, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)

// update client so the header constructed becomes a duplicate
err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
},
{
name: "unsuccessful update: header height revision and trusted height revision mismatch",
setup: func(suite *TendermintTestSuite) {
clientState = types.NewClientState(chainIDRevision1, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, clienttypes.NewHeight(1, 1), commitmenttypes.GetSDKSpecs(), upgradePath, false, false)
consensusState = types.NewConsensusState(suite.clientTime, commitmenttypes.NewMerkleRoot(suite.header.Header.GetAppHash()), suite.valsHash)
newHeader = suite.chainA.CreateTMClientHeader(chainIDRevision1, 3, height, suite.headerTime, suite.valSet, suite.valSet, suite.valSet, suite.signers)
currentTime = suite.now
},
expFrozen: false,
expPass: false,
expPass: true,
},

{
name: "unsuccessful update to a future revision",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)

trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)

header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID+"-1", suite.chainB.CurrentHeader.Height+5, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
},
{
name: "unsuccessful update: trusting period has passed since last client timestamp",
setup: func(suite *TendermintTestSuite) {
clientState = types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs(), upgradePath, false, false)
consensusState = types.NewConsensusState(suite.clientTime, commitmenttypes.NewMerkleRoot(suite.header.Header.GetAppHash()), suite.valsHash)
newHeader = suite.chainA.CreateTMClientHeader(chainID, int64(heightPlus1.RevisionHeight), height, suite.headerTime, suite.valSet, suite.valSet, suite.valSet, suite.signers)
// make current time pass trusting period from last timestamp on clientstate
currentTime = suite.now.Add(trustingPeriod)
},
expFrozen: false,
expPass: false,
expPass: false,
},

colin-axner marked this conversation as resolved.
Show resolved Hide resolved
{
name: "unsuccessful update: header height revision and trusted height revision mismatch",
malleate: func() {
trustedHeight := path.EndpointA.GetClientState().GetLatestHeight().(clienttypes.Height)

trustedVals, found := suite.chainB.GetValsAtHeight(int64(trustedHeight.RevisionHeight) + 1)
suite.Require().True(found)

// increment the revision of the chain
err = path.EndpointB.UpgradeChain()
suite.Require().NoError(err)

header = suite.chainB.CreateTMClientHeader(suite.chainB.ChainID, suite.chainB.CurrentHeader.Height, trustedHeight, suite.chainB.CurrentHeader.Time, suite.chainB.Vals, suite.chainB.NextVals, trustedVals, suite.chainB.Signers)
},
*/
expPass: false,
},
}

for _, tc := range testCases {
Expand Down
49 changes: 49 additions & 0 deletions testing/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,55 @@ func (endpoint *Endpoint) UpdateClient() (err error) {
return endpoint.Chain.sendMsgs(msg)
}

// UpgradeChain will upgrade a chain's chainID to the next revision number.
// It will also update the counterparty client.
// TODO: implement actual upgrade chain functionality via scheduling an upgrade
// and upgrading the client via MsgUpgradeClient
// see reference https://github.com/cosmos/ibc-go/pull/1169
func (endpoint *Endpoint) UpgradeChain() error {
if endpoint.Counterparty.ClientID == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Should we use strings.TrimSpace() here?

return fmt.Errorf("cannot upgrade chain if there is no counterparty client")
}

clientState := endpoint.Counterparty.GetClientState().(*ibctmtypes.ClientState)

// increment revision number in chainID
oldChainID := clientState.ChainId
revisionNumber := clienttypes.ParseChainID(oldChainID)
newChainID, err := clienttypes.SetRevisionNumber(oldChainID, revisionNumber+1)
if err != nil {
// current chainID is not in revision format
newChainID = clientState.ChainId + "-1"
}

// update chain
endpoint.Chain.ChainID = newChainID
endpoint.Chain.CurrentHeader.ChainID = newChainID
endpoint.Chain.NextBlock() // commit changes

// update counterparty client manually
clientState.ChainId = newChainID
clientState.LatestHeight = clienttypes.NewHeight(revisionNumber+1, clientState.LatestHeight.GetRevisionHeight()+1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be resetting revision height here to prove that resetting height still works

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too difficult right now. Would require resetting the SimApp such that it uses a zero height rather than incrementing its existing height

endpoint.Counterparty.SetClientState(clientState)

consensusState := &ibctmtypes.ConsensusState{
Timestamp: endpoint.Chain.LastHeader.GetTime(),
Root: commitmenttypes.NewMerkleRoot(endpoint.Chain.LastHeader.Header.GetAppHash()),
NextValidatorsHash: endpoint.Chain.LastHeader.Header.NextValidatorsHash,
}
endpoint.Counterparty.SetConsensusState(consensusState, clientState.GetLatestHeight())

// ensure the next update isn't identical to the one set in state
endpoint.Chain.Coordinator.IncrementTime()
endpoint.Chain.NextBlock()

if err = endpoint.Counterparty.UpdateClient(); err != nil {
return err
}

return nil
}

// ConnOpenInit will construct and execute a MsgConnectionOpenInit on the associated endpoint.
func (endpoint *Endpoint) ConnOpenInit() error {
msg := connectiontypes.NewMsgConnectionOpenInit(
Expand Down