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 is not None:
|
||||||
if existing_block.valid:
|
if existing_block.valid:
|
||||||
break
|
break
|
||||||
|
block_list.append(request_block_hash)
|
||||||
request_block_hash = existing_block.previous_hash
|
request_block_hash = existing_block.previous_hash
|
||||||
if request_block_hash == 32*b"\0":
|
if request_block_hash == 32*b"\0":
|
||||||
break
|
break
|
||||||
@@ -212,11 +213,16 @@ def transfer_block(addr, node, receive_observer, b):
|
|||||||
request_block_hash = b.get_block(block_hash).previous_hash
|
request_block_hash = b.get_block(block_hash).previous_hash
|
||||||
if request_block_hash == 32*b"\0":
|
if request_block_hash == 32*b"\0":
|
||||||
break
|
break
|
||||||
|
new_block_count = 0
|
||||||
for block_hash in reversed(block_list):
|
for block_hash in reversed(block_list):
|
||||||
if not b.get_block(block_hash).validate(b):
|
if not b.get_block(block_hash).validate(b):
|
||||||
return
|
break
|
||||||
if b.set_latest_block(block_hash):
|
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:
|
except NoReponseException:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user