-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.py
227 lines (203 loc) · 9.78 KB
/
blockchain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from functools import reduce
import hashlib as hl
import json
import pickle
# Import two functions from our hash_util.py file. Omit the ".py" in the import
from utility.hash_util import hash_block
from utility.verification import Verification
from block import Block
from transaction import Transaction
from wallet import Wallet
# The reward we give to miners (for creating a new block)
MINING_REWARD = 10
print(__name__)
class Blockchain:
"""The Blockchain class manages the chain of blocks as well as open transactions and the node on which it's running.
Attributes:
:chain: The list of blocks
:open_transactions (private): The list of open transactions
:hosting_node: The connected node (which runs the blockchain).
"""
def __init__(self, public_key, node_id):
"""The constructor of the Blockchain class."""
# Our starting block for the blockchain
genesis_block = Block(0, '', [], 100, 0)
# Initializing our (empty) blockchain list
self.chain = [genesis_block]
# Unhandled transactions
self.__open_transactions = []
self.public_key = public_key
self.__peer_nodes = set()
self.node_id = node_id
self.load_data()
# This turns the chain attribute into a property with a getter (the method below) and a setter (@chain.setter)
@property
def chain(self):
return self.__chain[:]
# The setter for the chain property
@chain.setter
def chain(self, val):
self.__chain = val
def get_open_transactions(self):
"""Returns a copy of the open transactions list."""
return self.__open_transactions[:]
def load_data(self):
"""Initialize blockchain + open transactions data from a file."""
try:
with open('blockchain-{}.txt'.format(self.node_id), mode='r') as f:
# file_content = pickle.loads(f.read())
file_content = f.readlines()
# blockchain = file_content['chain']
# open_transactions = file_content['ot']
blockchain = json.loads(file_content[0][:-1])
# We need to convert the loaded data because Transactions should use OrderedDict
updated_blockchain = []
for block in blockchain:
converted_tx = [Transaction(
tx['sender'], tx['recipient'], tx['signature'], tx['amount']) for tx in block['transactions']]
updated_block = Block(
block['index'], block['previous_hash'], converted_tx, block['proof'], block['timestamp'])
updated_blockchain.append(updated_block)
self.chain = updated_blockchain
open_transactions = json.loads(file_content[1][:-1])
# We need to convert the loaded data because Transactions should use OrderedDict
updated_transactions = []
for tx in open_transactions:
updated_transaction = Transaction(
tx['sender'], tx['recipient'], tx['signature'], tx['amount'])
updated_transactions.append(updated_transaction)
self.__open_transactions = updated_transactions
peer_nodes = json.loads(file_content[2])
self.__peer_nodes = set(peer_nodes)
except (IOError, IndexError):
pass
finally:
print('Cleanup!')
def save_data(self):
"""Save blockchain + open transactions snapshot to a file."""
try:
with open('blockchain-{}.txt'.format(self.node_id), mode='w') as f:
saveable_chain = [block.__dict__ for block in [Block(block_el.index, block_el.previous_hash, [
tx.__dict__ for tx in block_el.transactions], block_el.proof, block_el.timestamp) for block_el in self.__chain]]
f.write(json.dumps(saveable_chain))
f.write('\n')
saveable_tx = [tx.__dict__ for tx in self.__open_transactions]
f.write(json.dumps(saveable_tx))
f.write('\n')
f.write(json.dumps(list(self.__peer_nodes)))
# save_data = {
# 'chain': blockchain,
# 'ot': open_transactions
# }
# f.write(pickle.dumps(save_data))
except IOError:
print('Saving failed!')
def proof_of_work(self):
"""Generate a proof of work for the open transactions, the hash of the previous block and a random number (which is guessed until it fits)."""
last_block = self.__chain[-1]
last_hash = hash_block(last_block)
proof = 0
# Try different PoW numbers and return the first valid one
while not Verification.valid_proof(self.__open_transactions, last_hash, proof):
proof += 1
return proof
def get_balance(self):
"""Calculate and return the balance for a participant.
"""
if self.public_key == None:
return None
participant = self.public_key
# Fetch a list of all sent coin amounts for the given person (empty lists are returned if the person was NOT the sender)
# This fetches sent amounts of transactions that were already included in blocks of the blockchain
tx_sender = [[tx.amount for tx in block.transactions
if tx.sender == participant] for block in self.__chain]
# Fetch a list of all sent coin amounts for the given person (empty lists are returned if the person was NOT the sender)
# This fetches sent amounts of open transactions (to avoid double spending)
open_tx_sender = [tx.amount
for tx in self.__open_transactions if tx.sender == participant]
tx_sender.append(open_tx_sender)
print(tx_sender)
amount_sent = reduce(lambda tx_sum, tx_amt: tx_sum + sum(tx_amt)
if len(tx_amt) > 0 else tx_sum + 0, tx_sender, 0)
# This fetches received coin amounts of transactions that were already included in blocks of the blockchain
# We ignore open transactions here because you shouldn't be able to spend coins before the transaction was confirmed + included in a block
tx_recipient = [[tx.amount for tx in block.transactions
if tx.recipient == participant] for block in self.__chain]
amount_received = reduce(lambda tx_sum, tx_amt: tx_sum + sum(tx_amt)
if len(tx_amt) > 0 else tx_sum + 0, tx_recipient, 0)
# Return the total balance
return amount_received - amount_sent
def get_last_blockchain_value(self):
""" Returns the last value of the current blockchain. """
if len(self.__chain) < 1:
return None
return self.__chain[-1]
# This function accepts two arguments.
# One required one (transaction_amount) and one optional one (last_transaction)
# The optional one is optional because it has a default value => [1]
def add_transaction(self, recipient, sender, signature, amount=1.0):
""" Append a new value as well as the last blockchain value to the blockchain.
Arguments:
:sender: The sender of the coins.
:recipient: The recipient of the coins.
:amount: The amount of coins sent with the transaction (default = 1.0)
"""
# transaction = {
# 'sender': sender,
# 'recipient': recipient,
# 'amount': amount
# }
if self.public_key == None:
return False
transaction = Transaction(sender, recipient, signature, amount)
if Verification.verify_transaction(transaction, self.get_balance):
self.__open_transactions.append(transaction)
self.save_data()
return True
return False
def mine_block(self):
"""Create a new block and add open transactions to it."""
# Fetch the currently last block of the blockchain
if self.public_key == None:
return None
last_block = self.__chain[-1]
# Hash the last block (=> to be able to compare it to the stored hash value)
hashed_block = hash_block(last_block)
proof = self.proof_of_work()
# Miners should be rewarded, so let's create a reward transaction
# reward_transaction = {
# 'sender': 'MINING',
# 'recipient': owner,
# 'amount': MINING_REWARD
# }
reward_transaction = Transaction('MINING', self.public_key, '', MINING_REWARD)
# Copy transaction instead of manipulating the original open_transactions list
# This ensures that if for some reason the mining should fail, we don't have the reward transaction stored in the open transactions
copied_transactions = self.__open_transactions[:]
for tx in copied_transactions:
if not Wallet.verify_transaction(tx):
return None
copied_transactions.append(reward_transaction)
block = Block(len(self.__chain), hashed_block,
copied_transactions, proof)
self.__chain.append(block)
self.__open_transactions = []
self.save_data()
return block
def add_peer_node(self, node):
"""Adds a new node to a peer node set.
Arguments:
:node: The node URL which should be added.
"""
self.__peer_nodes.add(node)
self.save_data()
def remove_peer_node(self, node):
"""Removes a node from a peer node set.
Arguments:
:node: The node URL which should be removed.
"""
self.__peer_nodes.discard(node)
self.save_data()
def get_peer_nodes(self):
"""Return a list of connected peer nodes."""
return list(self.__peer_nodes)