osagga / bitcore-node-cash

Extensible full node using Bitcore
https://bitcore.io/
Other
9 stars 2 forks source link

incorrect Merkle Root #4

Open hxzqlh opened 6 years ago

hxzqlh commented 6 years ago

I set up your bitcore-node for bch, and I found that the Merkle Root info of block is not corresponded to official bch block explorer. Take block height at 1 for exmple, which block hash is 00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048:

in your verison, this block's merkle root is 982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e,

root@iZwz924dpebwff0lbha9n9Z:/mnt# curl -s localhost:32810/api/block/00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048 | json_pp
{
   "isMainChain" : true,
   "version" : 1,
   "difficulty" : 563857632138,
   "confirmations" : 136291,
   "previousblockhash" : "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
   "size" : 215,
   "merkleroot" : "982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e",
   "poolInfo" : {},
   "reward" : 50,
   "tx" : [
      "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
   ],
   "chainwork" : "0000000000000000000000000000000000000000000000000000000200020002",
   "nonce" : 2573394689,
   "hash" : "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
   "time" : 1231469665,
   "nextblockhash" : "000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd",
   "bits" : 486604799,
   "height" : 1
}

while official block explorer, the merkle root it is: 0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098

see: https://bch.btc.com/00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048

osagga commented 6 years ago

That's interesting, thanks for spotting this out. It seems that this problem is caused by something in the bitcore internals, when parsing the Merkle Root (it flips the order of the bytes if you noticed).

It also seems that the problem happens in bitcore's own BCH explorer https://bch-insight.bitpay.com/#/block/00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048

I will try to find the problem whenever I get a chance, thanks for pointing it out. Please feel free to provide a fix if you figured it out.

osagga commented 6 years ago

I just found a way to fix the problem from the API side, as it seems that bitcore have to store all the values reversed in the db, so my fix was to simply reverse the Merkle root before returning it to the API request.

This fix should be live in osagga/insight-api/tree/fixing-bch-address-format, just update the insight-api or rebuild the bitcore-node-cash (make sure to get the latest version from the master branch)

osagga commented 6 years ago

@hxzqlh Hey, can I get an update on this issue? Let it me if it's fixed now

hxzqlh commented 6 years ago

@osagga Yes,it is fixed, thanks. I use master branch of bitcore-node-cash

I found more things about /addr api , it seems supporting BTC legacy address(eg:12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX) and BCH new address(eg:qqgekzvw96vq5g57zwdfa5q6g609rrn0ycp33uc325), but things not happend like expected: a. legacy address format return txs of the addr(this is correct):

root@iZwz924dpebwff0lbha9n9Z:~# curl -s localhost:32812/api/addr/12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX | json_pp
{
   "unconfirmedBalance" : 0,
   "txApperances" : 0,
   "totalSentSat" : 0,
   "totalSent" : 0,
   "balanceSat" : 0,
   "totalReceived" : 0,
   "addrStr" : "12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX",
   "unconfirmedTxApperances" : 0,
   "unconfirmedBalanceSat" : 0,
   "totalReceivedSat" : 0,
   "balance" : 0,
   "transactions" : [
      "1b0235867691784492804fce22a18c2337c97d58a1c63fc275fe26137596d671",
      "91b21c74b6b9cc168fc9a079d0aadf4acc90706196a7278cd674611a9104122e",
      "24087a08309ea5796ef139e65f13ce10db1e4465057b665b9d5102a640aac6be",
      "a3a6f902a51a2cbebede144e48a88c05e608c2cce28024041a5b9874013a1e2a",
      "cea36d008badf5c7866894b191d3239de9582d89b6b452b596f1f1b76347f8cb",
      "065ef6b1463f552f675622a5d1fd2c08d6324b4402049f68e767a719e2049e8d",
      "a66dddd42f9f2491d3c336ce5527d45cc5c2163aaed3158f81dc054447f447a2",
      "ffd901679de65d4398de90cefe68d2c3ef073c41f7e8dbec2fb5cd75fe71dfe7",
      "d658ab87cc053b8dbcfd4aa2717fd23cc3edfe90ec75351fadd6a0f7993b461d",
      "36ebe0ca3237002acb12e1474a3859bde0ac84b419ec4ae373e63363ebef731c",
      "fd87f9adebb17f4ebb1673da76ff48ad29e64b7afa02fda0f2c14e43d220fe24",
      "dfdf0b375a987f17056e5e919ee6eadd87dad36c09c4016d4a03cea15e5c05e3",
      "cb2679bfd0a557b2dc0d8a6116822f3fcbe281ca3f3e18d3855aa7ea378fa373",
      "d6be34ccf6edddc3cf69842dce99fe503bf632ba2c2adb0f95c63f6706ae0c52",
      "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
   ]
}

b. bch format addr returns no txs(maybe, this is incorrect):

root@iZwz924dpebwff0lbha9n9Z:~# curl -s localhost:32812/api/addr/qqgekzvw96vq5g57zwdfa5q6g609rrn0ycp33uc325 | json_pp
{
   "addrStr" : "qqgekzvw96vq5g57zwdfa5q6g609rrn0ycp33uc325",
   "unconfirmedBalanceSat" : 0,
   "unconfirmedTxApperances" : 0,
   "totalSent" : 0,
   "balanceSat" : 0,
   "totalSentSat" : 0,
   "balance" : 0,
   "totalReceivedSat" : 0,
   "txApperances" : 0,
   "totalReceived" : 0,
   "unconfirmedBalance" : 0
}

by the way, the insight-ui cannot switch address format(currently, only shows bch addr format), while this site https://bitcoincash.blockexplorer.com/ supports showing the 2 version of addr.

osagga commented 6 years ago

@hxzqlh So in the recent updates that I made to the api and bcoin, only the new BCH address is supported, so in your example, try adding bitcoincash: before the address, it should show the correct transactions.

osagga@osagga:~bitcore-node-cash$ curl -s localhost:3001/api/addr/bitcoincash:qqgekzvw96vq5g57zwdfa5q6g609rrn0ycp33uc325 | json_pp 
{
   "totalSentSat" : 0,
   "txApperances" : 1,
   "unconfirmedBalance" : 0,
   "totalReceivedSat" : 5000000000,
   "unconfirmedBalanceSat" : 0,
   "totalSent" : 0,
   "totalReceived" : 50,
   "unconfirmedTxApperances" : 0,
   "balanceSat" : 5000000000,
   "balance" : 50,
   "transactions" : [
      "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
   ],
   "addrStr" : "bitcoincash:qqgekzvw96vq5g57zwdfa5q6g609rrn0ycp33uc325"
}
osagga@osagga:~bitcore-node-cash$ curl -s localhost:3001/api/addr/12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX | json_pp 
{
   "totalSent" : 0,
   "txApperances" : 0,
   "totalReceivedSat" : 0,
   "balance" : 0,
   "totalSentSat" : 0,
   "balanceSat" : 0,
   "addrStr" : "12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX",
   "unconfirmedBalanceSat" : 0,
   "unconfirmedBalance" : 0,
   "totalReceived" : 0,
   "unconfirmedTxApperances" : 0
}

Also for now, I think only BCH new address would work, I don't know how did your api call using the legacy BTC address returned the list of transactions (if you look at the log above).

I haven't modified the ui that much, I'm mainly interested in the api, feel free to fix or improve anything you see, or report issues.

osagga commented 6 years ago

@hxzqlh I just added the feature to the insight-api (my fork of it), now you should be able to search for an address using any format (bch with a prefix `bitcoincash:XX1 or without a prefix, or even legacy BTC addresses).

osagga@osagga:~/bitcore-node-cash$ curl -s localhost:3001/api/addr/12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX | json_pp 
{
   "totalReceivedSat" : 5002000000,
   "totalSent" : 0,
   "totalSentSat" : 0,
   "transactions" : [
      "d6be34ccf6edddc3cf69842dce99fe503bf632ba2c2adb0f95c63f6706ae0c52",
      "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
   ],
   "unconfirmedBalanceSat" : 0,
   "totalReceived" : 50.02,
   "unconfirmedBalance" : 0,
   "addrStr" : "bitcoincash:qqgekzvw96vq5g57zwdfa5q6g609rrn0ycp33uc325",
   "unconfirmedTxApperances" : 0,
   "txApperances" : 2,
   "balance" : 50.02,
   "balanceSat" : 5002000000
}
osagga@osagga:~/bitcore-node-cash$ curl -s localhost:3001/api/addr/bitcoincash:qqgekzvw96vq5g57zwdfa5q6g609rrn0ycp33uc325 | json_pp 
{
   "totalReceivedSat" : 5002000000,
   "unconfirmedTxApperances" : 0,
   "unconfirmedBalance" : 0,
   "totalSentSat" : 0,
   "addrStr" : "bitcoincash:qqgekzvw96vq5g57zwdfa5q6g609rrn0ycp33uc325",
   "totalReceived" : 50.02,
   "txApperances" : 2,
   "transactions" : [
      "d6be34ccf6edddc3cf69842dce99fe503bf632ba2c2adb0f95c63f6706ae0c52",
      "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
   ],
   "balanceSat" : 5002000000,
   "totalSent" : 0,
   "balance" : 50.02,
   "unconfirmedBalanceSat" : 0
}
osagga@osagga:~/bitcore-node-cash$ curl -s localhost:3001/api/addr/qqgekzvw96vq5g57zwdfa5q6g609rrn0ycp33uc325 | json_pp 
{
   "totalReceivedSat" : 5002000000,
   "transactions" : [
      "d6be34ccf6edddc3cf69842dce99fe503bf632ba2c2adb0f95c63f6706ae0c52",
      "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
   ],
   "unconfirmedTxApperances" : 0,
   "totalReceived" : 50.02,
   "txApperances" : 2,
   "balance" : 50.02,
   "balanceSat" : 5002000000,
   "unconfirmedBalance" : 0,
   "addrStr" : "bitcoincash:qqgekzvw96vq5g57zwdfa5q6g609rrn0ycp33uc325",
   "totalSentSat" : 0,
   "totalSent" : 0,
   "unconfirmedBalanceSat" : 0
}

Try to update the insight-api and test it out!

hxzqlh commented 6 years ago

@osagga Things not like expected, I made a docker image hxzqlh/bch-insight:v5 which is based on this bitcore-node-cash , it can only support 2 version addresses:

while, BTC legacy format is not supported.

root@iZwz924dpebwff0lbha9n9Z:/mnt# time curl localhost:32829/api/addr/bitcoincash:qznxpp40dpujeap0apcfhs272y3z67m9gvp4x7ylzt
{
   "unconfirmedTxApperances" : 0,
   "balance" : 0,
   "txApperances" : 1,
   "totalSent" : 7,
   "totalReceivedSat" : 700000000,
   "totalReceived" : 7,
   "unconfirmedBalance" : 0,
   "totalSentSat" : 700000000,
   "addrStr" : "bitcoincash:qznxpp40dpujeap0apcfhs272y3z67m9gvp4x7ylzt",
   "unconfirmedBalanceSat" : 0,
   "balanceSat" : 0,
   "transactions" : [
      "c6c5b200c47e82001714e2f9eafe856e27ec54d22f845a87f34aa2b1b14b6fac",
      "89c9b4187e85cfd5d43c939aff5a90375af7dccd3ace3196c6b28b58e9f8f66b"
   ]
}
real    0m58.107s
user    0m0.008s
sys 0m0.004s

root@iZwz924dpebwff0lbha9n9Z:/mnt# curl localhost:32829/api/addr/18LzFwP9DCasNmxZCeU3s3R5YC7F4QUifU
Invalid address: 18LzFwP9DCasNmxZCeU3s3R5YC7F4QUifU. Code:1

by the way, not only the sync speed but also the query speed of v5 version of bitcore-node is slow than bitcore-node@3.1.3.

osagga commented 6 years ago

@hxzqlh Interesting, how did you test the query speed? I've also noticed it's slower, but I wasn't sure.

Regarding the BTC legacy problem, make sure that the BTC legacy address you're querying converts to an existing BCH address.

For example, bitcoincash:qznxpp40dpujeap0apcfhs272y3z67m9gvp4x7ylzt (from your example) will convert to 1GAiobZNL3LrBBhsUfqr8hanWHXfApnxvY not 18LzFwP9DCasNmxZCeU3s3R5YC7F4QUifU So try to query for 1GAiobZNL3LrBBhsUfqr8hanWHXfApnxvY instead. Also to convert from addresses, try to use this website https://bch.btc.com/tools/address-converter or use the bitcore-lib-cash internal Address class

hxzqlh commented 6 years ago

@osagga Using the address converting tool, bitcoincash:qznxpp40dpujeap0apcfhs272y3z67m9gvp4x7ylzt will convert to 1GAiobZNL3LrBBhsUfqr8hanWHXfApnxvY, that's right,.

bch format addr can be queryed:

root@iZwz924dpebwff0lbha9n9Z:/mnt# time curl -s localhost:32837/api/addr/bitcoincash:qznxpp40dpujeap0apcfhs272y3z67m9gvp4x7ylzt | json_pp
{
   "addrStr" : "bitcoincash:qznxpp40dpujeap0apcfhs272y3z67m9gvp4x7ylzt",
   "unconfirmedBalance" : 0,
   "totalReceived" : 7,
   "totalSentSat" : 700000000,
   "balanceSat" : 0,
   "txApperances" : 1,
   "transactions" : [
      "c6c5b200c47e82001714e2f9eafe856e27ec54d22f845a87f34aa2b1b14b6fac",
      "89c9b4187e85cfd5d43c939aff5a90375af7dccd3ace3196c6b28b58e9f8f66b"
   ],
   "balance" : 0,
   "unconfirmedBalanceSat" : 0,
   "unconfirmedTxApperances" : 0,
   "totalReceivedSat" : 700000000,
   "totalSent" : 7
}

real    0m2.492s
user    0m0.032s
sys 0m0.004s

but, btc legacy format addr cannot:

root@iZwz924dpebwff0lbha9n9Z:/mnt# curl localhost:32837/api/addr/1GAiobZNL3LrBBhsUfqr8hanWHXfApnxvY
Invalid address: 1GAiobZNL3LrBBhsUfqr8hanWHXfApnxvY. Code:1 

while in your bitcore-node@3.1.3 setup, btc addr can be queryed:

curl localhost:32835/insight-api/addr/1GAiobZNL3LrBBhsUfqr8hanWHXfApnxvY
{
   "addrStr" : "bitcoincash:qznxpp40dpujeap0apcfhs272y3z67m9gvp4x7ylzt",
   "unconfirmedBalance" : 0,
   "totalReceived" : 7,
   "totalSentSat" : 700000000,
   "balanceSat" : 0,
   "txApperances" : 1,
   "transactions" : [
      "c6c5b200c47e82001714e2f9eafe856e27ec54d22f845a87f34aa2b1b14b6fac",
      "89c9b4187e85cfd5d43c939aff5a90375af7dccd3ace3196c6b28b58e9f8f66b"
   ],
   "balance" : 0,
   "unconfirmedBalanceSat" : 0,
   "unconfirmedTxApperances" : 0,
   "totalReceivedSat" : 700000000,
   "totalSent" : 7

So, I think there are some mistakes in your bitcore-node(v5) or insight-api(cash_v1).