Skip to content

Commit

Permalink
Add Proof of work as last 4 hash digits should be 0
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv-aggarwal committed Jan 28, 2018
1 parent b1db732 commit 1a24d02
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
11 changes: 8 additions & 3 deletions app/managers/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
from datetime import datetime
from app.models.block import Block
from config import NUM_ZEROS


def save_block(block):
Expand All @@ -16,12 +17,16 @@ def mine(last_block):
# random string for now
data = "I block #%s" % (int(last_block.index) + 1)
prev_hash = last_block.hash
block_hash = calculate_hash(index, prev_hash, data, timestamp)

nonce = 0
block_hash = calculate_hash(index, prev_hash, data, timestamp, nonce)
while str(block_hash[0:NUM_ZEROS]) != '0' * NUM_ZEROS:
nonce += 1
block_hash = calculate_hash(index, prev_hash, data, timestamp, nonce)
block_data = {}
block_data['index'] = int(last_block.index) + 1
block_data['timestamp'] = calculate_hash
block_data['timestamp'] = timestamp
block_data['data'] = "I block #%s" % last_block.index
block_data['prev_hash'] = last_block.hash
block_data['hash'] = block_hash
block_data['nonce'] = nonce
return Block(block_data)
12 changes: 6 additions & 6 deletions app/managers/common.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import os
from config import blockchain_dir
from config import DATA_DIR
import hashlib


def get_data_directory():
return os.path.abspath(
os.path.join(os.path.dirname(__file__), '../')
) + blockchain_dir
) + DATA_DIR


def generate_header(index, prev_hash, data, timestamp):
return str(index) + prev_hash + data + str(timestamp)
def generate_header(index, prev_hash, data, timestamp, nonce):
return str(index) + prev_hash + data + str(timestamp) + str(nonce)


def calculate_hash(index, prev_hash, data, timestamp):
header_string = generate_header(index, prev_hash, data, timestamp)
def calculate_hash(index, prev_hash, data, timestamp, nonce):
header_string = generate_header(index, prev_hash, data, timestamp, nonce)
sha = hashlib.sha256()
sha.update(header_string)
return sha.hexdigest()
Expand Down
3 changes: 3 additions & 0 deletions app/models/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ def __init__(self, **kwargs):
:type index: str (json)
:param prev_hash: The hash for the previous block in the chain
:type index: str
:param nonce: Counter used for the proof of work implementation
:type index: int
"""
self.index = kwargs.get('index')
self.hash = kwargs.get('hash')
self.timestamp = kwargs.get('timestamp')
self.data = kwargs.get('data')
self.prev_hash = kwargs.get('prev_hash')
self.nonce = kwargs.get('nonce')

def __dict__(self):
return {
Expand Down
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
blockchain_dir = 'data'
NUM_ZEROS = 4
8 changes: 6 additions & 2 deletions scripts/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from app.models.block import Block
from datetime import datetime
import os
from app.managers.common import get_data_directory
from app.managers.common import get_data_directory, calculate_hash
from app.managers.block import save_block


def initialize_block():
Expand All @@ -11,6 +12,9 @@ def initialize_block():
block_data['timestamp'] = datetime.now()
block_data['data'] = 'First block data'
block_data['prev_hash'] = None
block_data['hash'] = calculate_hash(
0, None, block_data['data'], block_data['timestamp'], 0
)
block = Block(block_data)
return block

Expand All @@ -29,4 +33,4 @@ def save_to_file():
if os.listdir(directory) == []:
# create first block
first_block = initialize_block()
# first_block.save()
save_block(first_block)

0 comments on commit 1a24d02

Please sign in to comment.