forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchain.py
More file actions
79 lines (62 loc) · 2.42 KB
/
Copy pathBlockchain.py
File metadata and controls
79 lines (62 loc) · 2.42 KB
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
import time, hashlib, json
"""
Created by trustgeek on 2017-10-23 (11:04).
"""
class Block:
def __init__(self, index, timestamp, data, previous_hash):
self._index = index
self._timestamp = timestamp
self._data = data
self._previous_hash = previous_hash
self._nounce = None
self._hash = self.hash()
def valid_hash(self, the_hash):
return the_hash.startswith('0000')
def mined(self):
return self._nounce is not None
def mine(self):
start_time = time.time()
the_nounce = self._nounce or 0
while True:
the_hash = self.hash(nonce=the_nounce)
if self.valid_hash(the_hash):
self._hash = the_hash
self._nounce = the_nounce
end_time = time.time()
time_taken = end_time - start_time
print("----- Mine time: %s -----" % time_taken)
return
else:
the_nounce += 1
def hash(self, nonce=None):
self._nounce = nonce or self._nounce
block_json = {'index': self._index, 'timestamp': self._timestamp, 'data': self._data,
'previous_hash': self._previous_hash, 'nounce': self._nounce}
return hashlib.sha256(json.dumps(block_json).encode('utf-8')).hexdigest()
def __str__(self):
return json.dumps(
{'index': self._index, 'timestamp': self._timestamp, 'data': self._data,
'previous_hash': self._previous_hash,
'hash': self._hash, 'nounce': self._nounce})
# genesis block is the first block in the blockchain
def genesis_block():
return Block(0, time.time(), "This is genesis block", '0')
# create next block for blockchain
def next_block(_previous_block, data):
_new_index = _previous_block._index + 1
_new_timestamp = time.time()
_previous_hash = _previous_block._hash
return Block(_new_index, _new_timestamp, data, _previous_hash)
blockchain = [genesis_block()]
# first block for blockchain
previous_block = blockchain[0]
num_of_blocks = 20
# iteratively adding block to blockchain
for i in range(0, num_of_blocks):
transaction = {'from': '0x12345' + str(i), 'to': '0x54321' + str(i * 3), 'amount': 100}
new_block = next_block(previous_block, transaction)
new_block.mine()
blockchain.append(new_block)
previous_block = new_block
print("Block added to blockchain")
print(new_block)