Skip to content

Commit df9581b

Browse files
committed
Add CChainLockSig::IsNull() and throw an exception in getbestchainlock if there is no known chainlock yet
1 parent 976447c commit df9581b

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/llmq/quorums_chainlocks.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ static const std::string CLSIG_REQUESTID_PREFIX = "clsig";
2323

2424
CChainLocksHandler* chainLocksHandler;
2525

26+
bool CChainLockSig::IsNull() const
27+
{
28+
return nHeight == -1 && blockHash == uint256();
29+
}
30+
2631
std::string CChainLockSig::ToString() const
2732
{
2833
return strprintf("CChainLockSig(nHeight=%d, blockHash=%s)", nHeight, blockHash.ToString());
@@ -107,7 +112,7 @@ void CChainLocksHandler::ProcessNewChainLock(NodeId from, const llmq::CChainLock
107112
return;
108113
}
109114

110-
if (bestChainLock.nHeight != -1 && clsig.nHeight <= bestChainLock.nHeight) {
115+
if (!bestChainLock.IsNull() && clsig.nHeight <= bestChainLock.nHeight) {
111116
// no need to process/relay older CLSIGs
112117
return;
113118
}

src/llmq/quorums_chainlocks.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CChainLockSig
2424
{
2525
public:
2626
int32_t nHeight{-1};
27-
uint256 blockHash;
27+
uint256 blockHash{uint256()};
2828
CBLSSignature sig;
2929

3030
public:
@@ -38,6 +38,7 @@ class CChainLockSig
3838
READWRITE(sig);
3939
}
4040

41+
bool IsNull() const;
4142
std::string ToString() const;
4243
};
4344

src/rpc/blockchain.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ UniValue getbestchainlock(const JSONRPCRequest& request)
212212
if (request.fHelp || request.params.size() != 0)
213213
throw std::runtime_error(
214214
"getbestchainlock\n"
215-
"\nReturns the block hash of the best chainlock.\n"
215+
"\nReturns the block hash of the best chainlock. Throws an error if there is no known chainlock yet.\n"
216216
"\nResult:\n"
217217
"{\n"
218218
" \"blockhash\" : \"hash\", (string) The block hash hex encoded\n"
@@ -225,6 +225,9 @@ UniValue getbestchainlock(const JSONRPCRequest& request)
225225
);
226226
UniValue result(UniValue::VOBJ);
227227
llmq::CChainLockSig clsig = llmq::chainLocksHandler->GetBestChainLock();
228+
if (clsig.IsNull()) {
229+
throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to find any chainlock");
230+
}
228231
result.push_back(Pair("blockhash", clsig.blockHash.GetHex()));
229232
result.push_back(Pair("height", clsig.nHeight));
230233
LOCK(cs_main);

0 commit comments

Comments
 (0)