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

test: add UpdateConnection to endpoint and update usages in tests. #5719

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 2 additions & 9 deletions modules/core/03-connection/keeper/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ func (suite *KeeperTestSuite) TestConnOpenTry() {
delayPeriod = uint64(time.Hour.Nanoseconds())

// set delay period on counterparty to non-zero value
conn := path.EndpointA.GetConnection()
conn.DelayPeriod = delayPeriod
suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID, conn)
path.EndpointA.UpdateConnection(func(connection *types.ConnectionEnd) { connection.DelayPeriod = delayPeriod })

// commit in order for proof to return correct value
suite.coordinator.CommitBlock(suite.chainA)
Expand Down Expand Up @@ -341,12 +339,7 @@ func (suite *KeeperTestSuite) TestConnOpenAck() {
suite.Require().NoError(err)

// modify connB to set counterparty connection identifier to wrong identifier
connection, found := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID)
suite.Require().True(found)

connection.Counterparty.ConnectionId = "badconnectionid"

suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID, connection)
path.EndpointA.UpdateConnection(func(c *types.ConnectionEnd) { c.Counterparty.ConnectionId = "badconnectionid" })

err = path.EndpointA.UpdateClient()
suite.Require().NoError(err)
Expand Down
44 changes: 11 additions & 33 deletions modules/core/03-connection/keeper/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ func (suite *KeeperTestSuite) TestVerifyClientState() {
}{
{"verification success", func() {}, true},
{"client state not found", func() {
connection := path.EndpointA.GetConnection()
connection.ClientId = ibctesting.InvalidID
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *types.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })
}, false},
{"consensus state for proof height not found", func() {
heightDiff = 5
Expand Down Expand Up @@ -95,9 +93,7 @@ func (suite *KeeperTestSuite) TestVerifyClientConsensusState() {
}{
{"verification success", func() {}, true},
{"client state not found", func() {
connection := path.EndpointA.GetConnection()
connection.ClientId = ibctesting.InvalidID
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *types.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })
}, false},
{"consensus state not found", func() {
heightDiff = 5
Expand Down Expand Up @@ -169,17 +165,13 @@ func (suite *KeeperTestSuite) TestVerifyConnectionState() {
}{
{"verification success", func() {}, true},
{"client state not found - changed client ID", func() {
connection := path.EndpointA.GetConnection()
connection.ClientId = ibctesting.InvalidID
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *types.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })
}, false},
{"consensus state not found - increased proof height", func() {
heightDiff = 5
}, false},
{"verification failed - connection state is different than proof", func() {
connection := path.EndpointA.GetConnection()
connection.State = types.TRYOPEN
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *types.ConnectionEnd) { c.State = types.TRYOPEN })
}, false},
{"client status is not active - client is expired", func() {
clientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
Expand Down Expand Up @@ -234,9 +226,7 @@ func (suite *KeeperTestSuite) TestVerifyChannelState() {
}{
{"verification success", func() {}, true},
{"client state not found- changed client ID", func() {
connection := path.EndpointA.GetConnection()
connection.ClientId = ibctesting.InvalidID
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *types.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })
}, false},
{"consensus state not found - increased proof height", func() {
heightDiff = 5
Expand Down Expand Up @@ -314,9 +304,7 @@ func (suite *KeeperTestSuite) TestVerifyPacketCommitment() {
timePerBlock = 1
}, false},
{"client state not found- changed client ID", func() {
connection := path.EndpointB.GetConnection()
connection.ClientId = ibctesting.InvalidID
path.EndpointB.SetConnection(connection)
path.EndpointB.UpdateConnection(func(c *types.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })
}, false},
{"consensus state not found - increased proof height", func() {
heightDiff = 5
Expand Down Expand Up @@ -406,9 +394,7 @@ func (suite *KeeperTestSuite) TestVerifyPacketAcknowledgement() {
timePerBlock = 1
}, false},
{"client state not found- changed client ID", func() {
connection := path.EndpointA.GetConnection()
connection.ClientId = ibctesting.InvalidID
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *types.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })
}, false},
{"consensus state not found - increased proof height", func() {
heightDiff = 5
Expand Down Expand Up @@ -507,9 +493,7 @@ func (suite *KeeperTestSuite) TestVerifyPacketReceiptAbsence() {
timePerBlock = 1
}, false},
{"client state not found - changed client ID", func() {
connection := path.EndpointA.GetConnection()
connection.ClientId = ibctesting.InvalidID
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *types.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })
}, false},
{"consensus state not found - increased proof height", func() {
heightDiff = 5
Expand Down Expand Up @@ -613,9 +597,7 @@ func (suite *KeeperTestSuite) TestVerifyNextSequenceRecv() {
timePerBlock = 1
}, false},
{"client state not found- changed client ID", func() {
connection := path.EndpointA.GetConnection()
connection.ClientId = ibctesting.InvalidID
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *types.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })
}, false},
{"consensus state not found - increased proof height", func() {
heightDiff = 5
Expand Down Expand Up @@ -709,9 +691,7 @@ func (suite *KeeperTestSuite) TestVerifyUpgradeErrorReceipt() {
{
name: "fails with bad client id",
malleate: func() {
connection := path.EndpointB.GetConnection()
connection.ClientId = ibctesting.InvalidID
path.EndpointB.SetConnection(connection)
path.EndpointB.UpdateConnection(func(c *types.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })
},
expPass: false,
},
Expand Down Expand Up @@ -792,9 +772,7 @@ func (suite *KeeperTestSuite) TestVerifyUpgrade() {
{
name: "fails with bad client id",
malleate: func() {
connection := path.EndpointB.GetConnection()
connection.ClientId = ibctesting.InvalidID
path.EndpointB.SetConnection(connection)
path.EndpointB.UpdateConnection(func(c *types.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })
},
expPass: false,
},
Expand Down
44 changes: 12 additions & 32 deletions modules/core/04-channel/keeper/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,10 @@ func (suite *KeeperTestSuite) TestChanOpenInit() {
path.SetupConnections()

// modify connA versions
conn := path.EndpointA.GetConnection()
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) {
c.Versions = append(c.Versions, connectiontypes.NewVersion("2", []string{"ORDER_ORDERED", "ORDER_UNORDERED"}))
})

version := connectiontypes.NewVersion("2", []string{"ORDER_ORDERED", "ORDER_UNORDERED"})
conn.Versions = append(conn.Versions, version)

suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(
suite.chainA.GetContext(),
path.EndpointA.ConnectionID, conn,
)
features = []string{"ORDER_ORDERED", "ORDER_UNORDERED"}
suite.chainA.CreatePortCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort)
portCap = suite.chainA.GetPortCapability(ibctesting.MockPort)
Expand All @@ -72,15 +67,10 @@ func (suite *KeeperTestSuite) TestChanOpenInit() {
path.SetupConnections()

// modify connA versions to only support UNORDERED channels
conn := path.EndpointA.GetConnection()
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) {
c.Versions = []*connectiontypes.Version{connectiontypes.NewVersion("1", []string{"ORDER_UNORDERED"})}
})

version := connectiontypes.NewVersion("1", []string{"ORDER_UNORDERED"})
conn.Versions = []*connectiontypes.Version{version}

suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(
suite.chainA.GetContext(),
path.EndpointA.ConnectionID, conn,
)
// NOTE: Opening UNORDERED channels is still expected to pass but ORDERED channels should fail
features = []string{"ORDER_UNORDERED"}
suite.chainA.CreatePortCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort)
Expand Down Expand Up @@ -225,15 +215,10 @@ func (suite *KeeperTestSuite) TestChanOpenTry() {
suite.Require().NoError(err)

// modify connB versions
conn := path.EndpointB.GetConnection()
path.EndpointB.UpdateConnection(func(c *connectiontypes.ConnectionEnd) {
c.Versions = append(c.Versions, connectiontypes.NewVersion("2", []string{"ORDER_ORDERED", "ORDER_UNORDERED"}))
})

version := connectiontypes.NewVersion("2", []string{"ORDER_ORDERED", "ORDER_UNORDERED"})
conn.Versions = append(conn.Versions, version)

suite.chainB.App.GetIBCKeeper().ConnectionKeeper.SetConnection(
suite.chainB.GetContext(),
path.EndpointB.ConnectionID, conn,
)
suite.chainB.CreatePortCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort)
portCap = suite.chainB.GetPortCapability(ibctesting.MockPort)
}, false},
Expand All @@ -244,15 +229,10 @@ func (suite *KeeperTestSuite) TestChanOpenTry() {
suite.Require().NoError(err)

// modify connA versions to only support UNORDERED channels
conn := path.EndpointA.GetConnection()
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) {
c.Versions = []*connectiontypes.Version{connectiontypes.NewVersion("1", []string{"ORDER_UNORDERED"})}
})

version := connectiontypes.NewVersion("1", []string{"ORDER_UNORDERED"})
conn.Versions = []*connectiontypes.Version{version}

suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(
suite.chainA.GetContext(),
path.EndpointA.ConnectionID, conn,
)
suite.chainA.CreatePortCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort)
portCap = suite.chainA.GetPortCapability(ibctesting.MockPort)
}, false},
Expand Down
20 changes: 8 additions & 12 deletions modules/core/04-channel/keeper/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ func (suite *KeeperTestSuite) TestSendPacket() {
solomachine := ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "solomachinesingle", "testing", 1)
path.EndpointA.ClientID = clienttypes.FormatClientIdentifier(exported.Solomachine, 10)
path.EndpointA.SetClientState(solomachine.ClientState())
connection := path.EndpointA.GetConnection()
connection.ClientId = path.EndpointA.ClientID
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.ClientId = path.EndpointA.ClientID })

channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
}, true},
Expand All @@ -78,9 +76,8 @@ func (suite *KeeperTestSuite) TestSendPacket() {
solomachine := ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "solomachinesingle", "testing", 1)
path.EndpointA.ClientID = clienttypes.FormatClientIdentifier(exported.Solomachine, 10)
path.EndpointA.SetClientState(solomachine.ClientState())
connection := path.EndpointA.GetConnection()
connection.ClientId = path.EndpointA.ClientID
path.EndpointA.SetConnection(connection)

path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.ClientId = path.EndpointA.ClientID })

channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
}, true},
Expand Down Expand Up @@ -134,9 +131,7 @@ func (suite *KeeperTestSuite) TestSendPacket() {
sourceChannel = path.EndpointA.ChannelID

// change connection client ID
connection := path.EndpointA.GetConnection()
connection.ClientId = ibctesting.InvalidID
suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID, connection)
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.ClientId = ibctesting.InvalidID })

channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
}, false},
Expand Down Expand Up @@ -185,11 +180,12 @@ func (suite *KeeperTestSuite) TestSendPacket() {
solomachine := ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "solomachinesingle", "testing", 1)
path.EndpointA.ClientID = clienttypes.FormatClientIdentifier(exported.Solomachine, 10)
path.EndpointA.SetClientState(solomachine.ClientState())
connection := path.EndpointA.GetConnection()
connection.ClientId = path.EndpointA.ClientID
path.EndpointA.SetConnection(connection)

path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.ClientId = path.EndpointA.ClientID })

clientState := path.EndpointA.GetClientState()
connection := path.EndpointA.GetConnection()

timestamp, err := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetTimestampAtHeight(suite.chainA.GetContext(), connection, clientState.GetLatestHeight())
suite.Require().NoError(err)

Expand Down
47 changes: 14 additions & 33 deletions modules/core/04-channel/keeper/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ func (suite *KeeperTestSuite) TestChanUpgradeInit() {
{
"invalid proposed channel connection state",
func() {
connectionEnd := path.EndpointA.GetConnection()
connectionEnd.State = connectiontypes.UNINITIALIZED

suite.chainA.GetSimApp().GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainA.GetContext(), "connection-100", connectionEnd)
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.State = connectiontypes.UNINITIALIZED })
upgradeFields.ConnectionHops = []string{"connection-100"}
},
false,
Expand Down Expand Up @@ -183,9 +180,7 @@ func (suite *KeeperTestSuite) TestChanUpgradeTry() {
{
"invalid connection state",
func() {
connectionEnd := path.EndpointB.GetConnection()
connectionEnd.State = connectiontypes.UNINITIALIZED
suite.chainB.GetSimApp().GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainB.GetContext(), path.EndpointB.ConnectionID, connectionEnd)
path.EndpointB.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.State = connectiontypes.UNINITIALIZED })
},
connectiontypes.ErrInvalidConnectionState,
},
Expand Down Expand Up @@ -706,9 +701,7 @@ func (suite *KeeperTestSuite) TestChanUpgradeAck() {
{
"invalid connection state",
func() {
connectionEnd := path.EndpointA.GetConnection()
connectionEnd.State = connectiontypes.UNINITIALIZED
path.EndpointA.SetConnection(connectionEnd)
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.State = connectiontypes.UNINITIALIZED })
},
connectiontypes.ErrInvalidConnectionState,
},
Expand Down Expand Up @@ -1074,9 +1067,7 @@ func (suite *KeeperTestSuite) TestChanUpgradeConfirm() {
{
"invalid connection state",
func() {
connectionEnd := path.EndpointB.GetConnection()
connectionEnd.State = connectiontypes.UNINITIALIZED
path.EndpointB.SetConnection(connectionEnd)
path.EndpointB.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.State = connectiontypes.UNINITIALIZED })
},
connectiontypes.ErrInvalidConnectionState,
},
Expand Down Expand Up @@ -1340,9 +1331,7 @@ func (suite *KeeperTestSuite) TestChanUpgradeOpen() {
{
"invalid connection state",
func() {
connectionEnd := path.EndpointA.GetConnection()
connectionEnd.State = connectiontypes.UNINITIALIZED
path.EndpointA.SetConnection(connectionEnd)
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.State = connectiontypes.UNINITIALIZED })
},
connectiontypes.ErrInvalidConnectionState,
},
Expand Down Expand Up @@ -2125,9 +2114,7 @@ func (suite *KeeperTestSuite) TestChanUpgradeTimeout() {
{
"connection not open",
func() {
connectionEnd := path.EndpointA.GetConnection()
connectionEnd.State = connectiontypes.UNINITIALIZED
path.EndpointA.SetConnection(connectionEnd)
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.State = connectiontypes.UNINITIALIZED })
},
connectiontypes.ErrInvalidConnectionState,
},
Expand Down Expand Up @@ -2316,9 +2303,7 @@ func (suite *KeeperTestSuite) TestStartFlush() {
{
"connection state is not in OPEN state",
func() {
conn := path.EndpointB.GetConnection()
conn.State = connectiontypes.INIT
path.EndpointB.SetConnection(conn)
path.EndpointB.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.State = connectiontypes.INIT })
},
connectiontypes.ErrInvalidConnectionState,
},
Expand Down Expand Up @@ -2423,9 +2408,7 @@ func (suite *KeeperTestSuite) TestValidateUpgradeFields() {
{
name: "fails when connection is not open",
malleate: func() {
connection := path.EndpointA.GetConnection()
connection.State = connectiontypes.UNINITIALIZED
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) { c.State = connectiontypes.UNINITIALIZED })
},
expPass: false,
},
Expand All @@ -2435,9 +2418,9 @@ func (suite *KeeperTestSuite) TestValidateUpgradeFields() {
// update channel version first so that existing channel end is not identical to proposed upgrade
proposedUpgrade.Version = mock.UpgradeVersion

connection := path.EndpointA.GetConnection()
connection.Versions = []*connectiontypes.Version{}
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) {
c.Versions = []*connectiontypes.Version{}
})
},
expPass: false,
},
Expand All @@ -2447,11 +2430,9 @@ func (suite *KeeperTestSuite) TestValidateUpgradeFields() {
// update channel version first so that existing channel end is not identical to proposed upgrade
proposedUpgrade.Version = mock.UpgradeVersion

connection := path.EndpointA.GetConnection()
connection.Versions = []*connectiontypes.Version{
connectiontypes.NewVersion("1", []string{"ORDER_ORDERED"}),
}
path.EndpointA.SetConnection(connection)
path.EndpointA.UpdateConnection(func(c *connectiontypes.ConnectionEnd) {
c.Versions = []*connectiontypes.Version{connectiontypes.NewVersion("1", []string{"ORDER_ORDERED"})}
})
},
expPass: false,
},
Expand Down
9 changes: 9 additions & 0 deletions testing/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -892,3 +892,12 @@ func (endpoint *Endpoint) GetProposedUpgrade() channeltypes.Upgrade {

return upgrade
}

// UpdateConnection updates the connection associated with the given endpoint. It accepts a
// closure which takes a connection allowing the caller to modify the connection fields.
func (endpoint *Endpoint) UpdateConnection(updater func(connection *connectiontypes.ConnectionEnd)) {
DimitrisJim marked this conversation as resolved.
Show resolved Hide resolved
connection := endpoint.GetConnection()
updater(&connection)

endpoint.SetConnection(connection)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

don't need to commit and update client as original issue had. Lmk if it would be good to add for some reason.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's only necessary if we need to generate a proof on the modified connection

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you think its beneficial to slip it back in? don't currently do that atm but maybe in futureland?

Copy link
Member

Choose a reason for hiding this comment

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

I think it could be done manually when required

}
Loading