Skip to content

Commit

Permalink
Add --query-packets-chunk-size flag to clear packets CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Jan 4, 2024
1 parent dd8f41d commit 6d69cb7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
50 changes: 49 additions & 1 deletion crates/relayer-cli/src/commands/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ pub struct ClearPacketsCmd {
help = "use the given signing key for the counterparty chain (default: `counterparty_key_name` config)"
)]
counterparty_key_name: Option<String>,

#[clap(
long = "query-packets-chunk-size",
help = "number of packets to fetch at once from the chain (default: `query_packets_chunk_size` config)"
)]
query_packets_chunk_size: Option<usize>,
}

impl Override<Config> for ClearPacketsCmd {
Expand Down Expand Up @@ -112,6 +118,17 @@ impl Runnable for ClearPacketsCmd {
}
}

// If `query_packets_chunk_size` is provided, overwrite the chain's
// `query_packets_chunk_size` parameter
if let Some(chunk_size) = self.query_packets_chunk_size {
match chains.src.config() {
Ok(mut src_chain_cfg) => {
src_chain_cfg.set_query_packets_chunk_size(chunk_size);
}
Err(e) => Output::error(e).exit(),
}
}

let mut ev_list = vec![];

// Construct links in both directions.
Expand Down Expand Up @@ -180,6 +197,7 @@ mod tests {
channel_id: ChannelId::from_str("channel-07").unwrap(),
key_name: None,
counterparty_key_name: None,
query_packets_chunk_size: None
},
ClearPacketsCmd::parse_from([
"test",
Expand All @@ -201,7 +219,8 @@ mod tests {
port_id: PortId::from_str("port_id").unwrap(),
channel_id: ChannelId::from_str("channel-07").unwrap(),
key_name: None,
counterparty_key_name: None
counterparty_key_name: None,
query_packets_chunk_size: None
},
ClearPacketsCmd::parse_from([
"test",
Expand All @@ -224,6 +243,7 @@ mod tests {
channel_id: ChannelId::from_str("channel-07").unwrap(),
key_name: Some("key_name".to_owned()),
counterparty_key_name: None,
query_packets_chunk_size: None
},
ClearPacketsCmd::parse_from([
"test",
Expand All @@ -248,6 +268,7 @@ mod tests {
channel_id: ChannelId::from_str("channel-07").unwrap(),
key_name: None,
counterparty_key_name: Some("counterparty_key_name".to_owned()),
query_packets_chunk_size: None
},
ClearPacketsCmd::parse_from([
"test",
Expand All @@ -263,6 +284,33 @@ mod tests {
)
}

#[test]
fn test_clear_packets_query_packets_chunk_size() {
assert_eq!(
ClearPacketsCmd {
chain_id: ChainId::from_string("chain_id"),
port_id: PortId::from_str("port_id").unwrap(),
channel_id: ChannelId::from_str("channel-07").unwrap(),
key_name: None,
counterparty_key_name: Some("counterparty_key_name".to_owned()),
query_packets_chunk_size: Some(100),
},
ClearPacketsCmd::parse_from([
"test",
"--chain",
"chain_id",
"--port",
"port_id",
"--channel",
"channel-07",
"--counterparty-key-name",
"counterparty_key_name",
"--query-packets-chunk-size",
"100"
])
)
}

#[test]
fn test_clear_packets_no_chan() {
assert!(ClearPacketsCmd::try_parse_from([
Expand Down
6 changes: 6 additions & 0 deletions crates/relayer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,12 @@ impl ChainConfig {
Self::CosmosSdk(config) => config.query_packets_chunk_size,
}
}

pub fn set_query_packets_chunk_size(&mut self, query_packets_chunk_size: usize) {
match self {
Self::CosmosSdk(config) => config.query_packets_chunk_size = query_packets_chunk_size,
}
}
}

/// Attempt to load and parse the TOML config file as a `Config`.
Expand Down
16 changes: 16 additions & 0 deletions crates/relayer/src/link/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,16 @@ impl<ChainA: ChainHandle, ChainB: ChainHandle> Link<ChainA, ChainB> {
None => Qualified::SmallerEqual(src_response_height),
};

let chunk_size = self
.a_to_b
.src_chain()
.config()
.map_or(50, |cfg| cfg.query_packets_chunk_size());

self.relay_packet_messages(
sequences,
query_height,
chunk_size,
query_send_packet_events,
TrackingId::new_static("packet-recv"),
)
Expand Down Expand Up @@ -163,9 +170,16 @@ impl<ChainA: ChainHandle, ChainB: ChainHandle> Link<ChainA, ChainB> {
None => Qualified::SmallerEqual(src_response_height),
};

let chunk_size = self
.a_to_b
.src_chain()
.config()
.map_or(50, |cfg| cfg.query_packets_chunk_size());

self.relay_packet_messages(
sequences,
query_height,
chunk_size,
query_write_ack_events,
TrackingId::new_static("packet-ack"),
)
Expand All @@ -175,6 +189,7 @@ impl<ChainA: ChainHandle, ChainB: ChainHandle> Link<ChainA, ChainB> {
&self,
sequences: Vec<Sequence>,
query_height: Qualified<Height>,
chunk_size: usize,
query_fn: QueryFn,
tracking_id: TrackingId,
) -> Result<Vec<IbcEvent>, LinkError>
Expand All @@ -191,6 +206,7 @@ impl<ChainA: ChainHandle, ChainB: ChainHandle> Link<ChainA, ChainB> {
query_height,
self.a_to_b.src_chain(),
&self.a_to_b.path_id,
chunk_size,
query_fn,
);

Expand Down

0 comments on commit 6d69cb7

Please sign in to comment.