Skip to content

Instantly share code, notes, and snippets.

Created Aug 23, 2017
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__ = ""
__reference__ = ""
class TBlock:
def __init__(self, idx=0,, data="Genesis 313",
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 = data
self.previous_hash = prev_hash
self.hash = hashlib.sha256()
self.hash.update(str(self.index) +
str(self.ts) +
str( +
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,, data, self.hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment