fixes for edge cases for share chains with near-0 difficulties
[p2pool.git] / p2pool / data.py
1 from __future__ import division
2
3 import hashlib
4 import os
5 import random
6 import time
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, sha256
13 from p2pool.util import math, forest, pack
14
15 # hashlink
16
17 hash_link_type = pack.ComposedType([
18     ('state', pack.FixedStrType(32)),
19     ('extra_data', pack.FixedStrType(0)), # bit of a hack, but since the donation script is at the end, const_ending is long enough to always make this empty
20     ('length', pack.VarIntType()),
21 ])
22
23 def prefix_to_hash_link(prefix, const_ending=''):
24     assert prefix.endswith(const_ending), (prefix, const_ending)
25     x = sha256.sha256(prefix)
26     return dict(state=x.state, extra_data=x.buf[:max(0, len(x.buf)-len(const_ending))], length=x.length//8)
27
28 def check_hash_link(hash_link, data, const_ending=''):
29     extra_length = hash_link['length'] % (512//8)
30     assert len(hash_link['extra_data']) == max(0, extra_length - len(const_ending))
31     extra = (hash_link['extra_data'] + const_ending)[len(hash_link['extra_data']) + len(const_ending) - extra_length:]
32     assert len(extra) == extra_length
33     return pack.IntType(256).unpack(hashlib.sha256(sha256.sha256(data, (hash_link['state'], extra, 8*hash_link['length'])).digest()).digest())
34
35 # shares
36
37 small_block_header_type = pack.ComposedType([
38     ('version', pack.VarIntType()), # XXX must be constrained to 32 bits
39     ('previous_block', pack.PossiblyNoneType(0, pack.IntType(256))),
40     ('timestamp', pack.IntType(32)),
41     ('bits', bitcoin_data.FloatingIntegerType()),
42     ('nonce', pack.IntType(32)),
43 ])
44
45 share_data_type = pack.ComposedType([
46     ('previous_share_hash', pack.PossiblyNoneType(0, pack.IntType(256))),
47     ('coinbase', pack.VarStrType()),
48     ('nonce', pack.IntType(32)),
49     ('pubkey_hash', pack.IntType(160)),
50     ('subsidy', pack.IntType(64)),
51     ('donation', pack.IntType(16)),
52     ('stale_info', pack.IntType(8)), # 0 nothing, 253 orphan, 254 doa
53 ])
54
55 share_info_type = pack.ComposedType([
56     ('share_data', share_data_type),
57     ('max_bits', bitcoin_data.FloatingIntegerType()),
58     ('bits', bitcoin_data.FloatingIntegerType()),
59     ('timestamp', pack.IntType(32)),
60 ])
61
62 share1a_type = pack.ComposedType([
63     ('min_header', small_block_header_type),
64     ('share_info', share_info_type),
65     ('hash_link', hash_link_type),
66     ('merkle_branch', bitcoin_data.merkle_branch_type),
67 ])
68
69 share1b_type = pack.ComposedType([
70     ('min_header', small_block_header_type),
71     ('share_info', share_info_type),
72     ('hash_link', hash_link_type),
73     ('other_txs', pack.ListType(bitcoin_data.tx_type)),
74 ])
75
76
77 # type:
78 # 2: share1a
79 # 3: share1b
80
81 share_type = pack.ComposedType([
82     ('type', pack.VarIntType()),
83     ('contents', pack.VarStrType()),
84 ])
85
86
87 def get_pool_attempts_per_second(tracker, previous_share_hash, dist, min_work=False, integer=False):
88     assert dist >= 2
89     near = tracker.shares[previous_share_hash]
90     far = tracker.shares[tracker.get_nth_parent_hash(previous_share_hash, dist - 1)]
91     attempts = tracker.get_work(near.hash) - tracker.get_work(far.hash) if not min_work else tracker.get_delta(near.hash).min_work - tracker.get_delta(far.hash).min_work
92     time = near.timestamp - far.timestamp
93     if time <= 0:
94         time = 1
95     if integer:
96         return attempts//time
97     return attempts/time
98
99 def get_average_stale_prop(tracker, share_hash, lookbehind):
100     stales = sum(1 for share in tracker.get_chain(share_hash, lookbehind) if share.share_data['stale_info'] in [253, 254])
101     return stales/(lookbehind + stales)
102
103 DONATION_SCRIPT = '4104ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664bac'.decode('hex')
104
105 ref_type = pack.ComposedType([
106     ('identifier', pack.FixedStrType(64//8)),
107     ('share_info', share_info_type),
108 ])
109
110 gentx_before_refhash = pack.VarStrType().pack(DONATION_SCRIPT) + pack.IntType(64).pack(0) + pack.VarStrType().pack('\x20' + pack.IntType(256).pack(0))[:2]
111
112 def generate_transaction(tracker, share_data, block_target, desired_timestamp, desired_target, net):
113     previous_share = tracker.shares[share_data['previous_share_hash']] if share_data['previous_share_hash'] is not None else None
114     
115     height, last = tracker.get_height_and_last(share_data['previous_share_hash'])
116     assert height >= net.REAL_CHAIN_LENGTH or last is None
117     if height < net.TARGET_LOOKBEHIND:
118         pre_target3 = net.MAX_TARGET
119     else:
120         attempts_per_second = get_pool_attempts_per_second(tracker, share_data['previous_share_hash'], net.TARGET_LOOKBEHIND, min_work=True, integer=True)
121         pre_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1 if attempts_per_second else 2**256-1
122         pre_target2 = math.clip(pre_target, (previous_share.max_target*9//10, previous_share.max_target*11//10))
123         pre_target3 = math.clip(pre_target2, (0, net.MAX_TARGET))
124     max_bits = bitcoin_data.FloatingInteger.from_target_upper_bound(pre_target3)
125     bits = bitcoin_data.FloatingInteger.from_target_upper_bound(math.clip(desired_target, (pre_target3//10, pre_target3)))
126     
127     weights, total_weight, donation_weight = tracker.get_cumulative_weights(share_data['previous_share_hash'],
128         min(height, net.REAL_CHAIN_LENGTH),
129         65535*net.SPREAD*bitcoin_data.target_to_average_attempts(block_target),
130     )
131     assert total_weight == sum(weights.itervalues()) + donation_weight, (total_weight, sum(weights.itervalues()) + donation_weight)
132     
133     amounts = dict((script, share_data['subsidy']*(199*weight)//(200*total_weight)) for script, weight in weights.iteritems()) # 99.5% goes according to weights prior to this share
134     this_script = bitcoin_data.pubkey_hash_to_script2(share_data['pubkey_hash'])
135     amounts[this_script] = amounts.get(this_script, 0) + share_data['subsidy']//200 # 0.5% goes to block finder
136     amounts[DONATION_SCRIPT] = amounts.get(DONATION_SCRIPT, 0) + share_data['subsidy'] - sum(amounts.itervalues()) # all that's left over is the donation weight and some extra satoshis due to rounding
137     
138     if sum(amounts.itervalues()) != share_data['subsidy'] or any(x < 0 for x in amounts.itervalues()):
139         raise ValueError()
140     
141     dests = sorted(amounts.iterkeys(), key=lambda script: (script == DONATION_SCRIPT, amounts[script], script))[-4000:] # block length limit, unlikely to ever be hit
142     
143     share_info = dict(
144         share_data=share_data,
145         max_bits=max_bits,
146         bits=bits,
147         timestamp=math.clip(desired_timestamp, (
148             (previous_share.timestamp + net.SHARE_PERIOD) - (net.SHARE_PERIOD - 1), # = previous_share.timestamp + 1
149             (previous_share.timestamp + net.SHARE_PERIOD) + (net.SHARE_PERIOD - 1),
150         )) if previous_share is not None else desired_timestamp,
151     )
152     
153     return share_info, dict(
154         version=1,
155         tx_ins=[dict(
156             previous_output=None,
157             sequence=None,
158             script=share_data['coinbase'].ljust(2, '\x00'),
159         )],
160         tx_outs=[dict(value=amounts[script], script=script) for script in dests if amounts[script]] + [dict(
161             value=0,
162             script='\x20' + pack.IntType(256).pack(bitcoin_data.hash256(ref_type.pack(dict(
163                 identifier=net.IDENTIFIER,
164                 share_info=share_info,
165             )))),
166         )],
167         lock_time=0,
168     )
169
170 def get_expected_payouts(tracker, best_share_hash, block_target, subsidy, net):
171     weights, total_weight, donation_weight = tracker.get_cumulative_weights(best_share_hash, min(tracker.get_height(best_share_hash), net.REAL_CHAIN_LENGTH), 65535*net.SPREAD*bitcoin_data.target_to_average_attempts(block_target))
172     res = dict((script, subsidy*weight//total_weight) for script, weight in weights.iteritems())
173     res[DONATION_SCRIPT] = res.get(DONATION_SCRIPT, 0) + subsidy - sum(res.itervalues())
174     return res
175
176 class Share(object):
177     __slots__ = 'net min_header share_info hash_link merkle_branch other_txs hash share_data max_target target timestamp previous_hash new_script gentx_hash header pow_hash header_hash time_seen peer'.split(' ')
178     
179     @classmethod
180     def from_share(cls, share, net, peer):
181         if share['type'] == 2:
182             return cls(net, peer, other_txs=None, **share1a_type.unpack(share['contents']))
183         elif share['type'] == 3:
184             share1b = share1b_type.unpack(share['contents'])
185             return cls(net, peer, merkle_branch=bitcoin_data.calculate_merkle_branch([0] + [bitcoin_data.hash256(bitcoin_data.tx_type.pack(x)) for x in share1b['other_txs']], 0), **share1b)
186         else:
187             raise ValueError('unknown share type: %r' % (share['type'],))
188     
189     def __init__(self, net, peer, min_header, share_info, hash_link, merkle_branch, other_txs):
190         if len(share_info['share_data']['coinbase']) > 100:
191             raise ValueError('''coinbase too large! %i bytes''' % (len(self.share_data['coinbase']),))
192         
193         if len(merkle_branch) > 16:
194             raise ValueError('merkle_branch too long!')
195         
196         if p2pool.DEBUG and other_txs is not None and bitcoin_data.calculate_merkle_branch([0] + [bitcoin_data.hash256(bitcoin_data.tx_type.pack(x)) for x in other_txs], 0) != merkle_branch:
197             raise ValueError('merkle_branch and other_txs do not match')
198         
199         assert not hash_link['extra_data'], repr(hash_link['extra_data'])
200         
201         self.net = net
202         self.peer = peer
203         self.min_header = min_header
204         self.share_info = share_info
205         self.hash_link = hash_link
206         self.merkle_branch = merkle_branch
207         self.other_txs = other_txs
208         
209         self.share_data = self.share_info['share_data']
210         self.max_target = self.share_info['max_bits'].target
211         self.target = self.share_info['bits'].target
212         self.timestamp = self.share_info['timestamp']
213         self.previous_hash = self.share_data['previous_share_hash']
214         self.new_script = bitcoin_data.pubkey_hash_to_script2(self.share_data['pubkey_hash'])
215         
216         self.gentx_hash = check_hash_link(
217             hash_link,
218             pack.IntType(256).pack(bitcoin_data.hash256(ref_type.pack(dict(
219                 identifier=net.IDENTIFIER,
220                 share_info=share_info,
221             )))) + pack.IntType(32).pack(0),
222             gentx_before_refhash,
223         )
224         merkle_root = bitcoin_data.check_merkle_branch(self.gentx_hash, 0, merkle_branch)
225         self.header = dict(min_header, merkle_root=merkle_root)
226         self.pow_hash = net.PARENT.POW_FUNC(bitcoin_data.block_header_type.pack(self.header))
227         self.header_hash = bitcoin_data.hash256(bitcoin_data.block_header_type.pack(self.header))
228         
229         if self.pow_hash > self.target:
230             print 'hash %x' % self.pow_hash
231             print 'targ %x' % self.target
232             raise ValueError('not enough work!')
233         
234         if other_txs is not None and not self.pow_hash <= self.header['bits'].target:
235             raise ValueError('other_txs provided when not a block solution')
236         if other_txs is None and self.pow_hash <= self.header['bits'].target:
237             raise ValueError('other_txs not provided when a block solution')
238         
239         self.hash = bitcoin_data.hash256(share_type.pack(self.as_share()))
240         
241         # XXX eww
242         self.time_seen = time.time()
243     
244     def __repr__(self):
245         return '<Share %s>' % (' '.join('%s=%r' % (k, getattr(self, k)) for k in self.__slots__),)
246     
247     def check(self, tracker):
248         share_info, gentx = generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.net)
249         if share_info != self.share_info:
250             raise ValueError('share difficulty invalid')
251         if bitcoin_data.hash256(bitcoin_data.tx_type.pack(gentx)) != self.gentx_hash:
252             raise ValueError('''gentx doesn't match hash_link''')
253     
254     def as_share(self):
255         if not self.pow_hash <= self.header['bits'].target: # share1a
256             return dict(type=2, contents=share1a_type.pack(dict(min_header=self.min_header, share_info=self.share_info, hash_link=self.hash_link, merkle_branch=self.merkle_branch)))
257         else: # share1b
258             return dict(type=3, contents=share1b_type.pack(dict(min_header=self.min_header, share_info=self.share_info, hash_link=self.hash_link, other_txs=self.other_txs)))
259     
260     def as_block(self, tracker):
261         if self.other_txs is None:
262             raise ValueError('share does not contain all txs')
263         
264         share_info, gentx = generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.net)
265         assert share_info == self.share_info
266         
267         return dict(header=self.header, txs=[gentx] + self.other_txs)
268
269 class OkayTracker(forest.Tracker):
270     def __init__(self, net, my_share_hashes, my_doa_share_hashes):
271         forest.Tracker.__init__(self, delta_type=forest.get_attributedelta_type(dict(forest.AttributeDelta.attrs,
272             work=lambda share: bitcoin_data.target_to_average_attempts(share.target),
273             min_work=lambda share: bitcoin_data.target_to_average_attempts(share.max_target),
274         )))
275         self.net = net
276         self.verified = forest.Tracker(delta_type=forest.get_attributedelta_type(dict(forest.AttributeDelta.attrs,
277             work=lambda share: bitcoin_data.target_to_average_attempts(share.target),
278             my_count=lambda share: 1 if share.hash in my_share_hashes else 0,
279             my_doa_count=lambda share: 1 if share.hash in my_doa_share_hashes else 0,
280             my_orphan_announce_count=lambda share: 1 if share.hash in my_share_hashes and share.share_data['stale_info'] == 253 else 0,
281             my_dead_announce_count=lambda share: 1 if share.hash in my_share_hashes and share.share_data['stale_info'] == 254 else 0,
282         )))
283         self.verified.get_nth_parent_hash = self.get_nth_parent_hash # self is a superset of self.verified
284         
285         self.get_cumulative_weights = skiplists.WeightsSkipList(self)
286     
287     def attempt_verify(self, share):
288         if share.hash in self.verified.shares:
289             return True
290         height, last = self.get_height_and_last(share.hash)
291         if height < self.net.CHAIN_LENGTH + 1 and last is not None:
292             raise AssertionError()
293         try:
294             share.check(self)
295         except:
296             log.err(None, 'Share check failed:')
297             return False
298         else:
299             self.verified.add(share)
300             return True
301     
302     def think(self, block_rel_height_func, previous_block, bits):
303         desired = set()
304         
305         # O(len(self.heads))
306         #   make 'unverified heads' set?
307         # for each overall head, attempt verification
308         # if it fails, attempt on parent, and repeat
309         # if no successful verification because of lack of parents, request parent
310         bads = set()
311         for head in set(self.heads) - set(self.verified.heads):
312             head_height, last = self.get_height_and_last(head)
313             
314             for share in self.get_chain(head, head_height if last is None else min(5, max(0, head_height - self.net.CHAIN_LENGTH))):
315                 if self.attempt_verify(share):
316                     break
317                 if share.hash in self.heads:
318                     bads.add(share.hash)
319             else:
320                 if last is not None:
321                     desired.add((
322                         self.shares[random.choice(list(self.reverse_shares[last]))].peer,
323                         last,
324                         max(x.timestamp for x in self.get_chain(head, min(head_height, 5))),
325                         min(x.target for x in self.get_chain(head, min(head_height, 5))),
326                     ))
327         for bad in bads:
328             assert bad not in self.verified.shares
329             assert bad in self.heads
330             if p2pool.DEBUG:
331                 print "BAD", bad
332             self.remove(bad)
333         
334         # try to get at least CHAIN_LENGTH height for each verified head, requesting parents if needed
335         for head in list(self.verified.heads):
336             head_height, last_hash = self.verified.get_height_and_last(head)
337             last_height, last_last_hash = self.get_height_and_last(last_hash)
338             # XXX review boundary conditions
339             want = max(self.net.CHAIN_LENGTH - head_height, 0)
340             can = max(last_height - 1 - self.net.CHAIN_LENGTH, 0) if last_last_hash is not None else last_height
341             get = min(want, can)
342             #print 'Z', head_height, last_hash is None, last_height, last_last_hash is None, want, can, get
343             for share in self.get_chain(last_hash, get):
344                 if not self.attempt_verify(share):
345                     break
346             if head_height < self.net.CHAIN_LENGTH and last_last_hash is not None:
347                 desired.add((
348                     self.shares[random.choice(list(self.verified.reverse_shares[last_hash]))].peer,
349                     last_last_hash,
350                     max(x.timestamp for x in self.get_chain(head, min(head_height, 5))),
351                     min(x.target for x in self.get_chain(head, min(head_height, 5))),
352                 ))
353         
354         # decide best tree
355         decorated_tails = sorted((self.score(max(self.verified.tails[tail_hash], key=self.verified.get_height), block_rel_height_func), tail_hash) for tail_hash in self.verified.tails) # XXX using get_height here is quite possibly incorrect and vulnerable
356         if p2pool.DEBUG:
357             print len(decorated_tails), 'tails:'
358             for score, tail_hash in decorated_tails:
359                 print format_hash(tail_hash), score
360         best_tail_score, best_tail = decorated_tails[-1] if decorated_tails else (None, None)
361         
362         # decide best verified head
363         decorated_heads = sorted(((
364             self.verified.get_work(self.verified.get_nth_parent_hash(h, min(5, self.verified.get_height(h)))),
365             #self.shares[h].peer is None,
366             self.shares[h].pow_hash <= self.shares[h].header['bits'].target, # is block solution
367             (self.shares[h].header['previous_block'], self.shares[h].header['bits']) == (previous_block, bits) or self.shares[h].peer is None,
368             -self.shares[h].time_seen,
369         ), h) for h in self.verified.tails.get(best_tail, []))
370         if p2pool.DEBUG:
371             print len(decorated_heads), 'heads. Top 10:'
372             for score, head_hash in decorated_heads[-10:]:
373                 print '   ', format_hash(head_hash), format_hash(self.shares[head_hash].previous_hash), score
374         best_head_score, best = decorated_heads[-1] if decorated_heads else (None, None)
375         
376         # eat away at heads
377         if decorated_heads:
378             for i in xrange(1000):
379                 to_remove = set()
380                 for share_hash, tail in self.heads.iteritems():
381                     if share_hash in [head_hash for score, head_hash in decorated_heads[-5:]]:
382                         #print 1
383                         continue
384                     if self.shares[share_hash].time_seen > time.time() - 300:
385                         #print 2
386                         continue
387                     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
388                         #print 3
389                         continue
390                     to_remove.add(share_hash)
391                 if not to_remove:
392                     break
393                 for share_hash in to_remove:
394                     self.remove(share_hash)
395                     if share_hash in self.verified.shares:
396                         self.verified.remove(share_hash)
397                 #print "_________", to_remove
398         
399         # drop tails
400         for i in xrange(1000):
401             to_remove = set()
402             for tail, heads in self.tails.iteritems():
403                 if min(self.get_height(head) for head in heads) < 2*self.net.CHAIN_LENGTH + 10:
404                     continue
405                 for aftertail in self.reverse_shares.get(tail, set()):
406                     if len(self.reverse_shares[self.shares[aftertail].previous_hash]) > 1: # XXX
407                         print "raw"
408                         continue
409                     to_remove.add(aftertail)
410             if not to_remove:
411                 break
412             # if removed from this, it must be removed from verified
413             #start = time.time()
414             for aftertail in to_remove:
415                 if self.shares[aftertail].previous_hash not in self.tails:
416                     print "erk", aftertail, self.shares[aftertail].previous_hash
417                     continue
418                 self.remove(aftertail)
419                 if aftertail in self.verified.shares:
420                     self.verified.remove(aftertail)
421             #end = time.time()
422             #print "removed! %i %f" % (len(to_remove), (end - start)/len(to_remove))
423         
424         if best is not None:
425             best_share = self.shares[best]
426             if (best_share.header['previous_block'], best_share.header['bits']) != (previous_block, bits) and best_share.header_hash != previous_block and best_share.peer is not None:
427                 if p2pool.DEBUG:
428                     print 'Stale detected! %x < %x' % (best_share.header['previous_block'], previous_block)
429                 best = best_share.previous_hash
430             
431             timestamp_cutoff = min(int(time.time()), best_share.timestamp) - 3600
432             target_cutoff = 2**256//(self.net.SHARE_PERIOD*best_tail_score[1] + 1) * 2 if best_tail_score[1] is not None else 2**256-1
433         else:
434             timestamp_cutoff = int(time.time()) - 24*60*60
435             target_cutoff = 2**256-1
436         
437         if p2pool.DEBUG:
438             print 'Desire %i shares. Cutoff: %s old diff>%.2f' % (len(desired), math.format_dt(time.time() - timestamp_cutoff), bitcoin_data.target_to_difficulty(target_cutoff))
439             for peer, hash, ts, targ in desired:
440                 print '   ', '%s:%i' % peer.addr if peer is not None else None, format_hash(hash), math.format_dt(time.time() - ts), bitcoin_data.target_to_difficulty(targ), ts >= timestamp_cutoff, targ <= target_cutoff
441         
442         return best, [(peer, hash) for peer, hash, ts, targ in desired if ts >= timestamp_cutoff and targ <= target_cutoff]
443     
444     def score(self, share_hash, block_rel_height_func):
445         # returns approximate lower bound on chain's hashrate in the last self.net.CHAIN_LENGTH*15//16*self.net.SHARE_PERIOD time
446         
447         head_height = self.verified.get_height(share_hash)
448         if head_height < self.net.CHAIN_LENGTH:
449             return head_height, None
450         
451         end_point = self.verified.get_nth_parent_hash(share_hash, self.net.CHAIN_LENGTH*15//16)
452         
453         block_height = max(block_rel_height_func(share.header['previous_block']) for share in
454             self.verified.get_chain(end_point, self.net.CHAIN_LENGTH//16))
455         
456         return self.net.CHAIN_LENGTH, (self.verified.get_work(share_hash) - self.verified.get_work(end_point))//((0 - block_height + 1)*self.net.PARENT.BLOCK_PERIOD)
457
458 def format_hash(x):
459     if x is None:
460         return 'xxxxxxxx'
461     return '%08x' % (x % 2**32)
462
463 class ShareStore(object):
464     def __init__(self, prefix, net):
465         self.filename = prefix
466         self.dirname = os.path.dirname(os.path.abspath(prefix))
467         self.filename = os.path.basename(os.path.abspath(prefix))
468         self.net = net
469         self.known = None # will be filename -> set of share hashes, set of verified hashes
470         self.known_desired = None
471     
472     def get_shares(self):
473         if self.known is not None:
474             raise AssertionError()
475         known = {}
476         filenames, next = self.get_filenames_and_next()
477         for filename in filenames:
478             share_hashes, verified_hashes = known.setdefault(filename, (set(), set()))
479             with open(filename, 'rb') as f:
480                 for line in f:
481                     try:
482                         type_id_str, data_hex = line.strip().split(' ')
483                         type_id = int(type_id_str)
484                         if type_id == 0:
485                             pass
486                         elif type_id == 1:
487                             pass
488                         elif type_id == 2:
489                             verified_hash = int(data_hex, 16)
490                             yield 'verified_hash', verified_hash
491                             verified_hashes.add(verified_hash)
492                         elif type_id == 5:
493                             raw_share = share_type.unpack(data_hex.decode('hex'))
494                             if raw_share['type'] in [0, 1]:
495                                 continue
496                             share = Share.from_share(raw_share, self.net, None)
497                             yield 'share', share
498                             share_hashes.add(share.hash)
499                         else:
500                             raise NotImplementedError("share type %i" % (type_id,))
501                     except Exception:
502                         log.err(None, "Error while reading saved shares, continuing where left off:")
503         self.known = known
504         self.known_desired = dict((k, (set(a), set(b))) for k, (a, b) in known.iteritems())
505     
506     def _add_line(self, line):
507         filenames, next = self.get_filenames_and_next()
508         if filenames and os.path.getsize(filenames[-1]) < 10e6:
509             filename = filenames[-1]
510         else:
511             filename = next
512         
513         with open(filename, 'ab') as f:
514             f.write(line + '\n')
515         
516         return filename
517     
518     def add_share(self, share):
519         for filename, (share_hashes, verified_hashes) in self.known.iteritems():
520             if share.hash in share_hashes:
521                 break
522         else:
523             filename = self._add_line("%i %s" % (5, share_type.pack(share.as_share()).encode('hex')))
524             share_hashes, verified_hashes = self.known.setdefault(filename, (set(), set()))
525             share_hashes.add(share.hash)
526         share_hashes, verified_hashes = self.known_desired.setdefault(filename, (set(), set()))
527         share_hashes.add(share.hash)
528     
529     def add_verified_hash(self, share_hash):
530         for filename, (share_hashes, verified_hashes) in self.known.iteritems():
531             if share_hash in verified_hashes:
532                 break
533         else:
534             filename = self._add_line("%i %x" % (2, share_hash))
535             share_hashes, verified_hashes = self.known.setdefault(filename, (set(), set()))
536             verified_hashes.add(share_hash)
537         share_hashes, verified_hashes = self.known_desired.setdefault(filename, (set(), set()))
538         verified_hashes.add(share_hash)
539     
540     def get_filenames_and_next(self):
541         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())
542         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)))
543     
544     def forget_share(self, share_hash):
545         for filename, (share_hashes, verified_hashes) in self.known_desired.iteritems():
546             if share_hash in share_hashes:
547                 share_hashes.remove(share_hash)
548         self.check_remove()
549     
550     def forget_verified_share(self, share_hash):
551         for filename, (share_hashes, verified_hashes) in self.known_desired.iteritems():
552             if share_hash in verified_hashes:
553                 verified_hashes.remove(share_hash)
554         self.check_remove()
555     
556     def check_remove(self):
557         to_remove = set()
558         for filename, (share_hashes, verified_hashes) in self.known_desired.iteritems():
559             #print filename, len(share_hashes) + len(verified_hashes)
560             if not share_hashes and not verified_hashes:
561                 to_remove.add(filename)
562         for filename in to_remove:
563             self.known.pop(filename)
564             self.known_desired.pop(filename)
565             os.remove(filename)
566             print "REMOVED", filename