From 921fa1e6f94776ec2b5be23cf2a32f5a37cb1eaa Mon Sep 17 00:00:00 2001 From: Ouziel Slama Date: Thu, 19 Dec 2024 13:13:10 +0000 Subject: [PATCH 1/2] Fix previous_block on reorg --- .../counterpartycore/lib/blocks.py | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/counterparty-core/counterpartycore/lib/blocks.py b/counterparty-core/counterpartycore/lib/blocks.py index e97f54a85..94deee00e 100644 --- a/counterparty-core/counterpartycore/lib/blocks.py +++ b/counterparty-core/counterpartycore/lib/blocks.py @@ -1272,7 +1272,14 @@ def parse_new_block(db, decoded_block, tx_index=None): if tx_index is None: tx_index = get_next_tx_index(db) - if util.CURRENT_BLOCK_INDEX > config.BLOCK_FIRST: + if util.CURRENT_BLOCK_INDEX == config.BLOCK_FIRST: + previous_block = { + "ledger_hash": None, + "txlist_hash": None, + "messages_hash": None, + "block_index": config.BLOCK_FIRST - 1, + } + else: # get previous block previous_block = ledger.get_block(db, util.CURRENT_BLOCK_INDEX - 1) @@ -1294,20 +1301,21 @@ def parse_new_block(db, decoded_block, tx_index=None): logger.warning("Blockchain reorganization detected at block %s.", previous_block_index) # rollback to the previous block rollback(db, block_index=previous_block_index + 1) + previous_block = ledger.get_block(db, previous_block_index) util.CURRENT_BLOCK_INDEX = previous_block_index + 1 tx_index = get_next_tx_index(db) - else: - previous_block = { - "ledger_hash": None, - "txlist_hash": None, - "messages_hash": None, - } if "height" not in decoded_block: decoded_block["block_index"] = util.CURRENT_BLOCK_INDEX else: decoded_block["block_index"] = decoded_block["height"] + # Sanity checks + if decoded_block["block_index"] != config.BLOCK_FIRST: + assert previous_block["ledger_hash"] is not None + assert previous_block["txlist_hash"] is not None + assert previous_block["block_index"] == decoded_block["block_index"] - 1 + with db: # ensure all the block or nothing logger.info(f"Block {decoded_block['block_index']}", extra={"bold": True}) # insert block From fe9b6de92dfcdfa6bbf0d01534e4869f9ef53814 Mon Sep 17 00:00:00 2001 From: Ouziel Slama Date: Thu, 19 Dec 2024 14:49:18 +0000 Subject: [PATCH 2/2] update release notes --- release-notes/release-notes-v10.9.0.md | 1 + 1 file changed, 1 insertion(+) diff --git a/release-notes/release-notes-v10.9.0.md b/release-notes/release-notes-v10.9.0.md index 342de9d71..24fec240e 100644 --- a/release-notes/release-notes-v10.9.0.md +++ b/release-notes/release-notes-v10.9.0.md @@ -11,6 +11,7 @@ - Fix endpoint to get info from raw transaction when block index is not provided - Catch errors correctly when composing MPMA send +- Fix consensus hashes calculation after a Blockchain reorg ## Codebase