calling importaddress with same address

1

While experimenting with importing addresses and keys via json rpc I noticed that importing an address twice triggers two rescans. This strikes me as very odd. My question is:

  • Is this intended behaviour and if so, what is the reasoning behind it?
  • OR: Is this a bug? If so, is it a known bug?

blues

Posted 2017-11-28T08:25:02.717

Reputation: 276

Answers

1

Calling importaddress will rescan the whole blockchain in default according to bitcoin.org

Set rescan parameter to false to not rescan the block database (rescanning can be performed at any time by restarting Bitcoin Core with the -rescan command-line argument). Rescanning may take several minutes.


help importaddress

importaddress "address" ( "label" rescan p2sh )

Adds a script (in hex) or address that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.

Arguments:
1. "script" (string, required) The hex-encoded script (or address)
2. "label" (string, optional, default="") An optional label
3. rescan (boolean, optional, default=true) Rescan the wallet for transactions
4. p2sh (boolean, optional, default=false) Add the P2SH version of the script as well

Note: This call can take minutes to complete if rescan is true, during that time, other rpc calls may report that the imported address exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.
If you have the full public key, you should call importpubkey instead of this.

Note: If you import a non-standard raw script in hex form, outputs sending to it will be treated as change, and not show up in many RPCs.

Examples:

Import a script with rescan
bitcoin-cli importaddress "myscript"

Import using a label without rescan
bitcoin-cli importaddress "myscript" "testing" false


UPDATE Since Bitcoin core v0.14 you can use importmulti to import addresses.

bitcoin-cli importmulti '
  [
    {
      "scriptPubKey" : { "address": "1NL9w5fP9kX2D9ToNZPxaiwFJCngNYEYJo" },
      "timestamp" : "0",
      "label" : "Personal",
      "watchonly": true
    },
    {
      "scriptPubKey" : "76a9149e857da0a5b397559c78c98c9d3f7f655d19c68688ac",
      "timestamp" : 1493912405,
      "label" : "TestFailure"
    }
  ]' '{ "rescan": true }'

Response:

 [
{
  "success": true
}, 
{
  "success": false,
  "error": {
  "code": -8,
  "message": "Internal must be set for hex scriptPubKey"
  }
}
]

Note:

  • Bech32 addresses are not supported using this method until the moment. (You're free to edit this answer when it's available.)
  • importmulti will not speed up importing progress since this method locks your wallet and block any further RPC calls.

Adam

Posted 2017-11-28T08:25:02.717

Reputation: 3 215

But if an address has already been imported earlier why would it trigger the rescan again? That seems very odd. The node could simply do nothing in that case and all would be fine, and a lot of CPU time saved.blues 2017-11-28T08:55:18.063

Bitcoin core commands is like for advanced users and it's not an interface you can just use bitcoin-qt instead.

in your case if you would like to use bitcoin-cli you can check if address not exist you can call importaddress – Adam 2017-11-28T08:59:40.560

So what you are telling me is that it is a bug, but a workaround exists by checking first? A rescan of an address that is already in the wallet and was rescanned for before can never result in any new information.blues 2017-11-28T09:12:17.747

I have taken the liberty of adding further info about importaddress to your answer.Willtech 2018-04-04T04:03:59.817

Now it's even better! @WilltechAdam 2018-04-05T20:16:01.883

This is not entirely correct. importmulti is not supposed to rescan the whole change, but only the part affected by the keys being imported (you can specify their birth time). I would say this is a bug.Pieter Wuille 2018-04-05T21:01:55.357