Add Litecoin network.
[p2pool.git] / p2pool / data.py
index 0c5bc5f..161edc3 100644 (file)
@@ -8,7 +8,7 @@ import os
 from twisted.python import log
 
 import p2pool
-from p2pool import skiplists, namecoin, ixcoin
+from p2pool import skiplists, namecoin, ixcoin, i0coin, solidcoin, litecoin
 from p2pool.bitcoin import data as bitcoin_data, script
 from p2pool.util import memoize, expiring_dict, math
 
@@ -189,7 +189,7 @@ class Share(object):
         gentx = share_info_to_gentx(self.share_info, self.header['target'], tracker, net)
         
         if len(gentx['tx_ins'][0]['script']) > 100:
-            raise ValueError('''coinbase too large!''')
+            raise ValueError('''coinbase too large! %i bytes''' % (len(gentx['tx_ins'][0]['script']),))
         
         if check_merkle_branch(gentx, self.merkle_branch) != self.header['merkle_root']:
             raise ValueError('''gentx doesn't match header via merkle_branch''')
@@ -225,8 +225,8 @@ def generate_transaction(tracker, previous_share_hash, new_script, subsidy, nonc
         target = bitcoin_data.FloatingInteger.from_target_upper_bound(net.MAX_TARGET)
     else:
         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_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1
         pre_target2 = math.clip(pre_target, (previous_share.target*9//10, previous_share.target*11//10))
         pre_target3 = math.clip(pre_target2, (0, net.MAX_TARGET))
         target = bitcoin_data.FloatingInteger.from_target_upper_bound(pre_target3)
@@ -350,33 +350,33 @@ class OkayTracker(bitcoin_data.Tracker):
         # decide best verified head
         scores = sorted(self.verified.tails.get(best_tail, []), key=lambda h: (
             self.verified.get_work(self.verified.get_nth_parent_hash(h, min(5, self.verified.get_height(h)))),
-            ht.get_min_height(self.verified.shares[h].previous_block),
             #self.verified.shares[h].peer is None,
+            ht.get_min_height(self.verified.shares[h].previous_block),
             -self.verified.shares[h].time_seen
         ))
         
         
         if p2pool.DEBUG:
-            print len(self.verified.tails.get(best_tail, [])), '\\/\\/\\/\\/\\/'
+            print len(self.verified.tails), "chain tails and", len(self.verified.tails.get(best_tail, [])), 'chain heads. Top 10 tails:'
             if len(scores) > 10:
                 print '    ...'
             for h in scores[-10:]:
                 print '   ', format_hash(h), format_hash(self.verified.shares[h].previous_hash), (
                     self.verified.get_work(self.verified.get_nth_parent_hash(h, min(5, self.verified.get_height(h)))),
-                    ht.get_min_height(self.verified.shares[h].previous_block),
                     self.verified.shares[h].peer is None,
+                    ht.get_min_height(self.verified.shares[h].previous_block),
                     -self.verified.shares[h].time_seen
                 )
         
         # eat away at heads
         if scores:
-            while True:
+            for i in xrange(1000):
                 to_remove = set()
                 for share_hash, tail in self.heads.iteritems():
                     if share_hash in scores[-5:]:
                         #print 1
                         continue
-                    if self.shares[share_hash].time_seen > time.time() - 30:
+                    if self.shares[share_hash].time_seen > time.time() - 300:
                         #print 2
                         continue
                     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
@@ -392,7 +392,7 @@ class OkayTracker(bitcoin_data.Tracker):
                 #print "_________", to_remove
         
         # drop tails
-        while True:
+        for i in xrange(1000):
             to_remove = set()
             for tail, heads in self.tails.iteritems():
                 if min(self.get_height(head) for head in heads) < 2*self.net.CHAIN_LENGTH + 10:
@@ -420,7 +420,7 @@ class OkayTracker(bitcoin_data.Tracker):
         
         if best is not None:
             best_share = self.verified.shares[best]
-            if ht.get_min_height(best_share.header['previous_block']) < ht.get_min_height(previous_block) and best_share.bitcoin_hash != previous_block: # and best_share.peer is not None:
+            if ht.get_min_height(best_share.header['previous_block']) < ht.get_min_height(previous_block) and best_share.bitcoin_hash != previous_block and best_share.peer is not None:
                 if p2pool.DEBUG:
                     print 'Stale detected! %x < %x' % (best_share.header['previous_block'], previous_block)
                 best = best_share.previous_hash
@@ -514,22 +514,21 @@ class ShareStore(object):
         return [self.filename + str(suffix) for suffix in suffixes], self.filename + str(suffixes[-1] + 1) if suffixes else self.filename + str(0)
     
     def forget_share(self, share_hash):
-        to_remove = set()
         for filename, (share_hashes, verified_hashes) in self.known.iteritems():
             if share_hash in share_hashes:
                 share_hashes.remove(share_hash)
-            if not share_hashes and not verified_hashes:
-                to_remove.add(filename)
-        for filename in to_remove:
-            self.known.pop(filename)
-            os.remove(filename)
-            print "REMOVED", filename
+        self.check_remove()
     
     def forget_verified_share(self, share_hash):
-        to_remove = set()
         for filename, (share_hashes, verified_hashes) in self.known.iteritems():
             if share_hash in verified_hashes:
                 verified_hashes.remove(share_hash)
+        self.check_remove()
+    
+    def check_remove(self):
+        to_remove = set()
+        for filename, (share_hashes, verified_hashes) in self.known.iteritems():
+            #print filename, len(share_hashes) + len(verified_hashes)
             if not share_hashes and not verified_hashes:
                 to_remove.add(filename)
         for filename in to_remove:
@@ -538,7 +537,7 @@ class ShareStore(object):
             print "REMOVED", filename
 
 class Mainnet(bitcoin_data.Mainnet):
-    SHARE_PERIOD = 5 # seconds
+    SHARE_PERIOD = 10 # seconds
     CHAIN_LENGTH = 24*60*60//5 # shares
     TARGET_LOOKBEHIND = 200 # shares
     SPREAD = 3 # blocks
@@ -549,6 +548,7 @@ class Mainnet(bitcoin_data.Mainnet):
     P2P_PORT = 9333
     MAX_TARGET = 2**256//2**32 - 1
     PERSIST = True
+    WORKER_PORT = 9332
 
 class Testnet(bitcoin_data.Testnet):
     SHARE_PERIOD = 1 # seconds
@@ -560,8 +560,9 @@ class Testnet(bitcoin_data.Testnet):
     PREFIX = '3f6057a15036f441'.decode('hex')
     NAME = 'bitcoin_testnet'
     P2P_PORT = 19333
-    MAX_TARGET = 2**256//2**20 - 1
+    MAX_TARGET = 2**256//2**32 - 1
     PERSIST = False
+    WORKER_PORT = 19332
 
 class NamecoinMainnet(namecoin.NamecoinMainnet):
     SHARE_PERIOD = 10 # seconds
@@ -575,6 +576,7 @@ class NamecoinMainnet(namecoin.NamecoinMainnet):
     P2P_PORT = 9334
     MAX_TARGET = 2**256//2**32 - 1
     PERSIST = True
+    WORKER_PORT = 9331
 
 class NamecoinTestnet(namecoin.NamecoinTestnet):
     SHARE_PERIOD = 1 # seconds
@@ -588,6 +590,7 @@ class NamecoinTestnet(namecoin.NamecoinTestnet):
     P2P_PORT = 19334
     MAX_TARGET = 2**256//2**20 - 1
     PERSIST = False
+    WORKER_PORT = 19331
 
 class IxcoinMainnet(ixcoin.IxcoinMainnet):
     SHARE_PERIOD = 10 # seconds
@@ -601,6 +604,7 @@ class IxcoinMainnet(ixcoin.IxcoinMainnet):
     P2P_PORT = 9335
     MAX_TARGET = 2**256//2**32 - 1
     PERSIST = True
+    WORKER_PORT = 9330
 
 class IxcoinTestnet(ixcoin.IxcoinTestnet):
     SHARE_PERIOD = 1 # seconds
@@ -614,3 +618,76 @@ class IxcoinTestnet(ixcoin.IxcoinTestnet):
     P2P_PORT = 19335
     MAX_TARGET = 2**256//2**20 - 1
     PERSIST = False
+    WORKER_PORT = 19330
+
+class I0coinMainnet(i0coin.I0coinMainnet):
+    SHARE_PERIOD = 10 # seconds
+    CHAIN_LENGTH = 24*60*60//10 # shares
+    TARGET_LOOKBEHIND = 3600//10 # shares
+    SPREAD = 3 # blocks
+    SCRIPT = '410403ad3dee8ab3d8a9ce5dd2abfbe7364ccd9413df1d279bf1a207849310465b0956e5904b1155ecd17574778f9949589ebfd4fb33ce837c241474a225cf08d85dac'.decode('hex')
+    IDENTIFIER = 'b32e3f10c2ff221b'.decode('hex')
+    PREFIX = '6155537ed977a3b5'.decode('hex')
+    NAME = 'i0coin'
+    P2P_PORT = 9336
+    MAX_TARGET = 2**256//2**32 - 1
+    PERSIST = False
+    WORKER_PORT = 9329
+
+class I0coinTestnet(i0coin.I0coinTestnet):
+    SHARE_PERIOD = 1 # seconds
+    CHAIN_LENGTH = 24*60*60//5 # shares
+    TARGET_LOOKBEHIND = 200 # shares
+    SPREAD = 3 # blocks
+    SCRIPT = '410403ad3dee8ab3d8a9ce5dd2abfbe7364ccd9413df1d279bf1a207849310465b0956e5904b1155ecd17574778f9949589ebfd4fb33ce837c241474a225cf08d85dac'.decode('hex')
+    IDENTIFIER = '7712c1a8181b5f2e'.decode('hex')
+    PREFIX = '792d2e7d770fbe68'.decode('hex')
+    NAME = 'i0coin_testnet'
+    P2P_PORT = 19336
+    MAX_TARGET = 2**256//2**20 - 1
+    PERSIST = False
+    WORKER_PORT = 19329
+
+class SolidcoinMainnet(solidcoin.SolidcoinMainnet):
+    SHARE_PERIOD = 10
+    CHAIN_LENGTH = 24*60*60//10 # shares
+    TARGET_LOOKBEHIND = 3600//10 # shares
+    SPREAD = 3 # blocks
+    SCRIPT = bitcoin_data.pubkey_hash_to_script2(bitcoin_data.address_to_pubkey_hash('sMKZ1yxHETxPYKh4Z2anWnwZDJZU7ztroy', solidcoin.SolidcoinMainnet))
+    IDENTIFIER = '9cc9c421cca258cd'.decode('hex')
+    PREFIX = 'c059125b8070f00a'.decode('hex')
+    NAME = 'solidcoin'
+    P2P_PORT = 9337
+    MAX_TARGET = 2**256//2**32 - 1
+    PERSIST = True
+    WORKER_PORT = 9328
+
+class LitecoinMainnet(litecoin.LitecoinMainnet):
+    SHARE_PERIOD = 10 # seconds
+    CHAIN_LENGTH = 24*60*60//5 # shares
+    TARGET_LOOKBEHIND = 200 # shares
+    SPREAD = 12 # blocks
+    SCRIPT = '410403ad3dee8ab3d8a9ce5dd2abfbe7364ccd9413df1d279bf1a207849310465b0956e5904b1155ecd17574778f9949589ebfd4fb33ce837c241474a225cf08d85dac'.decode('hex')
+    IDENTIFIER = 'e037d5b8c6923410'.decode('hex')
+    PREFIX = '7208c1a53ef629b0'.decode('hex')
+    NAME = 'litecoin'
+    P2P_PORT = 9338
+    MAX_TARGET = 2**256//2**32 - 1
+    PERSIST = True
+    WORKER_PORT = 9327
+
+class LitecoinTestnet(litecoin.LitecoinTestnet):
+    SHARE_PERIOD = 1 # seconds
+    CHAIN_LENGTH = 24*60*60//5 # shares
+    TARGET_LOOKBEHIND = 200 # shares
+    SPREAD = 12 # blocks
+    SCRIPT = '410403ad3dee8ab3d8a9ce5dd2abfbe7364ccd9413df1d279bf1a207849310465b0956e5904b1155ecd17574778f9949589ebfd4fb33ce837c241474a225cf08d85dac'.decode('hex')
+    IDENTIFIER = 'cca5e24ec6408b1e'.decode('hex')
+    PREFIX = 'ad9614f6466a39cf'.decode('hex')
+    NAME = 'litecoin_testnet'
+    P2P_PORT = 19338
+    MAX_TARGET = 2**256//2**20 - 1
+    PERSIST = False
+    WORKER_PORT = 19327
+
+nets = dict((net.NAME, net) for net in set([Mainnet, Testnet, NamecoinMainnet, NamecoinTestnet, IxcoinMainnet, IxcoinTestnet, I0coinMainnet, I0coinTestnet, SolidcoinMainnet, LitecoinMainnet, LitecoinTestnet]))