1
+ #/usr/bin/env python3
2
+ """Simple Python Project
3
+
4
+ This python script goes over how a blockchain works.
5
+ A genesis block starts at zero, then hashes created for each new block.
6
+
7
+ Original code found here:
8
+ https://github.com/howCodeORG/Simple-Python-Blockchain
9
+ """
10
+
1
11
import datetime
2
12
import hashlib
3
13
4
- class Block :
5
- blockNo = 0
6
- data = None
7
- next = None
14
+
15
+ class Block (object ):
16
+ """Everything needed to create a Block."""
8
17
hash = None
9
- nonce = 0
10
- previous_hash = 0x0
11
- timestamp = datetime .datetime .now ()
12
18
13
19
def __init__ (self , data ):
14
20
self .data = data
21
+ self .blockNo = 0
22
+ self .next = None
23
+ self .nonce = 0
24
+ self .previous_hash = 0x0
25
+ self .timestamp = datetime .datetime .now ()
15
26
16
27
def hash (self ):
17
- h = hashlib .sha256 ()
18
- h .update (
19
- str (self .nonce ).encode ('utf-8' ) +
20
- str (self .data ).encode ('utf-8' ) +
21
- str (self .previous_hash ).encode ('utf-8' ) +
22
- str (self .timestamp ).encode ('utf-8' ) +
23
- str (self .blockNo ).encode ('utf-8' )
28
+ """Create a hash for each attribute and return."""
29
+ hash_signature = hashlib .sha256 ()
30
+ hash_signature .update (
31
+ str (self .nonce ).encode ('utf-8' ) +
32
+ str (self .data ).encode ('utf-8' ) +
33
+ str (self .previous_hash ).encode ('utf-8' ) +
34
+ str (self .timestamp ).encode ('utf-8' ) +
35
+ str (self .blockNo ).encode ('utf-8' )
24
36
)
25
- return h .hexdigest ()
37
+ return hash_signature .hexdigest ()
26
38
27
39
def __str__ (self ):
28
- return "Block Hash: " + str (self .hash ()) + "\n BlockNo: " + str (self .blockNo ) + "\n Block Data: " + str (self .data ) + "\n Hashes: " + str (self .nonce ) + "\n --------------"
40
+ return 'Block Hash: {}\n BlockNo: {}\n Block Data: {}\n Hashes: {}\n --------------' .format (str (self .hash ()), str (self .blockNo ), str (self .data ), str (self .nonce ))
41
+
29
42
30
- class Blockchain :
43
+ class Blockchain (object ):
44
+ """Formula for creating new blocks.
31
45
46
+ The Genesis block starts at zero,
47
+ then each new block is hashed out from the previous block.
48
+ """
32
49
diff = 20
33
50
maxNonce = 2 ** 32
34
51
target = 2 ** (256 - diff )
@@ -37,27 +54,35 @@ class Blockchain:
37
54
dummy = head = block
38
55
39
56
def add (self , block ):
40
-
57
+ """Add a new block to the blockchain."""
41
58
block .previous_hash = self .block .hash ()
42
59
block .blockNo = self .block .blockNo + 1
43
60
44
61
self .block .next = block
45
62
self .block = self .block .next
46
63
47
64
def mine (self , block ):
48
- for n in range (self .maxNonce ):
65
+ """Mine a new block."""
66
+ for count in range (self .maxNonce ):
49
67
if int (block .hash (), 16 ) <= self .target :
50
68
self .add (block )
51
69
print (block )
52
70
break
53
71
else :
54
72
block .nonce += 1
55
73
56
- blockchain = Blockchain ()
57
74
58
- for n in range (10 ):
59
- blockchain .mine (Block ("Block " + str (n + 1 )))
75
+ def main ():
76
+ """Create bew blocks on the blockchain."""
77
+ blockchain = Blockchain ()
78
+
79
+ for count in range (10 ):
80
+ blockchain .mine (Block ("Block " + str (count + 1 )))
81
+
82
+ while blockchain .head != None :
83
+ print (blockchain .head )
84
+ blockchain .head = blockchain .head .next
85
+
60
86
61
- while blockchain .head != None :
62
- print (blockchain .head )
63
- blockchain .head = blockchain .head .next
87
+ if __name__ == '__main__' :
88
+ main ()
0 commit comments