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

imp: add logic for setting NextSeqRecv, NextSeqAck in OnChanUpgradeOpen #5438

Merged
merged 26 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b7fbb39
add logic for setting next seq recv, update tests
charleenfei Dec 18, 2023
a3a10ab
update next seq recv and ack logic to spec
charleenfei Dec 19, 2023
045fdd2
Merge branch '04-channel-upgrades' into charly/set_nextseqrecv
charleenfei Dec 19, 2023
129cc62
typos
charleenfei Dec 19, 2023
c95192b
update test
charleenfei Dec 19, 2023
d7379a0
Merge branch 'charly/set_nextseqrecv' of github.com:cosmos/ibc-go int…
charleenfei Dec 19, 2023
972faa6
wip changes for spec
charleenfei Dec 19, 2023
3864e91
wip set counterparty upgrade
charleenfei Dec 19, 2023
122c25b
update naming
charleenfei Dec 19, 2023
81c31ee
update endpoint
charleenfei Dec 19, 2023
0ca4bca
spec changes
charleenfei Dec 19, 2023
a7a0834
test that counterparty upgrade has been set
charleenfei Dec 19, 2023
adaac86
Merge branch '04-channel-upgrades' into charly/set_nextseqrecv
charleenfei Dec 19, 2023
9a6f4c1
update err message
charleenfei Dec 19, 2023
ed80a45
pr review
charleenfei Dec 19, 2023
1dd6dcd
Merge branch 'charly/set_nextseqrecv' of github.com:cosmos/ibc-go int…
charleenfei Dec 19, 2023
7b2b487
naming nit
crodriguezvega Dec 19, 2023
4fcc075
Merge branch '04-channel-upgrades' into charly/set_nextseqrecv
charleenfei Dec 20, 2023
70973c9
pr review
charleenfei Dec 20, 2023
c6cd056
Merge branch '04-channel-upgrades' into charly/set_nextseqrecv
charleenfei Dec 20, 2023
9e96b13
refactor test
charleenfei Dec 20, 2023
09fafce
Merge branch 'charly/set_nextseqrecv' of github.com:cosmos/ibc-go int…
charleenfei Dec 20, 2023
8fdc927
small test changes
charleenfei Dec 20, 2023
7d7f60a
lint
charleenfei Dec 20, 2023
608d9e2
Add key for storing counterparty next sequence send during WriteUpgra…
DimitrisJim Dec 20, 2023
add52f8
Merge branch '04-channel-upgrades' into charly/set_nextseqrecv
charleenfei Dec 20, 2023
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
36 changes: 30 additions & 6 deletions modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,22 +611,46 @@ func (k Keeper) HasInflightPackets(ctx sdk.Context, portID, channelID string) bo
return iterator.Valid()
}

// GetAcknowledgementPruningSequence gets a channel's acknowledgement pruning sequence from the store.
func (k Keeper) GetAcknowledgementPruningSequence(ctx sdk.Context, portID, channelID string) (uint64, bool) {
// SetPruningSequenceEnd sets the channel's pruning sequence end to the store.
func (k Keeper) SetPruningSequenceEnd(ctx sdk.Context, portID, channelID string, sequence uint64) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.AckPruningSequenceKey(portID, channelID))
bz := sdk.Uint64ToBigEndian(sequence)
store.Set(host.PruningSequenceEndKey(portID, channelID), bz)
}

// GetPruningSequenceEnd gets a channel's pruning sequence end from the store.
func (k Keeper) GetPruningSequenceEnd(ctx sdk.Context, portID, channelID string) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.PruningSequenceEndKey(portID, channelID))
if len(bz) == 0 {
return 0, false
}

return sdk.BigEndianToUint64(bz), true
}

// SetAcknowledgementPruningSequence sets a channel's acknowledgement pruning sequence to the store.
func (k Keeper) SetAcknowledgementPruningSequence(ctx sdk.Context, portID, channelID string, sequence uint64) {
// SetPruningSequenceStart sets a channel's pruning sequence start to the store.
func (k Keeper) SetPruningSequenceStart(ctx sdk.Context, portID, channelID string, sequence uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(sequence)
store.Set(host.AckPruningSequenceKey(portID, channelID), bz)
store.Set(host.PruningSequenceStartKey(portID, channelID), bz)
}

// GetPruningSequenceStart gets a channel's pruning sequence start from the store.
func (k Keeper) GetPruningSequenceStart(ctx sdk.Context, portID, channelID string) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.PruningSequenceStartKey(portID, channelID))
if len(bz) == 0 {
return 0
}

return sdk.BigEndianToUint64(bz)
}

// HasPruningSequenceStart returns true if the pruning sequence start is set for the specified channel.
func (k Keeper) HasPruningSequenceStart(ctx sdk.Context, portID, channelID string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(host.PruningSequenceStartKey(portID, channelID))
}

// PruneAcknowledgements prunes packet acknowledgements from the store that have a sequence number less than or equal to the pruning sequence.
Expand Down
24 changes: 0 additions & 24 deletions modules/core/04-channel/keeper/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,30 +351,6 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
channel := path.EndpointB.GetChannel()
channel.State = types.FLUSHING
path.EndpointB.SetChannel(channel)

Copy link
Contributor Author

@charleenfei charleenfei Dec 19, 2023

Choose a reason for hiding this comment

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

i removed these tests bc we aren't checking these sequences in RecvPacket anymore

