Skip to content

Commit

Permalink
Implement persistence for LLMQ based InstantSend (PIVX-Project#2756)
Browse files Browse the repository at this point in the history
Don't pass CEvoDB to CDKGSessionHandler and CDKGSession

Was never used.

Introduce global llmq::llmqDb instance of CDBWrapper

This DB is for LLMQ related data that is not part of on-chain consensus.
This for example included LLMQ secret key shares and recovered signatures.

Don't use CEvoDB in CDKGSessionManager and instead use llmqDb

Contributions are not part of on-chain consensus and shouldn't be stored in
CEvoDB.

Use llmqDb for CRecoveredSigsDb

Instead of creating its own CDBWrapper internally.

Add key prefix to "rs_" for CRecoveredSigsDb keys

This avoids future conflicts with other data stored in llmqDb

Add HasVotedOnId/GetVoteForId to CSigningManager

These just pass through to CRecoveredSigsDb

Use unordered_lru_cache in CRecoveredSigsDb
  • Loading branch information
codablock authored and panleone committed Nov 5, 2024
1 parent 924ee8f commit cd1809f
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 111 deletions.
6 changes: 2 additions & 4 deletions src/llmq/quorums_dkgsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "bls/bls_worker.h"
#include "consensus/params.h"
#include "evo/deterministicmns.h"
#include "evo/evodb.h"
#include "net.h"
#include "llmq/quorums_utils.h"
#include "logging.h"
Expand Down Expand Up @@ -237,7 +236,6 @@ class CDKGSession
private:
const Consensus::LLMQParams& params;

CEvoDB& evoDb;
CBLSWorker& blsWorker;
CBLSWorkerCache cache;
CDKGSessionManager& dkgManager;
Expand Down Expand Up @@ -277,8 +275,8 @@ class CDKGSession
std::set<uint256> validCommitments;

public:
CDKGSession(const Consensus::LLMQParams& _params, CEvoDB& _evoDb, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager) :
params(_params), evoDb(_evoDb), blsWorker(_blsWorker), cache(_blsWorker), dkgManager(_dkgManager) {}
CDKGSession(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager) :
params(_params), blsWorker(_blsWorker), cache(_blsWorker), dkgManager(_dkgManager) {}

bool Init(const CBlockIndex* _pindexQuorum, const std::vector<CDeterministicMNCPtr>& mns, const uint256& _myProTxHash);

Expand Down
7 changes: 3 additions & 4 deletions src/llmq/quorums_dkgsessionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ void CDKGPendingMessages::Clear()

//////

CDKGSessionHandler::CDKGSessionHandler(const Consensus::LLMQParams& _params, CEvoDB& _evoDb, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager) :
CDKGSessionHandler::CDKGSessionHandler(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager) :
params(_params),
evoDb(_evoDb),
blsWorker(_blsWorker),
dkgManager(_dkgManager),
curSession(std::make_shared<CDKGSession>(_params, _evoDb, _blsWorker, _dkgManager)),
curSession(std::make_shared<CDKGSession>(_params, _blsWorker, _dkgManager)),
pendingContributions((size_t)_params.size * 2), // we allow size*2 messages as we need to make sure we see bad behavior (double messages)
pendingComplaints((size_t)_params.size * 2),
pendingJustifications((size_t)_params.size * 2),
Expand Down Expand Up @@ -162,7 +161,7 @@ void CDKGSessionHandler::StopThread()

bool CDKGSessionHandler::InitNewQuorum(const CBlockIndex* pindexQuorum)
{
curSession = std::make_shared<CDKGSession>(params, evoDb, blsWorker, dkgManager);
curSession = std::make_shared<CDKGSession>(params, blsWorker, dkgManager);

if (!deterministicMNManager->IsDIP3Enforced(pindexQuorum->nHeight) ||
!activeMasternodeManager) {
Expand Down
3 changes: 1 addition & 2 deletions src/llmq/quorums_dkgsessionhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class CDKGSessionHandler
std::atomic<bool> stopRequested{false};

const Consensus::LLMQParams& params;
CEvoDB& evoDb;
CBLSWorker& blsWorker;
CDKGSessionManager& dkgManager;

Expand All @@ -117,7 +116,7 @@ class CDKGSessionHandler
CDKGPendingMessages pendingPrematureCommitments;

public:
CDKGSessionHandler(const Consensus::LLMQParams& _params, CEvoDB& _evoDb, CBLSWorker& blsWorker, CDKGSessionManager& _dkgManager);
CDKGSessionHandler(const Consensus::LLMQParams& _params, CBLSWorker& blsWorker, CDKGSessionManager& _dkgManager);
~CDKGSessionHandler();

void UpdatedBlockTip(const CBlockIndex *pindexNew);
Expand Down
15 changes: 7 additions & 8 deletions src/llmq/quorums_dkgsessionmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ std::unique_ptr<CDKGSessionManager> quorumDKGSessionManager{nullptr};
static const std::string DB_VVEC = "qdkg_V";
static const std::string DB_SKCONTRIB = "qdkg_S";

CDKGSessionManager::CDKGSessionManager(CEvoDB& _evoDb, CBLSWorker& _blsWorker) :
evoDb(_evoDb),
blsWorker(_blsWorker)
CDKGSessionManager::CDKGSessionManager(CDBWrapper& _llmqDb, CBLSWorker& _blsWorker) : llmqDb(_llmqDb),
blsWorker(_blsWorker)
{
for (const auto& qt : Params().GetConsensus().llmqs) {
dkgSessionHandlers.emplace(std::piecewise_construct,
std::forward_as_tuple(qt.first),
std::forward_as_tuple(qt.second, evoDb, blsWorker, *this));
std::forward_as_tuple(qt.second, blsWorker, *this));
}
}

Expand Down Expand Up @@ -160,12 +159,12 @@ bool CDKGSessionManager::GetPrematureCommitment(const uint256& hash, CDKGPrematu

void CDKGSessionManager::WriteVerifiedVvecContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const BLSVerificationVectorPtr& vvec)
{
evoDb.GetRawDB().Write(std::make_tuple(DB_VVEC, (uint8_t)llmqType, pindexQuorum->GetBlockHash(), proTxHash), *vvec);
llmqDb.Write(std::make_tuple(DB_VVEC, (uint8_t)llmqType, pindexQuorum->GetBlockHash(), proTxHash), *vvec);
}

void CDKGSessionManager::WriteVerifiedSkContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const CBLSSecretKey& skContribution)
{
evoDb.GetRawDB().Write(std::make_tuple(DB_SKCONTRIB, (uint8_t)llmqType, pindexQuorum->GetBlockHash(), proTxHash), skContribution);
llmqDb.Write(std::make_tuple(DB_SKCONTRIB, (uint8_t)llmqType, pindexQuorum->GetBlockHash(), proTxHash), skContribution);
}

bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const std::vector<bool>& validMembers, std::vector<uint16_t>& memberIndexesRet, std::vector<BLSVerificationVectorPtr>& vvecsRet, BLSSecretKeyVector& skContributionsRet)
Expand Down Expand Up @@ -209,10 +208,10 @@ bool CDKGSessionManager::GetVerifiedContribution(Consensus::LLMQType llmqType, c
BLSVerificationVector vvec;
BLSVerificationVectorPtr vvecPtr;
CBLSSecretKey skContribution;
if (evoDb.GetRawDB().Read(std::make_tuple(DB_VVEC, (uint8_t)llmqType, quorumHash, proTxHash), vvec)) {
if (llmqDb.Read(std::make_tuple(DB_VVEC, (uint8_t)llmqType, quorumHash, proTxHash), vvec)) {
vvecPtr = std::make_shared<BLSVerificationVector>(std::move(vvec));
}
evoDb.GetRawDB().Read(std::make_tuple(DB_SKCONTRIB, (uint8_t)llmqType, quorumHash, proTxHash), skContribution);
llmqDb.Read(std::make_tuple(DB_SKCONTRIB, (uint8_t)llmqType, quorumHash, proTxHash), skContribution);

it = contributionsCache.emplace(cacheKey, ContributionsCacheEntry{GetTimeMillis(), vvecPtr, skContribution}).first;

Expand Down
4 changes: 2 additions & 2 deletions src/llmq/quorums_dkgsessionmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CDKGSessionManager
static const int64_t MAX_CONTRIBUTION_CACHE_TIME = 60 * 1000;

private:
CEvoDB& evoDb;
CDBWrapper& llmqDb;
CBLSWorker& blsWorker;

std::map<Consensus::LLMQType, CDKGSessionHandler> dkgSessionHandlers;
Expand All @@ -45,7 +45,7 @@ class CDKGSessionManager
std::map<ContributionsCacheKey, ContributionsCacheEntry> contributionsCache;

public:
CDKGSessionManager(CEvoDB& _evoDb, CBLSWorker& _blsWorker);
CDKGSessionManager(CDBWrapper& _evoDb, CBLSWorker& _blsWorker);
~CDKGSessionManager() {};

void StartThreads();
Expand Down
9 changes: 7 additions & 2 deletions src/llmq/quorums_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "llmq/quorums_init.h"

#include "bls/bls_worker.h"
#include "dbwrapper.h"
#include "llmq/quorums.h"
#include "llmq/quorums_blockprocessor.h"
#include "llmq/quorums_debug.h"
Expand All @@ -18,17 +19,19 @@ namespace llmq
{

CBLSWorker* blsWorker;
CDBWrapper* llmqDb;

void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests)
{
llmqDb = new CDBWrapper(unitTests ? "" : (GetDataDir() / "llmq"), 1 << 20, unitTests, false, CLIENT_VERSION | ADDRV2_FORMAT);
blsWorker = new CBLSWorker();

quorumDKGDebugManager.reset(new CDKGDebugManager());
quorumBlockProcessor.reset(new CQuorumBlockProcessor(evoDb));
quorumDKGSessionManager.reset(new CDKGSessionManager(evoDb, *blsWorker));
quorumDKGSessionManager.reset(new CDKGSessionManager(*llmqDb, *blsWorker));
quorumManager.reset(new CQuorumManager(evoDb, *blsWorker, *quorumDKGSessionManager));
quorumSigSharesManager.reset(new CSigSharesManager());
quorumSigningManager.reset(new CSigningManager(unitTests));
quorumSigningManager.reset(new CSigningManager(*llmqDb, unitTests));
chainLocksHandler.reset(new CChainLocksHandler(scheduler));
}

Expand All @@ -43,6 +46,8 @@ void DestroyLLMQSystem()
quorumManager.reset();
delete blsWorker;
blsWorker = nullptr;
delete llmqDb;
llmqDb = nullptr;
}

void StartLLMQSystem()
Expand Down
2 changes: 2 additions & 0 deletions src/llmq/quorums_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class CEvoDB;
namespace llmq
{

extern CDBWrapper* llmqDb;

// Init/destroy LLMQ globals
void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests);
void DestroyLLMQSystem();
Expand Down
Loading

0 comments on commit cd1809f

Please sign in to comment.