Created
May 20, 2019 18:21
-
-
Save WhiskersReneeWe/781b1caad5897a1087471bf26210f811 to your computer and use it in GitHub Desktop.
Linked List implementation of a simple blockchain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# blockchain implementation using Linked List | |
# Copyright: Renee S. Liu | |
# First is the information hash | |
# We do this for the information we want to store in the block chain | |
# such as transaction time, data, and information like the previous chain. | |
import hashlib | |
import datetime as date | |
# The next main component is the block class | |
class Block: | |
def __init__(self, index, timestamp, data, previous_hash): | |
self.index = index | |
self.timestamp = timestamp | |
self.data = data | |
self.previous_hash = previous_hash | |
self.hash = self.calc_hash() | |
self.prev = None | |
def calc_hash(self): | |
sha = hashlib.sha256() | |
sha.update(str(self.index).encode('utf-8') + | |
str(self.timestamp).encode('utf-8') + | |
str(self.data).encode('utf-8') + | |
str(self.previous_hash).encode('utf-8')) | |
return sha.hexdigest() | |
# This creates a first block in a chain | |
def create_block_eve(): | |
""" | |
create genesis block - eve | |
Initialize with arbitrary data and previous_hash values | |
""" | |
return Block(0, date.datetime.now(), 'heijre', 'uroe46jkf') | |
def createNewblock(Prevblock): | |
new_index = Prevblock.index + 1 | |
new_timestamp = date.datetime.now() | |
new_data = "Block {} was created at ".format(new_index) + new_timestamp.strftime('%m/%d/%y:%H:%M:%S:%f-%Z') | |
new_previous_hash = Prevblock.hash | |
new_prev = Prevblock | |
return Block(new_index, new_timestamp, new_data, new_previous_hash) | |
block_1 = createNewblock(genesis_block) | |
# Implementing Blockchain structure with a Linked List under the hood | |
class BlockChain_ll: | |
def __init__(self): | |
first_tail = create_block_eve() | |
self.head = first_tail | |
self.tail = first_tail | |
self.num_new_blocks = 0 | |
def append(self, new_block): | |
if self.tail == None: | |
print("Create genesis first!") | |
self.tail = genesis_block | |
else: | |
temp_block = self.tail | |
self.tail = new_block | |
self.tail.prev = temp_block | |
self.num_new_blocks += 1 | |
del temp_block | |
def size(self): | |
return self.num_new_blocks + 1 | |
def get_block(self, index): | |
tail_block = self.tail | |
tail_idx = tail_block.index | |
temp_tail_block = tail_block | |
if tail_idx == index: | |
return tail_block | |
if tail_idx < index: | |
return "Block index invalid" | |
while tail_idx > index: | |
temp_tail_block = temp_tail_block.prev | |
tail_idx = temp_tail_block.index | |
return temp_tail_block | |
######################################### | |
# create 20 blocks | |
# Initialize the blockchain | |
# get the genesis block from it | |
blockchain = BlockChain_ll() | |
gene_block = blockchain.tail | |
blockchain = BlockChain_ll() | |
gene_block = blockchain.tail | |
old_block = gene_block | |
for i in range(20): | |
new_block = createNewblock(old_block) | |
blockchain.append(new_block) | |
old_block = new_block | |
########################## Test ############### | |
# Blockchain | |
blockchain.get_block(20).index | |
blockchain.get_block(15).index | |
blockchain.get_block(15).data | |
print(blockchain.tail.index) | |
blockchain.append(block_1) | |
print(blockchain.tail.index) | |
blockchain.append(block_2) | |
print(blockchain.tail.index) | |
blockchain.append(block_3) | |
print(blockchain.tail.index) | |
blockchain.size() | |
# check the tail index | |
blockchain.tail.index | |
# check the index of the block before the tail | |
blockchain.tail.prev.index |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment