From 987060da3121dea2b971a5e212652286a5f30db1 Mon Sep 17 00:00:00 2001 From: David Rusu Date: Fri, 1 Nov 2024 23:48:43 +0400 Subject: [PATCH] cryptarchia: cleanup --- cryptarchia/cryptarchia.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/cryptarchia/cryptarchia.py b/cryptarchia/cryptarchia.py index c59b6d0..3446f43 100644 --- a/cryptarchia/cryptarchia.py +++ b/cryptarchia/cryptarchia.py @@ -499,15 +499,13 @@ class Follower: Orphans are returned in the order that they should be imported. """ tip_state = self.tip_state().copy() + tip = tip_state.block.id() orphans = [] for fork in self.forks: - _, fork_depth = common_prefix_depth( - tip_state.block.id(), fork, self.ledger_state - ) - fork_chain = chain_suffix(fork, fork_depth, self.ledger_state) - for block_state in fork_chain: + _, fork_depth = common_prefix_depth(tip, fork, self.ledger_state) + for block_state in chain_suffix(fork, fork_depth, self.ledger_state): b = block_state.block if b.leader_proof.nullifier not in tip_state.nullifiers: tip_state.nullifiers.add(b.leader_proof.nullifier) @@ -715,14 +713,9 @@ def chain_density( head: Id, slot: Slot, reorg_depth: int, states: Dict[Id, LedgerState] ) -> int: assert type(head) == Id - density = 0 - block = head - for _ in range(reorg_depth): - if states[block].block.slot.absolute_slot < slot.absolute_slot: - density += 1 - block = states[block].block.parent - - return density + chain = iter_chain(head, states) + segment = itertools.islice(chain, reorg_depth) + return sum(1 for b in segment if b.block.slot < slot) def block_children(states: Dict[Id, LedgerState]) -> Dict[Id, set[Id]]: