Adjust code to work with unpatched bitcoind
authorLuke Dashjr <luke-jr+git@utopios.org>
Thu, 7 Nov 2013 17:18:24 +0000 (17:18 +0000)
committerLuke Dashjr <luke-jr+git@utopios.org>
Thu, 7 Nov 2013 17:22:38 +0000 (17:22 +0000)
backends/abe/__init__.py
backends/bitcoind/blockchain_processor.py
patch/patch [deleted file]

index 48dd27e..9fd53cb 100644 (file)
@@ -473,8 +473,8 @@ class AbeStore(Datastore.Datastore):
         print_log("get_chunk", index, len(msg))
         return msg
 
-    def get_raw_tx(self, tx_hash, height):
-        postdata = dumps({"method": 'getrawtransaction', 'params': [tx_hash, 0, height], 'id': 'jsonrpc'})
+    def get_raw_tx(self, tx_hash):
+        postdata = dumps({"method": 'getrawtransaction', 'params': [tx_hash, 0], 'id': 'jsonrpc'})
         respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
         r = loads(respdata)
         if r['error'] is not None:
@@ -717,7 +717,7 @@ class BlockchainProcessor(Processor):
             try:
                 tx_hash = params[0]
                 height = params[1]
-                result = self.store.get_raw_tx(tx_hash, height)
+                result = self.store.get_raw_tx(tx_hash)
             except Exception, e:
                 error = str(e) + ': ' + tx_hash
                 print_log("error:", error)
index cb64178..6fb36fb 100644 (file)
@@ -217,7 +217,7 @@ class BlockchainProcessor(Processor):
 
     def get_mempool_transaction(self, txid):
         try:
-            raw_tx = self.bitcoind('getrawtransaction', [txid, 0, -1])
+            raw_tx = self.bitcoind('getrawtransaction', [txid, 0])
         except:
             return None
 
@@ -614,8 +614,7 @@ class BlockchainProcessor(Processor):
         elif method == 'blockchain.transaction.get':
             try:
                 tx_hash = params[0]
-                height = params[1]
-                result = self.bitcoind('getrawtransaction', [tx_hash, 0, height])
+                result = self.bitcoind('getrawtransaction', [tx_hash, 0])
             except BaseException, e:
                 error = str(e) + ': ' + tx_hash
                 print_log("error:", error)
@@ -636,6 +635,35 @@ class BlockchainProcessor(Processor):
         if addr not in self.watched_addresses:
             self.watched_addresses.append(addr)
 
+    def getfullblock(block_hash):
+        block = self.bitcoind('getblock', [block_hash])
+
+        rawtxreq = []
+        i = 0
+        for txid in block['tx']:
+            rawtxreq.append({
+                "method": "getrawtransaction",
+                "params": [txid],
+                "id": i,
+            })
+            i += 1
+
+        postdata = dumps(rawtxreq)
+        try:
+            respdata = urllib.urlopen(self.bitcoind_url, postdata).read()
+        except:
+            traceback.print_exc(file=sys.stdout)
+            self.shared.stop()
+
+        r = loads(respdata)
+        rawtxdata = []
+        for ir in r:
+            if r['error'] is not None:
+                raise BaseException(r['error'])
+            rawtxdata.append(r['result'])
+        block['tx'] = rawtxdata
+        return block
+
     def catch_up(self, sync=True):
         t1 = time.time()
 
@@ -651,7 +679,7 @@ class BlockchainProcessor(Processor):
             # not done..
             self.up_to_date = False
             next_block_hash = self.bitcoind('getblockhash', [self.height + 1])
-            next_block = self.bitcoind('getblock', [next_block_hash, 1])
+            next_block = self.getfullblock(next_block_hash)
 
             # fixme: this is unsafe, if we revert when the undo info is not yet written
             revert = (random.randint(1, 100) == 1) if self.is_test else False
@@ -670,7 +698,7 @@ class BlockchainProcessor(Processor):
 
             else:
                 # revert current block
-                block = self.bitcoind('getblock', [self.last_hash, 1])
+                block = self.getfullblock(self.last_hash)
                 print_log("blockchain reorg", self.height, block.get('previousblockhash'), self.last_hash)
                 self.import_block(block, self.last_hash, self.height, sync, revert=True)
                 self.pop_header()
