Repository navigation
Expand file tree
/
Copy pathconsensus.py
More file actions
90 lines (80 loc) · 2.7 KB
/
Copy pathconsensus.py
File metadata and controls
90 lines (80 loc) · 2.7 KB
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
import block
import random
import hashlib
import datetime
import threading
import blockchain
import sqldb
MSG_LASTBLOCK = 'getlastblock'
MSG_BLOCK = 'block'
MSG_BLOCKS = 'getblocks'
MSG_HELLO = 'hello'
def handleMessages(bc, messages):
cmd = messages[0] if isinstance(messages, list) else str(messages)
cmd = cmd.lower()
if cmd == MSG_LASTBLOCK:
return bc.getLastBlock() # sql query?
elif cmd == MSG_HELLO:
return MSG_HELLO
elif cmd == MSG_BLOCKS:
return sqldb.blocksQuery(messages)
elif cmd == MSG_BLOCK:
return sqldb.blockQuery(messages)
else:
return None
def validateBlockHeader(b):
# check block header
if b.hash == b.calcBlockhash():
return True
return False
def validateBlock(block, lastBlock):
# check block chaining
if block.prev_hash == lastBlock.hash:
return True
return False
def validateChain(bc, l):
lastBlock = bc.getLastBlock()
#print lastBlock.blockInfo()
for b in l:
#print b
b = sqldb.dbtoBlock(b) # maybe avoid converting
# validade block header ?
#validateBlockHeader(b)
if validateBlock(b, lastBlock):
lastBlock = b
bc.addBlocktoBlockchain(b)
sqldb.writeBlock(b)
else: # fork
#sqldb.writeFork(b)
return b
return None
class Consensus:
def __init__(self, difficulty):
self.difficulty = difficulty
self.type = "PoW"
self.MAX_NONCE = 2 ** 32
self.target = 2 ** (4 * self.difficulty) - 1
def POW(self, lastBlock, stop):
# TODO change stop to skip
# chr simplifies merkle root and add randomness
tx = chr(random.randint(1,100))
mroot = hashlib.sha256(tx).hexdigest()
timestamp = str(datetime.datetime.now())
c_header = str(lastBlock.hash) + mroot + timestamp # candidate header
for nonce in xrange(self.MAX_NONCE):
if stop.is_set():
return False, False, False, False
hash_result = hashlib.sha256(str(c_header)+str(nonce)).hexdigest()
if int(hash_result[0:self.difficulty], 16) == 0:
#print("Mined Block " + b.hash)
return hash_result, nonce, timestamp, tx
return False, nonce, timestamp, tx
def generateNewblock(self, lastBlock, stop=False):
while True:
new_hash, nonce, timestamp, tx = self.POW(lastBlock, stop)
if not nonce:
return None
if new_hash:
return block.Block(lastBlock.index + 1, lastBlock.hash, nonce, new_hash, timestamp, tx)
def rawConsensusInfo(self):
return {'difficulty': self.difficulty, 'type': self.type}