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:
10
node.py
10
node.py
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user