From 8396538315baeedf47dd14cd929cc052f7ffc78f Mon Sep 17 00:00:00 2001 From: Kousthub Raja Date: Mon, 14 Feb 2022 12:59:11 +0530 Subject: [PATCH 1/3] mine empty block --- app/codes/blockchain.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/app/codes/blockchain.py b/app/codes/blockchain.py index cb8a155..3968fe4 100644 --- a/app/codes/blockchain.py +++ b/app/codes/blockchain.py @@ -44,6 +44,14 @@ def get_block(self, block_index): return block + def empty_block_hash(self, block): + proof = 42 # Hardcoded value for empty block + block_hash = self.calculate_hash(block) + block_hash = '0000' + block_hash[:4] + block['proof'] = proof + + return block_hash + def proof_of_work(self, block): """Proof of work which takes a block with proof set as 0 as input and returns the proof that makes its hash start with 0000""" @@ -82,20 +90,27 @@ def mine_block(self, cur, text): """Mine a new block""" print("Starting the mining step 1") last_block_cursor = cur.execute( - 'SELECT block_index, hash FROM blocks ORDER BY block_index DESC LIMIT 1') + 'SELECT block_index, hash, timestamp FROM blocks ORDER BY block_index DESC LIMIT 1') last_block = last_block_cursor.fetchone() last_block_index = last_block[0] if last_block is not None else 0 last_block_hash = last_block[1] if last_block is not None else 0 + last_block_timestamp = last_block[2] if last_block is not None else 0 block = { 'index': last_block_index + 1, - 'timestamp': str(datetime.datetime.now()), 'proof': 0, 'text': text, 'previous_hash': last_block_hash } - block_hash = self.proof_of_work(block) + if len(text['transactions']) == 0: + # TODO - Fix the timestamp. Supposed to be real timestamp + block['timestamp'] = last_block_timestamp + 1 + block_hash = self.empty_block_hash(block) + else: + block['timestamp'] = str(datetime.datetime.now()) + block_hash = self.proof_of_work(block) + print("New block hash is ", block_hash) block = self.create_block(cur, block, block_hash) From 08c895548ebf66006a42413063ef7a30c47a5d6c Mon Sep 17 00:00:00 2001 From: Kousthub Raja Date: Mon, 14 Feb 2022 13:29:28 +0530 Subject: [PATCH 2/3] empty block validation --- app/codes/blockchain.py | 4 ++-- app/codes/validator.py | 14 +++++++++++++- app/constants.py | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/codes/blockchain.py b/app/codes/blockchain.py index 3968fe4..0a7b655 100644 --- a/app/codes/blockchain.py +++ b/app/codes/blockchain.py @@ -5,7 +5,7 @@ import sqlite3 -from ..constants import NEWRL_DB +from ..constants import EMPTY_BLOCK_NONCE, NEWRL_DB from .state_updater import update_db_states @@ -45,7 +45,7 @@ def get_block(self, block_index): return block def empty_block_hash(self, block): - proof = 42 # Hardcoded value for empty block + proof = EMPTY_BLOCK_NONCE # Hardcoded value for empty block block_hash = self.calculate_hash(block) block_hash = '0000' + block_hash[:4] block['proof'] = proof diff --git a/app/codes/validator.py b/app/codes/validator.py index 2a5368e..778377e 100644 --- a/app/codes/validator.py +++ b/app/codes/validator.py @@ -11,7 +11,7 @@ from app.codes.p2p.transport import send from .blockchain import get_last_block_hash from .transactionmanager import Transactionmanager -from ..constants import MEMPOOL_PATH +from ..constants import EMPTY_BLOCK_NONCE, MEMPOOL_PATH logging.basicConfig(level=logging.INFO) @@ -125,6 +125,9 @@ def validate_block(block, validate_receipts=True): # Also check for block index + if not validate_empty_block(block): + return False + sign_valid = validate_signature( data=block['data'], public_key=block['signature']['public_key'], @@ -142,3 +145,12 @@ def validate_block(block, validate_receipts=True): return False return True + + +def validate_empty_block(block): + data = block['data'] + + if len(data['text']['transactions']) == 0 and data['proof'] != EMPTY_BLOCK_NONCE: + return False + + return True diff --git a/app/constants.py b/app/constants.py index b13761a..58e96e6 100644 --- a/app/constants.py +++ b/app/constants.py @@ -5,6 +5,7 @@ if IS_TEST: print('Using constants for Test') +EMPTY_BLOCK_NONCE = 42 DATA_PATH = 'data_test/' if IS_TEST else 'data/' MEMPOOL_PATH = DATA_PATH + 'mempool/' TMP_PATH = DATA_PATH + 'tmp/' From 6ed10a571d011909b3ed381d188299075372b244 Mon Sep 17 00:00:00 2001 From: Kousthub Raja Date: Wed, 23 Feb 2022 08:13:37 +0530 Subject: [PATCH 3/3] fix proof in empty block --- app/codes/blockchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/codes/blockchain.py b/app/codes/blockchain.py index 0a7b655..0d1e1cb 100644 --- a/app/codes/blockchain.py +++ b/app/codes/blockchain.py @@ -46,9 +46,9 @@ def get_block(self, block_index): def empty_block_hash(self, block): proof = EMPTY_BLOCK_NONCE # Hardcoded value for empty block + block['proof'] = proof block_hash = self.calculate_hash(block) block_hash = '0000' + block_hash[:4] - block['proof'] = proof return block_hash