Skip to content

Commit

Permalink
fix: runtime upgrades in unstable backend (#1348)
Browse files Browse the repository at this point in the history
This commit changes the following:

1. FollowEvent::Finalized: just remove the finalized and pruned blocks from runtimes
2. FollowEvent::Initalized: just remove the finalized block from runtimes

Co-authored-by: James Wilson <james@jsdw.me>
  • Loading branch information
niklasad1 and jsdw authored Jan 16, 2024
1 parent b356cf3 commit df85e46
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions subxt/src/backend/unstable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl<T: Config + Send + Sync + 'static> Backend<T> for UnstableBackend<T> {
.filter_map(move |ev| {
let output = match ev {
FollowEvent::Initialized(ev) => {
runtimes.clear();
runtimes.remove(&ev.finalized_block_hash.hash());
ev.finalized_block_runtime
}
FollowEvent::NewBlock(ev) => {
Expand All @@ -363,14 +363,35 @@ impl<T: Config + Send + Sync + 'static> Backend<T> for UnstableBackend<T> {
None
}
FollowEvent::Finalized(ev) => {
let next_runtime = ev
let next_runtime = {
let mut it = ev
.finalized_block_hashes
.iter()
.rev()
.filter_map(|h| runtimes.get(&h.hash()).cloned())
.peekable();

let next = it.next();

if it.peek().is_some() {
tracing::warn!(
target: "subxt",
"Several runtime upgrades in the finalized blocks but only the latest runtime upgrade is returned"
);
}

next
};

// Remove finalized and pruned blocks as valid runtime upgrades.
for block in ev
.finalized_block_hashes
.iter()
.rev()
.filter_map(|h| runtimes.get(&h.hash()).cloned())
.next();
.chain(ev.pruned_block_hashes.iter())
{
runtimes.remove(&block.hash());
}

runtimes.clear();
next_runtime
}
_ => None,
Expand Down

0 comments on commit df85e46

Please sign in to comment.