-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoeda.py
91 lines (70 loc) · 2.77 KB
/
moeda.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
import hashlib
import time
import binascii
class Block:
def __init__(self, index, previousHash, timestamp, data, hash, difficulty, nonce):
self.index = index
self.previousHash = previousHash
self.timestamp = timestamp
self.data = data
self.hash = hash
self.difficulty = difficulty
self.nonce = nonce
class Blockain:
def __init__(self, genesisBlock):
self.__chain = []
self.__chain.append(genesisBlock)
self.DIFFICULTY_ADJUSTMENT = 10
self.BLOCK_INTERVAL = 120
def getLatestBlock(self):
return self.__chain[len(self.__chain) - 1]
def generateNextBlock(self, data):
previousBlock = self.getLatestBlock()
nextIndex = previousBlock.index + 1
nextTimestamp = int(round(time.time() * 1000))
nextPreviousHash = previousBlock.hash
newBlock = Block(nextIndex, nextPreviousHash, nextTimestamp, data,
calculateHash(nextIndex, nextPreviousHash, nextTimestamp, data))
if validatingBlock(newBlock) == True:
self.__chain.append(newBlock)
def validatingBlock(self, newBlock):
previousBlock = self.getLatestBlock()
if previousBlock.index + 1 != newBlock.index:
return False
elif previousBlock.hash != newBlock.previousHash:
return False
return True
def hashMatchesDifficulty(self,hash, difficulty):
hashBinary = binascii.unhexlify(hash)
requiredPrefix = '0' *int(difficulty)
return hashBinary.startswith(requiredPrefix)
def findBlock(self, index, previousHash, timestamp, data, difficulty):
nonce = 0
while True:
hash = self.calculateHash(index, previousHash, timestamp, data, difficulty, nonce)
if self.hashMatchesDifficulty(hash, difficulty):
block = Block(index, previousHash, timestamp, data, difficulty, nonce)
return block
nonce = nonce + 1
def getDifficulty(self):
LatestBlock = self.getLatestBlock()
if LatestBlock.index % self.DIFFICULTY_ADJUSTMENT == 0 and LatestBlock.index != 0:
return self.getAdjustedDifficulty()
else:
return LatestBlock.difficulty
def getAdjustedDifficulty(self):
LatestBlock = self.getLatestBlock()
prevAdjustmentBlock = self.blockchain[len(self.blockchain) - self.DIFFICULTY_ADJUSTMENT]
timeExpected = self.BLOCK_INTERVAL * self.DIFFICULTY_ADJUSTMENT
timeTaken = LatestBlock.timestamp - prevAdjustmentBlock.timestamp
if timeTaken < timeExpected * 2:
return prevAdjustmentBlock.difficulty + 1
elif timeTaken > timeExpected * 2:
return prevAdjustmentBlock.difficulty -1
else:
return prevAdjustmentBlock.difficulty
def calculateHash(index, previousHash, timestamp, data, difficulty, nonce):
return hashlib.sha256((str(index) + previousHash + str(timestamp) + data + str(difficulty) + str(nonce)).encode('utf-8')).hexdigest()
ts = int(round(time.time() * 1000))
genesisBlock = Block(0, "",ts, "genesis BLOCK",
calculateHash(0, "",ts, "Genesis BLOCK"))