| 81 | } |
| 82 | |
| 83 | UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false) |
| 84 | { |
| 85 | UniValue result(UniValue::VOBJ); |
| 86 | result.push_back(Pair("hash", block.GetHash().GetHex())); |
| 87 | int confirmations = -1; |
| 88 | // Only report confirmations if the block is on the main chain |
| 89 | if (chainActive.Contains(blockindex)) |
| 90 | confirmations = chainActive.Height() - blockindex->nHeight + 1; |
| 91 | result.push_back(Pair("confirmations", confirmations)); |
| 92 | result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION))); |
| 93 | result.push_back(Pair("height", blockindex->nHeight)); |
| 94 | result.push_back(Pair("version", block.nVersion)); |
| 95 | result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex())); |
| 96 | UniValue txs(UniValue::VARR); |
| 97 | BOOST_FOREACH(const CTransaction&tx, block.vtx) |
| 98 | { |
| 99 | if(txDetails) |
| 100 | { |
| 101 | UniValue objTx(UniValue::VOBJ); |
| 102 | TxToJSON(tx, uint256(), objTx); |
| 103 | txs.push_back(objTx); |
| 104 | } |
| 105 | else |
| 106 | txs.push_back(tx.GetHash().GetHex()); |
| 107 | } |
| 108 | result.push_back(Pair("tx", txs)); |
| 109 | result.push_back(Pair("time", block.GetBlockTime())); |
| 110 | result.push_back(Pair("mediantime", (int64_t)blockindex->GetMedianTimePast())); |
| 111 | result.push_back(Pair("nonce", (uint64_t)block.nNonce)); |
| 112 | result.push_back(Pair("bits", strprintf("%08x", block.nBits))); |
| 113 | result.push_back(Pair("difficulty", GetDifficulty(blockindex))); |
| 114 | result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex())); |
| 115 | result.push_back(Pair("sizelimit", blockindex->nMaxBlockSize)); |
| 116 | result.push_back(Pair("sizelimitvote", blockindex->nMaxBlockSizeVote)); |
| 117 | |
| 118 | if (blockindex->pprev) |
| 119 | result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex())); |
| 120 | CBlockIndex *pnext = chainActive.Next(blockindex); |
| 121 | if (pnext) |
| 122 | result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex())); |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | UniValue getblockcount(const UniValue& params, bool fHelp) |
| 127 | { |
no test coverage detected