Skip to content

Commit

Permalink
zcash_client_backend: Fix off-by-one in tree heights at NU activation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Nov 8, 2023
1 parent 1c2989f commit 2b87156
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions zcash_client_backend/src/scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,26 @@ pub(crate) fn scan_block_with_runner<
let cur_hash = block.hash();

let initial_sapling_tree_size = prior_block_metadata.and_then(|m| m.sapling_tree_size());
// If we're below Sapling activation, or Sapling activation is not set, the
// tree size is zero
let nu_sapling_tree_size =
params
.activation_height(NetworkUpgrade::Sapling)
.and_then(|sapling_activation| {
if cur_height < sapling_activation {
Some(0)
} else {
None
}
});
let mut sapling_commitment_tree_size = initial_sapling_tree_size
.or_else(|| {
block
.chain_metadata
.as_ref()
.and_then(|m| {
if m.sapling_commitment_tree_size == 0 {
None
nu_sapling_tree_size
} else {
let block_output_count: u32 = block
.vtx
Expand All @@ -354,28 +366,34 @@ pub(crate) fn scan_block_with_runner<
Some(m.sapling_commitment_tree_size - block_output_count)
}
})
.or_else(|| {
// If we're below Sapling activation, or Sapling activation is not set, the
// tree size is zero
params
.activation_height(NetworkUpgrade::Sapling)
.map_or(Some(0), |h| if h > cur_height { Some(0) } else { None })
})
.or(nu_sapling_tree_size)
})
.ok_or(ScanError::TreeSizeUnknown {
protocol: ShieldedProtocol::Sapling,
at_height: cur_height,
})?;

let initial_orchard_tree_size = prior_block_metadata.and_then(|m| m.orchard_tree_size());
// If we're below Nu5 activation, or Nu5 activation is not set, the tree size
// is zero
let nu_orchard_tree_size =
params
.activation_height(NetworkUpgrade::Nu5)
.and_then(|nu5_activation| {
if cur_height < nu5_activation {
Some(0)
} else {
None
}
});
let mut orchard_commitment_tree_size = initial_orchard_tree_size
.or_else(|| {
block
.chain_metadata
.as_ref()
.and_then(|m| {
if m.orchard_commitment_tree_size == 0 {
None
nu_orchard_tree_size
} else {
let block_action_count: u32 = block
.vtx
Expand All @@ -387,13 +405,7 @@ pub(crate) fn scan_block_with_runner<
Some(m.orchard_commitment_tree_size - block_action_count)
}
})
.or_else(|| {
// If we're below Nu5 activation, or Nu5 activation is not set, the tree size
// is zero
params
.activation_height(NetworkUpgrade::Nu5)
.map_or(Some(0), |h| if h > cur_height { Some(0) } else { None })
})
.or(nu_orchard_tree_size)
})
.ok_or(ScanError::TreeSizeUnknown {
protocol: ShieldedProtocol::Orchard,
Expand Down

0 comments on commit 2b87156

Please sign in to comment.