added separate tracking of stale types to graphs
[p2pool.git] / p2pool / data.py
index 2655f77..f615a08 100644 (file)
@@ -33,27 +33,23 @@ def check_hash_link(hash_link, data, const_ending=''):
 
 # shares
 
-# type:
-# 2: share1a
-# 3: share1b
-
 share_type = pack.ComposedType([
     ('type', pack.VarIntType()),
     ('contents', pack.VarStrType()),
 ])
 
 def load_share(share, net, peer):
-    if share['type'] in [0, 1]:
+    if share['type'] in [0, 1, 2, 3]:
         from p2pool import p2p
         raise p2p.PeerMisbehavingError('sent an obsolete share')
-    elif share['type'] == 2:
+    elif share['type'] == 4:
         return Share(net, peer, other_txs=None, **Share.share1a_type.unpack(share['contents']))
-    elif share['type'] == 3:
+    elif share['type'] == 5:
         share1b = Share.share1b_type.unpack(share['contents'])
         return Share(net, peer, merkle_link=bitcoin_data.calculate_merkle_link([0] + [bitcoin_data.hash256(bitcoin_data.tx_type.pack(x)) for x in share1b['other_txs']], 0), **share1b)
-    elif share['type'] == 4:
+    elif share['type'] == 6:
         return NewShare(net, peer, other_txs=None, **NewShare.share1a_type.unpack(share['contents']))
-    elif share['type'] == 5:
+    elif share['type'] == 7:
         share1b = NewShare.share1b_type.unpack(share['contents'])
         return NewShare(net, peer, merkle_link=bitcoin_data.calculate_merkle_link([0] + [bitcoin_data.hash256(bitcoin_data.tx_type.pack(x)) for x in share1b['other_txs']], 0), **share1b)
     else:
@@ -77,194 +73,7 @@ class Share(object):
         ('pubkey_hash', pack.IntType(160)),
         ('subsidy', pack.IntType(64)),
         ('donation', pack.IntType(16)),
-        ('stale_info', pack.IntType(8)), # 0 nothing, 253 orphan, 254 doa
-    ])
-    
-    share_info_type = pack.ComposedType([
-        ('share_data', share_data_type),
-        ('max_bits', bitcoin_data.FloatingIntegerType()),
-        ('bits', bitcoin_data.FloatingIntegerType()),
-        ('timestamp', pack.IntType(32)),
-    ])
-    
-    share1a_type = pack.ComposedType([
-        ('min_header', small_block_header_type),
-        ('share_info', share_info_type),
-        ('hash_link', hash_link_type),
-        ('merkle_link', pack.ComposedType([
-            ('branch', pack.ListType(pack.IntType(256))),
-            ('index', pack.IntType(0)), # it will always be 0
-        ])),
-    ])
-    
-    share1b_type = pack.ComposedType([
-        ('min_header', small_block_header_type),
-        ('share_info', share_info_type),
-        ('hash_link', hash_link_type),
-        ('other_txs', pack.ListType(bitcoin_data.tx_type)),
-    ])
-    
-    ref_type = pack.ComposedType([
-        ('identifier', pack.FixedStrType(64//8)),
-        ('share_info', share_info_type),
-    ])
-    
-    gentx_before_refhash = pack.VarStrType().pack(DONATION_SCRIPT) + pack.IntType(64).pack(0) + pack.VarStrType().pack('\x20' + pack.IntType(256).pack(0))[:2]
-    
-    @classmethod
-    def generate_transaction(cls, tracker, share_data, block_target, desired_timestamp, desired_target, net):
-        previous_share = tracker.shares[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'])
-        assert height >= net.REAL_CHAIN_LENGTH or last is None
-        if height < net.TARGET_LOOKBEHIND:
-            pre_target3 = net.MAX_TARGET
-        else:
-            attempts_per_second = get_pool_attempts_per_second(tracker, share_data['previous_share_hash'], net.TARGET_LOOKBEHIND, min_work=True, integer=True)
-            pre_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1 if attempts_per_second else 2**256-1
-            pre_target2 = math.clip(pre_target, (previous_share.max_target*9//10, previous_share.max_target*11//10))
-            pre_target3 = math.clip(pre_target2, (0, 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),
-            True,
-        )
-        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,
-            max_bits=max_bits,
-            bits=bits,
-            timestamp=math.clip(desired_timestamp, (
-                (previous_share.timestamp + net.SHARE_PERIOD) - (net.SHARE_PERIOD - 1), # = previous_share.timestamp + 1
-                (previous_share.timestamp + net.SHARE_PERIOD) + (net.SHARE_PERIOD - 1),
-            )) if previous_share is not None else desired_timestamp,
-        )
-        
-        return share_info, dict(
-            version=1,
-            tx_ins=[dict(
-                previous_output=None,
-                sequence=None,
-                script=share_data['coinbase'].ljust(2, '\x00'),
-            )],
-            tx_outs=[dict(value=amounts[script], script=script) for script in dests if amounts[script]] + [dict(
-                value=0,
-                script='\x20' + pack.IntType(256).pack(bitcoin_data.hash256(cls.ref_type.pack(dict(
-                    identifier=net.IDENTIFIER,
-                    share_info=share_info,
-                )))),
-            )],
-            lock_time=0,
-        )
-    
-    __slots__ = 'net min_header share_info hash_link merkle_link other_txs hash share_data max_target target timestamp previous_hash new_script desired_version gentx_hash header pow_hash header_hash time_seen peer'.split(' ')
-    
-    def __init__(self, net, peer, min_header, share_info, hash_link, merkle_link, other_txs):
-        if len(share_info['share_data']['coinbase']) > 100:
-            raise ValueError('''coinbase too large! %i bytes''' % (len(self.share_data['coinbase']),))
-        
-        if len(merkle_link['branch']) > 16:
-            raise ValueError('merkle_branch too long!')
-        
-        if p2pool.DEBUG and other_txs is not None and bitcoin_data.calculate_merkle_link([0] + [bitcoin_data.hash256(bitcoin_data.tx_type.pack(x)) for x in other_txs], 0) != merkle_link:
-            raise ValueError('merkle_link and other_txs do not match')
-        
-        assert not hash_link['extra_data'], repr(hash_link['extra_data'])
-        
-        self.net = net
-        self.peer = peer
-        self.min_header = min_header
-        self.share_info = share_info
-        self.hash_link = hash_link
-        self.merkle_link = merkle_link
-        self.other_txs = other_txs
-        
-        self.share_data = self.share_info['share_data']
-        self.max_target = self.share_info['max_bits'].target
-        self.target = self.share_info['bits'].target
-        self.timestamp = self.share_info['timestamp']
-        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 = 0
-        
-        if self.timestamp > net.SWITCH_TIME:
-            from p2pool import p2p
-            raise p2p.PeerMisbehavingError('peer sent an old-style share with a timestamp after the switch time')
-        
-        self.gentx_hash = check_hash_link(
-            hash_link,
-            pack.IntType(256).pack(bitcoin_data.hash256(self.ref_type.pack(dict(
-                identifier=net.IDENTIFIER,
-                share_info=share_info,
-            )))) + pack.IntType(32).pack(0),
-            self.gentx_before_refhash,
-        )
-        merkle_root = bitcoin_data.check_merkle_link(self.gentx_hash, merkle_link)
-        self.header = dict(min_header, merkle_root=merkle_root)
-        self.pow_hash = net.PARENT.POW_FUNC(bitcoin_data.block_header_type.pack(self.header))
-        self.header_hash = bitcoin_data.hash256(bitcoin_data.block_header_type.pack(self.header))
-        
-        if self.pow_hash > self.target:
-            raise p2p.PeerMisbehavingError('share PoW invalid')
-        
-        if other_txs is not None and not self.pow_hash <= self.header['bits'].target:
-            raise ValueError('other_txs provided when not a block solution')
-        if other_txs is None and self.pow_hash <= self.header['bits'].target:
-            raise ValueError('other_txs not provided when a block solution')
-        
-        self.hash = bitcoin_data.hash256(share_type.pack(self.as_share()))
-        
-        # XXX eww
-        self.time_seen = time.time()
-    
-    def __repr__(self):
-        return '<Share %s>' % (' '.join('%s=%r' % (k, getattr(self, k)) for k in self.__slots__),)
-    
-    def check(self, tracker):
-        share_info, gentx = self.generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.net)
-        if share_info != self.share_info:
-            raise ValueError('share difficulty invalid')
-        if bitcoin_data.hash256(bitcoin_data.tx_type.pack(gentx)) != self.gentx_hash:
-            raise ValueError('''gentx doesn't match hash_link''')
-    
-    def as_share(self):
-        if not self.pow_hash <= self.header['bits'].target: # share1a
-            return dict(type=2, contents=self.share1a_type.pack(dict(min_header=self.min_header, share_info=self.share_info, hash_link=self.hash_link, merkle_link=self.merkle_link)))
-        else: # share1b
-            return dict(type=3, contents=self.share1b_type.pack(dict(min_header=self.min_header, share_info=self.share_info, hash_link=self.hash_link, other_txs=self.other_txs)))
-    
-    def as_block(self, tracker):
-        if self.other_txs is None:
-            raise ValueError('share does not contain all txs')
-        
-        share_info, gentx = self.generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.net)
-        assert share_info == self.share_info
-        
-        return dict(header=self.header, txs=[gentx] + self.other_txs)
-
-class NewShare(Share):
-    share_data_type = pack.ComposedType([
-        ('previous_share_hash', pack.PossiblyNoneType(0, pack.IntType(256))),
-        ('coinbase', pack.VarStrType()),
-        ('nonce', pack.IntType(32)),
-        ('pubkey_hash', pack.IntType(160)),
-        ('subsidy', pack.IntType(64)),
-        ('donation', pack.IntType(16)),
-        ('stale_info', pack.IntType(8)), # 0 nothing, 253 orphan, 254 doa
+        ('stale_info', pack.EnumType(pack.IntType(8), dict((k, {0: None, 253: 'orphan', 254: 'doa'}.get(k, 'unk%i' % (k,))) for k in xrange(256)))),
         ('desired_version', pack.VarIntType()),
     ])
     
@@ -277,7 +86,7 @@ class NewShare(Share):
     ])
     
     share_common_type = pack.ComposedType([
-        ('min_header', Share.small_block_header_type),
+        ('min_header', small_block_header_type),
         ('share_info', share_info_type),
         ('ref_merkle_link', pack.ComposedType([
             ('branch', pack.ListType(pack.IntType(256))),
@@ -302,6 +111,8 @@ class NewShare(Share):
         ('share_info', share_info_type),
     ])
     
+    gentx_before_refhash = pack.VarStrType().pack(DONATION_SCRIPT) + pack.IntType(64).pack(0) + pack.VarStrType().pack('\x20' + pack.IntType(256).pack(0))[:2]
+    
     @classmethod
     def generate_transaction(cls, tracker, share_data, block_target, desired_timestamp, desired_target, ref_merkle_link, net):
         previous_share = tracker.shares[share_data['previous_share_hash']] if share_data['previous_share_hash'] is not None else None
@@ -321,7 +132,6 @@ class NewShare(Share):
         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),
-            False,
         )
         assert total_weight == sum(weights.itervalues()) + donation_weight, (total_weight, sum(weights.itervalues()) + donation_weight)
         
@@ -353,7 +163,7 @@ class NewShare(Share):
                 sequence=None,
                 script=share_data['coinbase'].ljust(2, '\x00'),
             )],
-            tx_outs=[dict(value=amounts[script], script=script) for script in dests if amounts[script]] + [dict(
+            tx_outs=[dict(value=amounts[script], script=script) for script in dests if amounts[script] or script == DONATION_SCRIPT] + [dict(
                 value=0,
                 script='\x20' + cls.get_ref_hash(net, share_info, ref_merkle_link),
             )],
@@ -398,10 +208,6 @@ class NewShare(Share):
         self.new_script = bitcoin_data.pubkey_hash_to_script2(self.share_data['pubkey_hash'])
         self.desired_version = self.share_data['desired_version']
         
-        if self.timestamp < net.SWITCH_TIME:
-            from p2pool import p2p
-            raise p2p.PeerMisbehavingError('peer sent a new-style share with a timestamp before the switch time')
-        
         self.gentx_hash = check_hash_link(
             self.hash_link,
             self.get_ref_hash(net, self.share_info, common['ref_merkle_link']) + pack.IntType(32).pack(0),
@@ -413,6 +219,7 @@ class NewShare(Share):
         self.hash = self.header_hash = bitcoin_data.hash256(bitcoin_data.block_header_type.pack(self.header))
         
         if self.pow_hash > self.target:
+            from p2pool import p2p
             raise p2p.PeerMisbehavingError('share PoW invalid')
         
         if other_txs is not None and not self.pow_hash <= self.header['bits'].target:
@@ -423,6 +230,9 @@ class NewShare(Share):
         # XXX eww
         self.time_seen = time.time()
     
+    def __repr__(self):
+        return '<Share %s>' % (' '.join('%s=%r' % (k, getattr(self, k)) for k in self.__slots__),)
+    
     def as_share(self):
         if not self.pow_hash <= self.header['bits'].target: # share1a
             return dict(type=4, contents=self.share1a_type.pack(dict(common=self.common, merkle_link=self.merkle_link)))
@@ -430,6 +240,11 @@ class NewShare(Share):
             return dict(type=5, contents=self.share1b_type.pack(dict(common=self.common, other_txs=self.other_txs)))
     
     def check(self, tracker):
+        if self.share_data['previous_share_hash'] is not None:
+            previous_share = tracker.shares[self.share_data['previous_share_hash']]
+            if isinstance(previous_share, NewShare):
+                from p2pool import p2p
+                raise p2p.PeerMisbehavingError('''Share can't follow NewShare''')
         share_info, gentx = self.generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.common['ref_merkle_link'], self.net)
         if share_info != self.share_info:
             raise ValueError('share_info invalid')
@@ -442,6 +257,39 @@ class NewShare(Share):
             raise ValueError('share does not contain all txs')
         return dict(header=self.header, txs=[self.check(tracker)] + self.other_txs)
 
+class NewShare(Share):
+    def __init__(self, *args, **kwargs):
+        Share.__init__(self, *args, **kwargs)
+        self.hash = bitcoin_data.hash256(share_type.pack(self.as_share()))
+    
+    def check(self, tracker):
+        if self.share_data['previous_share_hash'] is not None:
+            previous_share = tracker.shares[self.share_data['previous_share_hash']]
+            if isinstance(previous_share, Share):
+                if tracker.get_height(previous_share.hash) < self.net.CHAIN_LENGTH:
+                    from p2pool import p2p
+                    raise p2p.PeerMisbehavingError('NewShare without enough history')
+                else:
+                    # Share -> NewShare only valid if 85% of hashes in [self.net.CHAIN_LENGTH*9//10, self.net.CHAIN_LENGTH] for new version
+                    counts = get_desired_version_counts(tracker,
+                        tracker.get_nth_parent_hash(previous_share.hash, self.net.CHAIN_LENGTH*9//10), self.net.CHAIN_LENGTH//10)
+                    if counts.get(2, 0) < sum(counts.itervalues())*85//100:
+                        from p2pool import p2p
+                        raise p2p.PeerMisbehavingError('NewShare without enough hash power upgraded')
+        share_info, gentx = self.generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.common['ref_merkle_link'], self.net)
+        if share_info != self.share_info:
+            raise ValueError('share_info invalid')
+        if bitcoin_data.hash256(bitcoin_data.tx_type.pack(gentx)) != self.gentx_hash:
+            raise ValueError('''gentx doesn't match hash_link''')
+        return gentx # only used by as_block
+    
+    def as_share(self):
+        if not self.pow_hash <= self.header['bits'].target: # share1a
+            return dict(type=6, contents=self.share1a_type.pack(dict(common=self.common, merkle_link=self.merkle_link)))
+        else: # share1b
+            return dict(type=7, contents=self.share1b_type.pack(dict(common=self.common, other_txs=self.other_txs)))
+
+
 class WeightsSkipList(forest.TrackerSkipList):
     # share_count, weights, total_weight
     
@@ -454,22 +302,19 @@ class WeightsSkipList(forest.TrackerSkipList):
     def combine_deltas(self, (share_count1, weights1, total_weight1, total_donation_weight1), (share_count2, weights2, total_weight2, total_donation_weight2)):
         return share_count1 + share_count2, math.add_dicts(weights1, weights2), total_weight1 + total_weight2, total_donation_weight1 + total_donation_weight2
     
-    def initial_solution(self, start, (max_shares, desired_weight, broken_mode)):
+    def initial_solution(self, start, (max_shares, desired_weight)):
         assert desired_weight % 65535 == 0, divmod(desired_weight, 65535)
         return 0, None, 0, 0
     
-    def apply_delta(self, (share_count1, weights_list, total_weight1, total_donation_weight1), (share_count2, weights2, total_weight2, total_donation_weight2), (max_shares, desired_weight, broken_mode)):
+    def apply_delta(self, (share_count1, weights_list, total_weight1, total_donation_weight1), (share_count2, weights2, total_weight2, total_donation_weight2), (max_shares, desired_weight)):
         if total_weight1 + total_weight2 > desired_weight and share_count2 == 1:
             assert (desired_weight - total_weight1) % 65535 == 0
             script, = weights2.iterkeys()
-            if broken_mode:
-                new_weights = dict(script=(desired_weight - total_weight1)//65535*weights2[script]//(total_weight2//65535))
-            else:
-                new_weights = {script: (desired_weight - total_weight1)//65535*weights2[script]//(total_weight2//65535)}
+            new_weights = {script: (desired_weight - total_weight1)//65535*weights2[script]//(total_weight2//65535)}
             return share_count1 + share_count2, (weights_list, new_weights), desired_weight, total_donation_weight1 + (desired_weight - total_weight1)//65535*total_donation_weight2//(total_weight2//65535)
         return share_count1 + share_count2, (weights_list, weights2), total_weight1 + total_weight2, total_donation_weight1 + total_donation_weight2
     
-    def judge(self, (share_count, weights_list, total_weight, total_donation_weight), (max_shares, desired_weight, broken_mode)):
+    def judge(self, (share_count, weights_list, total_weight, total_donation_weight), (max_shares, desired_weight)):
         if share_count > max_shares or total_weight > desired_weight:
             return 1
         elif share_count == max_shares or total_weight == desired_weight:
@@ -477,7 +322,7 @@ class WeightsSkipList(forest.TrackerSkipList):
         else:
             return -1
     
-    def finalize(self, (share_count, weights_list, total_weight, total_donation_weight), (max_shares, desired_weight, broken_mode)):
+    def finalize(self, (share_count, weights_list, total_weight, total_donation_weight), (max_shares, desired_weight)):
         assert share_count <= max_shares and total_weight <= desired_weight
         assert share_count == max_shares or total_weight == desired_weight
         return math.add_dicts(*math.flatten_linked_list(weights_list)), total_weight, total_donation_weight
@@ -493,11 +338,9 @@ class OkayTracker(forest.Tracker):
             work=lambda share: bitcoin_data.target_to_average_attempts(share.target),
             my_count=lambda share: 1 if share.hash in my_share_hashes else 0,
             my_doa_count=lambda share: 1 if share.hash in my_doa_share_hashes else 0,
-            my_orphan_announce_count=lambda share: 1 if share.hash in my_share_hashes and share.share_data['stale_info'] == 253 else 0,
-            my_dead_announce_count=lambda share: 1 if share.hash in my_share_hashes and share.share_data['stale_info'] == 254 else 0,
-        )))
-        self.verified.get_nth_parent_hash = self.get_nth_parent_hash # self is a superset of self.verified
-        
+            my_orphan_announce_count=lambda share: 1 if share.hash in my_share_hashes and share.share_data['stale_info'] == 'orphan' else 0,
+            my_dead_announce_count=lambda share: 1 if share.hash in my_share_hashes and share.share_data['stale_info'] == 'doa' else 0,
+        )), subset_of=self)
         self.get_cumulative_weights = WeightsSkipList(self)
     
     def attempt_verify(self, share):
