-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockchain.py
67 lines (54 loc) · 1.75 KB
/
blockchain.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
from datetime import datetime
from hashlib import sha256
import json
class Block:
def __init__(self, previousHash, data):
self.data = data
self.previousHash = previousHash
self.timeStamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.proofOfWork = 0
self.hash = self.calculateHash()
def calculateHash(self):
return sha256(
(
self.previousHash +
json.dumps(self.data) +
self.timeStamp +
str(self.proofOfWork)
)
.encode() # encoding before hashing
).hexdigest()
def mine(self, difficulty):
# find the hash
while not self.hash.startswith('0' * difficulty):
self.proofOfWork += 1
self.hash = self.calculateHash()
class Blockchain:
def __init__(self):
genesisBlock = Block('0', {'isGenesis': True})
self.chain = [genesisBlock]
def addBlock(self, data):
lastBlock = self.chain[len(self.chain) - 1]
newBlock = Block(lastBlock.hash, data)
newBlock.mine(2) # find a hash for new block
self.chain.append(newBlock)
def getLatestBlock(self):
return self.chain[-1]
def isValid(self):
for i in range(1, len(self.chain)):
currentBlock = self.chain[i]
previousBlock = self.chain[i - 1]
if currentBlock.hash != currentBlock.calculateHash(): return False
if currentBlock.previousHash != previousBlock.hash: return False
return True
blockchain = Blockchain()
blockchain.addBlock({
'from': 'Vaibhav',
'to': 'Anubhav',
'amount': 100
})
blockchain.addBlock({
'from': 'Anubhav',
'to': 'David',
'amount': 150
})