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

Duplicate packets on start #2256

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
042480e
Pull in upstream changes
seanchen1991 Apr 22, 2022
69a1f8f
Merge branch 'informalsystems:master' into master
seanchen1991 Apr 22, 2022
d0be1f8
Pull in upstream changes
seanchen1991 May 3, 2022
45e082f
Pull in upstream changes
seanchen1991 May 4, 2022
f0fc83d
Merge branch 'master' of https://github.com/informalsystems/ibc-rs
seanchen1991 May 4, 2022
8bc79b7
Merge branch 'master' of https://github.com/informalsystems/ibc-rs
seanchen1991 May 10, 2022
9bee642
Merge branch 'master' of https://github.com/informalsystems/ibc-rs
seanchen1991 May 16, 2022
82cdf25
Merge branch 'master' of https://github.com/informalsystems/ibc-rs
seanchen1991 May 25, 2022
807f2ce
Remove unnecessary destructuring assignment
seanchen1991 May 26, 2022
d1f419e
Decrement height when scheduling packet clearing
seanchen1991 Jun 1, 2022
93f2a57
Add changelog entry
seanchen1991 Jun 1, 2022
305a80d
Clarify comment
seanchen1991 Jun 6, 2022
99f2bc3
fix merge conflict
seanchen1991 Jun 7, 2022
e338bbd
Remove unnecessary destructure
seanchen1991 Jun 7, 2022
087da42
Initiate clear packet flow when a IbcEvent is received
seanchen1991 Jun 8, 2022
5ebf439
Revert to more straightforward logic to see if CI still breaks
seanchen1991 Jun 8, 2022
f20b3df
Merge branch 'master' into duplicate-packets-on-start
seanchen1991 Jun 16, 2022
650a57a
Refactor handle_packet_cmd
seanchen1991 Jun 16, 2022
50e5d53
Clean up handle_packet_cmd
seanchen1991 Jun 16, 2022
566ca32
Update handle_packet_cmd doc comment
seanchen1991 Jun 16, 2022
f56d52f
Incorporate PR feedback
seanchen1991 Jun 16, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix duplicate packets being generated on start. ([#2093](https://github.com/informalsystems/ibc-rs/issues/2093))
74 changes: 48 additions & 26 deletions relayer/src/worker/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,49 +99,71 @@ pub fn spawn_packet_cmd_worker<ChainA: ChainHandle, ChainB: ChainHandle>(
})
}

/// Receives worker commands, which may be:
/// - IbcEvent => then it updates schedule
/// - NewBlock => schedules packet clearing
/// - Shutdown => exits
/// Receives worker commands and handles them accordingly.
///
/// Regardless of the incoming command, this method
/// also refreshes and executes any scheduled operational
/// data that is ready.
/// Given an `IbcEvent` command, updates the schedule and initiates
/// packet clearing if the `should_clear_on_start` flag has been toggled.
///
/// Given a `NewBlock` command, checks if packet clearing should occur
/// and performs it if so.
///
/// Given a `ClearPendingPackets` command, clears pending packets.
///
/// Regardless of the incoming command, this method also refreshes and
/// and executes any scheduled operational data that is ready.
fn handle_packet_cmd<ChainA: ChainHandle, ChainB: ChainHandle>(
link: &mut Link<ChainA, ChainB>,
should_clear_on_start: &mut bool,
clear_interval: u64,
path: &Packet,
cmd: WorkerCmd,
) -> Result<(), TaskError<RunError>> {
match cmd {
WorkerCmd::IbcEvents { batch } => handle_update_schedule(link, clear_interval, path, batch),

// Handle the arrival of an event signaling that the
// source chain has advanced to a new block.
WorkerCmd::NewBlock {
height,
new_block: _,
} => {
// Handle packet clearing which is triggered from a command
let (do_clear, maybe_height) = match &cmd {
WorkerCmd::IbcEvents { batch } => {
if *should_clear_on_start {
seanchen1991 marked this conversation as resolved.
Show resolved Hide resolved
handle_clear_packet(link, clear_interval, path, Some(height))?;
(true, Some(batch.height))
} else {
(false, None)
}
}

// Clear the flag only if handle_clear_packet succeeds
*should_clear_on_start = false;
Ok(())
} else if should_clear_packets(clear_interval, height) {
handle_clear_packet(link, clear_interval, path, Some(height))
// Handle the arrival of an event signaling that the
// source chain has advanced to a new block
WorkerCmd::NewBlock { height, .. } => {
if *should_clear_on_start || should_clear_packets(clear_interval, *height) {
(true, Some(*height))
} else {
Ok(())
(false, None)
}
}

WorkerCmd::ClearPendingPackets => handle_clear_packet(link, clear_interval, path, None),
WorkerCmd::ClearPendingPackets => (true, None),
};

if do_clear {
handle_clear_packet(link, clear_interval, path, maybe_height)?;

// Reset the `clear_on_start` flag
if *should_clear_on_start {
*should_clear_on_start = false;
}
}

// Handle command-specific task
if let WorkerCmd::IbcEvents { batch } = cmd {
handle_update_schedule(link, clear_interval, path, batch)
} else {
Ok(())
}
}

/// Whether or not to clear pending packets at this `step` for the given height.
/// Packets are cleared if `clear_interval` is not `0` and if we have reached the interval.
/// Whether or not to clear pending packets at this `step` for some height.
/// If the relayer has been configured to clear packets on start and that has not
/// occurred yet, then packets are cleared.
///
/// If the specified height is reached, then packets are cleared if `clear_interval`
/// is not `0` and if we have reached the interval.
fn should_clear_packets(clear_interval: u64, height: Height) -> bool {
clear_interval != 0 && height.revision_height % clear_interval == 0
}
Expand Down