-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.py
219 lines (161 loc) · 5.94 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
import json
import datetime
import hashlib
class Block:
def __init__(self,**kwargs):
"""
- index — it’s used to track the position of a block within the blockchain
- previous_hash — it used to reference the hash of the previous block within the blockchain.
- data — it gives details of the transactions done, for example, the amount bought.
- timestamp— it inserts a timestamp for all the transactions performed.
"""
self.index = kwargs['index']
self.data = kwargs['data']
self.nonce = kwargs['nonce']
self.previous_hash = kwargs['previous_hash']
self.timestamp = kwargs['timestamp']
def code_hash(self):
"""
is used to produce the cryptographic hash of each block based on the above values.
Returns:
[type]: imported the SHA-256 algorithm into the cryptocurrency blockchain project to help in getting the hashes of the blocks.
"""
encoded_block = json.dumps(self.__dict__, sort_keys=True).encode()
return hashlib.sha256(encoded_block).hexdigest()
def block_data(self):
"""
resulted new block
Returns:
dict: [description]
"""
return {
"hash": self.code_hash(),
"index":self.index,
"timestamp":self.timestamp,
"nonce":self.nonce,
"previous_hash": self.previous_hash,
"data": self.data,
}
class BlockChain:
__NOW_DATE = datetime.datetime.now()
def __init__(self):
"""
instantiates the blockchain
- self.chain — this variable stores all the blocks.
- self.current_data — this variable stores information about the transactions in the block.
- self.build_genesis() — this method is used to create the initial block in the chain.
"""
self.chain = []
self.data_current = []
self.genesis_block()
def genesis_block(self):
"""
method is used for creating the initial block in the chain
"""
self.build_block(nonce=1, previous_hash='0')
def build_block(self, nonce, previous_hash):
"""
transactions
Args:
- nonce ([int]): randomly incremented so that the result of the hash function on the block satisfies
- previous_hash ([int]): contains the result of the hash function on the previous block
Returns:
block
"""
block = Block(
index= len(self.chain) + 1,
timestamp= str(self.__NOW_DATE),
nonce= nonce,
previous_hash= previous_hash,
data= self.data_current
)
self.data_current = []
self.chain.append(block.block_data())
return block
def get_last_block(self):
"""
last block
Returns:
dict block
"""
return self.chain[-1]
def add_transaction(self, sender, receiver, amount, time):
""" data of transactions on a block
Args:
sender ([type]): sender’s information
receiver ([type]): receiver’s information
amount ([type]): amount send
time ([type]): time transactions
"""
self.data_current.append({
'sender': sender,
'receiver': receiver,
'amount': amount,
'time': str(self.__NOW_DATE)
})
def hash(self, block):
"""
generator previous_hash
Args:
block ([type]): [description]
Returns:
[type]: [description]
"""
encoded_block = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(encoded_block).hexdigest()
def proof_of_work(self):
"""is by taking the latest block and add a nonce such to satisfy tha
Returns:
[type]: [description]
"""
previous_block = self.get_last_block()
previous_nonce = previous_block['nonce']
previous_hash = self.hash(previous_block)
new_nonce = 1
check_nonce = False
while check_nonce is False:
hash_operation = hashlib.sha256(
str(new_nonce**2 - previous_nonce**2).encode()).hexdigest()
if hash_operation[:4] == '0000':
check_nonce = True
else:
new_nonce += 1
#return new_nonce
return self.build_block(new_nonce, previous_hash)
def is_chain_valid(self, chain):
previous_block = chain[0]
block_index = 1
while block_index < len(chain):
block = chain[block_index]
if block['previous_hash'] != self.hash(previous_block):
return False
previous_nonce = previous_block['nonce']
nonce = block['nonce']
hash_operation = hashlib.sha256(str(nonce**2 - previous_nonce**2).encode()).hexdigest()
if hash_operation[:4] != '0000':
return False
previous_block = block
block_index += 1
return True
# ----------------------------------- print--------------------------
blockchain = BlockChain()
node_address = "a0ea10d5b483109573c882de44"
root_node = '006cc49ce412b7c4ee5e48588e68e10'
def mine_block():
blockchain.add_transaction(
sender=root_node,
receiver=node_address,
amount=1.15,
time=str(datetime.datetime.now()))
block = blockchain.proof_of_work()
response = {
'message': 'Success a block!',
'index': block.block_data()['index'],
'timestamp': block.block_data()['timestamp'],
'nonce': block.block_data()['nonce'],
'previous_hash': block.block_data()['previous_hash'],
'transactions': block.block_data()['data'],
}
return response
for item in range(0,10):
print(mine_block())