MCPcopy Create free account
hub / github.com/BTCGPU/BTCGPU / blockToJSON

Function blockToJSON

src/rpc/blockchain.cpp:130–178  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128}
129
130UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails)
131{
132 AssertLockHeld(cs_main);
133 UniValue result(UniValue::VOBJ);
134 result.pushKV("hash", blockindex->GetBlockHash().GetHex());
135 int confirmations = -1;
136 // Only report confirmations if the block is on the main chain
137 if (chainActive.Contains(blockindex))
138 confirmations = chainActive.Height() - blockindex->nHeight + 1;
139 result.pushKV("confirmations", confirmations);
140 const Consensus::Params& consensusParams = Params().GetConsensus();
141 int ser_flags = (blockindex->nHeight < consensusParams.BTGHeight) ? SERIALIZE_BLOCK_LEGACY : 0;
142 result.pushKV("strippedsize", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS | ser_flags));
143 result.pushKV("size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | ser_flags));
144 result.pushKV("weight", (int)::GetBlockWeight(block, consensusParams));
145 result.pushKV("height", blockindex->nHeight);
146 result.pushKV("version", block.nVersion);
147 result.pushKV("versionHex", strprintf("%08x", block.nVersion));
148 result.pushKV("merkleroot", block.hashMerkleRoot.GetHex());
149 UniValue txs(UniValue::VARR);
150 for(const auto& tx : block.vtx)
151 {
152 if(txDetails)
153 {
154 UniValue objTx(UniValue::VOBJ);
155 TxToUniv(*tx, uint256(), objTx, true, RPCSerializationFlags());
156 txs.push_back(objTx);
157 }
158 else
159 txs.push_back(tx->GetHash().GetHex());
160 }
161 result.pushKV("tx", txs);
162 result.pushKV("time", block.GetBlockTime());
163 result.pushKV("mediantime", (int64_t)blockindex->GetMedianTimePast());
164 result.pushKV("nonceUint32", (uint64_t)((uint32_t)block.nNonce.GetUint64(0)));
165 result.pushKV("nonce", block.nNonce.GetHex());
166 result.pushKV("solution", HexStr(block.nSolution));
167 result.pushKV("bits", strprintf("%08x", block.nBits));
168 result.pushKV("difficulty", GetDifficulty(blockindex));
169 result.pushKV("chainwork", blockindex->nChainWork.GetHex());
170 result.pushKV("nTx", (uint64_t)blockindex->nTx);
171
172 if (blockindex->pprev)
173 result.pushKV("previousblockhash", blockindex->pprev->GetBlockHash().GetHex());
174 CBlockIndex *pnext = chainActive.Next(blockindex);
175 if (pnext)
176 result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex());
177 return result;
178}
179
180static UniValue getblockcount(const JSONRPCRequest& request)
181{

Callers 2

rest_blockFunction · 0.85
getblockFunction · 0.85

Calls 15

GetSerializeSizeFunction · 0.85
GetBlockWeightFunction · 0.85
TxToUnivFunction · 0.85
RPCSerializationFlagsFunction · 0.85
HexStrFunction · 0.85
GetDifficultyFunction · 0.85
HeightMethod · 0.80
GetMedianTimePastMethod · 0.80
GetUint64Method · 0.80
ParamsClass · 0.50
uint256Class · 0.50
pushKVMethod · 0.45

Tested by

no test coverage detected