Created
September 27, 2018 08:12
-
-
Save cylim/71547a59d8589f661ed6b309cdd5d8d1 to your computer and use it in GitHub Desktop.
simple blockchain by codeacademy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from hashlib import sha256 | |
#changed data to transactions | |
class Block: | |
def __init__(self, transactions, previous_hash): | |
self.time_stamp = datetime.datetime.now() | |
self.transactions = transactions | |
self.previous_hash = previous_hash | |
self.nonce = 0 | |
self.hash = self.generate_hash() | |
def generate_hash(self): | |
block_header = str(self.time_stamp) + str(self.transactions) +str(self.previous_hash) + str(self.nonce) | |
block_hash = sha256(block_header.encode()) | |
return block_hash.hexdigest() | |
def print_contents(self): | |
print("timestamp:", self.time_stamp) | |
print("transactions:", self.transactions) | |
print("current hash:", self.generate_hash()) | |
print("previous hash:", self.previous_hash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from block import Block | |
class Blockchain: | |
def __init__(self): | |
self.chain = [] | |
self.unconfirmed_transactions = [] | |
self.genesis_block() | |
def genesis_block(self): | |
transactions = [] | |
genesis_block = Block(transactions, "0") | |
genesis_block.generate_hash() | |
self.chain.append(genesis_block) | |
def add_block(self, transactions): | |
previous_hash = (self.chain[len(self.chain)-1]).hash | |
new_block = Block(transactions, previous_hash) | |
new_block.generate_hash() | |
# proof = proof_of_work(block) | |
self.chain.append(new_block) | |
def print_blocks(self): | |
for i in range(len(self.chain)): | |
current_block = self.chain[i] | |
print("Block {} {}".format(i, current_block)) | |
current_block.print_contents() | |
def validate_chain(self): | |
for i in range(1, len(self.chain)): | |
current = self.chain[i] | |
previous = self.chain[i-1] | |
if(current.hash != current.generate_hash()): | |
print("Current hash does not equal generated hash") | |
return False | |
if(current.previous_hash != previous.generate_hash()): | |
print("Previous block's hash got changed") | |
return False | |
return True | |
def proof_of_work(self, block, difficulty=2): | |
proof = block.generate_hash() | |
while proof[:2] != "0"*difficulty: | |
block.nonce += 1 | |
proof = block.generate_hash() | |
block.nonce = 0 | |
return proof | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment