Skip to content

Commit cd1809f

Browse files
codablockpanleone
authored andcommitted
Implement persistence for LLMQ based InstantSend (PIVX-Project#2756)
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
1 parent 924ee8f commit cd1809f

9 files changed

Lines changed: 85 additions & 111 deletions

src/llmq/quorums_dkgsession.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "bls/bls_worker.h"
1111
#include "consensus/params.h"
1212
#include "evo/deterministicmns.h"
13-
#include "evo/evodb.h"
1413
#include "net.h"
1514
#include "llmq/quorums_utils.h"
1615
#include "logging.h"
@@ -237,7 +236,6 @@ class CDKGSession
237236
private:
238237
const Consensus::LLMQParams& params;
239238

240-
CEvoDB& evoDb;
241239
CBLSWorker& blsWorker;
242240
CBLSWorkerCache cache;
243241
CDKGSessionManager& dkgManager;
@@ -277,8 +275,8 @@ class CDKGSession
277275
std::set<uint256> validCommitments;
278276

279277
public:
280-
CDKGSession(const Consensus::LLMQParams& _params, CEvoDB& _evoDb, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager) :
281-
params(_params), evoDb(_evoDb), blsWorker(_blsWorker), cache(_blsWorker), dkgManager(_dkgManager) {}
278+
CDKGSession(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager) :
279+
params(_params), blsWorker(_blsWorker), cache(_blsWorker), dkgManager(_dkgManager) {}
282280

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

src/llmq/quorums_dkgsessionhandler.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,11 @@ void CDKGPendingMessages::Clear()
8484

8585
//////
8686

87-
CDKGSessionHandler::CDKGSessionHandler(const Consensus::LLMQParams& _params, CEvoDB& _evoDb, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager) :
87+
CDKGSessionHandler::CDKGSessionHandler(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager) :
8888
params(_params),
89-
evoDb(_evoDb),
9089
blsWorker(_blsWorker),
9190
dkgManager(_dkgManager),
92-
curSession(std::make_shared<CDKGSession>(_params, _evoDb, _blsWorker, _dkgManager)),
91+
curSession(std::make_shared<CDKGSession>(_params, _blsWorker, _dkgManager)),
9392
pendingContributions((size_t)_params.size * 2), // we allow size*2 messages as we need to make sure we see bad behavior (double messages)
9493
pendingComplaints((size_t)_params.size * 2),
9594
pendingJustifications((size_t)_params.size * 2),
@@ -162,7 +161,7 @@ void CDKGSessionHandler::StopThread()
162161

163162
bool CDKGSessionHandler::InitNewQuorum(const CBlockIndex* pindexQuorum)
164163
{
165-
curSession = std::make_shared<CDKGSession>(params, evoDb, blsWorker, dkgManager);
164+
curSession = std::make_shared<CDKGSession>(params, blsWorker, dkgManager);
166165

167166
if (!deterministicMNManager->IsDIP3Enforced(pindexQuorum->nHeight) ||
168167
!activeMasternodeManager) {

src/llmq/quorums_dkgsessionhandler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ class CDKGSessionHandler
100100
std::atomic<bool> stopRequested{false};
101101

102102
const Consensus::LLMQParams& params;
103-
CEvoDB& evoDb;
104103
CBLSWorker& blsWorker;
105104
CDKGSessionManager& dkgManager;
106105

@@ -117,7 +116,7 @@ class CDKGSessionHandler
117116
CDKGPendingMessages pendingPrematureCommitments;
118117

119118
public:
120-
CDKGSessionHandler(const Consensus::LLMQParams& _params, CEvoDB& _evoDb, CBLSWorker& blsWorker, CDKGSessionManager& _dkgManager);
119+
CDKGSessionHandler(const Consensus::LLMQParams& _params, CBLSWorker& blsWorker, CDKGSessionManager& _dkgManager);
121120
~CDKGSessionHandler();
122121

123122
void UpdatedBlockTip(const CBlockIndex *pindexNew);

src/llmq/quorums_dkgsessionmgr.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ std::unique_ptr<CDKGSessionManager> quorumDKGSessionManager{nullptr};
1919
static const std::string DB_VVEC = "qdkg_V";
2020
static const std::string DB_SKCONTRIB = "qdkg_S";
2121

22-
CDKGSessionManager::CDKGSessionManager(CEvoDB& _evoDb, CBLSWorker& _blsWorker) :
23-
evoDb(_evoDb),
24-
blsWorker(_blsWorker)
22+
CDKGSessionManager::CDKGSessionManager(CDBWrapper& _llmqDb, CBLSWorker& _blsWorker) : llmqDb(_llmqDb),
23+
blsWorker(_blsWorker)
2524
{
2625
for (const auto& qt : Params().GetConsensus().llmqs) {
2726
dkgSessionHandlers.emplace(std::piecewise_construct,
2827
std::forward_as_tuple(qt.first),
29-
std::forward_as_tuple(qt.second, evoDb, blsWorker, *this));
28+
std::forward_as_tuple(qt.second, blsWorker, *this));
3029
}
3130
}
3231

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

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

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

171170
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)
@@ -209,10 +208,10 @@ bool CDKGSessionManager::GetVerifiedContribution(Consensus::LLMQType llmqType, c
209208
BLSVerificationVector vvec;
210209
BLSVerificationVectorPtr vvecPtr;
211210
CBLSSecretKey skContribution;
212-
if (evoDb.GetRawDB().Read(std::make_tuple(DB_VVEC, (uint8_t)llmqType, quorumHash, proTxHash), vvec)) {
211+
if (llmqDb.Read(std::make_tuple(DB_VVEC, (uint8_t)llmqType, quorumHash, proTxHash), vvec)) {
213212
vvecPtr = std::make_shared<BLSVerificationVector>(std::move(vvec));
214213
}
215-
evoDb.GetRawDB().Read(std::make_tuple(DB_SKCONTRIB, (uint8_t)llmqType, quorumHash, proTxHash), skContribution);
214+
llmqDb.Read(std::make_tuple(DB_SKCONTRIB, (uint8_t)llmqType, quorumHash, proTxHash), skContribution);
216215

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

src/llmq/quorums_dkgsessionmgr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CDKGSessionManager
2020
static const int64_t MAX_CONTRIBUTION_CACHE_TIME = 60 * 1000;
2121

2222
private:
23-
CEvoDB& evoDb;
23+
CDBWrapper& llmqDb;
2424
CBLSWorker& blsWorker;
2525

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

4747
public:
48-
CDKGSessionManager(CEvoDB& _evoDb, CBLSWorker& _blsWorker);
48+
CDKGSessionManager(CDBWrapper& _evoDb, CBLSWorker& _blsWorker);
4949
~CDKGSessionManager() {};
5050

5151
void StartThreads();

src/llmq/quorums_init.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "llmq/quorums_init.h"
77

88
#include "bls/bls_worker.h"
9+
#include "dbwrapper.h"
910
#include "llmq/quorums.h"
1011
#include "llmq/quorums_blockprocessor.h"
1112
#include "llmq/quorums_debug.h"
@@ -18,17 +19,19 @@ namespace llmq
1819
{
1920

2021
CBLSWorker* blsWorker;
22+
CDBWrapper* llmqDb;
2123

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

2629
quorumDKGDebugManager.reset(new CDKGDebugManager());
2730
quorumBlockProcessor.reset(new CQuorumBlockProcessor(evoDb));
28-
quorumDKGSessionManager.reset(new CDKGSessionManager(evoDb, *blsWorker));
31+
quorumDKGSessionManager.reset(new CDKGSessionManager(*llmqDb, *blsWorker));
2932
quorumManager.reset(new CQuorumManager(evoDb, *blsWorker, *quorumDKGSessionManager));
3033
quorumSigSharesManager.reset(new CSigSharesManager());
31-
quorumSigningManager.reset(new CSigningManager(unitTests));
34+
quorumSigningManager.reset(new CSigningManager(*llmqDb, unitTests));
3235
chainLocksHandler.reset(new CChainLocksHandler(scheduler));
3336
}
3437

@@ -43,6 +46,8 @@ void DestroyLLMQSystem()
4346
quorumManager.reset();
4447
delete blsWorker;
4548
blsWorker = nullptr;
49+
delete llmqDb;
50+
llmqDb = nullptr;
4651
}
4752

4853
void StartLLMQSystem()

src/llmq/quorums_init.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class CEvoDB;
1414
namespace llmq
1515
{
1616

17+
extern CDBWrapper* llmqDb;
18+
1719
// Init/destroy LLMQ globals
1820
void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests);
1921
void DestroyLLMQSystem();

0 commit comments

Comments
 (0)