Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC: Remove cs_main lock from blockToJSON, blockHeaderToJSON and TxToJSON #2732

Merged
merged 2 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ struct CCoin {
}
};

extern void TxToJSON(CWallet* const pwallet, const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
extern UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false);
extern void TxToJSON(CWallet* const pwallet, const CTransaction& tx, const CBlockIndex* tip, const CBlockIndex* blockindex, UniValue& entry);
extern UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false);
extern UniValue mempoolInfoToJSON();
extern UniValue mempoolToJSON(bool fVerbose = false);
extern UniValue blockheaderToJSON(const CBlockIndex* blockindex);
extern UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex);

static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string message)
{
Expand Down Expand Up @@ -127,10 +127,12 @@ static bool rest_headers(HTTPRequest* req,
if (!ParseHashStr(hashStr, hash))
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);

const CBlockIndex* tip;
std::vector<const CBlockIndex *> headers;
headers.reserve(count);
{
LOCK(cs_main);
tip = chainActive.Tip();
CBlockIndex* pindex = LookupBlockIndex(hash);
while (pindex != NULL && chainActive.Contains(pindex)) {
headers.push_back(pindex);
Expand Down Expand Up @@ -162,7 +164,7 @@ static bool rest_headers(HTTPRequest* req,
case RF_JSON: {
UniValue jsonHeaders(UniValue::VARR);
for (const CBlockIndex *pindex : headers) {
jsonHeaders.push_back(blockheaderToJSON(pindex));
jsonHeaders.push_back(blockheaderToJSON(tip, pindex));
}
std::string strJSON = jsonHeaders.write() + "\n";
req->WriteHeader("Content-Type", "application/json");
Expand Down Expand Up @@ -190,13 +192,16 @@ static bool rest_block(HTTPRequest* req,
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);

CBlock block;
CBlockIndex* pblockindex = NULL;
const CBlockIndex* pblockindex;
const CBlockIndex* tip;
{
LOCK(cs_main);
if (mapBlockIndex.count(hash) == 0)
tip = chainActive.Tip();
pblockindex = LookupBlockIndex(hash);
if (!pblockindex) {
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
}

pblockindex = mapBlockIndex[hash];
if (!(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0)
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");

Expand All @@ -223,7 +228,7 @@ static bool rest_block(HTTPRequest* req,
}

case RF_JSON: {
UniValue objBlock = WITH_LOCK(cs_main, return blockToJSON(block, pblockindex, showTxDetails); );
UniValue objBlock = blockToJSON(block, tip, pblockindex, showTxDetails);
std::string strJSON = objBlock.write() + "\n";
req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, strJSON);
Expand Down Expand Up @@ -352,8 +357,15 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
}

case RF_JSON: {
const CBlockIndex* pblockindex;
const CBlockIndex* tip;
{
LOCK(cs_main);
tip = chainActive.Tip();
pblockindex = LookupBlockIndex(hashBlock);
}
UniValue objTx(UniValue::VOBJ);
WITH_LOCK(cs_main, TxToJSON(nullptr, *tx, hashBlock, objTx); );
TxToJSON(nullptr, *tx, tip, pblockindex, objTx);
std::string strJSON = objTx.write() + "\n";
req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, strJSON);
Expand Down
50 changes: 26 additions & 24 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static std::mutex cs_blockchange;
static std::condition_variable cond_blockchange;
static CUpdatedBlock latestblock;

extern void TxToJSON(CWallet* const pwallet, const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
extern void TxToJSON(CWallet* const pwallet, const CTransaction& tx, const CBlockIndex* tip, const CBlockIndex* blockindex, UniValue& entry);

UniValue syncwithvalidationinterfacequeue(const JSONRPCRequest& request)
{
Expand Down Expand Up @@ -99,14 +99,22 @@ static UniValue ValuePoolDesc(
return rv;
}

UniValue blockheaderToJSON(const CBlockIndex* blockindex)
int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* blockindex, const CBlockIndex*& next)
{
next = tip->GetAncestor(blockindex->nHeight + 1);
if (next && next->pprev == blockindex) {
return tip->nHeight - blockindex->nHeight + 1;
}
next = nullptr;
return blockindex == tip ? 1 : -1;
}

UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex)
{
UniValue result(UniValue::VOBJ);
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
int confirmations = -1;
// Only report confirmations if the block is on the main chain
if (chainActive.Contains(blockindex))
confirmations = chainActive.Height() - blockindex->nHeight + 1;
const CBlockIndex* pnext;
int confirmations = ComputeNextBlockAndDepth(tip, blockindex, pnext);
result.pushKV("confirmations", confirmations);
result.pushKV("height", blockindex->nHeight);
result.pushKV("version", blockindex->nVersion);
Expand All @@ -122,22 +130,17 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
result.pushKV("shield_pool_value", ValuePoolDesc(blockindex->nChainSaplingValue, blockindex->nSaplingValue));
if (blockindex->pprev)
result.pushKV("previousblockhash", blockindex->pprev->GetBlockHash().GetHex());
CBlockIndex *pnext = chainActive.Next(blockindex);
if (pnext)
result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex());
return result;
}

UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false)
{
AssertLockHeld(cs_main);

UniValue result(UniValue::VOBJ);
result.pushKV("hash", block.GetHash().GetHex());
int confirmations = -1;
// Only report confirmations if the block is on the main chain
if (chainActive.Contains(blockindex))
confirmations = chainActive.Height() - blockindex->nHeight + 1;
const CBlockIndex* pnext;
int confirmations = ComputeNextBlockAndDepth(tip, blockindex, pnext);
result.pushKV("confirmations", confirmations);
result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION));
result.pushKV("height", blockindex->nHeight);
Expand All @@ -150,7 +153,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
const CTransaction& tx = *txIn;
if (txDetails) {
UniValue objTx(UniValue::VOBJ);
TxToJSON(nullptr, tx, UINT256_ZERO, objTx);
TxToJSON(nullptr, tx, nullptr, nullptr, objTx);
txs.push_back(objTx);
} else
txs.push_back(tx.GetHash().GetHex());
Expand All @@ -165,7 +168,6 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx

if (blockindex->pprev)
result.pushKV("previousblockhash", blockindex->pprev->GetBlockHash().GetHex());
CBlockIndex* pnext = chainActive.Next(blockindex);
if (pnext)
result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex());

Expand Down Expand Up @@ -574,12 +576,11 @@ UniValue getblock(const JSONRPCRequest& request)
if (request.params.size() > 1)
fVerbose = request.params[1].get_bool();

if (mapBlockIndex.count(hash) == 0)
CBlockIndex* pblockindex = LookupBlockIndex(hash);
if (pblockindex == nullptr)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");

CBlock block;
CBlockIndex* pblockindex = mapBlockIndex[hash];

if (!ReadBlockFromDisk(block, pblockindex))
throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");

Expand All @@ -590,7 +591,7 @@ UniValue getblock(const JSONRPCRequest& request)
return strHex;
}

return blockToJSON(block, pblockindex);
return blockToJSON(block, chainActive.Tip(), pblockindex);
}

