made share punishing message not go to stderr and show not only in debug mode
[p2pool.git] / p2pool / data.py
index ba1cb79..ef77a18 100644 (file)
@@ -275,12 +275,12 @@ class Share(object):
         return []
     
     def should_punish_reason(self, previous_block, bits, tracker, known_txs):
-        if self.pow_hash <= self.header['bits'].target:
-            return -1, 'block solution'
-        
         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.pow_hash <= self.header['bits'].target:
+            return -1, 'block solution'
+        
         return False, None
     
     def as_block(self, tracker, known_txs):
@@ -388,7 +388,7 @@ class NewShare(object):
                     break
             else:
                 if known_txs is not None:
-                    this_size = len(bitcoin_data.tx_type.pack(known_txs[tx_hash]))
+                    this_size = bitcoin_data.tx_type.packed_size(known_txs[tx_hash])
                     if new_transaction_size + this_size > 50000: # only allow 50 kB of new txns/share
                         break
                     new_transaction_size += this_size
@@ -546,23 +546,23 @@ class NewShare(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.pow_hash <= self.header['bits'].target:
-            return -1, 'block solution'
-        
         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.pow_hash <= self.header['bits'].target:
+            return -1, 'block solution'
+        
         other_txs = self._get_other_txs(tracker, known_txs)
         if other_txs is None:
             return True, 'not all txs present'
-        
-        all_txs_size = sum(len(bitcoin_data.tx_type.pack(tx)) for tx in other_txs)
-        if all_txs_size > 1000000:
-            return True, 'txs over block size limit'
-        
-        new_txs_size = sum(len(bitcoin_data.tx_type.pack(known_txs[tx_hash])) for tx_hash in self.share_info['new_transaction_hashes'])
-        if new_txs_size > 50000:
-            return True, 'new txs over limit'
+        else:
+            all_txs_size = sum(bitcoin_data.tx_type.packed_size(tx) for tx in other_txs)
+            if all_txs_size > 1000000:
+                return True, 'txs over block size limit'
+            
+            new_txs_size = sum(bitcoin_data.tx_type.packed_size(known_txs[tx_hash]) for tx_hash in self.share_info['new_transaction_hashes'])
+            if new_txs_size > 50000:
+                return True, 'new txs over limit'
         
         return False, None
     
@@ -713,9 +713,8 @@ class OkayTracker(forest.Tracker):
         if best is not None:
             best_share = self.items[best]
             punish, punish_reason = best_share.should_punish_reason(previous_block, bits, self, known_txs)
-            if punish:
-                if p2pool.DEBUG:
-                    print >>sys.stderr, 'Punishing share for %r! Jumping from %s to %s!' % (punish_reason, format_hash(best), format_hash(best_share.previous_hash))
+            if punish > 0:
+                print 'Punishing share for %r! Jumping from %s to %s!' % (punish_reason, format_hash(best), format_hash(best_share.previous_hash))
                 best = best_share.previous_hash
             
             timestamp_cutoff = min(int(time.time()), best_share.timestamp) - 3600