Skip to content

Commit 0c273e0

Browse files
committed
fix(blockchain): handle block that I already have
1 parent 344cefb commit 0c273e0

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

block.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def proof_of_work(last_block):
7272

7373
@staticmethod
7474
def valid_block(block_to_validate, previous_block):
75+
print('validating', block_to_validate.index, 'with prev_block', previous_block.index)
7576
previous_block_hash = Block.hash(previous_block)
7677

7778
if block_to_validate.previous_hash != previous_block_hash:
@@ -84,6 +85,7 @@ def valid_block(block_to_validate, previous_block):
8485
block_to_validate.nonce, ' prev_nonce=', previous_block.nonce)
8586
return False
8687

88+
print('Block valid')
8789
return True
8890

8991
def check_sender_stock(self, tx, nb):

blockchain.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ def submit_block(self, block):
156156
if not isinstance(block, Block):
157157
raise ValueError('block should be an instance of Block')
158158

159+
# If I already have the block, I ignore the received one.
160+
if block.index == self.last_block:
161+
print("I received a block, but I already have it, so I ignore it.")
162+
return False
163+
159164
"""
160165
Add a Block in the Blockchain if the Block signature is valid.
161166
"""

0 commit comments

Comments
 (0)