-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from block import Block | ||
from transaction import Transaction | ||
from wallet import Wallet | ||
|
||
class Chain: | ||
def __init__(self): | ||
self.chain = [self.create_genesis_block()] | ||
self.pending_transactions = [] | ||
self.mining_reward = 10 | ||
self.difficulty = 2 | ||
|
||
def create_genesis_block(self): | ||
return Block(0, '0', []) | ||
|
||
def get_latest_block(self): | ||
return self.chain[-1] | ||
|
||
def add_transaction(self, transaction): | ||
self.pending_transactions.append(transaction) | ||
|
||
def mine_pending_transactions(self, miner_wallet): | ||
if len(self.pending_transactions) < 1: | ||
return False | ||
|
||
new_block = Block(len(self.chain), self.get_latest_block().hash, self.pending_transactions) | ||
new_block.mine_block(self.difficulty) | ||
|
||
self.chain.append(new_block) | ||
|
||
self.pending_transactions = [Transaction(None, miner_wallet.public_key, self.mining_reward)] | ||
|
||
return True | ||
|
||
def get_balance_of_address(self, address): | ||
balance = 0 | ||
for block in self.chain: | ||
for transaction in block.transactions: | ||
if transaction.sender == address: | ||
balance -= transaction.amount | ||
elif transaction.recipient == address: | ||
balance += transaction.amount | ||
return balance | ||
|
||
def is_chain_valid(self): | ||
for i in range(1, len(self.chain)): | ||
current_block = self.chain[i] | ||
previous_block = self.chain[i - 1] | ||
|
||
if current_block.hash != current_block.calculate_hash(): | ||
return False | ||
|
||
if current_block.previous_hash != previous_block.hash: | ||
return False | ||
|
||
return True | ||
|
||
def __str__(self): | ||
return '\n'.join([str(block) for block in self.chain]) | ||
|
||
def __repr__(self): | ||
return self.__str__() |