added pool rate display
authorforrest <forrest@470744a7-cac9-478e-843e-5ec1b25c69e8>
Mon, 18 Jul 2011 10:39:44 +0000 (10:39 +0000)
committerforrest <forrest@470744a7-cac9-478e-843e-5ec1b25c69e8>
Mon, 18 Jul 2011 10:39:44 +0000 (10:39 +0000)
git-svn-id: svn://forre.st/p2pool@1405 470744a7-cac9-478e-843e-5ec1b25c69e8

p2pool/data.py
p2pool/main.py

index 1cf0809..12f988a 100644 (file)
@@ -230,25 +230,27 @@ class Share2(object):
     def flag_shared(self):
         self.shared = True
 
-def generate_transaction(tracker, previous_share_hash, new_script, subsidy, nonce, block_target, net):    
+def get_pool_attempts_per_second(tracker, previous_share_hash, net):
     chain = list(itertools.islice(tracker.get_chain_to_root(previous_share_hash), net.TARGET_LOOKBEHIND))
-    if len(chain) < net.TARGET_LOOKBEHIND:
+    attempts = sum(bitcoin_data.target_to_average_attempts(share.target2) for share in chain[:-1])
+    time = chain[0].timestamp - chain[-1].timestamp
+    if time == 0:
+        time = 1
+    return attempts//time
+
+def generate_transaction(tracker, previous_share_hash, new_script, subsidy, nonce, block_target, net):
+    height, last = tracker.get_height_and_last(previous_share_hash)
+    if height < net.TARGET_LOOKBEHIND:
         target2 = bitcoin_data.FloatingIntegerType().truncate_to(2**256//2**20 - 1)
     else:
-        previous_share = tracker.shares[previous_share_hash] if previous_share_hash is not None else None
-        attempts = sum(bitcoin_data.target_to_average_attempts(share.target2) for share in chain[:-1])
-        time = chain[0].timestamp - chain[-1].timestamp
-        if time == 0:
-            time = 1
-        attempts_per_second = attempts//time
+        attempts_per_second = get_pool_attempts_per_second(tracker, previous_share_hash, net)
         pre_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1
+        previous_share = tracker.shares[previous_share_hash] if previous_share_hash is not None else None
         pre_target2 = math.clip(pre_target, (previous_share.target2*9//10, previous_share.target2*11//10))
         pre_target3 = math.clip(pre_target2, (0, 2**256//2**20 - 1))
         target2 = bitcoin_data.FloatingIntegerType().truncate_to(pre_target3)
-        print attempts_per_second//1000, 'KHASH'
-        #print 'TARGET', 2**256//target2, 2**256/pre_target
-        #print 'ATT', bitcoin_data.target_to_average_attempts(target2)//1000
-    
+        if __debug__:
+            print 'generate_transaction. pool rate:', attempts_per_second//1000, 'KHASH'
     
     attempts_to_block = bitcoin_data.target_to_average_attempts(block_target)
     max_weight = net.SPREAD * attempts_to_block
@@ -377,11 +379,9 @@ class OkayTracker(bitcoin_data.Tracker):
         # decide best verified head
         scores = sorted(self.verified.heads, key=lambda h: self.score(h, ht))
         
-        print "---"
-        for a in scores:
-            print time.time()
-            print '%x'%a, self.score(a, ht)
-        print "---"
+        if __debug__:
+            for a in scores:
+                print '   ', '%x'%a, self.score(a, ht)
         
         for share_hash in scores[:-5]:
             if self.shares[share_hash].time_seen > time.time() - 30:
index 70cf88d..fa88569 100644 (file)
@@ -273,7 +273,7 @@ def main(args):
                 block_target=state['target'],
                 net=args.net,
             )
-            print 'Generating!', 2**256//p2pool.coinbase_type.unpack(generate_tx['tx_ins'][0]['script'])['share_data']['target2']//1000000
+            print 'Generating!'
             #print 'Target: %x' % (p2pool.coinbase_type.unpack(generate_tx['tx_ins'][0]['script'])['share_data']['target2'],)
             #, have', shares.count(my_script) - 2, 'share(s) in the current chain. Fee:', sum(tx.value_in - tx.value_out for tx in extra_txs)/100000000
             transactions = [generate_tx] + [tx.tx for tx in extra_txs]
@@ -410,7 +410,21 @@ def main(args):
         
         while True:
             yield deferral.sleep(1)
-            yield set_real_work()
+            try:
+                yield set_real_work()
+            except:
+                log.err()
+            try:
+                if current_work.value['best_share_hash'] is not None:
+                    height, last = tracker.get_height_and_last(current_work.value['best_share_hash'])
+                    if height > 5:
+                        weights, total_weight = tracker.get_cumulative_weights(current_work.value['best_share_hash'], min(height, 1000), 2**100)
+                        print 'Pool rate: %i mhash/s Contribution: %.02f%%' % (
+                            p2pool.get_pool_attempts_per_second(tracker, current_work.value['best_share_hash'], args.net)//1000000,
+                            weights.get(my_script, 0)/total_weight,
+                        )
+            except:
+                log.err()
     except:
         print
         print 'Fatal error:'