UniValue getblockheader(const JSONRPCRequest& request)
Expand Down Expand Up @@ -629,26 +630,27 @@ UniValue getblockheader(const JSONRPCRequest& request)
HelpExampleCli("getblockheader", "\"00000000000fd08c2fb661d2fcb0d49abb3a91e5f27082ce64feed3b4dede2e2\"") +
HelpExampleRpc("getblockheader", "\"00000000000fd08c2fb661d2fcb0d49abb3a91e5f27082ce64feed3b4dede2e2\""));

LOCK(cs_main);

std::string strHash = request.params[0].get_str();
uint256 hash(uint256S(strHash));

bool fVerbose = true;
if (request.params.size() > 1)
fVerbose = request.params[1].get_bool();

if (mapBlockIndex.count(hash) == 0)
CBlockIndex* pblockindex = LookupBlockIndex(hash);
if (pblockindex == nullptr)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");

CBlockIndex* pblockindex = mapBlockIndex[hash];

if (!fVerbose) {
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
ssBlock << pblockindex->GetBlockHeader();
std::string strHex = HexStr(ssBlock);
return strHex;
}

return blockheaderToJSON(pblockindex);
return blockheaderToJSON(chainActive.Tip(), pblockindex);
}

UniValue getsupplyinfo(const JSONRPCRequest& request)
Expand Down
37 changes: 21 additions & 16 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,17 @@ static void PayloadToJSON(const CTransaction& tx, UniValue& entry)
}
}

