From 652ed42c5eef2942d619950197cc8ef6478ce4f0 Mon Sep 17 00:00:00 2001 From: ygd58 Date: Thu, 16 Apr 2026 13:52:18 +0200 Subject: [PATCH] fix: count genesis block in height calculation Previously genesis block was assigned height=0, causing chain height to undercount by one. Block count should equal the height of the tip. Fix: genesis block now starts at height=1. Fixes #12 --- src/db/blocks.py | 9 ++++++--- src/node/api/fake.py | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/db/blocks.py b/src/db/blocks.py index 99721a7..8e47a9e 100644 --- a/src/db/blocks.py +++ b/src/db/blocks.py @@ -120,15 +120,18 @@ class BlockRepository: else: # Parent not found anywhere if block.slot == 0 or block.hash == chain_root_hash: - # Genesis block or chain root - no parent requirement - block.height = 0 + # Genesis block or chain root - no parent requirement. + # Height starts at 1 to count the genesis block itself, + # so chain height = total number of blocks. + # See: https://github.com/logos-blockchain/logos-blockchain-block-explorer-template/issues/12 + block.height = 1 parent_heights[block.hash] = block.height resolved.add(block.hash) made_progress = True if block.hash == chain_root_hash: logger.info( f"Chain root block: hash={block.hash.hex()[:16]}..., " - f"slot={block.slot}, height=0" + f"slot={block.slot}, height=1" ) else: # Orphan block - parent doesn't exist diff --git a/src/node/api/fake.py b/src/node/api/fake.py index 9dc6c77..8a0a1b3 100644 --- a/src/node/api/fake.py +++ b/src/node/api/fake.py @@ -28,7 +28,7 @@ class FakeNodeApi(NodeApi): lib="0" * 64, tip="0" * 64, slot=self.current_slot, - height=0, + height=1, # Genesis block counts as height 1 mode="Fake", )