-
Notifications
You must be signed in to change notification settings - Fork 0
/
mine.py
executable file
·50 lines (42 loc) · 1.35 KB
/
mine.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
#!/usr/bin/env python3
import utils
import codecs
import os
import sys
##############
difficulty = 4
##############
CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'
def mineNewBlock(prevBlock = utils.getLastBlock(), debug = False):
while True:
nonce = utils.setNonce()
timestamp = utils.setTimestamp()
prevBlock = codecs.decode(prevBlock, 'hex')
prevBlock = codecs.encode(prevBlock, 'hex')
block = (
utils.setVersion() +
prevBlock +
utils.setMerkleRoot() +
timestamp +
utils.setBits() +
nonce
)
nonce = utils.setNonce()
block = utils.getHash(block)
print('Trying nonce: ' + str(nonce))
sys.stdout.write(CURSOR_UP_ONE)
sys.stdout.write(ERASE_LINE)
if debug == True:
print('BLOCK:', block)
if block[:difficulty] == '0' * difficulty and block[difficulty:difficulty+1] != '0':
if utils.getTx() != '':
print('BLOCK FOUND:\n' + block + '\ntx: ' + '\'' + utils.getTx() + '\'')
else:
print('BLOCK FOUND:\n' + block + '\ntx: \'\'')
utils.createBlock(block)
utils.writeToBlock(block, utils.setVersion(), utils.getLittleEndian(prevBlock), utils.setMerkleRoot(), timestamp, utils.setBits(), nonce)
utils.writeTx(block)
utils.createNewTxFile()
return mineNewBlock(block)
mineNewBlock()