diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-08-28 17:14:22 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-08-29 17:25:23 +0200 |
commit | 57153d4e1ac6e2cc9fc3f2fcd6d786e9813d824f (patch) | |
tree | 57a499b68c12471e1e68d7b8010b7ccd0d5a0a0c | |
parent | 539abc4729ea16039d148cfa3b771929f7d37584 (diff) |
rpc: Compute number of confirmations of a block from block height
Currently this uses a CMerkleTx, but that makes no sense as we
have the CBlockIndex available. As noted by @jgarzik.
-rw-r--r-- | src/rpcblockchain.cpp | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 58cab14045..8a4b25cc54 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -54,9 +54,11 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex) { Object result; result.push_back(Pair("hash", block.GetHash().GetHex())); - CMerkleTx txGen(block.vtx[0]); - txGen.SetMerkleBranch(&block); - result.push_back(Pair("confirmations", (int)txGen.GetDepthInMainChain())); + int confirmations = -1; + // Only report confirmations if the block is on the main chain + if (chainActive.Contains(blockindex)) + confirmations = chainActive.Height() - blockindex->nHeight + 1; + result.push_back(Pair("confirmations", confirmations)); result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION))); result.push_back(Pair("height", blockindex->nHeight)); result.push_back(Pair("version", block.nVersion)); @@ -242,7 +244,7 @@ Value getblock(const Array& params, bool fHelp) "\nResult (for verbose = true):\n" "{\n" " \"hash\" : \"hash\", (string) the block hash (same as provided)\n" - " \"confirmations\" : n, (numeric) The number of confirmations\n" + " \"confirmations\" : n, (numeric) The number of confirmations, or -1 if the block is not on the main chain\n" " \"size\" : n, (numeric) The block size\n" " \"height\" : n, (numeric) The block height or index\n" " \"version\" : n, (numeric) The block version\n" |