Skip to content

Instantly share code, notes, and snippets.

@diopib
Created Aug 23, 2017
Embed
What would you like to do?
Very tiny BlockChain implementation for learning purpose
"""
Very tiny blockchain implementation -- for learning purpose
"""
import hashlib
import datetime
__author__ = "ibrahim@sikilabs.com"
__reference__ = "https://medium.com/crypto-currently/lets-build-the-tiniest-blockchain-e70965a248b"
class TBlock:
def __init__(self, idx=0, ts=datetime.datetime.now(), data="Genesis 313",
prev_hash="0"):
"""
Block entity -- creates a genesis block if no parameter is supplied
:param idx: block index
:param ts: timestamp
:param data: whatever data
:param prev_hash: previous block hash -- 0 if genesis
"""
self.index = idx
self.ts = ts
self.data = data
self.previous_hash = prev_hash
self.hash = hashlib.sha256()
self.hash.update(str(self.index) +
str(self.ts) +
str(self.data) +
str(self.previous_hash))
def __str__(self):
return str({"index": self.index, "hash": self.hash.hexdigest()})
def create_next(self, data):
"""
Create next block in chain from previous block
:param data: whatever data
:return: block instance
"""
return TBlock(self.index + 1, datetime.datetime.now(), data, self.hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment