-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockchain_network.py
167 lines (126 loc) · 5.11 KB
/
blockchain_network.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
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA
import binascii
import hashlib
import json
from block import Block
import time
DIFFICULTY = 5
time_limit = 10 # seconds
class Blockchain:
COMMISSION = 'commision'
def __init__(self):
self.peers = []
self.transactions = []
self.chain = []
self.isStopped = False
self.maxTransactionTimeStamp = -1
self.create_genesis_block()
# Create Genesis Block
def create_genesis_block(self):
genesis_block = Block(DIFFICULTY)
self.chain.append(genesis_block)
# Adding peer
def addPeers(self, obj):
if isinstance(obj, list):
for k in obj:
if isinstance(k, int) and k not in self.peers:
self.peers.append(k)
elif isinstance(obj, int) and obj not in self.peers:
self.peers.append(obj)
return True
def removePeer(self, obj):
self.peers.remove(obj)
def replaceChain(self, chain):
if self.verify_chain(chain):
self.chain = chain
self.maxTransactionTimeStamp = -1
for block in self.chain:
for t in block.transactions:
self.maxTransactionTimeStamp = max(
self.maxTransactionTimeStamp, t['timestamp'])
def verify_chain(self, chain):
# verifyication of new chain should be done here
return True
def get_balance(self, pubKey):
balance = 0
for block in self.chain:
for t in block.transactions:
if t['sender_public_key'] == pubKey:
balance = balance - t['amount']
if t['recipient_public_key'] == pubKey:
balance = balance + t['amount']
return balance
# Return last block
def last_block_chain(self):
return self.chain[-1]
# Proof of work
def proof_of_work(self, block):
last_block = self.last_block_chain()
difficulty = DIFFICULTY
if last_block.index != 0:
if last_block.minedAt - last_block.timestamp < time_limit:
difficulty = last_block.difficulty + 1
elif last_block.minedAt - last_block.timestamp > time_limit:
difficulty = last_block.difficulty - 1
else:
difficulty = last_block.difficulty
# block - block which needs to be added
block.difficulty = difficulty
block.nonce = 0
while not self.is_block_valid(block) and self.isStopped == False:
block.nonce += 1
if self.isStopped == False:
block.minedAt = time.time()
# Calculate hash of block
def calculate_hash_of_block(self, block):
string_format_block = json.dumps(block.get_block_data(),
sort_keys=True).encode()
return hashlib.sha256(string_format_block).hexdigest()
# Add transaction to blockchain network
def verify_transaction(self, transaction, digital_signature):
sender_public_key = transaction['sender_public_key']
# If any other check you want to perform -----------------
if digital_signature == self.COMMISSION:
return True
if (self.verify_signature(sender_public_key, transaction,
digital_signature)):
return True
else:
return False
# Verifying transaction singature
def verify_signature(self, sender_public_key, transaction,
digital_signature):
public_key = RSA.importKey(binascii.unhexlify(sender_public_key))
verifier = PKCS1_v1_5.new(public_key)
message = SHA.new(str(transaction).encode('utf8'))
return verifier.verify(message, binascii.unhexlify(digital_signature))
# Checking validity of block to be added
def is_block_valid(self, block):
# Calculate hash of block items
block_hash = self.calculate_hash_of_block(block)
return block_hash[:block.difficulty] == '0' * block.difficulty
# Add block to blockchain network
def add_block(self, block):
self.chain.append(block)
for t in block.transactions:
self.maxTransactionTimeStamp = max(self.maxTransactionTimeStamp,
t['timestamp'])
# Mining of block
def mine(self, transactions, minerId, timestamp):
prev_block = self.last_block_chain()
prev_hash = self.calculate_hash_of_block(prev_block)
# Creating new block
new_block = Block(DIFFICULTY)
new_block.minerId = minerId
new_block.index = prev_block.index + 1
new_block.previous_hash = prev_hash
# Coinbase transaction is also included
# new_block.transactions = self.get_reward(self.available_transactions) ## need to implement
new_block.transactions = transactions
new_block.timestamp = timestamp
# Nonce is correctily calculated
self.proof_of_work(new_block)
# This valid block will be added to block chain
return new_block