// pwallet can be nullptr. If not null, the json could include information available only to the wallet.
void TxToJSON(CWallet* const pwallet, const CTransaction& tx, const uint256 hashBlock, UniValue& entry) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
extern int ComputeNextBlockAndDepth(const CBlockIndex* tip, const CBlockIndex* blockindex, const CBlockIndex*& next);

static int ComputeConfirmations(const CBlockIndex* tip, const CBlockIndex* blockindex)
{
AssertLockHeld(cs_main);
const CBlockIndex* next{nullptr};
return ComputeNextBlockAndDepth(tip, blockindex, next);
}

// pwallet can be nullptr. If not null, the json could include information available only to the wallet.
void TxToJSON(CWallet* const pwallet, const CTransaction& tx, const CBlockIndex* tip, const CBlockIndex* blockindex, UniValue& entry)
{
// Call into TxToUniv() in bitcoin-common to decode the transaction hex.
//
// Blockchain contextual information (confirmations and blocktime) is not
Expand All @@ -99,17 +105,15 @@ void TxToJSON(CWallet* const pwallet, const CTransaction& tx, const uint256 hash
PayloadToJSON(tx, entry);
}

if (!hashBlock.IsNull()) {
entry.pushKV("blockhash", hashBlock.GetHex());
CBlockIndex* pindex = LookupBlockIndex(hashBlock);
if (pindex) {
if (chainActive.Contains(pindex)) {
entry.pushKV("confirmations", 1 + chainActive.Height() - pindex->nHeight);
entry.pushKV("time", pindex->GetBlockTime());
entry.pushKV("blocktime", pindex->GetBlockTime());
}
else
entry.pushKV("confirmations", 0);
if (blockindex && tip) {
entry.pushKV("blockhash", blockindex->GetBlockHash().ToString());
int confirmations = ComputeConfirmations(tip, blockindex);
if (confirmations != -1) {
entry.pushKV("confirmations", confirmations);
entry.pushKV("time", blockindex->GetBlockTime());
entry.pushKV("blocktime", blockindex->GetBlockTime());
} else {
entry.pushKV("confirmations", 0);
}
}
}
Expand Down Expand Up @@ -267,8 +271,9 @@ UniValue getrawtransaction(const JSONRPCRequest& request)

UniValue result(UniValue::VOBJ);
if (blockindex) result.pushKV("in_active_chain", in_active_chain);
else blockindex = LookupBlockIndex(hash_block);
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
TxToJSON(pwallet, *tx, hash_block, result);
TxToJSON(pwallet, *tx, chainActive.Tip(), blockindex, result);
return result;
}

Expand Down Expand Up @@ -437,7 +442,7 @@ UniValue decoderawtransaction(const JSONRPCRequest& request)

UniValue result(UniValue::VOBJ);
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
TxToJSON(pwallet, CTransaction(std::move(mtx)), UINT256_ZERO, result);
TxToJSON(pwallet, CTransaction(std::move(mtx)), nullptr, nullptr, result);

return result;
}
Expand Down