forked from p2pool/p2pool.info2
/
run.py
255 lines (212 loc) · 8.65 KB
/
run.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from __future__ import division
import sys
import os
import json
import time
import errno
import base64
import traceback
from twisted.internet import defer, reactor
from twisted.web import client
from p2pool.bitcoin import data as bitcoin_data, networks
from p2pool import data as p2pool_data
from p2pool.util import math as util_math, jsonrpc
net = networks.nets['bitcoin']
def _atomic_read(filename, default):
try:
with open(filename, 'rb') as f:
return f.read()
except IOError, e:
if e.errno != errno.ENOENT:
raise
try:
with open(filename + '.new', 'rb') as f:
return f.read()
except IOError, e:
if e.errno != errno.ENOENT:
raise
return default
def _atomic_write(filename, data):
with open(filename + '.new', 'wb') as f:
f.write(data)
f.flush()
try:
os.fsync(f.fileno())
except:
pass
try:
os.rename(filename + '.new', filename)
except: # XXX windows can't overwrite
os.remove(filename)
os.rename(filename + '.new', filename)
@defer.inlineCallbacks
def get_blocks(b, n, callback):
h = yield b.rpc_getbestblockhash()
for i in xrange(n):
print 'getting block', i, 'out of', n, 'back'
block_data = yield b.rpc_getblock(h, False)
block_data2 = yield b.rpc_getblock(h)
block = bitcoin_data.block_type.unpack(block_data.decode('hex'))
callback(dict(
block=block,
height=block_data2['height'],
gentx_hash=bitcoin_data.hash256(bitcoin_data.tx_type.pack(block['txs'][0])),
))
h = '%064x' % (block['header']['previous_block'],)
def blockchain(cmd):
return client.getPage('http://blockchain.info/' + cmd).addCallback(json.loads)
@defer.inlineCallbacks
def get_blocks2(n):
height = yield blockchain('q/getblockcount')
res = []
for i in xrange(n):
x = yield blockchain('block-height/%i?format=json' % (height - i,))
for block in x['blocks']:
#print block
header = dict(
version=block['ver'],
previous_block=int(block['prev_block'], 16),
merkle_root=int(block['mrkl_root'], 16),
timestamp=block['time'],
bits=bitcoin_data.FloatingInteger(block['bits']),
nonce=block['nonce'],
)
assert bitcoin_data.hash256(bitcoin_data.block_header_type.pack(header)) == int(block['hash'], 16)
# there seems to be no way to get the raw transaction
# from blockchain.info (?format=hex doesn't work for
# coinbase transctions ): so instead fake it
txs = [dict(
version=tx['ver'],
tx_ins=[dict(
previous_output=None,
script='',
sequence=0,
) for tx_in in tx['inputs']],
tx_outs=[dict(
value=tx_out['value'],
script='\x6a' + 'blah'*100 if tx_out['type'] == -1 else
p2pool_data.DONATION_SCRIPT if tx_out['addr'] == bitcoin_data.script2_to_address(p2pool_data.DONATION_SCRIPT, net) else
bitcoin_data.pubkey_hash_to_script2(bitcoin_data.address_to_pubkey_hash(tx_out['addr'], net)),
) for tx_out in tx['out']],
lock_time=0,
) for tx in block['tx']]
#print txs[0]
# fails because we don't have coinbase script ):
#assert bitcoin_data.hash256(bitcoin_data.tx_type.pack(txs[0])) == block['tx'][0]['hash']
block2 = dict(header=header, txs=txs)
res.append(dict(
block=block2,
height=block['height'],
gentx_hash=int(block['tx'][0]['hash'], 16),
))
defer.returnValue(res)
@defer.inlineCallbacks
def main():
datadir = sys.argv[1]
b = jsonrpc.HTTPProxy(sys.argv[2], dict(
Authorization='Basic ' + base64.b64encode(
sys.argv[3] + ':' + sys.argv[4]
),
), timeout=30)
p2pool_base_urls = sys.argv[5:]
@defer.inlineCallbacks
def get(blah):
for p2pool_base_url in util_math.shuffled(p2pool_base_urls):
url = p2pool_base_url.rstrip('/') + '/' + blah
print 'trying', url
try:
d = yield client.getPage(url)
except Exception:
traceback.print_exc()
else:
defer.returnValue(json.loads(d))
raise ValueError('no good p2pool servers')
# read old
old_blocks = json.loads(_atomic_read(
os.path.join(datadir, 'blocks'),
'[]',
))
old_stats = json.loads(_atomic_read(
os.path.join(datadir, 'stats'),
'{"rates": [], "maxRate": 0, "users": [], "maxUsers": 0}',
))
# update
#print stats
web_local_stats = yield get('local_stats')
web_global_stats = yield get('global_stats')
web_users = yield get('users')
web_current_payouts = yield get('current_payouts')
difficulty = bitcoin_data.target_to_difficulty(
bitcoin_data.average_attempts_to_target(web_local_stats['attempts_to_block']))
users = [dict(
Hashrate=util_math.format(int(frac * web_global_stats['pool_hash_rate'] + 1/2), add_space=True) + 'H/s',
Address=addr,
) for addr, frac in sorted(web_users.iteritems(), key=lambda (k, v): -v)]
payouts = [dict(
Address=addr,
Payment=amt,
) for addr, amt in sorted(web_current_payouts.iteritems(), key=lambda (k, v): -v)]
def update_timeseries(x, value, now_time):
lastlast_time = x[-2][0]
last_time = x[-1][0]
next_time = last_time + (last_time - lastlast_time)
if abs(now_time - last_time) < abs(now_time - next_time):
# update last
old_value = x[-1][1]
old_weight = x[-1][2] if len(x[-1]) >= 3 else 1e9
return x[:-1] + [
[last_time, (old_value * old_weight + value)/(old_weight + 1), old_weight + 1]
]
else:
# start next
return x + [
[next_time, value, 1]
]
stats = dict(
rates=update_timeseries(old_stats['rates'], web_global_stats['pool_hash_rate']/1e9, time.time()*1e3),
maxRate=max(old_stats['maxRate'], web_global_stats['pool_hash_rate']/1e9),
users=update_timeseries(old_stats['users'], len(web_users), time.time()*1e3),
maxUsers=max(old_stats['maxUsers'], len(web_users)),
)
blocks = list(old_blocks)
blocks_dict = dict((block['Id'], block) for block in blocks)
assert len(blocks_dict) == len(blocks)
def handle_block(block_data):
block = block_data['block']
txouts = block['txs'][0]['tx_outs']
if len(txouts) < 25: return
if not txouts[-1]['script'].startswith('\x6a'): return
if len(txouts[-1]['script']) < 33: return
if txouts[-1]['value'] != 0: return
if txouts[-2]['script'] != p2pool_data.DONATION_SCRIPT: return
hash_str = '%064x' % bitcoin_data.hash256(bitcoin_data.block_header_type.pack(block['header']))
print hash_str
if hash_str not in blocks_dict:
print 'inserted'
x = dict(
Id=hash_str,
PrevBlock='%064x' % block['header']['previous_block'],
GenerationTxHash='%064x' % block_data['gentx_hash'],
BlockHeight=block_data['height'],
Difficulty=bitcoin_data.target_to_difficulty(block['header']['bits'].target),
Timestamp=block['header']['timestamp'],
IsOrphaned=None, # XXX
)
blocks.append(x)
blocks_dict[hash_str] = x
yield get_blocks(b, 400, handle_block)
blocks.sort(key=lambda x: -x['Timestamp'])
# write
_atomic_write(os.path.join(datadir, 'blocks_5'), json.dumps(blocks[:5]))
_atomic_write(os.path.join(datadir, 'blocks_100'), json.dumps(blocks[:100]))
_atomic_write(os.path.join(datadir, 'blocks'), json.dumps(blocks))
_atomic_write(os.path.join(datadir, 'difficulty'), json.dumps(difficulty))
#_atomic_write(os.path.join(datadir, 'donations'), json.dumps(donations))
_atomic_write(os.path.join(datadir, 'payouts'), json.dumps(payouts))
_atomic_write(os.path.join(datadir, 'stats'), json.dumps(stats))
_atomic_write(os.path.join(datadir, 'users'), json.dumps(users))
def f(x):
reactor.stop()
return x
reactor.callWhenRunning(lambda: main().addBoth(f))
reactor.run()