diff --git a/patch/patch b/patch/patch
deleted file mode 100644 (file)
index 774363a..0000000
+++ /dev/null
@@ -1,146 +0,0 @@
-diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp
-index 21e37c7..1ce91c9 100644
---- a/src/bitcoinrpc.cpp
-+++ b/src/bitcoinrpc.cpp
-@@ -1147,6 +1147,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
-     if (strMethod == "listreceivedbyaccount"  && n > 1) ConvertTo<bool>(params[1]);
-     if (strMethod == "getbalance"             && n > 1) ConvertTo<boost::int64_t>(params[1]);
-     if (strMethod == "getblockhash"           && n > 0) ConvertTo<boost::int64_t>(params[0]);
-+    if (strMethod == "getblock"               && n > 1) ConvertTo<boost::int64_t>(params[1]);
-     if (strMethod == "move"                   && n > 2) ConvertTo<double>(params[2]);
-     if (strMethod == "move"                   && n > 3) ConvertTo<boost::int64_t>(params[3]);
-     if (strMethod == "sendfrom"               && n > 2) ConvertTo<double>(params[2]);
-@@ -1167,6 +1168,7 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
-     if (strMethod == "listunspent"            && n > 1) ConvertTo<boost::int64_t>(params[1]);
-     if (strMethod == "listunspent"            && n > 2) ConvertTo<Array>(params[2]);
-     if (strMethod == "getrawtransaction"      && n > 1) ConvertTo<boost::int64_t>(params[1]);
-+    if (strMethod == "getrawtransaction"      && n > 2) ConvertTo<boost::int64_t>(params[2]);
-     if (strMethod == "createrawtransaction"   && n > 0) ConvertTo<Array>(params[0]);
-     if (strMethod == "createrawtransaction"   && n > 1) ConvertTo<Object>(params[1]);
-     if (strMethod == "signrawtransaction"     && n > 1) ConvertTo<Array>(params[1], true);
-diff --git a/src/main.cpp b/src/main.cpp
-index 43bd5dd..b38ce88 100644
---- a/src/main.cpp
-+++ b/src/main.cpp
-@@ -925,7 +925,7 @@ bool CWalletTx::AcceptWalletTransaction(bool fCheckInputs)
- // Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock
--bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
-+bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow, int height)
- {
-     CBlockIndex *pindexSlow = NULL;
-     {
-@@ -940,8 +940,8 @@ bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock
-         }
-         if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
--            int nHeight = -1;
--            {
-+          int nHeight = height;
-+          if(nHeight == -1) {
-                 CCoinsViewCache &view = *pcoinsTip;
-                 CCoins coins;
-                 if (view.GetCoins(hash, coins))
-diff --git a/src/main.h b/src/main.h
-index 8327141..fa27788 100644
---- a/src/main.h
-+++ b/src/main.h
-@@ -156,7 +156,7 @@ bool IsInitialBlockDownload();
- /** Format a string that describes several potential problems detected by the core */
- std::string GetWarnings(std::string strFor);
- /** Retrieve a transaction (from memory pool, or from disk, if possible) */
--bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, bool fAllowSlow = false);
-+bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, bool fAllowSlow = false, int height = -1);
- /** Connect/disconnect blocks until pindexNew is the new tip of the active block chain */
- bool SetBestChain(CBlockIndex* pindexNew);
- /** Find the best known block, and make it the tip of the block chain */
-diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp
-index 3fde463..e60b71e 100644
---- a/src/rpcblockchain.cpp
-+++ b/src/rpcblockchain.cpp
-@@ -41,7 +41,7 @@ double GetDifficulty(const CBlockIndex* blockindex)
- }
--Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex)
-+Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool is_raw=false)
- {
-     Object result;
-     result.push_back(Pair("hash", block.GetHash().GetHex()));
-@@ -53,8 +53,16 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex)
-     result.push_back(Pair("version", block.nVersion));
-     result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
-     Array txs;
--    BOOST_FOREACH(const CTransaction&tx, block.vtx)
--        txs.push_back(tx.GetHash().GetHex());
-+    BOOST_FOREACH(const CTransaction&tx, block.vtx) {
-+      if(is_raw){
-+      CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
-+        ssTx << tx;
-+        string strHex = HexStr(ssTx.begin(), ssTx.end());
-+        txs.push_back(strHex);
-+      }
-+      else txs.push_back(tx.GetHash().GetHex());
-+    }
-+
-     result.push_back(Pair("tx", txs));
-     result.push_back(Pair("time", (boost::int64_t)block.GetBlockTime()));
-     result.push_back(Pair("nonce", (boost::uint64_t)block.nNonce));
-@@ -141,7 +149,7 @@ Value getblockhash(const Array& params, bool fHelp)
- Value getblock(const Array& params, bool fHelp)
- {
--    if (fHelp || params.size() != 1)
-+    if (fHelp || params.size() < 1)
-         throw runtime_error(
-             "getblock <hash>\n"
-             "Returns details of a block with given block-hash.");
-@@ -149,6 +157,10 @@ Value getblock(const Array& params, bool fHelp)
-     std::string strHash = params[0].get_str();
-     uint256 hash(strHash);
-+    bool is_raw = false;
-+    if (params.size() == 2)
-+        is_raw = params[1].get_int() != 0;
-+
-     if (mapBlockIndex.count(hash) == 0)
-         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
-@@ -156,7 +168,7 @@ Value getblock(const Array& params, bool fHelp)
-     CBlockIndex* pblockindex = mapBlockIndex[hash];
-     block.ReadFromDisk(pblockindex, true);
--    return blockToJSON(block, pblockindex);
-+    return blockToJSON(block, pblockindex, is_raw);
- }
- Value gettxoutsetinfo(const Array& params, bool fHelp)
-diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp
-index e82f4ad..624675f 100644
---- a/src/rpcrawtransaction.cpp
-+++ b/src/rpcrawtransaction.cpp
-@@ -134,7 +134,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
- Value getrawtransaction(const Array& params, bool fHelp)
- {
--    if (fHelp || params.size() < 1 || params.size() > 2)
-+    if (fHelp || params.size() < 1 || params.size() > 3)
-         throw runtime_error(
-             "getrawtransaction <txid> [verbose=0]\n"
-             "If verbose=0, returns a string that is\n"
-@@ -148,9 +148,13 @@ Value getrawtransaction(const Array& params, bool fHelp)
-     if (params.size() > 1)
-         fVerbose = (params[1].get_int() != 0);
-+    int height = -1;
-+    if (params.size() > 2)
-+        height = params[2].get_int();
-+
-     CTransaction tx;
-     uint256 hashBlock = 0;
--    if (!GetTransaction(hash, tx, hashBlock, true))
-+    if (!GetTransaction(hash, tx, hashBlock, true, height))
-         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");
-     CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);