-
Notifications
You must be signed in to change notification settings - Fork 69
/
workmaker.py
executable file
·173 lines (138 loc) · 4.43 KB
/
workmaker.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python3
import config
import logging
logging.basicConfig(level=logging.DEBUG)
def RaiseRedFlags(reason):
logging.getLogger('redflag').critical(reason)
return reason
from bitcoin.node import BitcoinLink
UpstreamBitcoind = BitcoinLink( config.UpstreamBitcoindNode, config.UpstreamNetworkId )
from bitcoin.script import BitcoinScript
from bitcoin.txn import Txn
from base58 import b58decode
from struct import pack
import subprocess
from time import time
CoinbasePrefix = config.CoinbasePrefix if hasattr(config, 'CoinbasePrefix') else b''
def makeCoinbase():
now = int(time())
if now > makeCoinbase.last:
makeCoinbase.last = now
makeCoinbase.extranonce = 0
else:
makeCoinbase.extranonce += 1
return CoinbasePrefix + pack('>L', now) + pack('>Q', makeCoinbase.extranonce).lstrip(b'\0')
makeCoinbase.last = 0
def makeCoinbaseTxn(coinbaseValue):
t = Txn.new()
t.setCoinbase(makeCoinbase())
if hasattr(config, 'CoinbaserCmd'):
coinbased = 0
try:
cmd = config.CoinbaserCmd
cmd = cmd.replace('%d', str(coinbaseValue))
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
nout = int(p.stdout.readline())
for i in range(nout):
amount = int(p.stdout.readline())
addr = p.stdout.readline().rstrip(b'\n').decode('utf8')
pkScript = BitcoinScript.toAddress(addr)
t.addOutput(amount, pkScript)
coinbased += amount
except:
coinbased = coinbaseValue + 1
if coinbased >= coinbaseValue:
logging.getLogger('makeCoinbaseTxn').error('Coinbaser failed!')
t.outputs = []
else:
coinbaseValue -= coinbased
pkScript = BitcoinScript.toAddress(config.TrackerAddr)
t.addOutput(coinbaseValue, pkScript)
t.assemble()
# TODO
# TODO: red flag on dupe coinbase
return t
from util import Bits2Target
workLog = {}
networkTarget = None
def blockChanged():
global MM, networkTarget
networkTarget = Bits2Target(MM.currentBlock[1])
workLog.clear()
from merklemaker import merkleMaker
MM = merkleMaker()
MM.__dict__.update(config.__dict__)
MM.makeCoinbaseTxn = makeCoinbaseTxn
MM.onBlockChange = blockChanged
MM._THISISUGLY = UpstreamBitcoind
MM.start()
from binascii import b2a_hex
from struct import pack, unpack
from time import time
from util import RejectedShare, dblsha, hash2int
def getBlockHeader(username):
MRD = MM.getMRD()
(merkleRoot, merkleTree, coinbaseTxn, prevBlock, bits, rollPrevBlk) = MRD
timestamp = pack('<L', int(time()))
hdr = b'\1\0\0\0' + prevBlock + merkleRoot + timestamp + bits + b'iolE'
workLog.setdefault(username, {})[merkleRoot] = MRD
return hdr
def checkShare(share):
data = share['data']
(prevBlock, bits) = MM.currentBlock
sharePrevBlock = data[4:36]
if sharePrevBlock != prevBlock:
if sharePrevBlock == MM.lastBlock[0]:
raise RejectedShare('stale-prevblk')
raise RejectedShare('bad-prevblk')
shareMerkleRoot = data[36:68]
# TODO: use userid
username = share['username']
if username not in workLog:
raise RejectedShare('unknown-user')
MWL = workLog[username]
if shareMerkleRoot not in MWL:
raise RejectedShare('unknown-work')
MRD = MWL[shareMerkleRoot]
share['MRD'] = MRD
shareTimestamp = unpack('<L', data[68:72])[0]
shareTime = share['time'] = time()
if shareTimestamp < shareTime - 300:
raise RejectedShare('time-too-old')
if shareTimestamp > shareTime + 7200:
raise RejectedShare('time-too-new')
if data[72:76] != bits:
raise RejectedShare('bad-diffbits')
if data[:4] != b'\1\0\0\0':
raise RejectedShare('bad-version')
blkhash = dblsha(data)
if blkhash[28:] != b'\0\0\0\0':
raise RejectedShare('H-not-zero')
blkhashn = hash2int(blkhash)
global networkTarget
logfunc = getattr(checkShare.logger, 'info' if blkhashn <= networkTarget else 'debug')
logfunc('BLKHASH: %64x' % (blkhashn,))
logfunc(' TARGET: %64x' % (networkTarget,))
if blkhashn <= networkTarget:
logfunc("Submitting upstream")
MRD[1].data[0] = MRD[2]
UpstreamBitcoind.submitBlock(data, MRD[1].data)
checkShare.logger = logging.getLogger('checkShare')
def receiveShare(share):
# TODO: username => userid
checkShare(share)
# TODO
def newBlockNotification(signum, frame):
MM.updateMerkleTree()
# TODO: Force RESPOND TO LONGPOLLS?
pass
from signal import signal, SIGUSR1
signal(SIGUSR1, newBlockNotification)
from jsonrpcserver import JSONRPCServer
import interactivemode
if __name__ == "__main__":
server = JSONRPCServer(('', 8444))
server.getBlockHeader = getBlockHeader
server.receiveShare = receiveShare
server.RaiseRedFlags = RaiseRedFlags
server.serve_forever()