try RPC submitblock and then fall back to submitting via getblocktemplate instead...
authorForrest Voight <forrest@forre.st>
Thu, 27 Jun 2013 17:19:14 +0000 (13:19 -0400)
committerForrest Voight <forrest@forre.st>
Thu, 27 Jun 2013 18:11:56 +0000 (14:11 -0400)
deals with Litecoin finally merging in RPC submitblock

p2pool/bitcoin/helper.py
p2pool/test/test_node.py

index 25792f8..f9c459f 100644 (file)
@@ -70,12 +70,11 @@ def submit_block_p2p(block, factory, net):
 @defer.inlineCallbacks
 def submit_block_rpc(block, ignore_failure, bitcoind, bitcoind_work, net):
     if bitcoind_work.value['use_getblocktemplate']:
-        if 'lite' in net.NAME:
-            result = yield bitcoind.rpc_getblocktemplate(dict(mode='submit', data=bitcoin_data.block_type.pack(block).encode('hex')))
-            success = result is None
-        else:
+        try:
             result = yield bitcoind.rpc_submitblock(bitcoin_data.block_type.pack(block).encode('hex'))
-            success = result is None
+        except jsonrpc.Error_for_code(-32601): # Method not found, for older litecoin versions
+            result = yield bitcoind.rpc_getblocktemplate(dict(mode='submit', data=bitcoin_data.block_type.pack(block).encode('hex')))
+        success = result is None
     else:
         result = yield bitcoind.rpc_getmemorypool(bitcoin_data.block_type.pack(block).encode('hex'))
         success = result
index 16bdd69..f1f9bea 100644 (file)
@@ -4,6 +4,7 @@ import random
 import tempfile
 
 from twisted.internet import defer, reactor
+from twisted.python import failure
 from twisted.trial import unittest
 from twisted.web import client, resource, server
 
@@ -53,8 +54,15 @@ class bitcoind(object): # can be used as p2p factory, p2p protocol, or rpc jsonr
         block_hash = int(block_hash_hex, 16)
         return dict(height=self.blocks.index(block_hash))
     
-    def rpc_getmemorypool(self, result=None):
-        if result is not None:
+    def __getattr__(self, name):
+        if name.startswith('rpc_'):
+            return lambda *args, **kwargs: failure.Failure(jsonrpc.Error_for_code(-32601)('Method not found'))
+    
+    def rpc_getblocktemplate(self, param):
+        if param['mode'] == 'template':
+            pass
+        elif param['mode'] == 'submit':
+            result = param['data']
             block = bitcoin_data.block_type.unpack(result.decode('hex'))
             if sum(tx_out['value'] for tx_out in block['txs'][0]['tx_outs']) != sum(tx['tx_outs'][0]['value'] for tx in block['txs'][1:]) + 5000000000:
                 print 'invalid fee'
@@ -67,6 +75,8 @@ class bitcoind(object): # can be used as p2p factory, p2p protocol, or rpc jsonr
             self.headers[header_hash] = block['header']
             reactor.callLater(0, self.new_block.happened)
             return True
+        else:
+            raise jsonrpc.Error_for_code(-1)('invalid request')
         
         txs = []
         for i in xrange(100):