Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib
- import json
- from time import time
- class Blockchain(object):
- def __init__(self):
- self.current_transactions = []
- self.chain = []
- # Create the genesis block
- genesis_block = self.new_block(previous_hash=1, proof=100)
- self.chain.append(genesis_block)
- 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.chain[-1]['hash'],
- }
- # Calculate the hash of this new Block
- block['hash'] = self.hash(block)
- # Reset the current list of transactions
- self.current_transactions = []
- self.chain.append(block)
- return block
- def new_transaction(self, sender, recipient, amount):
- """
- Creates a new transaction to go into the next mined Block
- :param sender: <str> Address of the Sender
- :param recipient: <str> Address of the Recipient
- :param amount: <int> Amount
- :return: <int> The index of the Block that will hold this transaction
- """
- self.current_transactions.append({
- 'sender': sender,
- 'recipient': recipient,
- 'amount': amount,
- })
- return self.last_block['index'] + 1
- @property
- def last_block(self):
- return self.chain[-1]
- @staticmethod
- def hash(block):
- """
- Creates a SHA-256 hash of a Block
- :param block: <dict> Block
- :return: <str>
- """
- block_string = json.dumps(block).encode()
- return hashlib.sha256(block_string).hexdigest()
- @staticmethod
- def proof_of_work(last_proof):
- # The algorithm used to mine new blocks
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement