-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.py
131 lines (102 loc) · 3.81 KB
/
block.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import datetime
import hashlib
import array
import json
"""
Title: Python Block Chain
Author: Bradley K. Hnatow
Description: A simple block chain program devloped in python later
to be used and updated for more complex projects.
"""
class block():
def __init__(self, prevHash, dataArray, proof, nonce=0):
self.timeOfCreation = block.calculateTime()
self.prevHash = prevHash
self.dataArray = dataArray
self.hash = block.calculateHash(self)
self.nonce = nonce
# calculateTime()
# Calculates the time that the newly created block was created
# Sets the date of the block in a (Year, Day Of Week, Month, Date,
# Hour, Minute, Second) format.
# @returns String
@staticmethod
def calculateTime():
date = datetime.datetime.now()
formalDate = date.strftime("%Y %a %b %d %H:%M:%S")
return formalDate
# calculateHash(self)
# Generates a hash for the newly created block by using
# variables like timeOfCreation, and the prevHash.
# Hash is generated using sha256
# @returns void
def calculateHash(self):
hashCode = hashlib.sha256(
(str(self.timeOfCreation) + self.prevHash).encode('utf-8')).hexdigest()
self.hash = hashCode
class blockChain():
def __init__(self):
self.unconfirmed = []
self.chain = []
genesis = block("0000", [], ) #(self, prevHash, data, proof)
self.chain.append(genesis)
self.valid = True
# printBlockChain(self)
# Prints the current block chain providing each
# block's Data, Time of creation, Hash, & PrevHash
# @reutns void
def printBlockChain(self):
for x in range(len(self.chain)):
print(
"Data: " + self.chain[x].data
+ "\nTime: " + self.chain[x].timeOfCreation
+ "\nHash: " + self.chain[x].blockHash
+ "\nPrevHash: " + self.chain[x].prevHash + "\n"
)
# getLatestHash(self)
# Provides the latest hash of blocks in the blockChain
# @returns String
@property
def getLatestHash(self):
return str(self.chain[len(self.chain)-1].blockHash)
# getLatestBlock(self)
# Returns the most recently added block on the chain
# @returns <block>
@property
def getLatestBlock(self):
return self.chain[-1]
def addNewUnconfirmed(self,data):
self.unconfirmed.append(data)
# addBlock(self, data)
# Adds a new block to the chain array
# @reutrns void
def addBlock(self, data, proof):
newBlock = block(blockChain.getLatestHash(self), len(self.chain), data)
self.chain.append(newBlock)
self.proof = self.testChainIntegrity()
# testChainIntegrity(self)
# Checks the current self.chain[x].hash and compares it to
# next block in the chain's prevHash
# @returns void
def testChainIntegrity(self):
for x in range(len(self.chain)-1):
if self.chain[x].blockHash == self.chain[x+1].prevHash:
self.valid = True
elif self.chain[x].blockHash != self.chain[x+1].prevHash:
self.valid = False
break
if self.valid == True:
print("Accepted: Block Chain is Valid.\n")
elif self.valid == False:
print("Error: Block Chain is invalid.\n")
else:
print("Error: Something went wrong.\n")
testBlockChain = blockChain()
testBlockChain.printBlockChain()
testBlockChain.addBlock("Second Block")
testBlockChain.printBlockChain()
testBlockChain.addBlock("Third Block")
testBlockChain.printBlockChain()
testBlockChain.addBlock("Forth Block")
testBlockChain.printBlockChain()
testBlockChain.testChainIntegrity()