dgarage / NBXplorer

NBitcoin Explorer
MIT License
319 stars 212 forks source link

Using btc.startheight on a block lower than the pruned node #252

Open bitcoinbrisbane opened 4 years ago

bitcoinbrisbane commented 4 years ago

If running a pruned node, and the last block stored on disk is lower than the setting btc.startheight, the index behaves strangely, as you would expect, but does not give the operator much indication as to what went wrong.

One solution could be to:

  1. Do an RPC call getblockchaininfo
  2. Get the value from the property pruneheight
  3. Compare with btc.startheight
  4. Display a better log, such as `Your nodes oldest block is higher than the target. Please change target to 621006

I'll try make a PR for this soon. Just a reminder to myself. :)

Example json return object from getblockchaininfo

{
  "chain": "main",
  "blocks": 624959,
  "headers": 624959,
  "bestblockhash": "0000000000000000000c799dc0e36302db7fbb471711f140dc308508ef19e343",
  "difficulty": 13912524048945.91,
  "mediantime": 1586333371,
  "verificationprogress": 0.9999971825740865,
  "initialblockdownload": false,
  "chainwork": "00000000000000000000000000000000000000000e4c955dbc140174f247f260",
  "size_on_disk": 5089213233,
  "pruned": true,
  "pruneheight": 621006,
  "automatic_pruning": true,
  "prune_target_size": 5242880000,
  "softforks": {
    "bip34": {
      "type": "buried",
      "active": true,
      "height": 227931
    },
    "bip66": {
      "type": "buried",
      "active": true,
      "height": 363725
    },
    "bip65": {
      "type": "buried",
      "active": true,
      "height": 388381
    },
    "csv": {
      "type": "buried",
      "active": true,
      "height": 419328
    },
    "segwit": {
      "type": "buried",
      "active": true,
      "height": 481824
    }
  },
  "warnings": ""
}
bitcoinbrisbane commented 4 months ago

Geez, where did that 4 years go! Need to get the pruneheight on the rpc call and check.

Note, the pruneheight property doesnt seem to exist on the blockchaininfo class https://github.com/MetacoSA/NBitcoin/blob/master/NBitcoin/RPC/RPCClient.cs#L2545-L2582

        async Task<SlimChainedBlock> SeemsStuck(CancellationToken cancellationToken)
        {
            if (State is not (BitcoinDWaiterState.NBXplorerSynching or BitcoinDWaiterState.Ready) ||
                        lastIndexedBlock is not { } lastBlock ||
                        GetConnectedClient() is not RPCClient rpc)
            {
                return null;
            }

            var blockchainInfo = await rpc.GetBlockchainInfoAsyncEx(cancellationToken);
            return blockchainInfo.BestBlockHash != lastBlock.Hash ? lastBlock : null;
        }
bitcoinbrisbane commented 4 months ago

Needs this merged and released to Nuget https://github.com/MetacoSA/NBitcoin/pull/1214