bitpay / insight-api

The bitcoin blockchain API powering Insight
https://github.com/bitpay/insight
590 stars 1.05k forks source link

Mismatch of block hash from Insight console to network #136

Closed dmpeterson closed 8 years ago

dmpeterson commented 10 years ago

Can someone please help me find out how to fix a mismatch of hashes with the Insight-API?

When running the Insight-API on a X11 blockchain I see for example the following block in the console.

[p2p_sync] Handle block: 39e18126b7a58073010dcaf016d2821faec13a56656cf751fe76dd09dfa6591d

However the hash for this block is actually as shown below.

000005f0587be758385ed4b981e284ff8875df1953f845b8a4a8091efd0241ff

I can query this block correctly in the API.

http://explorer.wampum.org:3000/api/block/000005f0587be758385ed4b981e284ff8875df1953f845b8a4a8091efd0241ff

Which returns.

{"hash":"000005f0587be758385ed4b981e284ff8875df1953f845b8a4a8091efd0241ff","confirmations":2,"size":188,"height":17166,"version":2,"merkleroot":"9fb0ef1b925e073e4a30414d289ab5befd7bcc562a140c2389435cd6180d8d68","tx":["9fb0ef1b925e073e4a30414d289ab5befd7bcc562a140c2389435cd6180d8d68"],"time":1406214883,"nonce":5347180,"bits":"1e0a2684","difficulty":0.00038483,"previousblockhash":"0000056fbab24affb7ac640c4ce2ab08a6eea441031549d8f65fb631c6719a43","nextblockhash":"000008980433c0462db86891f6c003d4b7378a4916d88fb9e28759d6426ef155","reward":50,"isMainChain":true}

However this mismatch of hashes causes some trouble for the Insight explorer.

