Skip to content

Commit e73c4f7

Browse files
committed
created base structure of the blockchain with directory structure, generating genesis block and adding new block with rule based blockhash
1 parent e48b95f commit e73c4f7

File tree

9 files changed

+172
-1
lines changed

9 files changed

+172
-1
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
blockchain_project
2+
test.txt
3+
.idea
4+
**/__pycache__/

Blockchain/Backend/core/block.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Block:
2+
"""
3+
Block is a storage container that stores transactions
4+
"""
5+
def __init__(self, Height, Blocksize, BlockHeader, TxCount, Txs):
6+
self.Height = Height
7+
self.Blocksize = Blocksize
8+
self.BlockHeader = BlockHeader
9+
self.TxCount = TxCount
10+
self.Txs = Txs

Blockchain/Backend/core/blockchain.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
import time
3+
import json
4+
5+
sys.path.append('D:/xampp/htdocs/sand_box/get_job/custom-python-blockchain')
6+
7+
from Blockchain.Backend.core.block import Block
8+
from Blockchain.Backend.core.blockheader import BlockHeader
9+
from Blockchain.Backend.util.util import hash256
10+
11+
ZERO_HASH = '0' * 64
12+
VERSION = 1
13+
14+
15+
class Blockchain:
16+
def __init__(self):
17+
self.chain = []
18+
self.GenesisBlock()
19+
20+
def GenesisBlock(self):
21+
BlockHeight = 0
22+
prevBlockHash = ZERO_HASH
23+
self.addBlock(BlockHeight, prevBlockHash)
24+
25+
def addBlock(self, BlockHeight, prevBlockHash):
26+
timestamp = int(time.time())
27+
Transaction = f"Code Architect sent {BlockHeight} Bitcoins to Indranil"
28+
merkleRoot = hash256(Transaction.encode()).hex()
29+
bits = 'ffff001f'
30+
blockheader = BlockHeader(version=VERSION, prevBlockHash=prevBlockHash, merkleRoot=merkleRoot,
31+
timestamp=timestamp, bits=bits)
32+
blockheader.mine()
33+
self.chain.append(Block(BlockHeight, 1, blockheader.__dict__, 1, Transaction).__dict__)
34+
print(json.dumps(self.chain, indent=4))
35+
36+
def main(self):
37+
while True:
38+
lastBlock = self.chain[::-1]
39+
BlockHeight = lastBlock[0]["Height"] + 1
40+
prevBlockHash = lastBlock[0]["BlockHeader"]["blockHash"]
41+
self.addBlock(BlockHeight, prevBlockHash)
42+
43+
44+
if __name__ == "__main__":
45+
blockchain = Blockchain()
46+
blockchain.main()
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from Blockchain.Backend.util.util import hash256
2+
3+
4+
class BlockHeader:
5+
def __init__(self, version, prevBlockHash, merkleRoot, timestamp, bits):
6+
self.bits = bits
7+
self.timestamp = timestamp
8+
self.merkleRoot = merkleRoot
9+
self.prevBlockHash = prevBlockHash
10+
self.version = version
11+
self.nonce = 0
12+
self.blockHash = ''
13+
14+
def mine(self):
15+
while (self.blockHash[0:4]) != '0000':
16+
self.blockHash = hash256((str(self.version) + self.prevBlockHash + self.merkleRoot + str(self.timestamp) + self.bits + str(self.nonce)).encode()).hex()
17+
self.nonce += 1
18+
print(f"Mining started {self.nonce}", end='\r')

Blockchain/Backend/util/__init__.py

Whitespace-only changes.

