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

Ensure all events in a batch are processed, even when it contains events for unknown chains #4022

Merged
merged 6 commits into from
Jun 10, 2024

Conversation

ljoss17
Copy link
Contributor

@ljoss17 ljoss17 commented Jun 7, 2024

Closes: #4021
Closes: #4034

Description

Note: This PR also bumps all the MSRVs to v1.76.0


PR author checklist:

  • Added changelog entry, using unclog.
  • Added tests: integration (for Hermes) or unit/mock tests (for modules).
  • Linked to GitHub issue.
  • Updated code comments and documentation (e.g., docs/).
  • Tagged one reviewer who will be the one responsible for shepherding this PR.

Reviewer checklist:

  • Reviewed Files changed in the GitHub PR explorer.
  • Manually tested (in case integration/unit/mock tests are absent).

@ljoss17 ljoss17 requested a review from romac June 7, 2024 07:46
@romac romac added this pull request to the merge queue Jun 7, 2024
@romac romac removed this pull request from the merge queue due to a manual request Jun 7, 2024
let client_filter_outcome = match object {
Object::Client(client) => client_state_filter.control_client_object(registry, client),
Object::Connection(conn) => client_state_filter.control_conn_object(registry, conn),
Object::Channel(chan) => client_state_filter.control_chan_object(registry, chan),
Object::Packet(packet) => client_state_filter.control_packet_object(registry, packet),
Object::CrossChainQuery(ccq) => Ok(ccq.intended_for_known_dst_chain(&config.chains)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curious why we didn't implement something like client_state_filter.control_query_object() here in the past?

Copy link
Member

Choose a reason for hiding this comment

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

An oversight. We could also implement that check there but right it's so simple that it does not require the whole filtering machinery.

Copy link
Collaborator

@ancazamfir ancazamfir Jun 7, 2024

Choose a reason for hiding this comment

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

sure but the added check is not really a "client_filter_outcome". Not sure if we tested the scenario with this PR branch in the issue but it looks like if the chain is not configured we will return an error from relay_on_object() and print a misleading trace.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Related to my other comment, why can't we do the config check for object.dst_chain_id() before relay_on_object call ?
Then do proper client filtering in relay_on_object and also fix that trace message.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another thing, without the fix I think we hit this line and we exit without processing the other events in the batch, maybe we need to log an error and continue.

Copy link
Member

Choose a reason for hiding this comment

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

Related to my other comment, why can't we do the config check for object.dst_chain_id() before relay_on_object call ?
Then do proper client filtering in relay_on_object and also fix that trace message

Agreed, seems like the best way to go

Copy link
Member

@romac romac Jun 10, 2024

Choose a reason for hiding this comment

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

@ancazamfir What do you think of this patch? It should address all the problems you've raised.

Show diff
diff --git a/crates/relayer/src/object.rs b/crates/relayer/src/object.rs
index 4716a5d5c..aac3efb25 100644
--- a/crates/relayer/src/object.rs
+++ b/crates/relayer/src/object.rs
@@ -21,9 +21,7 @@ use crate::chain::{
     handle::ChainHandle,
     requests::{IncludeProof, QueryClientStateRequest, QueryHeight},
 };
-use crate::config::ChainConfig;
 use crate::error::Error as RelayerError;
-use crate::supervisor::client_state_filter::Permission;
 use crate::supervisor::Error as SupervisorError;
 
 /// Client
@@ -147,17 +145,6 @@ impl CrossChainQuery {
     pub fn short_name(&self) -> String {
         format!("cross_chain_query::{}/{}", self.dst_chain_id, self.query_id)
     }
-
-    pub fn intended_for_known_dst_chain(&self, chain_configs: &[ChainConfig]) -> Permission {
-        if chain_configs
-            .iter()
-            .any(|config| *config.id() == self.dst_chain_id)
-        {
-            Permission::Allow
-        } else {
-            Permission::Deny
-        }
-    }
 }
 
 /// An object determines the amount of parallelism that can
diff --git a/crates/relayer/src/supervisor.rs b/crates/relayer/src/supervisor.rs
index 9a5b0f17a..478aecb84 100644
--- a/crates/relayer/src/supervisor.rs
+++ b/crates/relayer/src/supervisor.rs
@@ -381,7 +381,7 @@ fn relay_on_object<Chain: ChainHandle>(
         Object::Connection(conn) => client_state_filter.control_conn_object(registry, conn),
         Object::Channel(chan) => client_state_filter.control_chan_object(registry, chan),
         Object::Packet(packet) => client_state_filter.control_packet_object(registry, packet),
-        Object::CrossChainQuery(ccq) => Ok(ccq.intended_for_known_dst_chain(&config.chains)),
+        Object::CrossChainQuery(_ccq) => Ok(Permission::Allow),
         Object::Wallet(_wallet) => Ok(Permission::Allow),
     };
 
@@ -816,8 +816,33 @@ fn process_batch<Chain: ChainHandle>(
         workers.notify_new_block(&src_chain.id(), batch.height, new_block);
     }
 
-    // Forward the IBC events.
+    // Forward the IBC events to the appropriate workers
     for (object, events_with_heights) in collected.per_object.into_iter() {
+        if events_with_heights.is_empty() {
+            // Event batch is empty, nothing to do
+            continue;
+        }
+
+        let Ok(src_chain) = registry.get_or_spawn(object.src_chain_id()) else {
+            trace!(
+                "skipping events for '{}': source chain '{}' is not registered",
+                object.short_name(),
+                object.src_chain_id()
+            );
+
+            continue;
+        };
+
+        let Ok(dst_chain) = registry.get_or_spawn(object.dst_chain_id()) else {
+            trace!(
+                "skipping events for '{}': destination chain '{}' is not registered",
+                object.short_name(),
+                object.src_chain_id()
+            );
+
+            continue;
+        };
+
         if !relay_on_object(
             config,
             registry,
@@ -826,32 +851,23 @@ fn process_batch<Chain: ChainHandle>(
             &object,
         ) {
             trace!(
-                "skipping events for '{}'. \
-                reason: filtering is enabled and channel does not match any allowed channels",
+                "skipping events for '{}': filtering is enabled and channel does not match any allowed channels",
                 object.short_name()
             );
 
             continue;
         }
 
-        if events_with_heights.is_empty() {
-            continue;
-        }
-
-        let src = registry
-            .get_or_spawn(object.src_chain_id())
-            .map_err(Error::spawn)?;
-
-        let dst = registry
-            .get_or_spawn(object.dst_chain_id())
-            .map_err(Error::spawn)?;
-
         if let Object::Packet(ref _path) = object {
-            // Update telemetry info
-            telemetry!(send_telemetry(&src, &dst, &events_with_heights, _path));
+            telemetry!(send_telemetry(
+                &src_chain,
+                &dst_chain,
+                &events_with_heights,
+                _path
+            ));
         }
 
-        let worker = workers.get_or_spawn(object, src, dst, config);
+        let worker = workers.get_or_spawn(object, src_chain, dst_chain, config);
 
         worker.send_events(
             batch.height,

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks great! The skipping events trace message should be more general imo, it's not only channel filters that can dictate an event should be ignored, it's filtering in general.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps also change the issue/PR to make it more general as it applies to all events

Copy link
Member

Choose a reason for hiding this comment

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

Created a new issue and added a new changelog entry

@romac romac changed the title Discard CCQs for unconfigured chains Ensure all events in a batch are processed, even when it contains events for unknown chains Jun 10, 2024
@romac romac requested a review from ancazamfir June 10, 2024 14:33
Copy link
Collaborator

@ancazamfir ancazamfir left a comment

Choose a reason for hiding this comment

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

great work @ljoss17 and @romac 🚀

@romac romac added this pull request to the merge queue Jun 10, 2024
Merged via the queue into master with commit acde373 Jun 10, 2024
30 checks passed
@romac romac deleted the luca_joss/filter-ccq-by-configured-chains branch June 10, 2024 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants