From 093d503d22894fee57249387f25eb86a5bc8b985 Mon Sep 17 00:00:00 2001 From: teor Date: Thu, 8 Sep 2022 09:42:52 +1000 Subject: [PATCH] Document why we do a UTXO check that looks redundant (#5106) Also inline the call stack for the check, so it is efficient. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- zebra-state/src/service.rs | 8 ++++++++ zebra-state/src/service/pending_utxos.rs | 2 ++ 2 files changed, 10 insertions(+) diff --git a/zebra-state/src/service.rs b/zebra-state/src/service.rs index e20e1a6e..7740fdda 100644 --- a/zebra-state/src/service.rs +++ b/zebra-state/src/service.rs @@ -669,6 +669,14 @@ impl Service for StateService { let timer = CodeTimer::start(); + // # Consensus + // + // A non-finalized block verification could have called AwaitUtxo + // before this finalized block arrived in the state. + // So we need to check for pending UTXOs here for non-finalized blocks, + // even though it is redundant for most finalized blocks. + // (Finalized blocks are verified using block hash checkpoints + // and transaction merkle tree block header commitments.) self.pending_utxos.check_against(&finalized.new_outputs); // # Performance diff --git a/zebra-state/src/service/pending_utxos.rs b/zebra-state/src/service/pending_utxos.rs index b2566ff1..3742a721 100644 --- a/zebra-state/src/service/pending_utxos.rs +++ b/zebra-state/src/service/pending_utxos.rs @@ -38,6 +38,7 @@ impl PendingUtxos { /// Notify all requests waiting for the [`transparent::Utxo`] pointed to by /// the given [`transparent::OutPoint`] that the [`transparent::Utxo`] has /// arrived. + #[inline] pub fn respond(&mut self, outpoint: &transparent::OutPoint, utxo: transparent::Utxo) { if let Some(sender) = self.0.remove(outpoint) { // Adding the outpoint as a field lets us cross-reference @@ -59,6 +60,7 @@ impl PendingUtxos { } /// Check the list of pending UTXO requests against the supplied [`transparent::Utxo`] index. + #[inline] pub fn check_against(&mut self, utxos: &HashMap) { for (outpoint, utxo) in utxos.iter() { self.respond(outpoint, utxo.clone())