bitcore-node icon indicating copy to clipboard operation
bitcore-node copied to clipboard

getBlockHashesByTimestamp throwing error

Open RobertBaron opened this issue 9 years ago • 3 comments

The getBlockHashesByTimestamp method is passing an empty 3th paramter to the getBlockHashes causing an error when the function is used.

When calling: http://localhost:3001/insight-api/blocks?limit=5

The following error is returned getblockhashes timestamp Returns array of hashes of blocks within the timestamp range provided. Arguments: 1. high (numeric, required) The newer block timestamp 2. low (numeric, required) The older block timestamp Result: [ "hash" (string) The block hash ] Examples: > bitcoin-cli getblockhashes 1231614698 1231024505 > curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockhashes", "params": [1231614698, 1231024505] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/ . Code:-1

The issue is that we are calling self.client.getBlockHashes with an empty object (options) as a third parameter. Which by definition is not supported by the rpc method. This is what bitcoind-rpc is calling: getBlockHashes [ high, low, {} ]

This was introduced in 4a99870813d03554cf9d939504c16f917e71ef19 If the rpc method itself doesn't support a third parameter, can we remove the argument ? or remove the commit ? Any other suggestion to fix this? I can do a PR for it. Or it is an error on my side?

To reproduce:

bitcore-node create mynode
cd mynode
bitcore-node install insight-ui
bitcore-node install insight-api
bitcore-node start
open http://localhost:3001/insight-api/blocks?limit=5

Pasting the code as easy reference:

/**
 * Will retrieve an array of block hashes within a range of timestamps
 * @param {Number} high - The more recent timestamp in seconds
 * @param {Number} low - The older timestamp in seconds
 * @param {Function} callback
 */
Bitcoin.prototype.getBlockHashesByTimestamp = function(high, low, options, callback) {
  var self = this;
  if (_.isFunction(options)) {
    callback = options;
    options = {};
  }

  self.client.getBlockHashes(high, low, options, function(err, response) {
    if (err) {
      return callback(self._wrapRPCError(err));
    }
    callback(null, response.result);
  });
};

RobertBaron avatar Nov 21 '16 06:11 RobertBaron

Apparently the options argument was added in https://github.com/bitpay/bitcoin/commit/c8458bf963e2855ebd361b9caf82034cbd86ecdd

But for some reason, the node continue throwing the error, so it might be downloading a wrong version or something, will look at that later.

RobertBaron avatar Feb 26 '17 16:02 RobertBaron

+1 Thanks for identifying the problem commit. A proper fix would be greatly appreciated, it took a lot of debugging to identify the problem and find this issue!

tommcintyre avatar May 25 '17 02:05 tommcintyre

Actually, that doesn't seem to be it. This needs fixing on the bitcore side, rather than in the bitcoin patch. Not sure where exactly - I think in bitcore-node, as the extra param is already present when it gets into bitcore-rpc. However, I haven't been able to track it back any further.

Meanwhile, the following bitcoin hack works as a workaround, but obviously not ideal:

diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp
--- a/src/rpcblockchain.cpp
+++ b/src/rpcblockchain.cpp
@@ -277,7 +277,7 @@ UniValue getrawmempool(const UniValue& params, bool fHelp)

 UniValue getblockhashes(const UniValue& params, bool fHelp)
 {
-    if (fHelp || params.size() != 2)
+    if (fHelp || params.size() < 2)  // FIXME: Hack to work around bitcore bug
         throw runtime_error(
             "getblockhashes timestamp\n"
             "\nReturns array of hashes of blocks within the timestamp range provided.\n"

tommcintyre avatar May 25 '17 04:05 tommcintyre