Skip to content

Commit

Permalink
more reorg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 10, 2025
1 parent a179b65 commit 447f69c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions counterparty-core/counterpartycore/lib/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,18 +1278,21 @@ def handle_reorg(db):
previous_block_hash = backend.bitcoind.getblockhash(previous_block_index)
except exceptions.BlockOutOfRange:
# previous block and current block are not in the blockchain
logger.debug(f"Previous block is not in the blockchain ({previous_block_index}).")
previous_block_index -= 2
continue

try:
current_block_hash = backend.bitcoind.getblockhash(previous_block_index + 1)
except exceptions.BlockOutOfRange:
# current block is not in the blockchain
logger.debug(f"Current block is not in the blockchain ({previous_block_index + 1}).")
previous_block_index -= 1
continue

if previous_block_hash != ledger.get_block_hash(db, previous_block_index):
# hashes don't match
logger.debug(f"Hashes don't match ({previous_block_index}).")
previous_block_index -= 1
continue

Expand Down
29 changes: 28 additions & 1 deletion counterparty-core/counterpartycore/test/regtest/regtestnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ def test_reorg(self):
assert intermediate_xcp_balance > initial_xcp_balance

print("Mine a longest chain on the second node...")
before_reorg_block = int(self.bitcoin_cli_2("getblockcount").strip())
self.bitcoin_cli_2("generatetoaddress", 6, self.addresses[0])

print("Re-connect to the first node...")
Expand All @@ -679,13 +680,39 @@ def test_reorg(self):
self.wait_for_counterparty_server(block=last_block)

print("Burn count after reorganization: ", self.get_burn_count(self.addresses[0]))
assert "Blockchain reorganization detected" in self.server_out.getvalue()

assert (
f"Blockchain reorganization detected at block {last_block - 1}"
in self.server_out.getvalue()
)
assert f"Hashes don't match ({before_reorg_block + 1})" in self.server_out.getvalue()
assert self.get_burn_count(self.addresses[0]) == 1

final_xcp_balance = self.get_xcp_balance(self.addresses[0])
print("Final XCP balance: ", final_xcp_balance)
assert final_xcp_balance == initial_xcp_balance

# Test reorg with a shorter chain of one block
print("Disconnect from the first node...")
self.bitcoin_cli_2("disconnectnode", "localhost:18445")

print("Invalidate the last block on the first node...")
best_block_hash = self.bitcoin_cli("getbestblockhash").strip()
self.bitcoin_cli("invalidateblock", best_block_hash)

self.mine_blocks(1)
retry = 0
while (
f"Current block is not in the blockchain ({last_block + 1})."
not in self.server_out.getvalue()
):
time.sleep(1)
retry += 1
print("Waiting for reorg...")
assert retry < 100

print("Reorg test successful")

def test_electrs(self):
self.start_and_wait_second_node()

Expand Down

0 comments on commit 447f69c

Please sign in to comment.