Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/blockchain.cpp
Show First 20 Lines • Show All 756 Lines • ▼ Show 20 Lines | UniValue getblockheader(const Config &config, const JSONRPCRequest &request) { | ||||
std::string strHash = request.params[0].get_str(); | std::string strHash = request.params[0].get_str(); | ||||
uint256 hash(uint256S(strHash)); | uint256 hash(uint256S(strHash)); | ||||
bool fVerbose = true; | bool fVerbose = true; | ||||
if (!request.params[1].isNull()) { | if (!request.params[1].isNull()) { | ||||
fVerbose = request.params[1].get_bool(); | fVerbose = request.params[1].get_bool(); | ||||
} | } | ||||
if (mapBlockIndex.count(hash) == 0) { | const CBlockIndex *pblockindex = LookupBlockIndex(hash); | ||||
if (!pblockindex) { | |||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | ||||
} | } | ||||
CBlockIndex *pblockindex = mapBlockIndex[hash]; | |||||
if (!fVerbose) { | if (!fVerbose) { | ||||
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION); | CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION); | ||||
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(pblockindex); | return blockheaderToJSON(pblockindex); | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | UniValue getblock(const Config &config, const JSONRPCRequest &request) { | ||||
if (!request.params[1].isNull()) { | if (!request.params[1].isNull()) { | ||||
if (request.params[1].isNum()) { | if (request.params[1].isNum()) { | ||||
verbosity = request.params[1].get_int(); | verbosity = request.params[1].get_int(); | ||||
} else { | } else { | ||||
verbosity = request.params[1].get_bool() ? 1 : 0; | verbosity = request.params[1].get_bool() ? 1 : 0; | ||||
} | } | ||||
} | } | ||||
if (mapBlockIndex.count(hash) == 0) { | const CBlockIndex *pblockindex = LookupBlockIndex(hash); | ||||
if (!pblockindex) { | |||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | ||||
} | } | ||||
CBlock block; | CBlock block; | ||||
CBlockIndex *pblockindex = mapBlockIndex[hash]; | |||||
if (fHavePruned && !pblockindex->nStatus.hasData() && | if (fHavePruned && !pblockindex->nStatus.hasData() && | ||||
pblockindex->nTx > 0) { | pblockindex->nTx > 0) { | ||||
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)"); | throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)"); | ||||
} | } | ||||
if (!ReadBlockFromDisk(block, pblockindex, config)) { | if (!ReadBlockFromDisk(block, pblockindex, config)) { | ||||
// Block not found on disk. This could be because we have the block | // 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 | // header in our index but don't have the block (for example if a | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | |||||
//! Calculate statistics about the unspent transaction output set | //! Calculate statistics about the unspent transaction output set | ||||
static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats) { | static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats) { | ||||
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor()); | std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor()); | ||||
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); | CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); | ||||
stats.hashBlock = pcursor->GetBestBlock(); | stats.hashBlock = pcursor->GetBestBlock(); | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
stats.nHeight = mapBlockIndex.find(stats.hashBlock)->second->nHeight; | stats.nHeight = LookupBlockIndex(stats.hashBlock)->nHeight; | ||||
} | } | ||||
ss << stats.hashBlock; | ss << stats.hashBlock; | ||||
uint256 prevkey; | uint256 prevkey; | ||||
std::map<uint32_t, Coin> outputs; | std::map<uint32_t, Coin> outputs; | ||||
while (pcursor->Valid()) { | while (pcursor->Valid()) { | ||||
boost::this_thread::interruption_point(); | boost::this_thread::interruption_point(); | ||||
COutPoint key; | COutPoint key; | ||||
Coin coin; | Coin coin; | ||||
▲ Show 20 Lines • Show All 191 Lines • ▼ Show 20 Lines | if (fMempool) { | ||||
return NullUniValue; | return NullUniValue; | ||||
} | } | ||||
} else { | } else { | ||||
if (!pcoinsTip->GetCoin(out, coin)) { | if (!pcoinsTip->GetCoin(out, coin)) { | ||||
return NullUniValue; | return NullUniValue; | ||||
} | } | ||||
} | } | ||||
BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock()); | const CBlockIndex *pindex = LookupBlockIndex(pcoinsTip->GetBestBlock()); | ||||
CBlockIndex *pindex = it->second; | |||||
ret.pushKV("bestblock", pindex->GetBlockHash().GetHex()); | ret.pushKV("bestblock", pindex->GetBlockHash().GetHex()); | ||||
if (coin.GetHeight() == MEMPOOL_HEIGHT) { | if (coin.GetHeight() == MEMPOOL_HEIGHT) { | ||||
ret.pushKV("confirmations", 0); | ret.pushKV("confirmations", 0); | ||||
} else { | } else { | ||||
ret.pushKV("confirmations", | ret.pushKV("confirmations", | ||||
int64_t(pindex->nHeight - coin.GetHeight() + 1)); | int64_t(pindex->nHeight - coin.GetHeight() + 1)); | ||||
} | } | ||||
ret.pushKV("value", ValueFromAmount(coin.GetTxOut().nValue)); | ret.pushKV("value", ValueFromAmount(coin.GetTxOut().nValue)); | ||||
▲ Show 20 Lines • Show All 365 Lines • ▼ Show 20 Lines | UniValue preciousblock(const Config &config, const JSONRPCRequest &request) { | ||||
} | } | ||||
std::string strHash = request.params[0].get_str(); | std::string strHash = request.params[0].get_str(); | ||||
uint256 hash(uint256S(strHash)); | uint256 hash(uint256S(strHash)); | ||||
CBlockIndex *pblockindex; | CBlockIndex *pblockindex; | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
if (mapBlockIndex.count(hash) == 0) { | pblockindex = LookupBlockIndex(hash); | ||||
if (!pblockindex) { | |||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | ||||
} | } | ||||
pblockindex = mapBlockIndex[hash]; | |||||
} | } | ||||
CValidationState state; | CValidationState state; | ||||
PreciousBlock(config, state, pblockindex); | PreciousBlock(config, state, pblockindex); | ||||
if (!state.IsValid()) { | if (!state.IsValid()) { | ||||
throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason()); | throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason()); | ||||
} | } | ||||
Show All 17 Lines | UniValue finalizeblock(const Config &config, const JSONRPCRequest &request) { | ||||
} | } | ||||
std::string strHash = request.params[0].get_str(); | std::string strHash = request.params[0].get_str(); | ||||
uint256 hash(uint256S(strHash)); | uint256 hash(uint256S(strHash)); | ||||
CValidationState state; | CValidationState state; | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
if (mapBlockIndex.count(hash) == 0) { | CBlockIndex *pblockindex = LookupBlockIndex(hash); | ||||
if (!pblockindex) { | |||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | ||||
} | } | ||||
CBlockIndex *pblockindex = mapBlockIndex[hash]; | |||||
FinalizeBlockAndInvalidate(config, state, pblockindex); | FinalizeBlockAndInvalidate(config, state, pblockindex); | ||||
} | } | ||||
if (state.IsValid()) { | if (state.IsValid()) { | ||||
ActivateBestChain(config, state); | ActivateBestChain(config, state); | ||||
} | } | ||||
if (!state.IsValid()) { | if (!state.IsValid()) { | ||||
Show All 19 Lines | UniValue invalidateblock(const Config &config, const JSONRPCRequest &request) { | ||||
} | } | ||||
const std::string strHash = request.params[0].get_str(); | const std::string strHash = request.params[0].get_str(); | ||||
const uint256 hash(uint256S(strHash)); | const uint256 hash(uint256S(strHash)); | ||||
CValidationState state; | CValidationState state; | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
if (mapBlockIndex.count(hash) == 0) { | CBlockIndex *pblockindex = LookupBlockIndex(hash); | ||||
if (!pblockindex) { | |||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | ||||
} | } | ||||
CBlockIndex *pblockindex = mapBlockIndex[hash]; | |||||
InvalidateBlock(config, state, pblockindex); | InvalidateBlock(config, state, pblockindex); | ||||
} | } | ||||
if (state.IsValid()) { | if (state.IsValid()) { | ||||
ActivateBestChain(config, state); | ActivateBestChain(config, state); | ||||
} | } | ||||
if (!state.IsValid()) { | if (!state.IsValid()) { | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | if (request.fHelp || request.params.size() != 1) { | ||||
HelpExampleRpc("reconsiderblock", "\"blockhash\"")); | HelpExampleRpc("reconsiderblock", "\"blockhash\"")); | ||||
} | } | ||||
const std::string strHash = request.params[0].get_str(); | const std::string strHash = request.params[0].get_str(); | ||||
const uint256 hash(uint256S(strHash)); | const uint256 hash(uint256S(strHash)); | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
if (mapBlockIndex.count(hash) == 0) { | CBlockIndex *pblockindex = LookupBlockIndex(hash); | ||||
if (!pblockindex) { | |||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | ||||
} | } | ||||
CBlockIndex *pblockindex = mapBlockIndex[hash]; | |||||
ResetBlockFailureFlags(pblockindex); | ResetBlockFailureFlags(pblockindex); | ||||
} | } | ||||
CValidationState state; | CValidationState state; | ||||
ActivateBestChain(config, state); | ActivateBestChain(config, state); | ||||
if (!state.IsValid()) { | if (!state.IsValid()) { | ||||
throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason()); | throw JSONRPCError(RPC_DATABASE_ERROR, state.GetRejectReason()); | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | int blockcount = 30 * 24 * 60 * 60 / | ||||
config.GetChainParams().GetConsensus().nPowTargetSpacing; | config.GetChainParams().GetConsensus().nPowTargetSpacing; | ||||
if (request.params[1].isNull()) { | if (request.params[1].isNull()) { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
pindex = chainActive.Tip(); | pindex = chainActive.Tip(); | ||||
} else { | } else { | ||||
uint256 hash = uint256S(request.params[1].get_str()); | uint256 hash = uint256S(request.params[1].get_str()); | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
auto it = mapBlockIndex.find(hash); | pindex = LookupBlockIndex(hash); | ||||
if (it == mapBlockIndex.end()) { | if (!pindex) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); | ||||
} | } | ||||
pindex = it->second; | |||||
if (!chainActive.Contains(pindex)) { | if (!chainActive.Contains(pindex)) { | ||||
throw JSONRPCError(RPC_INVALID_PARAMETER, | throw JSONRPCError(RPC_INVALID_PARAMETER, | ||||
"Block is not in main chain"); | "Block is not in main chain"); | ||||
} | } | ||||
} | } | ||||
assert(pindex != nullptr); | assert(pindex != nullptr); | ||||
▲ Show 20 Lines • Show All 77 Lines • Show Last 20 Lines |