Skip to content
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
31 changes: 21 additions & 10 deletions src/evo/cbtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <llmq/commitment.h>
#include <llmq/options.h>
#include <llmq/quorums.h>
#include <llmq/utils.h>
#include <node/blockstorage.h>

#include <chain.h>
Expand Down Expand Up @@ -74,11 +75,12 @@ auto CachedGetQcHashesQcIndexedHashes(const CBlockIndex* pindexPrev, const llmq:

static Mutex cs_cache;
static std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> quorums_cached GUARDED_BY(cs_cache);
static std::map<Consensus::LLMQType, unordered_lru_cache<uint256, std::pair<uint256, int>, StaticSaltedHasher>> qc_hashes_cached
GUARDED_BY(cs_cache);
static QcHashMap qcHashes_cached GUARDED_BY(cs_cache);
static QcIndexedHashMap qcIndexedHashes_cached GUARDED_BY(cs_cache);

LOCK(cs_cache);

if (quorums == quorums_cached) {
return std::make_pair(qcHashes_cached, qcIndexedHashes_cached);
}
Expand All @@ -87,6 +89,9 @@ auto CachedGetQcHashesQcIndexedHashes(const CBlockIndex* pindexPrev, const llmq:
quorums_cached.clear();
qcHashes_cached.clear();
qcIndexedHashes_cached.clear();
if (qc_hashes_cached.empty()) {
llmq::utils::InitQuorumsCache(qc_hashes_cached);
}

for (const auto& [llmqType, vecBlockIndexes] : quorums) {
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
Expand All @@ -96,21 +101,27 @@ auto CachedGetQcHashesQcIndexedHashes(const CBlockIndex* pindexPrev, const llmq:
vec_hashes.reserve(vecBlockIndexes.size());
auto& map_indexed_hashes = qcIndexedHashes_cached[llmqType];
for (const auto& blockIndex : vecBlockIndexes) {
uint256 dummyHash;
llmq::CFinalCommitmentPtr pqc = quorum_block_processor.GetMinedCommitment(llmqType, blockIndex->GetBlockHash(), dummyHash);
if (pqc == nullptr) {
// this should never happen
return std::nullopt;
uint256 block_hash{blockIndex->GetBlockHash()};

std::pair<uint256, int> qc_hash;
if (!qc_hashes_cached[llmqType].get(block_hash, qc_hash)) {
auto [pqc, dummy_hash] = quorum_block_processor.GetMinedCommitment(llmqType, block_hash);
if (dummy_hash == uint256::ZERO) {
// this should never happen
return std::nullopt;
}
qc_hash.first = ::SerializeHash(pqc);
qc_hash.second = rotation_enabled ? pqc.quorumIndex : 0;
qc_hashes_cached[llmqType].insert(block_hash, qc_hash);
}
auto qcHash = ::SerializeHash(*pqc);
if (rotation_enabled) {
map_indexed_hashes[pqc->quorumIndex] = qcHash;
map_indexed_hashes[qc_hash.second] = qc_hash.first;
} else {
vec_hashes.emplace_back(qcHash);
vec_hashes.emplace_back(qc_hash.first);
}
}
}
quorums_cached = quorums;
std::swap(quorums_cached, quorums);
return std::make_pair(qcHashes_cached, qcIndexedHashes_cached);
}

Expand Down
11 changes: 6 additions & 5 deletions src/evo/smldiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <consensus/merkle.h>
#include <core_io.h>
#include <deploymentstatus.h>
#include <evo/cbtx.h>
#include <evo/deterministicmns.h>
#include <evo/specialtx.h>
#include <llmq/blockprocessor.h>
Expand All @@ -23,6 +22,9 @@

using node::ReadBlockFromDisk;

// Forward declaration
std::optional<std::pair<CBLSSignature, uint32_t>> GetNonNullCoinbaseChainlock(const CBlockIndex* pindex);

CSimplifiedMNListDiff::CSimplifiedMNListDiff() = default;

CSimplifiedMNListDiff::~CSimplifiedMNListDiff() = default;
Expand Down Expand Up @@ -54,12 +56,11 @@ bool CSimplifiedMNListDiff::BuildQuorumsDiff(const CBlockIndex* baseBlockIndex,
for (const auto& p : quorumHashes) {
const auto& [llmqType, hash] = p;
if (!baseQuorumHashes.count(p)) {
uint256 minedBlockHash;
llmq::CFinalCommitmentPtr qc = quorum_block_processor.GetMinedCommitment(llmqType, hash, minedBlockHash);
if (qc == nullptr) {
auto [qc, minedBlockHash] = quorum_block_processor.GetMinedCommitment(llmqType, hash);
if (minedBlockHash == uint256::ZERO) {
return false;
}
newQuorums.emplace_back(*qc);
newQuorums.emplace_back(std::move(qc));
}
}

Expand Down
42 changes: 15 additions & 27 deletions src/llmq/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,15 @@ bool CQuorumBlockProcessor::HasMinedCommitment(Consensus::LLMQType llmqType, con
return fExists;
}

CFinalCommitmentPtr CQuorumBlockProcessor::GetMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash, uint256& retMinedBlockHash) const
std::pair<CFinalCommitment, uint256> CQuorumBlockProcessor::GetMinedCommitment(Consensus::LLMQType llmqType,
const uint256& quorumHash) const
{
auto key = std::make_pair(DB_MINED_COMMITMENT, std::make_pair(llmqType, quorumHash));
std::pair<CFinalCommitment, uint256> p;
if (!m_evoDb.Read(key, p)) {
return nullptr;
std::pair<CFinalCommitment, uint256> ret;
if (!m_evoDb.Read(key, ret)) {
return {CFinalCommitment{}, uint256::ZERO};
}
retMinedBlockHash = p.second;
return std::make_unique<CFinalCommitment>(p.first);
return ret;
}

// The returned quorums are in reversed order, so the most recent one is at index 0
Expand Down Expand Up @@ -572,16 +572,17 @@ std::optional<const CBlockIndex*> CQuorumBlockProcessor::GetLastMinedCommitments
return std::nullopt;
}

std::vector<std::pair<int, const CBlockIndex*>> CQuorumBlockProcessor::GetLastMinedCommitmentsPerQuorumIndexUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t cycle) const
std::vector<const CBlockIndex*> CQuorumBlockProcessor::GetLastMinedCommitmentsPerQuorumIndexUntilBlock(
Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t cycle) const
{
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
assert(llmq_params_opt.has_value());
std::vector<std::pair<int, const CBlockIndex*>> ret;
std::vector<const CBlockIndex*> ret;

for (const auto quorumIndex : irange::range(llmq_params_opt->signingActiveQuorumCount)) {
std::optional<const CBlockIndex*> q = GetLastMinedCommitmentsByQuorumIndexUntilBlock(llmqType, pindex, quorumIndex, cycle);
if (q.has_value()) {
ret.emplace_back(quorumIndex, q.value());
ret.emplace_back(q.value());
}
}

Expand All @@ -595,23 +596,14 @@ std::vector<const CBlockIndex*> CQuorumBlockProcessor::GetMinedCommitmentsIndexe
size_t cycle = 0;

while (ret.size() < maxCount) {
std::vector<std::pair<int, const CBlockIndex*>> cycleRet = GetLastMinedCommitmentsPerQuorumIndexUntilBlock(llmqType, pindex, cycle);
std::vector<const CBlockIndex*> cycleRet = GetLastMinedCommitmentsPerQuorumIndexUntilBlock(llmqType, pindex, cycle);

if (cycleRet.empty()) {
return ret;
}

std::vector<const CBlockIndex*> cycleRetTransformed;
std::transform(cycleRet.begin(),
cycleRet.end(),
std::back_inserter(cycleRetTransformed),
[](const std::pair<int, const CBlockIndex*>& p) { return p.second; });

size_t needToCopy = maxCount - ret.size();

std::copy_n(cycleRetTransformed.begin(),
std::min(needToCopy, cycleRetTransformed.size()),
std::back_inserter(ret));
std::copy_n(cycleRet.begin(), std::min(needToCopy, cycleRet.size()), std::back_inserter(ret));
cycle++;
}

Expand All @@ -624,15 +616,11 @@ std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> CQuorumBlockProce
std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> ret;

for (const auto& params : Params().GetConsensus().llmqs) {
auto& v = ret[params.type];
v.reserve(params.signingActiveQuorumCount);
auto& commitments = ret[params.type];
if (IsQuorumRotationEnabled(params, pindex)) {
std::vector<std::pair<int, const CBlockIndex*>> commitments = GetLastMinedCommitmentsPerQuorumIndexUntilBlock(params.type, pindex, 0);
std::transform(commitments.begin(), commitments.end(), std::back_inserter(v),
[](const std::pair<int, const CBlockIndex*>& p) { return p.second; });
commitments = GetLastMinedCommitmentsPerQuorumIndexUntilBlock(params.type, pindex, 0);
} else {
auto commitments = GetMinedCommitmentsUntilBlock(params.type, pindex, params.signingActiveQuorumCount);
std::copy(commitments.begin(), commitments.end(), std::back_inserter(v));
commitments = GetMinedCommitmentsUntilBlock(params.type, pindex, params.signingActiveQuorumCount);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/llmq/blockprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ namespace llmq
class CFinalCommitment;
class CQuorumSnapshotManager;

using CFinalCommitmentPtr = std::unique_ptr<CFinalCommitment>;

class CQuorumBlockProcessor
{
private:
Expand Down Expand Up @@ -65,13 +63,15 @@ class CQuorumBlockProcessor
std::optional<std::vector<CFinalCommitment>> GetMineableCommitments(const Consensus::LLMQParams& llmqParams, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
bool GetMineableCommitmentsTx(const Consensus::LLMQParams& llmqParams, int nHeight, std::vector<CTransactionRef>& ret) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
bool HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash) const;
CFinalCommitmentPtr GetMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash, uint256& retMinedBlockHash) const;
std::pair<CFinalCommitment, uint256> GetMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash) const;

std::vector<const CBlockIndex*> GetMinedCommitmentsUntilBlock(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pindex, size_t maxCount) const;
std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> GetMinedAndActiveCommitmentsUntilBlock(gsl::not_null<const CBlockIndex*> pindex) const;

std::vector<const CBlockIndex*> GetMinedCommitmentsIndexedUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t maxCount) const;
std::vector<std::pair<int, const CBlockIndex*>> GetLastMinedCommitmentsPerQuorumIndexUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t cycle) const;
std::vector<const CBlockIndex*> GetLastMinedCommitmentsPerQuorumIndexUntilBlock(Consensus::LLMQType llmqType,
const CBlockIndex* pindex,
size_t cycle) const;
std::optional<const CBlockIndex*> GetLastMinedCommitmentsByQuorumIndexUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, int quorumIndex, size_t cycle) const;
private:
static bool GetCommitmentsFromBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex, std::multimap<Consensus::LLMQType, CFinalCommitment>& ret, BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
Expand Down
12 changes: 6 additions & 6 deletions src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,20 +399,20 @@ void CQuorumManager::CheckQuorumConnections(CConnman& connman, const Consensus::
CQuorumPtr CQuorumManager::BuildQuorumFromCommitment(const Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool populate_cache) const
{
const uint256& quorumHash{pQuorumBaseBlockIndex->GetBlockHash()};
uint256 minedBlockHash;
CFinalCommitmentPtr qc = quorumBlockProcessor.GetMinedCommitment(llmqType, quorumHash, minedBlockHash);
if (qc == nullptr) {

auto [qc, minedBlockHash] = quorumBlockProcessor.GetMinedCommitment(llmqType, quorumHash);
if (minedBlockHash == uint256::ZERO) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- No mined commitment for llmqType[%d] nHeight[%d] quorumHash[%s]\n", __func__, ToUnderlying(llmqType), pQuorumBaseBlockIndex->nHeight, pQuorumBaseBlockIndex->GetBlockHash().ToString());
return nullptr;
}
assert(qc->quorumHash == pQuorumBaseBlockIndex->GetBlockHash());
assert(qc.quorumHash == pQuorumBaseBlockIndex->GetBlockHash());

const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
assert(llmq_params_opt.has_value());
auto quorum = std::make_shared<CQuorum>(llmq_params_opt.value(), blsWorker);
auto members = utils::GetAllQuorumMembers(qc->llmqType, m_dmnman, m_qsnapman, pQuorumBaseBlockIndex);
auto members = utils::GetAllQuorumMembers(qc.llmqType, m_dmnman, m_qsnapman, pQuorumBaseBlockIndex);

quorum->Init(std::move(qc), pQuorumBaseBlockIndex, minedBlockHash, members);
quorum->Init(std::make_unique<CFinalCommitment>(std::move(qc)), pQuorumBaseBlockIndex, minedBlockHash, members);

if (populate_cache && llmq_params_opt->size == 1) {
WITH_LOCK(cs_map_quorums, mapQuorumsCache[llmqType].insert(quorumHash, quorum));
Expand Down
12 changes: 6 additions & 6 deletions src/llmq/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,17 @@ bool BuildQuorumRotationInfo(CDeterministicMNManager& dmnman, CQuorumSnapshotMan

std::set<int> snapshotHeightsNeeded;

std::vector<std::pair<int, const CBlockIndex*>> qdata = qblockman.GetLastMinedCommitmentsPerQuorumIndexUntilBlock(llmqType, blockIndex, 0);
std::vector<const CBlockIndex*> qdata = qblockman.GetLastMinedCommitmentsPerQuorumIndexUntilBlock(llmqType,
blockIndex, 0);

for (const auto& obj : qdata) {
uint256 minedBlockHash;
llmq::CFinalCommitmentPtr qc = qblockman.GetMinedCommitment(llmqType, obj.second->GetBlockHash(), minedBlockHash);
if (qc == nullptr) {
auto [qc, minedBlockHash] = qblockman.GetMinedCommitment(llmqType, obj->GetBlockHash());
if (minedBlockHash == uint256::ZERO) {
return false;
}
response.lastCommitmentPerIndex.push_back(*qc);
response.lastCommitmentPerIndex.emplace_back(std::move(qc));

int quorumCycleStartHeight = obj.second->nHeight - (obj.second->nHeight % llmq_params_opt->dkgInterval);
int quorumCycleStartHeight = obj->nHeight - (obj->nHeight % llmq_params_opt->dkgInterval);
snapshotHeightsNeeded.insert(quorumCycleStartHeight - cycleLength);
snapshotHeightsNeeded.insert(quorumCycleStartHeight - 2 * cycleLength);
snapshotHeightsNeeded.insert(quorumCycleStartHeight - 3 * cycleLength);
Expand Down
4 changes: 4 additions & 0 deletions src/llmq/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,5 +953,9 @@ template void InitQuorumsCache<std::map<Consensus::LLMQType, unordered_lru_cache
template void InitQuorumsCache<std::map<Consensus::LLMQType, unordered_lru_cache<uint256, std::shared_ptr<llmq::CQuorum>, StaticSaltedHasher, 0ul, 0ul>, std::less<Consensus::LLMQType>, std::allocator<std::pair<Consensus::LLMQType const, unordered_lru_cache<uint256, std::shared_ptr<llmq::CQuorum>, StaticSaltedHasher, 0ul, 0ul>>>>>(std::map<Consensus::LLMQType, unordered_lru_cache<uint256, std::shared_ptr<llmq::CQuorum>, StaticSaltedHasher, 0ul, 0ul>, std::less<Consensus::LLMQType>, std::allocator<std::pair<Consensus::LLMQType const, unordered_lru_cache<uint256, std::shared_ptr<llmq::CQuorum>, StaticSaltedHasher, 0ul, 0ul>>>>&cache, bool limit_by_connections);
template void InitQuorumsCache<std::map<Consensus::LLMQType, unordered_lru_cache<uint256, int, StaticSaltedHasher>>>(std::map<Consensus::LLMQType, unordered_lru_cache<uint256, int, StaticSaltedHasher>>& cache, bool limit_by_connections);
template void InitQuorumsCache<std::map<Consensus::LLMQType, unordered_lru_cache<uint256, uint256, StaticSaltedHasher>>>(std::map<Consensus::LLMQType, unordered_lru_cache<uint256, uint256, StaticSaltedHasher>>& cache, bool limit_by_connections);
template void
InitQuorumsCache<std::map<Consensus::LLMQType, unordered_lru_cache<uint256, std::pair<uint256, int>, StaticSaltedHasher>>>(
std::map<Consensus::LLMQType, unordered_lru_cache<uint256, std::pair<uint256, int>, StaticSaltedHasher>>& cache,
bool limit_by_connections);
} // namespace utils
} // namespace llmq
Loading