Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blockchain-Code #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Blockchain/Blockchain-Code
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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