1 from __future__ import division
8 from twisted.python import log
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
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')),
26 share_info_type = bitcoin_data.ComposedType([
27 ('share_data', share_data_type),
28 ('target', bitcoin_data.FloatingIntegerType()),
29 ('timestamp', bitcoin_data.StructType('<I')),
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),
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)),
48 share_type = bitcoin_data.ComposedType([
49 ('type', bitcoin_data.VarIntType()),
50 ('contents', bitcoin_data.VarStrType()),
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(' ')
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')
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')
69 raise ValueError('unknown share type: %r' % (share['type'],))
72 def from_share1a(cls, share1a, net):
73 return cls(net, **share1a)
76 def from_share1b(cls, share1b, net):
77 return cls(net, **share1b)
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
89 if len(merkle_branch) > 16:
90 raise ValueError('merkle_branch too long!')
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
98 self.share_data = self.share_info['share_data']
99 self.target = self.share_info['target']
100 self.timestamp = self.share_info['timestamp']
102 self.new_script = self.share_data['new_script']
103 self.subsidy = self.share_data['subsidy']
104 self.donation = self.share_data['donation']
106 if len(self.new_script) > 100:
107 raise ValueError('new_script too long!')
109 self.previous_hash = self.previous_share_hash = self.share_data['previous_share_hash']
110 self.nonce = self.share_data['nonce']
112 if len(self.nonce) > 100:
113 raise ValueError('nonce too long!')
115 if len(self.share_data['coinbase']) > 100:
116 raise ValueError('''coinbase too large! %i bytes''' % (len(self.share_data['coinbase']),))
118 self.pow_hash = net.BITCOIN_POW_FUNC(header)
119 self.header_hash = bitcoin_data.block_header_type.hash256(header)
121 self.hash = share1a_type.hash256(self.as_share1a())
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!')
128 if script.get_sigop_count(self.new_script) > 1:
129 raise ValueError('too many sigops!')
131 self.stale_frac = self.share_data['stale_frac']/254 if self.share_data['stale_frac'] != 255 else None
134 self.time_seen = time.time()
138 return '<Share %s>' % (' '.join('%s=%r' % (k, getattr(self, k)) for k in self.__slots__),)
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')
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''')
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()))
154 raise AssertionError()
156 def as_share1a(self):
157 return dict(header=self.header, share_info=self.share_info, merkle_branch=self.merkle_branch)
159 def as_share1b(self):
160 if self.other_txs is None:
161 raise ValueError('share does not contain all txs')
163 return dict(header=self.header, share_info=self.share_info, other_txs=self.other_txs)
165 def as_block(self, tracker, net):
166 if self.other_txs is None:
167 raise ValueError('share does not contain all txs')
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
172 return dict(header=self.header, txs=[gentx] + self.other_txs)
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
181 return attempts//time
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
190 if len(share_data['coinbase']) > 100:
191 raise ValueError('coinbase too long!')
193 previous_share = tracker.shares[previous_share_hash] if previous_share_hash is not None else None
195 chain_length = getattr(net, 'REAL_CHAIN_LENGTH_FUNC', lambda _: net.REAL_CHAIN_LENGTH)(previous_share.timestamp if previous_share is not None else None)
197 height, last = tracker.get_height_and_last(previous_share_hash)
198 assert height >= chain_length or last is None
199 if height < net.TARGET_LOOKBEHIND:
200 target = bitcoin_data.FloatingInteger.from_target_upper_bound(net.MAX_TARGET)
202 attempts_per_second = get_pool_attempts_per_second(tracker, previous_share_hash, net.TARGET_LOOKBEHIND)
203 pre_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1
204 pre_target2 = math.clip(pre_target, (previous_share.target*9//10, previous_share.target*11//10))
205 pre_target3 = math.clip(pre_target2, (0, net.MAX_TARGET))
206 target = bitcoin_data.FloatingInteger.from_target_upper_bound(pre_target3)
208 attempts_to_block = bitcoin_data.target_to_average_attempts(block_target)
209 max_att = net.SPREAD * attempts_to_block
211 this_att = min(bitcoin_data.target_to_average_attempts(target), max_att)
212 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))
213 assert other_total_weight == sum(other_weights.itervalues()) + other_donation_weight, (other_total_weight, sum(other_weights.itervalues()) + other_donation_weight)
214 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
215 assert total_weight == sum(weights.itervalues()) + donation_weight, (total_weight, sum(weights.itervalues()) + donation_weight)
217 SCRIPT = '4104ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664bac'.decode('hex')
219 # 1 satoshi is always donated so that a list of p2pool generated blocks can be easily found by looking at the donation address
220 amounts = dict((script, (subsidy-1)*(199*weight)//(200*total_weight)) for (script, weight) in weights.iteritems())
221 amounts[new_script] = amounts.get(new_script, 0) + (subsidy-1)//200
222 amounts[SCRIPT] = amounts.get(SCRIPT, 0) + (subsidy-1)*(199*donation_weight)//(200*total_weight)
223 amounts[SCRIPT] = amounts.get(SCRIPT, 0) + subsidy - sum(amounts.itervalues()) # collect any extra satoshis :P
225 if sum(amounts.itervalues()) != subsidy:
227 if any(x < 0 for x in amounts.itervalues()):
230 dests = sorted(amounts.iterkeys(), key=lambda script: (amounts[script], script))
231 dests = dests[-4000:] # block length limit, unlikely to ever be hit
234 share_data=share_data,
236 timestamp=math.clip(desired_timestamp, (previous_share.timestamp - 60, previous_share.timestamp + 60)) if previous_share is not None else desired_timestamp,
239 return share_info, dict(
242 previous_output=None,
244 script=share_data['coinbase'].ljust(2, '\x00'),
246 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]],
251 class OkayTracker(forest.Tracker):
252 def __init__(self, net):
253 forest.Tracker.__init__(self)
255 self.verified = forest.Tracker()
256 self.verified.get_nth_parent_hash = self.get_nth_parent_hash # self is a superset of self.verified
258 self.get_cumulative_weights = skiplists.WeightsSkipList(self)
260 def attempt_verify(self, share, now):
261 if share.hash in self.verified.shares:
263 height, last = self.get_height_and_last(share.hash)
264 if height < self.net.CHAIN_LENGTH + 1 and last is not None:
265 raise AssertionError()
267 share.check(self, now, self.net)
269 log.err(None, 'Share check failed:')
272 self.verified.add(share)
275 def think(self, ht, previous_block, now):
279 # make 'unverified heads' set?
280 # for each overall head, attempt verification
281 # if it fails, attempt on parent, and repeat
282 # if no successful verification because of lack of parents, request parent
284 for head in set(self.heads) - set(self.verified.heads):
285 head_height, last = self.get_height_and_last(head)
287 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))):
288 if self.attempt_verify(share, now):
290 if share.hash in self.heads:
294 desired.add((self.shares[random.choice(list(self.reverse_shares[last]))].peer, last))
296 assert bad not in self.verified.shares
297 assert bad in self.heads
302 # try to get at least CHAIN_LENGTH height for each verified head, requesting parents if needed
303 for head in list(self.verified.heads):
304 head_height, last_hash = self.verified.get_height_and_last(head)
305 last_height, last_last_hash = self.get_height_and_last(last_hash)
306 # XXX review boundary conditions
307 want = max(self.net.CHAIN_LENGTH - head_height, 0)
308 can = max(last_height - 1 - self.net.CHAIN_LENGTH, 0) if last_last_hash is not None else last_height
310 #print 'Z', head_height, last_hash is None, last_height, last_last_hash is None, want, can, get
311 for share in itertools.islice(self.get_chain_known(last_hash), get):
312 if not self.attempt_verify(share, now):
314 if head_height < self.net.CHAIN_LENGTH and last_last_hash is not None:
315 desired.add((self.verified.shares[random.choice(list(self.verified.reverse_shares[last_hash]))].peer, last_last_hash))
318 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
319 # decide best verified head
320 scores = sorted(self.verified.tails.get(best_tail, []), key=lambda h: (
321 self.verified.get_work(self.verified.get_nth_parent_hash(h, min(5, self.verified.get_height(h)))),
322 #self.verified.shares[h].peer is None,
323 ht.get_min_height(self.verified.shares[h].previous_block),
324 -self.verified.shares[h].time_seen
329 print len(self.verified.tails), "chain tails and", len(self.verified.tails.get(best_tail, [])), 'chain heads. Top 10 heads:'
332 for h in scores[-10:]:
333 print ' ', format_hash(h), format_hash(self.verified.shares[h].previous_hash), (
334 self.verified.get_work(self.verified.get_nth_parent_hash(h, min(5, self.verified.get_height(h)))),
335 self.verified.shares[h].peer is None,
336 ht.get_min_height(self.verified.shares[h].previous_block),
337 -self.verified.shares[h].time_seen
342 for i in xrange(1000):
344 for share_hash, tail in self.heads.iteritems():
345 if share_hash in scores[-5:]:
348 if self.shares[share_hash].time_seen > time.time() - 300:
351 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
354 to_remove.add(share_hash)
357 for share_hash in to_remove:
358 self.remove(share_hash)
359 if share_hash in self.verified.shares:
360 self.verified.remove(share_hash)
361 #print "_________", to_remove
364 for i in xrange(1000):
366 for tail, heads in self.tails.iteritems():
367 if min(self.get_height(head) for head in heads) < 2*self.net.CHAIN_LENGTH + 10:
369 for aftertail in self.reverse_shares.get(tail, set()):
370 if len(self.reverse_shares[self.shares[aftertail].previous_hash]) > 1: # XXX
373 to_remove.add(aftertail)
376 # if removed from this, it must be removed from verified
378 for aftertail in to_remove:
379 if self.shares[aftertail].previous_hash not in self.tails:
380 print "erk", aftertail, self.shares[aftertail].previous_hash
382 self.remove(aftertail)
383 if aftertail in self.verified.shares:
384 self.verified.remove(aftertail)
386 #print "removed! %i %f" % (len(to_remove), (end - start)/len(to_remove))
388 best = scores[-1] if scores else None
391 best_share = self.verified.shares[best]
392 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:
394 print 'Stale detected! %x < %x' % (best_share.header['previous_block'], previous_block)
395 best = best_share.previous_hash
399 @memoize.memoize_with_backing(expiring_dict.ExpiringDict(5, get_touches=False))
400 def score(self, share_hash, ht):
401 head_height, last = self.verified.get_height_and_last(share_hash)
405 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
406 for share in reversed(list(itertools.islice(self.verified.get_chain_known(share2_hash), self.net.CHAIN_LENGTH))):
407 max_height = max(max_height, ht.get_min_height(share.header['previous_block']))
408 attempts += bitcoin_data.target_to_average_attempts(share.target)
409 this_score = attempts//(ht.get_highest_height() - max_height + 1)
410 if this_score > score2:
412 return min(head_height, self.net.CHAIN_LENGTH), score2
417 return '%08x' % (x % 2**32)
419 class ShareStore(object):
420 def __init__(self, prefix, net):
421 self.filename = prefix
422 self.dirname = os.path.dirname(os.path.abspath(prefix))
423 self.filename = os.path.basename(os.path.abspath(prefix))
425 self.known = None # will be filename -> set of share hashes, set of verified hashes
427 def get_shares(self):
428 if self.known is not None:
429 raise AssertionError()
431 filenames, next = self.get_filenames_and_next()
432 for filename in filenames:
433 share_hashes, verified_hashes = known.setdefault(filename, (set(), set()))
434 with open(filename, 'rb') as f:
437 type_id_str, data_hex = line.strip().split(' ')
438 type_id = int(type_id_str)
444 verified_hash = int(data_hex, 16)
445 yield 'verified_hash', verified_hash
446 verified_hashes.add(verified_hash)
448 share = Share.from_share(share_type.unpack(data_hex.decode('hex')), self.net)
450 share_hashes.add(share.hash)
452 raise NotImplementedError("share type %i" % (type_id,))
454 log.err(None, "Error while reading saved shares, continuing where left off:")
457 def _add_line(self, line):
458 filenames, next = self.get_filenames_and_next()
459 if filenames and os.path.getsize(filenames[-1]) < 10e6:
460 filename = filenames[-1]
464 with open(filename, 'ab') as f:
469 def add_share(self, share):
470 type_id, data = 5, share_type.pack(share.as_share())
471 filename = self._add_line("%i %s" % (type_id, data.encode('hex')))
472 share_hashes, verified_hashes = self.known.setdefault(filename, (set(), set()))
473 share_hashes.add(share.hash)
475 def add_verified_hash(self, share_hash):
476 filename = self._add_line("%i %x" % (2, share_hash))
477 share_hashes, verified_hashes = self.known.setdefault(filename, (set(), set()))
478 verified_hashes.add(share_hash)
480 def get_filenames_and_next(self):
481 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())
482 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)))
484 def forget_share(self, share_hash):
485 for filename, (share_hashes, verified_hashes) in self.known.iteritems():
486 if share_hash in share_hashes:
487 share_hashes.remove(share_hash)
490 def forget_verified_share(self, share_hash):
491 for filename, (share_hashes, verified_hashes) in self.known.iteritems():
492 if share_hash in verified_hashes:
493 verified_hashes.remove(share_hash)
496 def check_remove(self):
498 for filename, (share_hashes, verified_hashes) in self.known.iteritems():
499 #print filename, len(share_hashes) + len(verified_hashes)
500 if not share_hashes and not verified_hashes:
501 to_remove.add(filename)
502 for filename in to_remove:
503 self.known.pop(filename)
505 print "REMOVED", filename