Skip to content

Commit 45952bf

Browse files
First fork for blockchain.
This project is to start building a blockchain, and benchmark differences between CPU and GPU.
1 parent e2e017c commit 45952bf

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ Simple Python Blockchain with MINING!
44
This is the source code for howCode's simple Python Blockchain.
55

66
You can watch the video that accompanies this source code here: https://youtu.be/b81Ib_oYbFk
7+
8+
9+
##############################
10+
# Forked by BuildAndDestroy #
11+
##############################
12+
13+
14+
The end goal here is to learn how block chain works with CPU vs. GPU.

blockchain.py

100644100755
Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
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+
111
import datetime
212
import hashlib
313

4-
class Block:
5-
blockNo = 0
6-
data = None
7-
next = None
14+
15+
class Block(object):
16+
"""Everything needed to create a Block."""
817
hash = None
9-
nonce = 0
10-
previous_hash = 0x0
11-
timestamp = datetime.datetime.now()
1218

1319
def __init__(self, data):
1420
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()
1526

1627
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')
2436
)
25-
return h.hexdigest()
37+
return hash_signature.hexdigest()
2638

2739
def __str__(self):
28-
return "Block Hash: " + str(self.hash()) + "\nBlockNo: " + str(self.blockNo) + "\nBlock Data: " + str(self.data) + "\nHashes: " + str(self.nonce) + "\n--------------"
40+
return 'Block Hash: {}\nBlockNo: {}\nBlock Data: {}\nHashes: {}\n--------------'.format(str(self.hash()), str(self.blockNo), str(self.data), str(self.nonce))
41+
2942

30-
class Blockchain:
43+
class Blockchain(object):
44+
"""Formula for creating new blocks.
3145
46+
The Genesis block starts at zero,
47+
then each new block is hashed out from the previous block.
48+
"""
3249
diff = 20
3350
maxNonce = 2**32
3451
target = 2 ** (256-diff)
@@ -37,27 +54,35 @@ class Blockchain:
3754
dummy = head = block
3855

3956
def add(self, block):
40-
57+
"""Add a new block to the blockchain."""
4158
block.previous_hash = self.block.hash()
4259
block.blockNo = self.block.blockNo + 1
4360

4461
self.block.next = block
4562
self.block = self.block.next
4663

4764
def mine(self, block):
48-
for n in range(self.maxNonce):
65+
"""Mine a new block."""
66+
for count in range(self.maxNonce):
4967
if int(block.hash(), 16) <= self.target:
5068
self.add(block)
5169
print(block)
5270
break
5371
else:
5472
block.nonce += 1
5573

56-
blockchain = Blockchain()
5774

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+
6086

61-
while blockchain.head != None:
62-
print(blockchain.head)
63-
blockchain.head = blockchain.head.next
87+
if __name__ == '__main__':
88+
main()

0 commit comments

Comments
 (0)