Fix LIB calculations with short chain (#128)

Fix LIB calculation where the chain is shorter than K
and add a regression test for this case
This commit is contained in:
Giacomo Pasini 2025-06-05 12:21:19 +02:00 committed by GitHub
parent 30ef110f24
commit 89dd2efacb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 2 deletions

View File

@ -401,7 +401,7 @@ class Follower:
return
# prune forks that do not descend from the last immutable block, this is needed to avoid Genesis rule to roll back
# past the LIB
self.lib = next(islice(iter_chain(self.local_chain, self.ledger_state), self.config.k, None), self.local_chain).block.id()
self.lib = next(islice(iter_chain(self.local_chain, self.ledger_state), self.config.k, None), self.genesis_state).block.id()
self.forks = [
f for f in self.forks if is_ancestor(self.lib, f, self.ledger_state)
]

View File

@ -374,3 +374,26 @@ class TestForkChoice(TestCase):
assert follower.tip_id() == b12.id()
assert follower.lib == b7.id(), follower.lib
def test_lib_calc_short_chain(self):
# Test that the LIB is correctly calculated for a short chain
n_a = Note(sk=0, value=10)
notes = [n_a]
config = mk_config(notes)
config.k = 10
genesis = mk_genesis_state(notes)
follower = Follower(genesis, config)
follower.to_online()
assert follower.lib == genesis.block.id(), follower.lib
blocks = [genesis.block]
for i in range(1, 11):
b = mk_block(blocks[-1], i, n_a)
blocks.append(b)
follower.on_block(b)
assert follower.lib == genesis.block.id(), follower.lib
b11 = mk_block(blocks[-1], 11, n_a)
follower.on_block(b11)
assert follower.lib == blocks[1].id(), follower.lib