From ecf70a9b8c7ead1ac37c0b3e88a32a0ac876d2b5 Mon Sep 17 00:00:00 2001 From: Juan Eugenio Abadie Date: Sat, 5 Jun 2021 23:30:36 +0200 Subject: [PATCH] Reset pending_transactions only if block is valid --- chapters/chapter_4/full_blockchain.py | 6 +++--- chapters/chapter_6/blockchain.py | 6 +++--- chapters/chapter_7/funcoin/blockchain.py | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/chapters/chapter_4/full_blockchain.py b/chapters/chapter_4/full_blockchain.py index fcbb011..3ce625d 100644 --- a/chapters/chapter_4/full_blockchain.py +++ b/chapters/chapter_4/full_blockchain.py @@ -27,9 +27,6 @@ def new_block(self): block_hash = self.hash(block) block["hash"] = block_hash - # Reset the list of pending transactions - self.pending_transactions = [] - return block @staticmethod @@ -54,5 +51,8 @@ def proof_of_work(self): if self.valid_block(new_block): break + # Reset the list of pending transactions + self.pending_transactions = [] + self.chain.append(new_block) print("Found a new block: ", new_block) diff --git a/chapters/chapter_6/blockchain.py b/chapters/chapter_6/blockchain.py index de1847d..bf4d7b4 100644 --- a/chapters/chapter_6/blockchain.py +++ b/chapters/chapter_6/blockchain.py @@ -29,9 +29,6 @@ def new_block(self): block_hash = self.hash(block) block["hash"] = block_hash - # Reset the list of pending transactions - self.pending_transactions = [] - return block @staticmethod @@ -90,6 +87,9 @@ async def proof_of_work(self): await asyncio.sleep(0) + # Reset the list of pending transactions + self.pending_transactions = [] + self.chain.append(new_block) print("Found a new block: ", new_block) diff --git a/chapters/chapter_7/funcoin/blockchain.py b/chapters/chapter_7/funcoin/blockchain.py index 5a38a92..a9f64e7 100644 --- a/chapters/chapter_7/funcoin/blockchain.py +++ b/chapters/chapter_7/funcoin/blockchain.py @@ -30,9 +30,6 @@ def new_block(self): timestamp=time(), ) - # Reset the list of pending transactions - self.pending_transactions = [] - return block @staticmethod @@ -113,5 +110,8 @@ async def mine_new_block(self): await asyncio.sleep(0) + # Reset the list of pending transactions + self.pending_transactions = [] + self.chain.append(new_block) logger.info("Found a new block: ", new_block)