Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/blockchain.cpp
Show All 10 Lines | |||||
#include "checkpoints.h" | #include "checkpoints.h" | ||||
#include "coins.h" | #include "coins.h" | ||||
#include "config.h" | #include "config.h" | ||||
#include "consensus/validation.h" | #include "consensus/validation.h" | ||||
#include "hash.h" | #include "hash.h" | ||||
#include "policy/policy.h" | #include "policy/policy.h" | ||||
#include "primitives/transaction.h" | #include "primitives/transaction.h" | ||||
#include "rpc/server.h" | #include "rpc/server.h" | ||||
#include "rpc/tojson.h" | |||||
#include "streams.h" | #include "streams.h" | ||||
#include "sync.h" | #include "sync.h" | ||||
#include "txmempool.h" | #include "txmempool.h" | ||||
#include "util.h" | #include "util.h" | ||||
#include "utilstrencodings.h" | #include "utilstrencodings.h" | ||||
#include "validation.h" | #include "validation.h" | ||||
#include <cstdint> | |||||
#include <boost/thread/thread.hpp> // boost::thread::interrupt | #include <boost/thread/thread.hpp> // boost::thread::interrupt | ||||
#include <condition_variable> | #include <condition_variable> | ||||
#include <cstdint> | |||||
#include <mutex> | #include <mutex> | ||||
struct CUpdatedBlock { | struct CUpdatedBlock { | ||||
uint256 hash; | uint256 hash; | ||||
int height; | int height; | ||||
}; | }; | ||||
static std::mutex cs_blockchange; | static std::mutex cs_blockchange; | ||||
static std::condition_variable cond_blockchange; | static std::condition_variable cond_blockchange; | ||||
static CUpdatedBlock latestblock; | static CUpdatedBlock latestblock; | ||||
extern void TxToJSON(const CTransaction &tx, const uint256 hashBlock, | |||||
UniValue &entry); | |||||
void ScriptPubKeyToJSON(const CScript &scriptPubKey, UniValue &out, | |||||
bool fIncludeHex); | |||||
static double GetDifficultyFromBits(uint32_t nBits) { | static double GetDifficultyFromBits(uint32_t nBits) { | ||||
int nShift = (nBits >> 24) & 0xff; | int nShift = (nBits >> 24) & 0xff; | ||||
double dDiff = 0x0000ffff / double(nBits & 0x00ffffff); | double dDiff = 0x0000ffff / double(nBits & 0x00ffffff); | ||||
while (nShift < 29) { | while (nShift < 29) { | ||||
dDiff *= 256.0; | dDiff *= 256.0; | ||||
nShift++; | nShift++; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | UniValue blockheaderToJSON(const CBlockIndex *blockindex) { | ||||
} | } | ||||
CBlockIndex *pnext = chainActive.Next(blockindex); | CBlockIndex *pnext = chainActive.Next(blockindex); | ||||
if (pnext) { | if (pnext) { | ||||
result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex())); | result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex())); | ||||
} | } | ||||
return result; | return result; | ||||
} | } | ||||
UniValue blockToJSON(const CBlock &block, const CBlockIndex *blockindex, | UniValue blockToJSON(const Config &config, const CBlock &block, | ||||
bool txDetails = false) { | const CBlockIndex *blockindex, bool txDetails) { | ||||
UniValue result(UniValue::VOBJ); | UniValue result(UniValue::VOBJ); | ||||
result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex())); | result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex())); | ||||
int confirmations = -1; | int confirmations = -1; | ||||
// Only report confirmations if the block is on the main chain | // Only report confirmations if the block is on the main chain | ||||
if (chainActive.Contains(blockindex)) { | if (chainActive.Contains(blockindex)) { | ||||
confirmations = chainActive.Height() - blockindex->nHeight + 1; | confirmations = chainActive.Height() - blockindex->nHeight + 1; | ||||
} | } | ||||
result.push_back(Pair("confirmations", confirmations)); | result.push_back(Pair("confirmations", confirmations)); | ||||
result.push_back(Pair( | result.push_back(Pair( | ||||
"size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION))); | "size", (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION))); | ||||
result.push_back(Pair("height", blockindex->nHeight)); | result.push_back(Pair("height", blockindex->nHeight)); | ||||
result.push_back(Pair("version", block.nVersion)); | result.push_back(Pair("version", block.nVersion)); | ||||
result.push_back(Pair("versionHex", strprintf("%08x", block.nVersion))); | result.push_back(Pair("versionHex", strprintf("%08x", block.nVersion))); | ||||
result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex())); | result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex())); | ||||
UniValue txs(UniValue::VARR); | UniValue txs(UniValue::VARR); | ||||
for (const auto &tx : block.vtx) { | for (const auto &tx : block.vtx) { | ||||
if (txDetails) { | if (txDetails) { | ||||
UniValue objTx(UniValue::VOBJ); | UniValue objTx(UniValue::VOBJ); | ||||
TxToJSON(*tx, uint256(), objTx); | TxToJSON(config, *tx, uint256(), objTx); | ||||
txs.push_back(objTx); | txs.push_back(objTx); | ||||
} else | } else { | ||||
txs.push_back(tx->GetId().GetHex()); | txs.push_back(tx->GetId().GetHex()); | ||||
} | } | ||||
} | |||||
result.push_back(Pair("tx", txs)); | result.push_back(Pair("tx", txs)); | ||||
result.push_back(Pair("time", block.GetBlockTime())); | result.push_back(Pair("time", block.GetBlockTime())); | ||||
result.push_back( | result.push_back( | ||||
Pair("mediantime", int64_t(blockindex->GetMedianTimePast()))); | Pair("mediantime", int64_t(blockindex->GetMedianTimePast()))); | ||||
result.push_back(Pair("nonce", uint64_t(block.nNonce))); | result.push_back(Pair("nonce", uint64_t(block.nNonce))); | ||||
result.push_back(Pair("bits", strprintf("%08x", block.nBits))); | result.push_back(Pair("bits", strprintf("%08x", block.nBits))); | ||||
result.push_back(Pair("difficulty", GetDifficulty(blockindex))); | result.push_back(Pair("difficulty", GetDifficulty(blockindex))); | ||||
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex())); | result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex())); | ||||
▲ Show 20 Lines • Show All 706 Lines • ▼ Show 20 Lines | UniValue getblock(const Config &config, const JSONRPCRequest &request) { | ||||
if (!fVerbose) { | if (!fVerbose) { | ||||
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; | ||||
} | } | ||||
return blockToJSON(block, pblockindex); | return blockToJSON(config, block, pblockindex); | ||||
} | } | ||||
struct CCoinsStats { | struct CCoinsStats { | ||||
int nHeight; | int nHeight; | ||||
uint256 hashBlock; | uint256 hashBlock; | ||||
uint64_t nTransactions; | uint64_t nTransactions; | ||||
uint64_t nTransactionOutputs; | uint64_t nTransactionOutputs; | ||||
uint64_t nBogoSize; | uint64_t nBogoSize; | ||||
▲ Show 20 Lines • Show All 248 Lines • ▼ Show 20 Lines | UniValue gettxout(const Config &config, const JSONRPCRequest &request) { | ||||
if (coin.GetHeight() == MEMPOOL_HEIGHT) { | if (coin.GetHeight() == MEMPOOL_HEIGHT) { | ||||
ret.push_back(Pair("confirmations", 0)); | ret.push_back(Pair("confirmations", 0)); | ||||
} else { | } else { | ||||
ret.push_back(Pair("confirmations", | ret.push_back(Pair("confirmations", | ||||
int64_t(pindex->nHeight - coin.GetHeight() + 1))); | int64_t(pindex->nHeight - coin.GetHeight() + 1))); | ||||
} | } | ||||
ret.push_back(Pair("value", ValueFromAmount(coin.GetTxOut().nValue))); | ret.push_back(Pair("value", ValueFromAmount(coin.GetTxOut().nValue))); | ||||
UniValue o(UniValue::VOBJ); | UniValue o(UniValue::VOBJ); | ||||
ScriptPubKeyToJSON(coin.GetTxOut().scriptPubKey, o, true); | ScriptPubKeyToJSON(config, coin.GetTxOut().scriptPubKey, o, true); | ||||
ret.push_back(Pair("scriptPubKey", o)); | ret.push_back(Pair("scriptPubKey", o)); | ||||
ret.push_back(Pair("coinbase", coin.IsCoinBase())); | ret.push_back(Pair("coinbase", coin.IsCoinBase())); | ||||
return ret; | return ret; | ||||
} | } | ||||
UniValue verifychain(const Config &config, const JSONRPCRequest &request) { | UniValue verifychain(const Config &config, const JSONRPCRequest &request) { | ||||
int nCheckLevel = GetArg("-checklevel", DEFAULT_CHECKLEVEL); | int nCheckLevel = GetArg("-checklevel", DEFAULT_CHECKLEVEL); | ||||
▲ Show 20 Lines • Show All 538 Lines • Show Last 20 Lines |