Skip to content
This repository was archived by the owner on Jul 15, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions app/codes/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -44,6 +44,14 @@ def get_block(self, block_index):

return block

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]

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"""
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion app/codes/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'],
Expand All @@ -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
1 change: 1 addition & 0 deletions app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
Expand Down