Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import hashlib
- import json
- import time
- class Blockchain(object):
- def __init__(self):
- self.chain = []
- self.memPool = []
- self.createGenesisBlock()
- def createGenesisBlock(self):
- self.createBlock()
- def createBlock(self, nonce=0, previousHash=0):
- # Lembre que o hash do bloco anterior é o hash na verdade do CABEÇALHO do bloco anterior.
- if(len(self.chain)==0):
- Block = dict(
- index=0,
- timestamp=(int(time.time())),
- nonce=0,
- merkleRoot=0,
- previousHash=0,
- transactions=self.memPool)
- else:
- header = self.chain[-1].copy()
- header.pop("transactions")
- Block = dict(
- index=(len(self.chain))+1,
- timestamp=(int(time.time())),
- nonce=0,
- merkleRoot=0,
- previousHash=self.generateHash(header),
- transactions=self.memPool)
- self.chain.append(Block)
- @staticmethod
- def generateHash(data):
- blkSerial = json.dumps(data, sort_keys=True).encode()
- return hashlib.sha256(blkSerial).hexdigest()
- def printChain(self):
- for i in self.chain:
- header = self.chain[-1].copy()
- header.pop("transactions")
- print(header["previousHash"])
- print(json.dumps(i, sort_keys=True).encode())
- blockchain = Blockchain()
- for x in range(1, 4):
- blockchain.createBlock()
- blockchain.printChain()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement