Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/blockchain.cpp
Show First 20 Lines • Show All 810 Lines • ▼ Show 20 Lines | if (!fVerbose) { | ||||
ssBlock << pblockindex->GetBlockHeader(); | ssBlock << pblockindex->GetBlockHeader(); | ||||
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()); | std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()); | ||||
return strHex; | return strHex; | ||||
} | } | ||||
return blockheaderToJSON(chainActive.Tip(), pblockindex); | return blockheaderToJSON(chainActive.Tip(), pblockindex); | ||||
} | } | ||||
static CBlock GetBlockChecked(const Config &config, | |||||
const CBlockIndex *pblockindex) { | |||||
CBlock block; | |||||
if (fHavePruned && !pblockindex->nStatus.hasData() && | |||||
pblockindex->nTx > 0) { | |||||
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)"); | |||||
} | |||||
if (!ReadBlockFromDisk(block, pblockindex, config)) { | |||||
// Block not found on disk. This could be because we have the block | |||||
// header in our index but don't have the block (for example if a | |||||
// non-whitelisted node sends us an unrequested long chain of valid | |||||
// blocks, we add the headers to our index, but don't accept the block). | |||||
throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk"); | |||||
} | |||||
return block; | |||||
} | |||||
static UniValue getblock(const Config &config, const JSONRPCRequest &request) { | static UniValue getblock(const Config &config, const JSONRPCRequest &request) { | ||||
if (request.fHelp || request.params.size() < 1 || | if (request.fHelp || request.params.size() < 1 || | ||||
request.params.size() > 2) { | request.params.size() > 2) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"getblock \"blockhash\" ( verbosity )\n" | "getblock \"blockhash\" ( verbosity )\n" | ||||
"\nIf verbosity is 0 or false, returns a string that is " | "\nIf verbosity is 0 or false, returns a string that is " | ||||
"serialized, hex-encoded data for block 'hash'.\n" | "serialized, hex-encoded data for block 'hash'.\n" | ||||
"If verbosity is 1 or true, returns an Object with information " | "If verbosity is 1 or true, returns an Object with information " | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | if (!request.params[1].isNull()) { | ||||
} | } | ||||
} | } | ||||
const CBlockIndex *pblockindex = LookupBlockIndex(hash); | const CBlockIndex *pblockindex = LookupBlockIndex(hash); | ||||
if (!pblockindex) { | if (!pblockindex) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | ||||
} | } | ||||
CBlock block; | const CBlock block = GetBlockChecked(config, pblockindex); | ||||
if (fHavePruned && !pblockindex->nStatus.hasData() && | |||||
pblockindex->nTx > 0) { | |||||
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)"); | |||||
} | |||||
if (!ReadBlockFromDisk(block, pblockindex, config)) { | |||||
// Block not found on disk. This could be because we have the block | |||||
// header in our index but don't have the block (for example if a | |||||
// non-whitelisted node sends us an unrequested long chain of valid | |||||
// blocks, we add the headers to our index, but don't accept the block). | |||||
throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk"); | |||||
} | |||||
if (verbosity <= 0) { | if (verbosity <= 0) { | ||||
CDataStream ssBlock(SER_NETWORK, | CDataStream ssBlock(SER_NETWORK, | ||||
PROTOCOL_VERSION | RPCSerializationFlags()); | PROTOCOL_VERSION | RPCSerializationFlags()); | ||||
ssBlock << block; | ssBlock << block; | ||||
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()); | std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()); | ||||
return strHex; | return strHex; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 1,036 Lines • Show Last 20 Lines |