Added BITCOIN_POW_FUNC to network definitions
authorForrest Voight <forrest.voight@gmail.com>
Thu, 24 Nov 2011 01:11:16 +0000 (20:11 -0500)
committerForrest Voight <forrest.voight@gmail.com>
Thu, 24 Nov 2011 01:11:16 +0000 (20:11 -0500)
p2pool/bitcoin/data.py
p2pool/bitcoin/networks.py
p2pool/data.py
p2pool/main.py
p2pool/p2p.py

index 3b8bb70..d4643a2 100644 (file)
@@ -105,12 +105,9 @@ class Type(object):
     
     def hash256(self, obj):
         return HashType().unpack(hashlib.sha256(hashlib.sha256(self.pack(obj)).digest()).digest())
-
-    ltc_scrypt = None
+    
     def scrypt(self, obj):
-        # dynamically import ltc_scrypt so you will only get an error on runtime
-        if (not self.ltc_scrypt):
-            self.ltc_scrypt = __import__('ltc_scrypt')
+        import ltc_scrypt
         return HashType().unpack(self.ltc_scrypt.getPoWHash(self.pack(obj)))
 
 class VarIntType(Type):
index 3481975..7f9d358 100644 (file)
@@ -1,5 +1,7 @@
 from twisted.internet import defer
 
+from . import data
+
 
 class BitcoinMainnet(object):
     BITCOIN_P2P_PREFIX = 'f9beb4d9'.decode('hex')
@@ -12,6 +14,7 @@ class BitcoinMainnet(object):
         not (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 50*100000000 >> (height + 1)//210000)
+    BITCOIN_POW_FUNC = data.block_header_type.hash256
     BITCOIN_SYMBOL = 'BTC'
 
 class BitcoinTestnet(object):
@@ -25,6 +28,7 @@ class BitcoinTestnet(object):
         (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 50*100000000 >> (height + 1)//210000)
+    BITCOIN_POW_FUNC = data.block_header_type.hash256
     BITCOIN_SYMBOL = 'tBTC'
 
 
@@ -39,6 +43,7 @@ class NamecoinMainnet(object):
         not (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 50*100000000 >> (height + 1)//210000)
+    BITCOIN_POW_FUNC = data.block_header_type.hash256
     BITCOIN_SYMBOL = 'NMC'
 
 class NamecoinTestnet(object):
@@ -52,6 +57,7 @@ class NamecoinTestnet(object):
         (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 50*100000000 >> (height + 1)//210000)
+    BITCOIN_POW_FUNC = data.block_header_type.hash256
     BITCOIN_SYMBOL = 'tNMC'
 
 
@@ -66,6 +72,7 @@ class IxcoinMainnet(object):
         not (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 96*100000000 >> (height + 1)//210000)
+    BITCOIN_POW_FUNC = data.block_header_type.hash256
     BITCOIN_SYMBOL = 'IXC'
 
 class IxcoinTestnet(object):
@@ -79,6 +86,7 @@ class IxcoinTestnet(object):
         (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 96*100000000 >> (height + 1)//210000)
+    BITCOIN_POW_FUNC = data.block_header_type.hash256
     BITCOIN_SYMBOL = 'tIXC'
 
 
@@ -94,6 +102,7 @@ class I0coinMainnet(object):
         not (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 48*100000000 >> (height + 1)//218750)
+    BITCOIN_POW_FUNC = data.block_header_type.hash256
     BITCOIN_SYMBOL = 'I0C'
 
 class I0coinTestnet(object):
@@ -108,6 +117,7 @@ class I0coinTestnet(object):
         (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 48*100000000 >> (height + 1)//218750)
+    BITCOIN_POW_FUNC = data.block_header_type.hash256
     BITCOIN_SYMBOL = 'tI0C'
 
 
@@ -121,6 +131,7 @@ class SolidcoinMainnet(object):
         not (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 32*100000000 >> (height + 1)//300000)
+    BITCOIN_POW_FUNC = data.block_header_type.hash256
     BITCOIN_SYMBOL = 'SC'
 
 
@@ -134,7 +145,7 @@ class LitecoinMainnet(object):
         not (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 50*100000000 >> (height + 1)//840000)
-    BITCOIN_POW_SCRYPT = True;
+    BITCOIN_POW_FUNC = data.block_header_type.scrypt
     BITCOIN_SYMBOL = 'LTC'
 
 class LitecoinTestnet(object):
@@ -147,5 +158,5 @@ class LitecoinTestnet(object):
         (yield bitcoind.rpc_getinfo())['testnet']
     )))
     BITCOIN_SUBSIDY_FUNC = staticmethod(lambda height: 50*100000000 >> (height + 1)//840000)
-    BITCOIN_POW_SCRYPT = True;
+    BITCOIN_POW_FUNC = data.block_header_type.scrypt
     BITCOIN_SYMBOL = 'tLTC'
index cabea0c..db48338 100644 (file)
@@ -145,15 +145,10 @@ class Share(object):
         
         if len(self.nonce) > 100:
             raise ValueError('nonce too long!')
-
-        # use scrypt for Litecoin
-        if (getattr(net, 'BITCOIN_POW_SCRYPT', False)):
-            self.bitcoin_hash = bitcoin_data.block_header_type.scrypt(header)
-            self.hash = share1a_type.scrypt(self.as_share1a())
-        else:
-            self.bitcoin_hash = bitcoin_data.block_header_type.hash256(header)
-            self.hash = share1a_type.hash256(self.as_share1a())
-
+        
+        self.bitcoin_hash = net.BITCOIN_POW_FUNC(header)
+        self.hash = net.BITCOIN_POW_FUNC(header) # XXX was a bug in litecoin pull
+        
         if self.bitcoin_hash > self.target:
             print 'hash %x' % self.bitcoin_hash
             print 'targ %x' % self.target
index 1bb2eb9..5885b8e 100644 (file)
@@ -481,27 +481,19 @@ def main(args):
                     return False
                 block = dict(header=header, txs=transactions)
                 hash_ = bitcoin.data.block_header_type.hash256(block['header'])
-                pow = hash_;
-
-                # use scrypt for Litecoin
-                if (getattr(args.net, 'BITCOIN_POW_SCRYPT', False)):
-                    pow = bitcoin.data.block_header_type.scrypt(block['header']);
-#                    print 'LTC: hash256 %x' % hash_
-#                    print 'LTC: scrypt  %x' % pow
-#                    print 'LTC: target  %x' % block['header']['target']
-#                    print 'LTC: starget %x' % p2pool.coinbase_type.unpack(transactions[0]['tx_ins'][0]['script'])['share_data']['target']
-
-                if pow <= block['header']['target'] or p2pool_init.DEBUG:
+                pow_hash = args.net.BITCOIN_POW_FUNC(block['header'])
+                
+                if pow_hash <= block['header']['target'] or p2pool_init.DEBUG:
                     if factory.conn.value is not None:
                         factory.conn.value.send_block(block=block)
                     else:
                         print 'No bitcoind connection! Erp!'
-                    if pow <= block['header']['target']:
+                    if pow_hash <= block['header']['target']:
                         print
                         print 'GOT BLOCK! Passing to bitcoind! bitcoin: %x' % (hash_,)
                         print
                 
-                if current_work.value['aux_work'] is not None and pow <= current_work.value['aux_work']['target']:
+                if current_work.value['aux_work'] is not None and pow_hash <= current_work.value['aux_work']['target']:
                     try:
                         aux_pow = dict(
                             merkle_tx=dict(
@@ -525,8 +517,8 @@ def main(args):
                         log.err(None, 'Error while processing merged mining POW:')
                 
                 target = p2pool.coinbase_type.unpack(transactions[0]['tx_ins'][0]['script'])['share_data']['target']
-                if pow > target:
-                    print 'Worker submitted share with hash > target:\nhash  : %x\ntarget: %x' % (pow, target)
+                if pow_hash > target:
+                    print 'Worker submitted share with hash > target:\nhash  : %x\ntarget: %x' % (pow_hash, target)
                     return False
                 share = p2pool.Share.from_block(block, args.net)
                 my_shares.add(share.hash)
index 071ac04..6add502 100644 (file)
@@ -212,12 +212,7 @@ class Protocol(bitcoin_p2p.BaseProtocol):
     def handle_share1as(self, share1as):
         shares = []
         for share1a in share1as:
-            # use scrypt for Litecoin
-            if (getattr(self.node.net, 'BITCOIN_POW_SCRYPT', False)):
-                hash_ = bitcoin_data.block_header_type.scrypt(share1a['header']);
-            else:
-                hash_ = bitcoin_data.block_header_type.hash256(share1a['header'])
-            if hash_ <= share1a['header']['target']:
+            if self.node.net.BITCOIN_POW_FUNC(share1a['header']) <= share1a['header']['target']:
                 print 'Dropping peer %s:%i due to invalid share' % self.addr
                 self.transport.loseConnection()
                 return
@@ -232,12 +227,7 @@ class Protocol(bitcoin_p2p.BaseProtocol):
     def handle_share1bs(self, share1bs):
         shares = []
         for share1b in share1bs:
-            # use scrypt for Litecoin
-            if (getattr(self.node.net, 'BITCOIN_POW_SCRYPT', False)):
-                hash_ = bitcoin_data.block_header_type.scrypt(share1b['header']);
-            else:
-                hash_ = bitcoin_data.block_header_type.hash256(share1b['header'])
-            if not hash_ <= share1b['header']['target']:
+            if not self.node.net.BITCOIN_POW_FUNC(share1a['header']) <= share1b['header']['target']:
                 print 'Dropping peer %s:%i due to invalid share' % self.addr
                 self.transport.loseConnection()
                 return