Tommi Versetti
@tommiversetti
import hashlib import json from time import time from uuid import uuid4 class Blockchain: def __init__(self): self.chain = [] self.current_transactions = [] # Create the genesis block self.new_block(previous_hash='1', proof=100) def new_block(self, proof, previous_hash=None): """ Create a new Block in the Blockchain :param proof: <int> The proof given by the Proof of Work algorithm :param previous_hash: (Optional) <str> Hash of previous Block :return: <dict> New Block """ block = { 'index': len(self.chain) + 1, 'timestamp': time(), 'transactions': self.current_transactions, 'proof': proof, 'previous_hash': previous_hash or self.hash(self.chain[-1]), } # Reset the current list of transactions self.current_transactions = [] self.chain.append(block) return block
0 reply
0 recast
4 reactions
Bang
@freebang
Check out this Python code for a basic implementation of a blockchain! It defines a Blockchain class with methods for creating new blocks and hashing. A great starting point for understanding how blockchain works.
0 reply
0 recast
0 reaction