// set last packet sent sequence to sequence + 1
counterpartyUpgrade := types.Upgrade{LatestSequenceSend: sequence + 1}
path.EndpointB.SetChannelCounterpartyUpgrade(counterpartyUpgrade)
},
nil,
},
{
"success with an counterparty latest sequence send set to 0",
func() {
suite.coordinator.Setup(path)
sequence, err := path.EndpointA.SendPacket(defaultTimeoutHeight, disabledTimeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)

packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

channel := path.EndpointB.GetChannel()
channel.State = types.FLUSHING
path.EndpointB.SetChannel(channel)

// set last packet sent sequence to zero.
counterpartyUpgrade := types.Upgrade{LatestSequenceSend: 0}
path.EndpointB.SetChannelCounterpartyUpgrade(counterpartyUpgrade)
},
nil,
},
Expand Down
44 changes: 34 additions & 10 deletions modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,6 @@ func (k Keeper) WriteUpgradeAckChannel(ctx sdk.Context, portID, channelID string
if !k.HasInflightPackets(ctx, portID, channelID) {
channel.State = types.FLUSHCOMPLETE
k.SetChannel(ctx, portID, channelID, channel)
} else {
// the counterparty upgrade is only required if the channel is still in the FLUSHING state.
// this gets read when timing out and acknowledging packets.
k.SetCounterpartyUpgrade(ctx, portID, channelID, counterpartyUpgrade)
}

upgrade, found := k.GetUpgrade(ctx, portID, channelID)
Expand All @@ -364,6 +360,7 @@ func (k Keeper) WriteUpgradeAckChannel(ctx sdk.Context, portID, channelID string
upgrade.Fields.Version = counterpartyUpgrade.Fields.Version

k.SetUpgrade(ctx, portID, channelID, upgrade)
k.SetCounterpartyUpgrade(ctx, portID, channelID, counterpartyUpgrade)

k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "state", channel.State.String())
return channel, upgrade
Expand Down Expand Up @@ -461,13 +458,10 @@ func (k Keeper) WriteUpgradeConfirmChannel(ctx sdk.Context, portID, channelID st
previousState := channel.State
channel.State = types.FLUSHCOMPLETE
k.SetChannel(ctx, portID, channelID, channel)

k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", channel.State)
} else {
// the counterparty upgrade is only required if the channel is still in the FLUSHING state.
// this gets read when timing out and acknowledging packets.
k.SetCounterpartyUpgrade(ctx, portID, channelID, counterpartyUpgrade)
}

k.SetCounterpartyUpgrade(ctx, portID, channelID, counterpartyUpgrade)
charleenfei marked this conversation as resolved.
Show resolved Hide resolved
return channel
}

Expand Down Expand Up @@ -568,6 +562,36 @@ func (k Keeper) WriteUpgradeOpenChannel(ctx sdk.Context, portID, channelID strin
panic(fmt.Errorf("could not find upgrade when updating channel state, channelID: %s, portID: %s", channelID, portID))
}

counterpartyUpgrade, found := k.GetCounterpartyUpgrade(ctx, portID, channelID)
if !found {
panic(fmt.Errorf("could not find counterparty upgrade when updating channel state, channelID: %s, portID: %s", channelID, portID))
}

// next seq recv and ack is used for ordered channels to verify the packet has been received/acked in the correct order
// this is no longer necessary if the channel is UNORDERED and should be reset to 1
if channel.Ordering == types.ORDERED && upgrade.Fields.Ordering == types.UNORDERED {
k.SetNextSequenceRecv(ctx, portID, channelID, 1)
k.SetNextSequenceAck(ctx, portID, channelID, 1)
}

// next seq recv and ack should updated when moving from UNORDERED to ORDERED using the counterparty NextSequenceSend as set just after blocking new packet sends.
// we can be sure that the next packet we are set to receive will be the first packet the counterparty sends after reopening.
// we can be sure that our next acknowledgement will be our first packet sent after upgrade, as the counterparty processed all sent packets after flushing completes.
if channel.Ordering == types.UNORDERED && upgrade.Fields.Ordering == types.ORDERED {
k.SetNextSequenceRecv(ctx, portID, channelID, counterpartyUpgrade.NextSequenceSend)
k.SetNextSequenceAck(ctx, portID, channelID, upgrade.NextSequenceSend)
}

charleenfei marked this conversation as resolved.
Show resolved Hide resolved
// set the counterparty next sequence send as pruning sequence end in order to have upper bound to prune to
k.SetPruningSequenceEnd(ctx, portID, channelID, counterpartyUpgrade.NextSequenceSend)

// First upgrade for this channel will set the pruning sequence to 1, the starting sequence for pruning.
// Subsequent upgrades will not modify the pruning sequence thereby allowing pruning to continue from the last
// pruned sequence.
if !k.HasPruningSequenceStart(ctx, portID, channelID) {
k.SetPruningSequenceStart(ctx, portID, channelID, 1)
}

// Switch channel fields to upgrade fields and set channel state to OPEN
previousState := channel.State
channel.Ordering = upgrade.Fields.Ordering
Expand Down Expand Up @@ -803,7 +827,7 @@ func (k Keeper) startFlushing(ctx sdk.Context, portID, channelID string, upgrade
return errorsmod.Wrapf(types.ErrSequenceSendNotFound, "port ID (%s) channel ID (%s)", portID, channelID)
}

upgrade.LatestSequenceSend = nextSequenceSend - 1
upgrade.NextSequenceSend = nextSequenceSend
upgrade.Timeout = k.getAbsoluteUpgradeTimeout(ctx)
k.SetUpgrade(ctx, portID, channelID, *upgrade)

Expand Down
Loading
Loading