/root/insight/node_modules/insight-bitcore-api/app/controllers/blocks.js:63 if (!block.info) { ^ TypeError: Cannot read property 'info' of undefined at /root/insight/node_modules/insight-bitcore-api/app/controllers/blocks.js:63:15 at /root/insight/node_modules/insight-bitcore-api/lib/BlockDb.js:280:30 at /root/insight/node_modules/insight-bitcore-api/lib/Rpc.js:88:40 at IncomingMessage. (/root/insight/node_modules/insight-bitcore-api/node_modules/bitcore/lib/RpcClient.js:189:7) at IncomingMessage.emit (events.js:117:20) at _stream_readable.js:929:16 at process._tickCallback (node.js:419:13)

It seems that it is trying to lookup the incorrect hash and cannot find it resulting in an error.

maraoz commented 10 years ago

The hashes in the network handler and the API should be the same. Is X11 an altcoin?

On Thu, Jul 24, 2014 at 12:20 PM, dmpeterson notifications@github.com wrote:

Can someone please help me find out how to fix a mismatch of hashes with the Insight-API? When running the Insight-API on a X11 blockchain I see for example the following block in the console. [p2p_sync] Handle block: 39e18126b7a58073010dcaf016d2821faec13a56656cf751fe76dd09dfa6591d However the hash for this block is actually as shown below. 000005f0587be758385ed4b981e284ff8875df1953f845b8a4a8091efd0241ff I can query this block correctly in the API. http://explorer.wampum.org:3000/api/block/000005f0587be758385ed4b981e284ff8875df1953f845b8a4a8091efd0241ff Which returns. {"hash":"000005f0587be758385ed4b981e284ff8875df1953f845b8a4a8091efd0241ff","confirmations":2,"size":188,"height":17166,"version":2,"merkleroot":"9fb0ef1b925e073e4a30414d289ab5befd7bcc562a140c2389435cd6180d8d68","tx":["9fb0ef1b925e073e4a30414d289ab5befd7bcc562a140c2389435cd6180d8d68"],"time":1406214883,"nonce":5347180,"bits":"1e0a2684","difficulty":0.00038483,"previousblockhash":"0000056fbab24affb7ac640c4ce2ab08a6eea441031549d8f65fb631c6719a43","nextblockhash":"000008980433c0462db86891f6c003d4b7378a4916d88fb9e28759d6426ef155","reward":50,"isMainChain":true} However this mismatch of hashes causes some trouble for the Insight explorer. /root/insight/node_modules/insight-bitcore-api/app/controllers/blocks.js:63 if (!block.info) { ^ TypeError: Cannot read property 'info' of undefined at /root/insight/node_modules/insight-bitcore-api/app/controllers/blocks.js:63:15 at /root/insight/node_modules/insight-bitcore-api/lib/BlockDb.js:280:30 at /root/insight/node_modules/insight-bitcore-api/lib/Rpc.js:88:40 at IncomingMessage. (/root/insight/node_modules/insight-bitcore-api/node_modules/bitcore/lib/RpcClient.js:189:7) at IncomingMessage.emit (events.js:117:20) at _stream_readable.js:929:16 at process._tickCallback (node.js:419:13)

It seems that it is trying to lookup the incorrect hash and cannot find it resulting in an error.

Reply to this email directly or view it on GitHub: https://github.com/bitpay/insight-api/issues/136

dmpeterson commented 10 years ago

X11 is a hashing solution used in several alt coins.

dmpeterson commented 10 years ago

It runs the hash through this before displaying it to the console. Somehow this process results in a different hash from what is in the blockchain.

var blockHash = bitcoreUtil.formatHashFull(block.calcHash());

calcHash looks like this...

calcHash=function(){return this.hash=u.twoSha256(this.getBuffer()),this.hash}

I wonder if this is the problem. Scrypt coins leave the getHash function alone where X11 coins override the getHash function with X11 which is unnecessary.

dmpeterson commented 10 years ago

I believe that this is the cause of the problem which effects X11 based coins but not Scrypt coins.

In Bitcoin and Litecoin this is the GetHash function in main.h. Different algo for PoW is employed in the GetPoWHash function.

uint256 GetHash() const
{
    return Hash(BEGIN(nVersion), END(nNonce));
}

In X11 coins this was done to GetHash as well as GetPoWHash.

uint256 GetHash() const
{
    return Hash9(BEGIN(nVersion), END(nNonce));
}

I believe that if X11 left GetHash alone then there would not be this compatibility issue.

OmarxGx commented 10 years ago

Were you able to find a solution to allow compatibility with x11 coins?

dmpeterson commented 10 years ago

No, ended up rebuilding with new algo called neoscrypt.

On Wed, Sep 24, 2014 at 12:17 PM, OmarxGx notifications@github.com wrote:

Were you able to find a solution to allow compatibility with x11 coins?

— Reply to this email directly or view it on GitHub https://github.com/bitpay/insight-api/issues/136#issuecomment-56695727.

In Christ,

Derek Peterson Eph 6:12 www.ONEWAY.com http://www.oneway.com/ www.ONLYWAY.com http://www.onlyway.com/

genecyber commented 9 years ago

So the problem here is simple, insight was made for Bitcoin and is a sha256 coin. The error described amongst a few threads all with differing coin hashing algo's likely all suffer the same problem. This line here in "insight-bitcore-api/lib/peer sync.js".
var blockHash = bitcoreUtil.formatHashFull(block.calcHash())

Looking deeper we find a hard coded reference to sha256 hashing. Which of course won't return a valid block hash on say a sCrypt block.

My quick fix to remedy this temporarily is to look for malformed hashed (no leading zeros) and fall back to RPC which naturally works because the altcoin daemons are using the correct algo to parse and store blocks.

See the patch here https://gist.github.com/genecyber/46a835038221d8e30cfb

Note: the block reader that's reads from disk also has a similar issue but hey... Teach a man to fish and all...

braydonf commented 8 years ago

Outdated now.

However there could be larger topic here of supporting other block hashes in bitcore-lib: https://github.com/bitpay/bitcore/issues/1387