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

Introduce getbestchainlock rpc and fix llmq-is-cl-conflicts.py #3094

Merged
merged 4 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/llmq/quorums_chainlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ bool CChainLocksHandler::GetChainLockByHash(const uint256& hash, llmq::CChainLoc
return true;
}

CChainLockSig CChainLocksHandler::GetBestChainLock()
{
LOCK(cs);
return bestChainLock;
}

void CChainLocksHandler::ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman)
{
if (!sporkManager.IsSporkActive(SPORK_19_CHAINLOCKS_ENABLED)) {
Expand Down
1 change: 1 addition & 0 deletions src/llmq/quorums_chainlocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class CChainLocksHandler : public CRecoveredSigsListener

bool AlreadyHave(const CInv& inv);
bool GetChainLockByHash(const uint256& hash, CChainLockSig& ret);
CChainLockSig GetBestChainLock();

void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
void ProcessNewChainLock(NodeId from, const CChainLockSig& clsig, const uint256& hash);
Expand Down
23 changes: 23 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,28 @@ UniValue getbestblockhash(const JSONRPCRequest& request)
return chainActive.Tip()->GetBlockHash().GetHex();
}

UniValue getbestchainlock(const JSONRPCRequest& request)
UdjinM6 marked this conversation as resolved.
Show resolved Hide resolved
{
if (request.fHelp || request.params.size() != 0)
throw std::runtime_error(
"getbestchainlock\n"
"\nReturns the block hash of the best chainlock.\n"
"\nResult:\n"
"{\n"
" \"blockhash\" : \"hash\", (string) The block hash hex encoded\n"
" \"height\" : n, (numeric) The block height or index\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("getbestchainlock", "")
+ HelpExampleRpc("getbestchainlock", "")
);
UniValue result(UniValue::VOBJ);
llmq::CChainLockSig clsig = llmq::chainLocksHandler->GetBestChainLock();
UdjinM6 marked this conversation as resolved.
Show resolved Hide resolved
result.push_back(Pair("blockhash", clsig.blockHash.GetHex()));
result.push_back(Pair("height", clsig.nHeight));
return result;
}

void RPCNotifyBlockChange(bool ibd, const CBlockIndex * pindex)
{
if(pindex) {
Expand Down Expand Up @@ -2184,6 +2206,7 @@ static const CRPCCommand commands[] =
{ "blockchain", "preciousblock", &preciousblock, true, {"blockhash"} },

/* Not shown in help */
{ "hidden", "getbestchainlock", &getbestchainlock, true, {} },
UdjinM6 marked this conversation as resolved.
Show resolved Hide resolved
{ "hidden", "invalidateblock", &invalidateblock, true, {"blockhash"} },
{ "hidden", "reconsiderblock", &reconsiderblock, true, {"blockhash"} },
{ "hidden", "waitfornewblock", &waitfornewblock, true, {"timeout"} },
Expand Down
15 changes: 12 additions & 3 deletions test/functional/llmq-is-cl-conflicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ def test_chainlock_overrides_islock(self, test_block_conflict):
cl = self.create_chainlock(self.nodes[0].getblockcount() + 1, block.sha256)
self.test_node.send_clsig(cl)

# Give the CLSIG some time to propagate. We unfortunately can't check propagation here as "getblock/getblockheader"
# is required to check for CLSIGs, but this requires the block header to be propagated already
time.sleep(1)
self.wait_for_best_chainlock(self.nodes[1], "%064x" % block.sha256)

# The block should get accepted now, and at the same time prune the conflicting ISLOCKs
submit_result = self.nodes[1].submitblock(ToHex(block))
Expand Down Expand Up @@ -223,6 +221,17 @@ def wait_for_chainlock(self, node, block_hash):
time.sleep(0.1)
raise AssertionError("wait_for_chainlock timed out")

def wait_for_best_chainlock(self, node, block_hash):
t = time.time()
while time.time() - t < 15:
try:
if node.getbestchainlock()["blockhash"] == block_hash:
return
except:
pass
time.sleep(0.1)
raise AssertionError("wait_for_best_chainlock timed out")

def create_block(self, node, vtx=[]):
bt = node.getblocktemplate()
height = bt['height']
Expand Down