remove data.OkayTracker.add
[p2pool.git] / p2pool / data.py
1 from __future__ import division
2
3 import itertools
4 import random
5 import time
6 import os
7
8 from twisted.python import log
9
10 import p2pool
11 from p2pool import skiplists
12 from p2pool.bitcoin import data as bitcoin_data, script
13 from p2pool.util import memoize, expiring_dict, math, forest
14
15
16 share_data_type = bitcoin_data.ComposedType([
17     ('previous_share_hash', bitcoin_data.PossiblyNoneType(0, bitcoin_data.HashType())),
18     ('coinbase', bitcoin_data.VarStrType()),
19     ('nonce', bitcoin_data.VarStrType()),
20     ('new_script', bitcoin_data.VarStrType()),
21     ('subsidy', bitcoin_data.StructType('<Q')),
22     ('donation', bitcoin_data.StructType('<H')),
23     ('stale_frac', bitcoin_data.StructType('<B')),
24 ])
25
26 share_info_type = bitcoin_data.ComposedType([
27     ('share_data', share_data_type),
28     ('target', bitcoin_data.FloatingIntegerType()),
29     ('timestamp', bitcoin_data.StructType('<I')),
30 ])
31
32 share1a_type = bitcoin_data.ComposedType([
33     ('header', bitcoin_data.block_header_type),
34     ('share_info', share_info_type),
35     ('merkle_branch', bitcoin_data.merkle_branch_type),
36 ])
37
38 share1b_type = bitcoin_data.ComposedType([
39     ('header', bitcoin_data.block_header_type),
40     ('share_info', share_info_type),
41     ('other_txs', bitcoin_data.ListType(bitcoin_data.tx_type)),
42 ])
43
44 # type:
45 # 0: share1a
46 # 1: share1b
47
48 share_type = bitcoin_data.ComposedType([
49     ('type', bitcoin_data.VarIntType()),
50     ('contents', bitcoin_data.VarStrType()),
51 ])
52
53 class Share(object):
54     __slots__ = 'header previous_block share_info merkle_branch other_txs timestamp share_data new_script subsidy previous_hash previous_share_hash target nonce pow_hash header_hash hash time_seen peer donation stale_frac'.split(' ')
55     
56     @classmethod
57     def from_share(cls, share, net):
58         if share['type'] == 0:
59             res = cls.from_share1a(share1a_type.unpack(share['contents']), net)
60             if not (res.pow_hash > res.header['target']):
61                 raise ValueError('invalid share type')
62             return res
63         elif share['type'] == 1:
64             res = cls.from_share1b(share1b_type.unpack(share['contents']), net)
65             if not (res.pow_hash <= res.header['target']):
66                 raise ValueError('invalid share type')
67             return res
68         else:
69             raise ValueError('unknown share type: %r' % (share['type'],))
70     
71     @classmethod
72     def from_share1a(cls, share1a, net):
73         return cls(net, **share1a)
74     
75     @classmethod
76     def from_share1b(cls, share1b, net):
77         return cls(net, **share1b)
78     
79     def __init__(self, net, header, share_info, merkle_branch=None, other_txs=None):
80         if merkle_branch is None and other_txs is None:
81             raise ValueError('need either merkle_branch or other_txs')
82         if other_txs is not None:
83             new_merkle_branch = bitcoin_data.calculate_merkle_branch([dict(version=0, tx_ins=[], tx_outs=[], lock_time=0)] + other_txs, 0)
84             if merkle_branch is not None:
85                 if merke_branch != new_merkle_branch:
86                     raise ValueError('invalid merkle_branch and other_txs')
87             merkle_branch = new_merkle_branch
88         
89         if len(merkle_branch) > 16:
90             raise ValueError('merkle_branch too long!')
91         
92         self.header = header
93         self.previous_block = header['previous_block']
94         self.share_info = share_info
95         self.merkle_branch = merkle_branch
96         self.other_txs = other_txs
97         
98         self.share_data = self.share_info['share_data']
99         self.target = self.share_info['target']
100         self.timestamp = self.share_info['timestamp']
101         
102         self.new_script = self.share_data['new_script']
103         self.subsidy = self.share_data['subsidy']
104         self.donation = self.share_data['donation']
105         
106         if len(self.new_script) > 100:
107             raise ValueError('new_script too long!')
108         
109         self.previous_hash = self.previous_share_hash = self.share_data['previous_share_hash']
110         self.nonce = self.share_data['nonce']
111         
112         if len(self.nonce) > 100:
113             raise ValueError('nonce too long!')
114         
115         if len(self.share_data['coinbase']) > 100:
116             raise ValueError('''coinbase too large! %i bytes''' % (len(self.share_data['coinbase']),))
117         
118         self.pow_hash = net.BITCOIN_POW_FUNC(header)
119         self.header_hash = bitcoin_data.block_header_type.hash256(header)
120         
121         self.hash = share1a_type.hash256(self.as_share1a())
122         
123         if self.pow_hash > self.target:
124             print 'hash %x' % self.pow_hash
125             print 'targ %x' % self.target
126             raise ValueError('not enough work!')
127         
128         if script.get_sigop_count(self.new_script) > 1:
129             raise ValueError('too many sigops!')
130         
131         self.stale_frac = self.share_data['stale_frac']/254 if self.share_data['stale_frac'] != 255 else None
132         
133         # XXX eww
134         self.time_seen = time.time()
135         self.peer = None
136     
137     def __repr__(self):
138         return '<Share %s>' % (' '.join('%s=%r' % (k, getattr(self, k)) for k in self.__slots__),)
139     
140     def check(self, tracker, now, net):
141         share_info, gentx = generate_transaction(tracker, self.share_info['share_data'], self.header['target'], self.share_info['timestamp'], net)
142         if share_info != self.share_info:
143             raise ValueError('share difficulty invalid')
144         
145         if bitcoin_data.check_merkle_branch(gentx, 0, self.merkle_branch) != self.header['merkle_root']:
146             raise ValueError('''gentx doesn't match header via merkle_branch''')
147     
148     def as_share(self):
149         if self.pow_hash > self.header['target']: # share1a
150             return dict(type=0, contents=share1a_type.pack(self.as_share1a()))
151         elif self.pow_hash <= self.header['target']: # share1b
152             return dict(type=1, contents=share1b_type.pack(self.as_share1b()))
153         else:
154             raise AssertionError()
155     
156     def as_share1a(self):
157         return dict(header=self.header, share_info=self.share_info, merkle_branch=self.merkle_branch)
158     
159     def as_share1b(self):
160         if self.other_txs is None:
161             raise ValueError('share does not contain all txs')
162         
163         return dict(header=self.header, share_info=self.share_info, other_txs=self.other_txs)
164     
165     def as_block(self, tracker, net):
166         if self.other_txs is None:
167             raise ValueError('share does not contain all txs')
168         
169         share_info, gentx = generate_transaction(tracker, self.share_info['share_data'], self.header['target'], self.share_info['timestamp'], net)
170         assert share_info == self.share_info
171         
172         return dict(header=self.header, txs=[gentx] + self.other_txs)
173
174 def get_pool_attempts_per_second(tracker, previous_share_hash, dist):
175     near = tracker.shares[previous_share_hash]
176     far = tracker.shares[tracker.get_nth_parent_hash(previous_share_hash, dist - 1)]
177     attempts = tracker.get_work(near.hash) - tracker.get_work(far.hash)
178     time = near.timestamp - far.timestamp
179     if time == 0:
180         time = 1
181     return attempts//time
182
183 def generate_transaction(tracker, share_data, block_target, desired_timestamp, net):
184     previous_share_hash = share_data['previous_share_hash']
185     new_script = share_data['new_script']
186     subsidy = share_data['subsidy']
187     donation = share_data['donation']
188     assert 0 <= donation <= 65535
189     
190     if len(share_data['coinbase']) > 100:
191         raise ValueError('coinbase too long!')
192     
193     previous_share = tracker.shares[previous_share_hash] if previous_share_hash is not None else None
194     
195     height, last = tracker.get_height_and_last(previous_share_hash)
196     assert height >= net.CHAIN_LENGTH or last is None
197     if height < net.TARGET_LOOKBEHIND:
198         target = bitcoin_data.FloatingInteger.from_target_upper_bound(net.MAX_TARGET)
199     else:
200         attempts_per_second = get_pool_attempts_per_second(tracker, previous_share_hash, net.TARGET_LOOKBEHIND)
201         pre_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1
202         pre_target2 = math.clip(pre_target, (previous_share.target*9//10, previous_share.target*11//10))
203         pre_target3 = math.clip(pre_target2, (0, net.MAX_TARGET))
204         target = bitcoin_data.FloatingInteger.from_target_upper_bound(pre_target3)
205     
206     attempts_to_block = bitcoin_data.target_to_average_attempts(block_target)
207     max_att = net.SPREAD * attempts_to_block
208     
209     this_att = min(bitcoin_data.target_to_average_attempts(target), max_att)
210     chain_length = getattr(net, 'REAL_CHAIN_LENGTH_FUNC', lambda _: net.REAL_CHAIN_LENGTH)(previous_share.timestamp if previous_share is not None else None)
211     other_weights, other_total_weight, other_donation_weight = tracker.get_cumulative_weights(previous_share_hash, min(height, chain_length), 65535*max(0, max_att - this_att))
212     assert other_total_weight == sum(other_weights.itervalues()) + other_donation_weight, (other_total_weight, sum(other_weights.itervalues()) + other_donation_weight)
213     weights, total_weight, donation_weight = math.add_dicts([{new_script: this_att*(65535-donation)}, other_weights]), this_att*65535 + other_total_weight, this_att*donation + other_donation_weight
214     assert total_weight == sum(weights.itervalues()) + donation_weight, (total_weight, sum(weights.itervalues()) + donation_weight)
215     
216     SCRIPT = '4104ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664bac'.decode('hex')
217     
218     # 1 satoshi is always donated so that a list of p2pool generated blocks can be easily found by looking at the donation address
219     amounts = dict((script, (subsidy-1)*(199*weight)//(200*total_weight)) for (script, weight) in weights.iteritems())
220     amounts[new_script] = amounts.get(new_script, 0) + (subsidy-1)//200
221     amounts[SCRIPT] = amounts.get(SCRIPT, 0) + (subsidy-1)*(199*donation_weight)//(200*total_weight)
222     amounts[SCRIPT] = amounts.get(SCRIPT, 0) + subsidy - sum(amounts.itervalues()) # collect any extra satoshis :P
223     
224     if sum(amounts.itervalues()) != subsidy:
225         raise ValueError()
226     if any(x < 0 for x in amounts.itervalues()):
227         raise ValueError()
228     
229     dests = sorted(amounts.iterkeys(), key=lambda script: (amounts[script], script))
230     dests = dests[-4000:] # block length limit, unlikely to ever be hit
231     
232     share_info = dict(
233         share_data=share_data,
234         target=target,
235         timestamp=math.clip(desired_timestamp, (previous_share.timestamp - 60, previous_share.timestamp + 60)) if previous_share is not None else desired_timestamp,
236     )
237     
238     return share_info, dict(
239         version=1,
240         tx_ins=[dict(
241             previous_output=None,
242             sequence=None,
243             script=share_data['coinbase'].ljust(2, '\x00'),
244         )],
245         tx_outs=[dict(value=0, script='\x20' + bitcoin_data.HashType().pack(share_info_type.hash256(share_info)))] + [dict(value=amounts[script], script=script) for script in dests if amounts[script]],
246         lock_time=0,
247     )
248
249
250 class OkayTracker(forest.Tracker):
251     def __init__(self, net):
252         forest.Tracker.__init__(self)
253         self.net = net
254         self.verified = forest.Tracker()
255         self.verified.get_nth_parent_hash = self.get_nth_parent_hash # self is a superset of self.verified
256         
257         self.get_cumulative_weights = skiplists.WeightsSkipList(self)
258     
259     def attempt_verify(self, share, now):
260         if share.hash in self.verified.shares:
261             return True
262         height, last = self.get_height_and_last(share.hash)
263         if height < self.net.CHAIN_LENGTH + 1 and last is not None:
264             raise AssertionError()
265         try:
266             share.check(self, now, self.net)
267         except:
268             log.err(None, 'Share check failed:')
269             return False
270         else:
271             self.verified.add(share)
272             return True
273     
274     def think(self, ht, previous_block, now):
275         desired = set()
276         
277         # O(len(self.heads))
278         #   make 'unverified heads' set?
279         # for each overall head, attempt verification
280         # if it fails, attempt on parent, and repeat
281         # if no successful verification because of lack of parents, request parent
282         bads = set()
283         for head in set(self.heads) - set(self.verified.heads):
284             head_height, last = self.get_height_and_last(head)
285             
286             for share in itertools.islice(self.get_chain_known(head), None if last is None else min(5, max(0, head_height - self.net.CHAIN_LENGTH))):
287                 if self.attempt_verify(share, now):
288                     break
289                 if share.hash in self.heads:
290                     bads.add(share.hash)
291             else:
292                 if last is not None:
293                     desired.add((self.shares[random.choice(list(self.reverse_shares[last]))].peer, last))
294         for bad in bads:
295             assert bad not in self.verified.shares
296             assert bad in self.heads
297             if p2pool.DEBUG:
298                 print "BAD", bad
299             self.remove(bad)
300         
301         # try to get at least CHAIN_LENGTH height for each verified head, requesting parents if needed
302         for head in list(self.verified.heads):
303             head_height, last_hash = self.verified.get_height_and_last(head)
304             last_height, last_last_hash = self.get_height_and_last(last_hash)
305             # XXX review boundary conditions
306             want = max(self.net.CHAIN_LENGTH - head_height, 0)
307             can = max(last_height - 1 - self.net.CHAIN_LENGTH, 0) if last_last_hash is not None else last_height
308             get = min(want, can)
309             #print 'Z', head_height, last_hash is None, last_height, last_last_hash is None, want, can, get
310             for share in itertools.islice(self.get_chain_known(last_hash), get):
311                 if not self.attempt_verify(share, now):
312                     break
313             if head_height < self.net.CHAIN_LENGTH and last_last_hash is not None:
314                 desired.add((self.verified.shares[random.choice(list(self.verified.reverse_shares[last_hash]))].peer, last_last_hash))
315         
316         # decide best tree
317         best_tail = max(self.verified.tails, key=lambda h: self.score(max(self.verified.tails[h], key=self.verified.get_height), ht)) if self.verified.tails else None
318         # decide best verified head
319         scores = sorted(self.verified.tails.get(best_tail, []), key=lambda h: (
320             self.verified.get_work(self.verified.get_nth_parent_hash(h, min(5, self.verified.get_height(h)))),
321             #self.verified.shares[h].peer is None,
322             ht.get_min_height(self.verified.shares[h].previous_block),
323             -self.verified.shares[h].time_seen
324         ))
325         
326         
327         if p2pool.DEBUG:
328             print len(self.verified.tails), "chain tails and", len(self.verified.tails.get(best_tail, [])), 'chain heads. Top 10 heads:'
329             if len(scores) > 10:
330                 print '    ...'
331             for h in scores[-10:]:
332                 print '   ', format_hash(h), format_hash(self.verified.shares[h].previous_hash), (
333                     self.verified.get_work(self.verified.get_nth_parent_hash(h, min(5, self.verified.get_height(h)))),
334                     self.verified.shares[h].peer is None,
335                     ht.get_min_height(self.verified.shares[h].previous_block),
336                     -self.verified.shares[h].time_seen
337                 )
338         
339         # eat away at heads
340         if scores:
341             for i in xrange(1000):
342                 to_remove = set()
343                 for share_hash, tail in self.heads.iteritems():
344                     if share_hash in scores[-5:]:
345                         #print 1
346                         continue
347                     if self.shares[share_hash].time_seen > time.time() - 300:
348                         #print 2
349                         continue
350                     if share_hash not in self.verified.shares and max(self.shares[after_tail_hash].time_seen for after_tail_hash in self.reverse_shares.get(tail)) > time.time() - 120: # XXX stupid
351                         #print 3
352                         continue
353                     to_remove.add(share_hash)
354                 if not to_remove:
355                     break
356                 for share_hash in to_remove:
357                     self.remove(share_hash)
358                     if share_hash in self.verified.shares:
359                         self.verified.remove(share_hash)
360                 #print "_________", to_remove
361         
362         # drop tails
363         for i in xrange(1000):
364             to_remove = set()
365             for tail, heads in self.tails.iteritems():
366                 if min(self.get_height(head) for head in heads) < 2*self.net.CHAIN_LENGTH + 10:
367                     continue
368                 for aftertail in self.reverse_shares.get(tail, set()):
369                     if len(self.reverse_shares[self.shares[aftertail].previous_hash]) > 1: # XXX
370                         print "raw"
371                         continue
372                     to_remove.add(aftertail)
373             if not to_remove:
374                 break
375             # if removed from this, it must be removed from verified
376             #start = time.time()
377             for aftertail in to_remove:
378                 if self.shares[aftertail].previous_hash not in self.tails:
379                     print "erk", aftertail, self.shares[aftertail].previous_hash
380                     continue
381                 self.remove(aftertail)
382                 if aftertail in self.verified.shares:
383                     self.verified.remove(aftertail)
384             #end = time.time()
385             #print "removed! %i %f" % (len(to_remove), (end - start)/len(to_remove))
386         
387         best = scores[-1] if scores else None
388         
389         if best is not None:
390             best_share = self.verified.shares[best]
391             if ht.get_min_height(best_share.header['previous_block']) < ht.get_min_height(previous_block) and best_share.header_hash != previous_block and best_share.peer is not None:
392                 if p2pool.DEBUG:
393                     print 'Stale detected! %x < %x' % (best_share.header['previous_block'], previous_block)
394                 best = best_share.previous_hash
395         
396         return best, desired
397     
398     @memoize.memoize_with_backing(expiring_dict.ExpiringDict(5, get_touches=False))
399     def score(self, share_hash, ht):
400         head_height, last = self.verified.get_height_and_last(share_hash)
401         score2 = 0
402         attempts = 0
403         max_height = 0
404         share2_hash = self.verified.get_nth_parent_hash(share_hash, min(self.net.CHAIN_LENGTH//2, head_height//2)) if last is not None else share_hash
405         for share in reversed(list(itertools.islice(self.verified.get_chain_known(share2_hash), self.net.CHAIN_LENGTH))):
406             max_height = max(max_height, ht.get_min_height(share.header['previous_block']))
407             attempts += bitcoin_data.target_to_average_attempts(share.target)
408             this_score = attempts//(ht.get_highest_height() - max_height + 1)
409             if this_score > score2:
410                 score2 = this_score
411         return min(head_height, self.net.CHAIN_LENGTH), score2
412
413 def format_hash(x):
414     if x is None:
415         return 'xxxxxxxx'
416     return '%08x' % (x % 2**32)
417
418 class ShareStore(object):
419     def __init__(self, prefix, net):
420         self.filename = prefix
421         self.dirname = os.path.dirname(os.path.abspath(prefix))
422         self.filename = os.path.basename(os.path.abspath(prefix))
423         self.net = net
424         self.known = None # will be filename -> set of share hashes, set of verified hashes
425     
426     def get_shares(self):
427         if self.known is not None:
428             raise AssertionError()
429         known = {}
430         filenames, next = self.get_filenames_and_next()
431         for filename in filenames:
432             share_hashes, verified_hashes = known.setdefault(filename, (set(), set()))
433             with open(filename, 'rb') as f:
434                 for line in f:
435                     try:
436                         type_id_str, data_hex = line.strip().split(' ')
437                         type_id = int(type_id_str)
438                         if type_id == 0:
439                             pass
440                         elif type_id == 1:
441                             pass
442                         elif type_id == 2:
443                             verified_hash = int(data_hex, 16)
444                             yield 'verified_hash', verified_hash
445                             verified_hashes.add(verified_hash)
446                         elif type_id == 5:
447                             share = Share.from_share(share_type.unpack(data_hex.decode('hex')), self.net)
448                             yield 'share', share
449                             share_hashes.add(share.hash)
450                         else:
451                             raise NotImplementedError("share type %i" % (type_id,))
452                     except Exception:
453                         log.err(None, "Error while reading saved shares, continuing where left off:")
454         self.known = known
455     
456     def _add_line(self, line):
457         filenames, next = self.get_filenames_and_next()
458         if filenames and os.path.getsize(filenames[-1]) < 10e6:
459             filename = filenames[-1]
460         else:
461             filename = next
462         
463         with open(filename, 'ab') as f:
464             f.write(line + '\n')
465         
466         return filename
467     
468     def add_share(self, share):
469         type_id, data = 5, share_type.pack(share.as_share())
470         filename = self._add_line("%i %s" % (type_id, data.encode('hex')))
471         share_hashes, verified_hashes = self.known.setdefault(filename, (set(), set()))
472         share_hashes.add(share.hash)
473     
474     def add_verified_hash(self, share_hash):
475         filename = self._add_line("%i %x" % (2, share_hash))
476         share_hashes, verified_hashes = self.known.setdefault(filename, (set(), set()))
477         verified_hashes.add(share_hash)
478     
479     def get_filenames_and_next(self):
480         suffixes = sorted(int(x[len(self.filename):]) for x in os.listdir(self.dirname) if x.startswith(self.filename) and x[len(self.filename):].isdigit())
481         return [os.path.join(self.dirname, self.filename + str(suffix)) for suffix in suffixes], os.path.join(self.dirname, self.filename + (str(suffixes[-1] + 1) if suffixes else str(0)))
482     
483     def forget_share(self, share_hash):
484         for filename, (share_hashes, verified_hashes) in self.known.iteritems():
485             if share_hash in share_hashes:
486                 share_hashes.remove(share_hash)
487         self.check_remove()
488     
489     def forget_verified_share(self, share_hash):
490         for filename, (share_hashes, verified_hashes) in self.known.iteritems():
491             if share_hash in verified_hashes:
492                 verified_hashes.remove(share_hash)
493         self.check_remove()
494     
495     def check_remove(self):
496         to_remove = set()
497         for filename, (share_hashes, verified_hashes) in self.known.iteritems():
498             #print filename, len(share_hashes) + len(verified_hashes)
499             if not share_hashes and not verified_hashes:
500                 to_remove.add(filename)
501         for filename in to_remove:
502             self.known.pop(filename)
503             os.remove(filename)
504             print "REMOVED", filename