bitcoinjs-lib icon indicating copy to clipboard operation
bitcoinjs-lib copied to clipboard

Significantly different performance to generate address on RN with debug mode and debug mode connect to Chrome Debugger

Open DavidLiuBlockChainTech opened this issue 7 years ago • 4 comments
trafficstars

Hi, I am using the bitcoinjs-lib 3.3.2 and 4.0.2 to generate the address on RN Android. When I start the App on debug mode without Chrome Debugger connected, the performance is really poor, however, if I start the App with Chrome Debugger connected, the performance is very good.

The IDE is WebStorm 2018.2.1

Here is the test code and result. All the tests have the same input and on the same device (similar result for 3.3.2 and 4.0.2).

The test result is shown by:

consume Xms(with Chrome Debugger) | Xms(without Chrome Debugger)

static getAddresses = (publicKey, coinName, chain, indexStart, indexEnd) => {

  const network = getNetwork(coinName);// consume 0ms | 1ms

  const hdNode = bitcoin.HDNode.fromBase58(publicKey, [network]);// consume 54ms | 545ms

  const chainNode = hdNode.derive(chain);// consume 23ms | 701ms

  const array = new Array((indexEnd - indexStart) + 1).fill().map((e, i) => i + indexStart);

  // consume 196ms | 6639ms
  const result = array.map((element) => {
    const addressString = AccountService.nodeToP2shSegwitAddress(chainNode.derive(element), network);
  return {index: element, addressString, chain};
  });

  return result
};

static nodeToP2shSegwitAddress = (hdNode, network) => {
  const pubkeyBuf = hdNode.keyPair.getPublicKeyBuffer();
  const hash = bitcoin.crypto.hash160(pubkeyBuf);
  const redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(hash);
  const hash2 = bitcoin.crypto.hash160(redeemScript);
  const scriptPubkey = bitcoin.script.scriptHash.output.encode(hash2);

  return bitcoin.address.fromOutputScript(scriptPubkey, network);
};

Based on my experience, the performance should be poorer if debugging with Chrome debugger, so it really confuses me.

Any help would be grateful.

DavidLiuBlockChainTech avatar Nov 16 '18 04:11 DavidLiuBlockChainTech

The biggest performance impact in your loop is derive as it performs ECDSA multiplication and addition which is expensive.

My guess:

When attached to debugger, you are using private xprv and when not attached to debugger you are using public xpub???

junderw avatar Nov 16 '18 04:11 junderw

The biggest performance impact in your loop is derive as it performs ECDSA multiplication and addition which is expensive.

My guess:

When attached to debugger, you are using private xprv and when not attached to debugger you are using public xpub???

Thanks for your quick reply. The inputs actually are same for all tests, including the one with Chrome Debugger so I only use the xpub which get from Trezor hardware device. The performance can be 30 times different.

DavidLiuBlockChainTech avatar Nov 16 '18 04:11 DavidLiuBlockChainTech

@DavidLiuBlockChainTech I recommend you isolate the problem further and determine which of those functions are taking the most time, to avoid pointless speculation on our part.

dcousens avatar Nov 27 '18 01:11 dcousens

@DavidLiuBlockChainTech You should measure the time each line of HDNode.prototype.derive takes.

It seems extremely obvious that derive is causing the problem... so you need to measure each step in there.

junderw avatar Nov 27 '18 04:11 junderw