Blockchain/Backend/util/util.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import hashlib
2+
3+
4+
def hash256(s):
5+
"""Two rounds of SHA256"""
6+
return hashlib.sha256(hashlib.sha256(s).digest()).digest()

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
"# custom-python-blockchain"
1+
# Custom Python Blockchain
2+
3+
4+
1. Let's create the directory structure first Root folder `Blockchain`, under that `Backend`, `Frontend` and `client`.
5+
Under Backend, `core` and `util`.
6+
2. Inside cre create a file name `block.py` and create a class name `Block`. Inside `__init__` method talke `
7+
Height, Blocksize, BlockHeader, TxCount, Txs` variables
8+
3. Create a new module `blockheader.py`, create a class call `BlockHeader`. Inside the `__init__` method will take
9+
`version, prevBlockHash, merkleRoot, timestamp, bits` and declare `self.nonce = 0 , self.blockHash = ''`
10+
4. Create a

requirements.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
certifi
2+
charset-normalizer
3+
click
4+
colorama
5+
Flask
6+
Flask-QRcode
7+
idna
8+
itsdangerous
9+
Jinja2
10+
MarkupSafe
11+
Pillow
12+
pycryptodome
13+
qrcode
14+
requests
15+
urllib3
16+
Werkzeug

test.txt

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[
2+
{
3+
"Height": 0,
4+
"Blocksize": 1,
5+
"BlockHeader": {
6+
"bits": "ffff001f",
7+
"timestamp": 1692624756,
8+
"merkleRoot": "01d3c8ffac385f49854b12bc69248645efddb80a0435dd09631643bdc605d1d1",
9+
"prevBlockHash": "0000000000000000000000000000000000000000000000000000000000000000",
10+
"version": 1,
11+
"nonce": 52879,
12+
"blockHash": "00001037bff589288c44d4294b840b974b6f412d321c8aab3886ea61a9d2ddd1"
13+
},
14+
"TxCount": 1,
15+
"Txs": "Code Architect sent 0 Bitcoins to Indranil"
16+
},
17+
{
18+
"Height": 1,
19+
"Blocksize": 1,
20+
"BlockHeader": {
21+
"bits": "ffff001f",
22+
"timestamp": 1692624771,
23+
"merkleRoot": "ede8645e1159205c35365fc6b765fbea7acab3d30d83f2094e87b1dc2829b69b",
24+
"prevBlockHash": "00001037bff589288c44d4294b840b974b6f412d321c8aab3886ea61a9d2ddd1",
25+
"version": 1,
26+
"nonce": 29018,
27+
"blockHash": "00003fde2fa0db36931e389f003c7b73c023ab7b764ad0787fa601c8b02e9ab0"
28+
},
29+
"TxCount": 1,
30+
"Txs": "Code Architect sent 1 Bitcoins to Indranil"
31+
},
32+
{
33+
"Height": 2,
34+
"Blocksize": 1,
35+
"BlockHeader": {
36+
"bits": "ffff001f",
37+
"timestamp": 1692624778,
38+
"merkleRoot": "ee3d44bff24ade0bd29fbf90bcfa1a8a2ef2c991a92bf92e8e8d7bf85cf75e30",
39+
"prevBlockHash": "00003fde2fa0db36931e389f003c7b73c023ab7b764ad0787fa601c8b02e9ab0",
40+
"version": 1,
41+
"nonce": 27230,
42+
"blockHash": "00005ad8ab369e6c399cbbf13170ecb403e4d103f3d7c3f1bce28946e672fb23"
43+
},
44+
"TxCount": 1,
45+
"Txs": "Code Architect sent 2 Bitcoins to Indranil"
46+
},
47+
{
48+
"Height": 3,
49+
"Blocksize": 1,
50+
"BlockHeader": {
51+
"bits": "ffff001f",
52+
"timestamp": 1692624784,
53+
"merkleRoot": "d0249e654794fa76a823925895732057dc5c51075131f4784a65d31f93754113",
54+
"prevBlockHash": "00005ad8ab369e6c399cbbf13170ecb403e4d103f3d7c3f1bce28946e672fb23",
55+
"version": 1,
56+
"nonce": 89050,
57+
"blockHash": "0000a4906e8daed09d5ad654ec4d6d119a060483b7f91e0f931cb68c98e28d88"
58+
},
59+
"TxCount": 1,
60+
"Txs": "Code Architect sent 3 Bitcoins to Indranil"
61+
}
62+
]

0 commit comments

Comments
 (0)