-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-chain.py
executable file
·75 lines (55 loc) · 2.25 KB
/
custom-chain.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
# Cameron M. Merrick
# 2/10/2017
# Python - Custom Blockchain
# An implementation of the blockchain in Python.
# For educational purposes and for development of my GitHub portfolio.
import hashlib as hasher
import datetime as date
class Block:
def __init__(self, index, timestamp, data, p_hash):
# Initialize the members of a Block object
self.index = index
self.timestamp = timestamp
self.data = data
self.p_hash = p_hash
self.hash = self.hash_block()
def hash_block(self):
sha = hasher.sha256()
sha.update(str(self.index).encode(encoding='utf_8', errors='strict') + str(self.timestamp).encode(encoding='utf_8', errors='strict') + str(self.data).encode(encoding='utf_8', errors='strict') + str(self.p_hash).encode(encoding='utf_8'))
return sha.hexdigest()
# Create a new block at the margin (end) of the chain as it grows
def next_block(last_block):
this_index = last_block.index + 1
this_timestamp = date.datetime.now()
this_data = "001010001011010111010100" + str(this_index)
this_hash = last_block.hash
return Block(this_index, this_timestamp, this_data, this_hash)
# Create 'Genesis' Block (to be called first in main)
def make_genesis():
# Manually create the first block with user defined params
return Block(0, date.datetime.now(), "GENESIS [...]", "0")
# Testing the blockchain architecture
def main():
# Start with the genesis block as a standalone code chunk
blockchain = [make_genesis()]
previous_block = blockchain[0]
# Define the number of blocks to simulate
blockchain_length = 20
# Add blocks to the chain via a for loop
for i in range(0, blockchain_length):
staged_block = next_block(previous_block)
blockchain.append(staged_block)
previous_block = staged_block
print("Block # {} added to the blockchain".format(staged_block.index))
print("Hash: {}\n".format(staged_block.hash))
if __name__ == "__main__":
main()
# Minor thing: For Python 3.6, you need to encode hash_block like
#
# def hash_block(self):
# sha = hasher.sha256()
# sha.update((str(self.index) +
# str(self.timestamp) +
# str(self.data) +
# str(self.previous_hash)).encode()) #change here
# return sha.hexdigest()