Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
BlockChain from Scratch Part 01 - python
import hashlib
import json
class TRBlock(object) :
def __init__(self, index, timestamp, data, prevHash, nonce, target):
Default constructor for creating a block.
index : int
The index of the block
timestamp: long
Timestamp in epoch, number of seconds since 1 Jan 1970
data : object
The actual data that needs to be stored on block chain
prevHash : str
The hash of the previous block
nonce : str
The nonce which has been mined
target: int
Number of leading zeros
self.index = index
self.timestamp = timestamp = data
self.prevHash = prevHash
self.nonce = nonce = target
def compute_hash(self):
Compute sha1 hash and convert it into hexadecimal string, json.dumps
uses ":" separator without space
sha1 hash of the attributes converted into hexadecimal string
return hashlib.sha1(json.dumps(self.__dict__, separators=(',', ':')) \
if __name__ == "__main__":
block = TRBlock(0,1514528022,"Hello World", "000000000000000000000000000", \
"Random nonce",8)
Copy link

ulfateh commented Oct 24, 2018

It will more fruitful if mention the formula to convert. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment