| 72 | } |
| 73 | |
| 74 | UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false) |
| 75 | { |
| 76 | // printf("%s \n",__func__); |
| 77 | UniValue result(UniValue::VOBJ); |
| 78 | result.push_back(Pair("hash", block.GetHash(blockindex->nHeight).GetHex())); |
| 79 | int confirmations = -1; |
| 80 | // Only report confirmations if the block is on the main chain |
| 81 | if (chainActive.Contains(blockindex)) |
| 82 | confirmations = chainActive.Height() - blockindex->nHeight + 1; |
| 83 | result.push_back(Pair("confirmations", confirmations)); |
| 84 | result.push_back(Pair("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION))); |
| 85 | result.push_back(Pair("height", blockindex->nHeight)); |
| 86 | result.push_back(Pair("version", block.nVersion)); |
| 87 | result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex())); |
| 88 | result.push_back(Pair("stateroot", block.hashStateRoot.GetHex())); |
| 89 | result.push_back(Pair("utxoroot", block.hashUTXORoot.GetHex())); |
| 90 | UniValue txs(UniValue::VARR); |
| 91 | for (const CTransaction& tx : block.vtx) { |
| 92 | if (txDetails) { |
| 93 | UniValue objTx(UniValue::VOBJ); |
| 94 | TxToJSON(tx, uint256(), objTx); |
| 95 | txs.push_back(objTx); |
| 96 | } else |
| 97 | txs.push_back(tx.GetHash().GetHex()); |
| 98 | } |
| 99 | result.push_back(Pair("tx", txs)); |
| 100 | result.push_back(Pair("time", block.GetBlockTime())); |
| 101 | result.push_back(Pair("nonce", (uint64_t)block.nNonce)); |
| 102 | result.push_back(Pair("bits", strprintf("%08x", block.nBits))); |
| 103 | result.push_back(Pair("difficulty", GetDifficulty(blockindex))); |
| 104 | result.push_back(Pair("flags", strprintf("%s%s", blockindex->IsProofOfStake()?"proof-of-stake":"proof-of-work", |
| 105 | blockindex->IsProofOfStake() && blockindex->GeneratedStakeModifier()?" stake-modifier":""))); |
| 106 | result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex())); |
| 107 | |
| 108 | if (blockindex->pprev) |
| 109 | result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex())); |
| 110 | CBlockIndex* pnext = chainActive.Next(blockindex); |
| 111 | if (pnext) |
| 112 | result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex())); |
| 113 | |
| 114 | // printf("leaving %s \n",__func__); |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | UniValue blockHeaderToJSON(const CBlock& block, const CBlockIndex* blockindex) |
no test coverage detected