@@ -607,9 +450,9 @@ class OkayTracker(forest.Tracker):
                 if not to_remove:
                     break
                 for share_hash in to_remove:
-                    self.remove(share_hash)
                     if share_hash in self.verified.shares:
                         self.verified.remove(share_hash)
+                    self.remove(share_hash)
                 #print "_________", to_remove
         
         # drop tails
@@ -631,9 +474,9 @@ class OkayTracker(forest.Tracker):
                 if self.shares[aftertail].previous_hash not in self.tails:
                     print "erk", aftertail, self.shares[aftertail].previous_hash
                     continue
-                self.remove(aftertail)
                 if aftertail in self.verified.shares:
                     self.verified.remove(aftertail)
+                self.remove(aftertail)
             #end = time.time()
             #print "removed! %i %f" % (len(to_remove), (end - start)/len(to_remove))
         
@@ -675,7 +518,7 @@ def get_pool_attempts_per_second(tracker, previous_share_hash, dist, min_work=Fa
     assert dist >= 2
     near = tracker.shares[previous_share_hash]
     far = tracker.shares[tracker.get_nth_parent_hash(previous_share_hash, dist - 1)]
-    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
+    attempts = tracker.get_delta(near.hash, far.hash).work if not min_work else tracker.get_delta(near.hash, far.hash).min_work
     time = near.timestamp - far.timestamp
     if time <= 0:
         time = 1
@@ -684,11 +527,23 @@ def get_pool_attempts_per_second(tracker, previous_share_hash, dist, min_work=Fa
     return attempts/time
 
 def get_average_stale_prop(tracker, share_hash, lookbehind):
-    stales = sum(1 for share in tracker.get_chain(share_hash, lookbehind) if share.share_data['stale_info'] != 0)
+    stales = sum(1 for share in tracker.get_chain(share_hash, lookbehind) if share.share_data['stale_info'] is not None)
     return stales/(lookbehind + stales)
 
+def get_stale_counts(tracker, share_hash, lookbehind, rates=False):
+    res = {}
+    for share in tracker.get_chain(share_hash, lookbehind - 1):
+        res['good'] = res.get('good', 0) + bitcoin_data.target_to_average_attempts(share.target)
+        s = share.share_data['stale_info']
+        if s is not None:
+            res[s] = res.get(s, 0) + bitcoin_data.target_to_average_attempts(share.target)
+    if rates:
+        dt = tracker.shares[share_hash].timestamp - tracker.shares[tracker.get_nth_parent_hash(share_hash, lookbehind - 1)].timestamp
+        res = dict((k, v/dt) for k, v in res.iteritems())
+    return res
+
 def get_expected_payouts(tracker, best_share_hash, block_target, subsidy, net):
-    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), False)
+    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))
     res = dict((script, subsidy*weight//total_weight) for script, weight in weights.iteritems())
     res[DONATION_SCRIPT] = res.get(DONATION_SCRIPT, 0) + subsidy - sum(res.itervalues())
     return res
@@ -699,6 +554,19 @@ 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, current_work, net):
+    res = []
+    
+    desired_version_counts = get_desired_version_counts(tracker, current_work.value['best_share_hash'],
+        min(60*60//net.SHARE_PERIOD, tracker.get_height(current_work.value['best_share_hash'])))
+    majority_desired_version = max(desired_version_counts, key=lambda k: desired_version_counts[k])
+    if majority_desired_version not in [0, 1, 2] 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())))
+    
+    return res
+
 def format_hash(x):
     if x is None:
         return 'xxxxxxxx'
@@ -735,7 +603,7 @@ class ShareStore(object):
                             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]:
+                            if raw_share['type'] in [0, 1, 2, 3]:
                                 continue
                             share = load_share(raw_share, self.net, None)
                             yield 'share', share