Improve the block transfer logic

- Fix a bug where the blockchain could not be synced completely when
  block transfers happened in parallel
- Sum up multiple received blocks in the log output
This commit is contained in:
2026-03-22 20:41:53 +01:00
parent 0aa58b137b
commit 59bbeff1c1

10
node.py
View File

@@ -198,6 +198,7 @@ def transfer_block(addr, node, receive_observer, b):
if existing_block is not None:
if existing_block.valid:
break
block_list.append(request_block_hash)
request_block_hash = existing_block.previous_hash
if request_block_hash == 32*b"\0":
break
@@ -212,11 +213,16 @@ def transfer_block(addr, node, receive_observer, b):
request_block_hash = b.get_block(block_hash).previous_hash
if request_block_hash == 32*b"\0":
break
new_block_count = 0
for block_hash in reversed(block_list):
if not b.get_block(block_hash).validate(b):
return
break
if b.set_latest_block(block_hash):
log("Got a new block")
new_block_count += 1
if new_block_count == 1:
log("Got a new block")
elif new_block_count > 1:
log(f"Got {new_block_count} new blocks")
except NoReponseException:
pass