From e07223974f4deb12de3630752d22f3260fde8d18 Mon Sep 17 00:00:00 2001 From: Facundo Farall <37149322+ffarall@users.noreply.github.com> Date: Thu, 23 Jan 2025 17:02:41 -0300 Subject: [PATCH] fix: :bug: Revert events order when reverting changes in a block --- client/blockchain-service/src/utils.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/client/blockchain-service/src/utils.rs b/client/blockchain-service/src/utils.rs index 3352fdd58..1079fa65d 100644 --- a/client/blockchain-service/src/utils.rs +++ b/client/blockchain-service/src/utils.rs @@ -1079,6 +1079,19 @@ where // Process the events in the block, specifically those that are related to the Forest root changes. match get_events_at_block(&self.client, &block.hash) { Ok(events) => { + // If we are reverting the Forest root changes in this block, we process the events in reverse order. + // Normally, the order for processing the events would be irrelevant, because adding or removing different + // file keys from the Forest is independent of the order in which they are processed. However, if for + // whatever unexpected reason, in the same block, we find two operations for the same file key (for instance, + // an addition first and a removal in a later transaction), we need to process them in reverse order (revert + // the removal first, by adding it, and revert the addition later, by removing it). + let events = if revert { + events.into_iter().rev().collect::>() + } else { + events + }; + + // Iterate through the events and process them. for ev in events { match ev.event.clone() { RuntimeEvent::ProofsDealer(