X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=p2pool%2Fdata.py;h=0bc04e3230939cd2fabd0cfd647a18da153c7bd8;hb=a089f37c8db20913077418dd6198a5a279fecc0d;hp=a68eedf003818bf7747091a23bf27e66edd57a14;hpb=82b587c0bc063c75416317106b70fc57f5d16981;p=p2pool.git diff --git a/p2pool/data.py b/p2pool/data.py index a68eedf..0bc04e3 100644 --- a/p2pool/data.py +++ b/p2pool/data.py @@ -39,23 +39,23 @@ share_type = pack.ComposedType([ ('contents', pack.VarStrType()), ]) -def load_share(share, net, peer): - if share['type'] in [0, 1, 2, 3, 4, 5, 6, 7, 8]: +def load_share(share, net, peer_addr): + assert peer_addr is None or isinstance(peer_addr, tuple) + if share['type'] < Share.VERSION: from p2pool import p2p raise p2p.PeerMisbehavingError('sent an obsolete share') elif share['type'] == Share.VERSION: - return Share(net, peer, Share.share_type.unpack(share['contents'])) + return Share(net, peer_addr, Share.share_type.unpack(share['contents'])) else: raise ValueError('unknown share type: %r' % (share['type'],)) -DONATION_SCRIPT = '4104ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664bac'.decode('hex') +DONATION_SCRIPT = '410418ca0d3ae9712c1640692030f4774d66428edce1d75790d470d2e460fd03327141279a73beb92cdcbd64204a90901d135b6e5e6f9d299517424c5ab08d4fac22ac'.decode('hex') class Share(object): - VERSION = 9 + VERSION = 13 + VOTING_VERSION = 13 SUCCESSOR = None - other_txs = None - small_block_header_type = pack.ComposedType([ ('version', pack.VarIntType()), ('previous_block', pack.PossiblyNoneType(0, pack.IntType(256))), @@ -76,14 +76,13 @@ class Share(object): ('desired_version', pack.VarIntType()), ])), ('new_transaction_hashes', pack.ListType(pack.IntType(256))), - ('transaction_hash_refs', pack.ListType(pack.ComposedType([ # compressed by referencing previous shares' hashes - ('share_count', pack.VarIntType()), - ('tx_count', pack.VarIntType()), - ]))), + ('transaction_hash_refs', pack.ListType(pack.VarIntType(), 2)), # pairs of share_count, tx_count ('far_share_hash', pack.PossiblyNoneType(0, pack.IntType(256))), ('max_bits', bitcoin_data.FloatingIntegerType()), ('bits', bitcoin_data.FloatingIntegerType()), ('timestamp', pack.IntType(32)), + ('absheight', pack.IntType(32)), + ('abswork', pack.IntType(128)), ]) share_type = pack.ComposedType([ @@ -93,7 +92,7 @@ class Share(object): ('branch', pack.ListType(pack.IntType(256))), ('index', pack.IntType(0)), ])), - ('last_txout_nonce', pack.IntType(32)), + ('last_txout_nonce', pack.IntType(64)), ('hash_link', hash_link_type), ('merkle_link', pack.ComposedType([ ('branch', pack.ListType(pack.IntType(256))), @@ -106,10 +105,10 @@ class Share(object): ('share_info', share_info_type), ]) - gentx_before_refhash = pack.VarStrType().pack(DONATION_SCRIPT) + pack.IntType(64).pack(0) + pack.VarStrType().pack('\x24' + pack.IntType(256).pack(0) + pack.IntType(32).pack(0))[:2] + gentx_before_refhash = pack.VarStrType().pack(DONATION_SCRIPT) + pack.IntType(64).pack(0) + pack.VarStrType().pack('\x6a\x28' + pack.IntType(256).pack(0) + pack.IntType(64).pack(0))[:3] @classmethod - def generate_transaction(cls, tracker, share_data, block_target, desired_timestamp, desired_target, ref_merkle_link, desired_other_transaction_hashes, net, known_txs=None, last_txout_nonce=0): + def generate_transaction(cls, tracker, share_data, block_target, desired_timestamp, desired_target, ref_merkle_link, desired_other_transaction_hashes_and_fees, net, known_txs=None, last_txout_nonce=0, base_subsidy=None): previous_share = tracker.items[share_data['previous_share_hash']] if share_data['previous_share_hash'] is not None else None height, last = tracker.get_height_and_last(share_data['previous_share_hash']) @@ -122,34 +121,22 @@ class Share(object): pre_target2 = math.clip(pre_target, (previous_share.max_target*9//10, previous_share.max_target*11//10)) pre_target3 = math.clip(pre_target2, (net.MIN_TARGET, net.MAX_TARGET)) max_bits = bitcoin_data.FloatingInteger.from_target_upper_bound(pre_target3) - bits = bitcoin_data.FloatingInteger.from_target_upper_bound(math.clip(desired_target, (pre_target3//10, pre_target3))) - - weights, total_weight, donation_weight = tracker.get_cumulative_weights(share_data['previous_share_hash'], - min(height, net.REAL_CHAIN_LENGTH), - 65535*net.SPREAD*bitcoin_data.target_to_average_attempts(block_target), - ) - assert total_weight == sum(weights.itervalues()) + donation_weight, (total_weight, sum(weights.itervalues()) + donation_weight) - - 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 - this_script = bitcoin_data.pubkey_hash_to_script2(share_data['pubkey_hash']) - amounts[this_script] = amounts.get(this_script, 0) + share_data['subsidy']//200 # 0.5% goes to block finder - 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 - - if sum(amounts.itervalues()) != share_data['subsidy'] or any(x < 0 for x in amounts.itervalues()): - raise ValueError() - - dests = sorted(amounts.iterkeys(), key=lambda script: (script == DONATION_SCRIPT, amounts[script], script))[-4000:] # block length limit, unlikely to ever be hit + bits = bitcoin_data.FloatingInteger.from_target_upper_bound(math.clip(desired_target, (pre_target3//30, pre_target3))) new_transaction_hashes = [] new_transaction_size = 0 transaction_hash_refs = [] other_transaction_hashes = [] - for tx_hash in desired_other_transaction_hashes: - for i, share in enumerate(tracker.get_chain(share_data['previous_share_hash'], min(height, 100))): - if tx_hash in share.new_transaction_hashes: - this = dict(share_count=i+1, tx_count=share.new_transaction_hashes.index(tx_hash)) - break + past_shares = list(tracker.get_chain(share_data['previous_share_hash'], min(height, 100))) + tx_hash_to_this = {} + for i, share in enumerate(past_shares): + for j, tx_hash in enumerate(share.new_transaction_hashes): + if tx_hash not in tx_hash_to_this: + tx_hash_to_this[tx_hash] = [1+i, j] # share_count, tx_count + for tx_hash, fee in desired_other_transaction_hashes_and_fees: + if tx_hash in tx_hash_to_this: + this = tx_hash_to_this[tx_hash] else: if known_txs is not None: this_size = bitcoin_data.tx_type.packed_size(known_txs[tx_hash]) @@ -157,10 +144,35 @@ class Share(object): break new_transaction_size += this_size new_transaction_hashes.append(tx_hash) - this = dict(share_count=0, tx_count=len(new_transaction_hashes)-1) - transaction_hash_refs.append(this) + this = [0, len(new_transaction_hashes)-1] + transaction_hash_refs.extend(this) other_transaction_hashes.append(tx_hash) + included_transactions = set(other_transaction_hashes) + removed_fees = [fee for tx_hash, fee in desired_other_transaction_hashes_and_fees if tx_hash not in included_transactions] + definite_fees = sum(0 if fee is None else fee for tx_hash, fee in desired_other_transaction_hashes_and_fees if tx_hash in included_transactions) + if None not in removed_fees: + share_data = dict(share_data, subsidy=share_data['subsidy'] - sum(removed_fees)) + else: + assert base_subsidy is not None + share_data = dict(share_data, subsidy=base_subsidy + definite_fees) + + weights, total_weight, donation_weight = tracker.get_cumulative_weights(previous_share.share_data['previous_share_hash'] if previous_share is not None else None, + max(0, min(height, net.REAL_CHAIN_LENGTH) - 1), + 65535*net.SPREAD*bitcoin_data.target_to_average_attempts(block_target), + ) + assert total_weight == sum(weights.itervalues()) + donation_weight, (total_weight, sum(weights.itervalues()) + donation_weight) + + 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 + this_script = bitcoin_data.pubkey_hash_to_script2(share_data['pubkey_hash']) + amounts[this_script] = amounts.get(this_script, 0) + share_data['subsidy']//200 # 0.5% goes to block finder + 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 + + if sum(amounts.itervalues()) != share_data['subsidy'] or any(x < 0 for x in amounts.itervalues()): + raise ValueError() + + dests = sorted(amounts.iterkeys(), key=lambda script: (script == DONATION_SCRIPT, amounts[script], script))[-4000:] # block length limit, unlikely to ever be hit + share_info = dict( share_data=share_data, far_share_hash=None if last is None and height < 99 else tracker.get_nth_parent_hash(share_data['previous_share_hash'], 99), @@ -172,10 +184,15 @@ class Share(object): )) if previous_share is not None else desired_timestamp, new_transaction_hashes=new_transaction_hashes, transaction_hash_refs=transaction_hash_refs, + absheight=((previous_share.absheight if previous_share is not None else 0) + 1) % 2**32, + abswork=((previous_share.abswork if previous_share is not None else 0) + bitcoin_data.target_to_average_attempts(bits.target)) % 2**128, ) gentx = dict( version=1, + # coinbase timestamp must be older than share/block timestamp + # maybe there are more elegant solution, but this hack works quite well for now + timestamp=share_info['timestamp'], tx_ins=[dict( previous_output=None, sequence=None, @@ -183,21 +200,23 @@ class Share(object): )], tx_outs=[dict(value=amounts[script], script=script) for script in dests if amounts[script] or script == DONATION_SCRIPT] + [dict( value=0, - script='\x24' + cls.get_ref_hash(net, share_info, ref_merkle_link) + pack.IntType(32).pack(last_txout_nonce), + script='\x6a\x28' + cls.get_ref_hash(net, share_info, ref_merkle_link) + pack.IntType(64).pack(last_txout_nonce), )], lock_time=0, ) - def get_share(header, transactions): - min_header=dict(header);del min_header['merkle_root'] - return cls(net, None, dict( + def get_share(header, last_txout_nonce=last_txout_nonce): + min_header = dict(header); del min_header['merkle_root'] + share = cls(net, None, dict( min_header=min_header, share_info=share_info, ref_merkle_link=dict(branch=[], index=0), last_txout_nonce=last_txout_nonce, - hash_link=prefix_to_hash_link(bitcoin_data.tx_type.pack(gentx)[:-32-4-4], cls.gentx_before_refhash), + hash_link=prefix_to_hash_link(bitcoin_data.tx_type.pack(gentx)[:-32-8-4], cls.gentx_before_refhash), merkle_link=bitcoin_data.calculate_merkle_link([None] + other_transaction_hashes, 0), )) + assert share.header == header # checks merkle_root + return share return share_info, gentx, other_transaction_hashes, get_share @@ -208,11 +227,11 @@ class Share(object): share_info=share_info, ))), ref_merkle_link)) - __slots__ = 'net peer contents min_header share_info hash_link merkle_link hash share_data max_target target timestamp previous_hash new_script desired_version gentx_hash header pow_hash header_hash new_transaction_hashes time_seen'.split(' ') + __slots__ = 'net peer_addr contents min_header share_info hash_link merkle_link hash share_data max_target target timestamp previous_hash new_script desired_version gentx_hash header pow_hash header_hash new_transaction_hashes time_seen absheight abswork'.split(' ') - def __init__(self, net, peer, contents): + def __init__(self, net, peer_addr, contents): self.net = net - self.peer = peer + self.peer_addr = peer_addr self.contents = contents self.min_header = contents['min_header'] @@ -235,21 +254,24 @@ class Share(object): self.previous_hash = self.share_data['previous_share_hash'] self.new_script = bitcoin_data.pubkey_hash_to_script2(self.share_data['pubkey_hash']) self.desired_version = self.share_data['desired_version'] + self.absheight = self.share_info['absheight'] + self.abswork = self.share_info['abswork'] - for x in self.share_info['transaction_hash_refs']: - assert x['share_count'] < 110 - for i, x in enumerate(self.share_info['new_transaction_hashes']): - assert dict(share_count=0, tx_count=i) in self.share_info['transaction_hash_refs'] + n = set() + for share_count, tx_count in self.iter_transaction_hash_refs(): + assert share_count < 110 + if share_count == 0: + n.add(tx_count) + assert n == set(range(len(self.share_info['new_transaction_hashes']))) self.gentx_hash = check_hash_link( self.hash_link, - self.get_ref_hash(net, self.share_info, contents['ref_merkle_link']) + pack.IntType(32).pack(self.contents['last_txout_nonce']) + pack.IntType(32).pack(0), + self.get_ref_hash(net, self.share_info, contents['ref_merkle_link']) + pack.IntType(64).pack(self.contents['last_txout_nonce']) + pack.IntType(32).pack(0), self.gentx_before_refhash, ) merkle_root = bitcoin_data.check_merkle_link(self.gentx_hash, self.merkle_link) self.header = dict(self.min_header, merkle_root=merkle_root) - self.pow_hash = net.PARENT.POW_FUNC(bitcoin_data.block_header_type.pack(self.header)) - self.hash = self.header_hash = bitcoin_data.hash256(bitcoin_data.block_header_type.pack(self.header)) + self.pow_hash = self.hash = self.header_hash = bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(self.header)) if self.target > net.MAX_TARGET: from p2pool import p2p @@ -265,11 +287,14 @@ class Share(object): self.time_seen = time.time() def __repr__(self): - return 'Share' + repr((self.net, self.peer, self.contents)) + return 'Share' + repr((self.net, self.peer_addr, self.contents)) def as_share(self): return dict(type=self.VERSION, contents=self.share_type.pack(self.contents)) + def iter_transaction_hash_refs(self): + return zip(self.share_info['transaction_hash_refs'][::2], self.share_info['transaction_hash_refs'][1::2]) + def check(self, tracker): from p2pool import p2p if self.share_data['previous_share_hash'] is not None: @@ -289,10 +314,11 @@ class Share(object): else: raise p2p.PeerMisbehavingError('''%s can't follow %s''' % (type(self).__name__, type(previous_share).__name__)) - other_tx_hashes = [tracker.items[tracker.get_nth_parent_hash(self.hash, x['share_count'])].share_info['new_transaction_hashes'][x['tx_count']] for x in self.share_info['transaction_hash_refs']] + other_tx_hashes = [tracker.items[tracker.get_nth_parent_hash(self.hash, share_count)].share_info['new_transaction_hashes'][tx_count] for share_count, tx_count in self.iter_transaction_hash_refs()] - share_info, gentx, other_tx_hashes2, get_share = self.generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.contents['ref_merkle_link'], other_tx_hashes, self.net, last_txout_nonce=self.contents['last_txout_nonce']) + share_info, gentx, other_tx_hashes2, get_share = self.generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.contents['ref_merkle_link'], [(h, None) for h in other_tx_hashes], self.net, last_txout_nonce=self.contents['last_txout_nonce']) assert other_tx_hashes2 == other_tx_hashes +# workaround if share_info != self.share_info: raise ValueError('share_info invalid') if bitcoin_data.hash256(bitcoin_data.tx_type.pack(gentx)) != self.gentx_hash: @@ -304,10 +330,12 @@ class Share(object): return gentx # only used by as_block def get_other_tx_hashes(self, tracker): + parents_needed = max(share_count for share_count, tx_count in self.iter_transaction_hash_refs()) if self.share_info['transaction_hash_refs'] else 0 parents = tracker.get_height(self.hash) - 1 - if not all(x['share_count'] <= parents for x in self.share_info['transaction_hash_refs']): + if parents < parents_needed: return None - return [tracker.items[tracker.get_nth_parent_hash(self.hash, x['share_count'])].share_info['new_transaction_hashes'][x['tx_count']] for x in self.share_info['transaction_hash_refs']] + last_shares = list(tracker.get_chain(self.hash, parents_needed + 1)) + return [last_shares[share_count].share_info['new_transaction_hashes'][tx_count] for share_count, tx_count in self.iter_transaction_hash_refs()] def _get_other_txs(self, tracker, known_txs): other_tx_hashes = self.get_other_tx_hashes(tracker) @@ -320,16 +348,15 @@ class Share(object): return [known_txs[tx_hash] for tx_hash in other_tx_hashes] def should_punish_reason(self, previous_block, bits, tracker, known_txs): - if (self.header['previous_block'], self.header['bits']) != (previous_block, bits) and self.header_hash != previous_block and self.peer is not None: - return True, 'Block-stale detected! %x < %x' % (self.header['previous_block'], previous_block) + if (self.header['previous_block'], self.header['bits']) != (previous_block, bits) and self.header_hash != previous_block and self.peer_addr is not None: + return True, 'Block-stale detected! height(%x) < height(%x) or %08x != %08x' % (self.header['previous_block'], previous_block, self.header['bits'].bits, bits.bits) if self.pow_hash <= self.header['bits'].target: return -1, 'block solution' other_txs = self._get_other_txs(tracker, known_txs) if other_txs is None: - if self.time_seen is not None: # ignore if loaded from ShareStore - return True, 'not all txs present' + pass else: all_txs_size = sum(bitcoin_data.tx_type.packed_size(tx) for tx in other_txs) if all_txs_size > 1000000: @@ -345,7 +372,7 @@ class Share(object): other_txs = self._get_other_txs(tracker, known_txs) if other_txs is None: return None # not all txs present - return dict(header=self.header, txs=[self.check(tracker)] + other_txs) + return dict(header=self.header, txs=[self.check(tracker)] + other_txs, signature='') class WeightsSkipList(forest.TrackerSkipList): @@ -406,7 +433,7 @@ class OkayTracker(forest.Tracker): try: share.check(self) except: - log.err(None, 'Share check failed:') + log.err(None, 'Share check failed: %064x -> %064x' % (share.hash, share.previous_hash if share.previous_hash is not None else 0)) return False else: self.verified.add(share) @@ -414,35 +441,41 @@ class OkayTracker(forest.Tracker): def think(self, block_rel_height_func, previous_block, bits, known_txs): desired = set() + bad_peer_addresses = set() # O(len(self.heads)) # make 'unverified heads' set? # for each overall head, attempt verification # if it fails, attempt on parent, and repeat # if no successful verification because of lack of parents, request parent - bads = set() + bads = [] for head in set(self.heads) - set(self.verified.heads): head_height, last = self.get_height_and_last(head) for share in self.get_chain(head, head_height if last is None else min(5, max(0, head_height - self.net.CHAIN_LENGTH))): if self.attempt_verify(share): break - if share.hash in self.heads: - bads.add(share.hash) + bads.append(share.hash) else: if last is not None: desired.add(( - self.items[random.choice(list(self.reverse[last]))].peer, + self.items[random.choice(list(self.reverse[last]))].peer_addr, last, max(x.timestamp for x in self.get_chain(head, min(head_height, 5))), min(x.target for x in self.get_chain(head, min(head_height, 5))), )) for bad in bads: assert bad not in self.verified.items - assert bad in self.heads + #assert bad in self.heads + bad_share = self.items[bad] + if bad_share.peer_addr is not None: + bad_peer_addresses.add(bad_share.peer_addr) if p2pool.DEBUG: print "BAD", bad - self.remove(bad) + try: + self.remove(bad) + except NotImplementedError: + pass # try to get at least CHAIN_LENGTH height for each verified head, requesting parents if needed for head in list(self.verified.heads): @@ -458,7 +491,7 @@ class OkayTracker(forest.Tracker): break if head_height < self.net.CHAIN_LENGTH and last_last_hash is not None: desired.add(( - self.items[random.choice(list(self.verified.reverse[last_hash]))].peer, + self.items[random.choice(list(self.verified.reverse[last_hash]))].peer_addr, last_last_hash, max(x.timestamp for x in self.get_chain(head, min(head_height, 5))), min(x.target for x in self.get_chain(head, min(head_height, 5))), @@ -475,7 +508,7 @@ class OkayTracker(forest.Tracker): # decide best verified head decorated_heads = sorted((( self.verified.get_work(self.verified.get_nth_parent_hash(h, min(5, self.verified.get_height(h)))), - #self.items[h].peer is None, + #self.items[h].peer_addr is None, -self.items[h].should_punish_reason(previous_block, bits, self, known_txs)[0], -self.items[h].time_seen, ), h) for h in self.verified.tails.get(best_tail, [])) @@ -493,17 +526,17 @@ class OkayTracker(forest.Tracker): best = best_share.previous_hash timestamp_cutoff = min(int(time.time()), best_share.timestamp) - 3600 - 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 + target_cutoff = int(2**256//(self.net.SHARE_PERIOD*best_tail_score[1] + 1) * 2 + .5) if best_tail_score[1] is not None else 2**256-1 else: timestamp_cutoff = int(time.time()) - 24*60*60 target_cutoff = 2**256-1 if p2pool.DEBUG: 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)) - for peer, hash, ts, targ in desired: - 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 + for peer_addr, hash, ts, targ in desired: + print ' ', None if peer_addr is None else '%s:%i' % peer_addr, format_hash(hash), math.format_dt(time.time() - ts), bitcoin_data.target_to_difficulty(targ), ts >= timestamp_cutoff, targ <= target_cutoff - return best, [(peer, hash) for peer, hash, ts, targ in desired if ts >= timestamp_cutoff], decorated_heads + return best, [(peer_addr, hash) for peer_addr, hash, ts, targ in desired if ts >= timestamp_cutoff], decorated_heads, bad_peer_addresses def score(self, share_hash, block_rel_height_func): # returns approximate lower bound on chain's hashrate in the last self.net.CHAIN_LENGTH*15//16*self.net.SHARE_PERIOD time @@ -517,7 +550,7 @@ class OkayTracker(forest.Tracker): block_height = max(block_rel_height_func(share.header['previous_block']) for share in self.verified.get_chain(end_point, self.net.CHAIN_LENGTH//16)) - return self.net.CHAIN_LENGTH, self.verified.get_delta(share_hash, end_point).work//((0 - block_height + 1)*self.net.PARENT.BLOCK_PERIOD) + return self.net.CHAIN_LENGTH, self.verified.get_delta(share_hash, end_point).work/((0 - block_height + 1)*self.net.PARENT.BLOCK_PERIOD) def get_pool_attempts_per_second(tracker, previous_share_hash, dist, min_work=False, integer=False): assert dist >= 2 @@ -570,20 +603,24 @@ def get_desired_version_counts(tracker, best_share_hash, dist): res[share.desired_version] = res.get(share.desired_version, 0) + bitcoin_data.target_to_average_attempts(share.target) return res -def get_warnings(tracker, best_share, net, bitcoind_warning, bitcoind_work_value): +def get_warnings(tracker, best_share, net, bitcoind_getinfo, bitcoind_work_value): res = [] desired_version_counts = get_desired_version_counts(tracker, best_share, min(net.CHAIN_LENGTH, 60*60//net.SHARE_PERIOD, tracker.get_height(best_share))) majority_desired_version = max(desired_version_counts, key=lambda k: desired_version_counts[k]) - if majority_desired_version > Share.VERSION and desired_version_counts[majority_desired_version] > sum(desired_version_counts.itervalues())/2: + if majority_desired_version > (Share.SUCCESSOR if Share.SUCCESSOR is not None else Share).VOTING_VERSION and desired_version_counts[majority_desired_version] > sum(desired_version_counts.itervalues())/2: res.append('A MAJORITY OF SHARES CONTAIN A VOTE FOR AN UNSUPPORTED SHARE IMPLEMENTATION! (v%i with %i%% support)\n' 'An upgrade is likely necessary. Check http://p2pool.forre.st/ for more information.' % ( majority_desired_version, 100*desired_version_counts[majority_desired_version]/sum(desired_version_counts.itervalues()))) - if bitcoind_warning is not None: - if 'This is a pre-release test build' not in bitcoind_warning: - res.append('(from bitcoind) %s' % (bitcoind_warning,)) + if bitcoind_getinfo['errors'] != '': + if 'This is a pre-release test build' not in bitcoind_getinfo['errors']: + res.append('(from bitcoind) %s' % (bitcoind_getinfo['errors'],)) + + version_warning = getattr(net, 'VERSION_WARNING', lambda v: None)(bitcoind_getinfo['version']) + if version_warning is not None: + res.append(version_warning) if time.time() > bitcoind_work_value['last_update'] + 60: res.append('''LOST CONTACT WITH BITCOIND for %s! Check that it isn't frozen or dead!''' % (math.format_dt(time.time() - bitcoind_work_value['last_update']),)) @@ -596,17 +633,11 @@ def format_hash(x): return '%08x' % (x % 2**32) class ShareStore(object): - def __init__(self, prefix, net): - self.filename = prefix + def __init__(self, prefix, net, share_cb, verified_hash_cb): self.dirname = os.path.dirname(os.path.abspath(prefix)) self.filename = os.path.basename(os.path.abspath(prefix)) self.net = net - self.known = None # will be filename -> set of share hashes, set of verified hashes - self.known_desired = None - - def get_shares(self): - if self.known is not None: - raise AssertionError() + known = {} filenames, next = self.get_filenames_and_next() for filename in filenames: @@ -622,20 +653,21 @@ class ShareStore(object): pass elif type_id == 2: verified_hash = int(data_hex, 16) - yield 'verified_hash', verified_hash + verified_hash_cb(verified_hash) verified_hashes.add(verified_hash) elif type_id == 5: raw_share = share_type.unpack(data_hex.decode('hex')) - if raw_share['type'] in [0, 1, 2, 3, 4, 5, 6, 7, 8]: + if raw_share['type'] < Share.VERSION: continue share = load_share(raw_share, self.net, None) - yield 'share', share + share_cb(share) share_hashes.add(share.hash) else: raise NotImplementedError("share type %i" % (type_id,)) except Exception: log.err(None, "HARMLESS error while reading saved shares, continuing where left off:") - self.known = known + + self.known = known # filename -> (set of share hashes, set of verified hashes) self.known_desired = dict((k, (set(a), set(b))) for k, (a, b) in known.iteritems()) def _add_line(self, line):