@@ -40,6 +40,7 @@ void CChainLocksHandler::Start()
40
40
{
41
41
quorumSigningManager->RegisterRecoveredSigsListener (this );
42
42
scheduler->scheduleEvery ([&]() {
43
+ CheckActiveState ();
43
44
EnforceBestChainLock ();
44
45
// regularly retry signing the current chaintip as it might have failed before due to missing ixlocks
45
46
TrySignChainTip ();
@@ -153,6 +154,7 @@ void CChainLocksHandler::ProcessNewChainLock(NodeId from, const llmq::CChainLock
153
154
}
154
155
155
156
scheduler->scheduleFromNow ([&]() {
157
+ CheckActiveState ();
156
158
EnforceBestChainLock ();
157
159
}, 0 );
158
160
@@ -177,6 +179,7 @@ void CChainLocksHandler::AcceptedBlockHeader(const CBlockIndex* pindexNew)
177
179
// when EnforceBestChainLock is called later, it might end up invalidating other chains but not activating the
178
180
// CLSIG locked chain. This happens when only the header is known but the block is still missing yet. The usual
179
181
// block processing logic will handle this when the block arrives
182
+ bestChainLockWithKnownBlock = bestChainLock;
180
183
bestChainLockBlockIndex = pindexNew;
181
184
}
182
185
}
@@ -192,13 +195,38 @@ void CChainLocksHandler::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBl
192
195
}
193
196
tryLockChainTipScheduled = true ;
194
197
scheduler->scheduleFromNow ([&]() {
198
+ CheckActiveState ();
195
199
EnforceBestChainLock ();
196
200
TrySignChainTip ();
197
201
LOCK (cs);
198
202
tryLockChainTipScheduled = false ;
199
203
}, 0 );
200
204
}
201
205
206
+ void CChainLocksHandler::CheckActiveState ()
207
+ {
208
+ bool fDIP0008Active ;
209
+ {
210
+ LOCK (cs_main);
211
+ fDIP0008Active = VersionBitsState (chainActive.Tip ()->pprev , Params ().GetConsensus (), Consensus::DEPLOYMENT_DIP0008, versionbitscache) == THRESHOLD_ACTIVE;
212
+ }
213
+
214
+ LOCK (cs);
215
+ bool oldIsEnforced = isEnforced;
216
+ isSporkActive = sporkManager.IsSporkActive (SPORK_19_CHAINLOCKS_ENABLED);
217
+ isEnforced = fDIP0008Active && isSporkActive;
218
+
219
+
220
+ if (!oldIsEnforced && isEnforced) {
221
+ // ChainLocks got activated just recently, but it's possible that it was already running before, leaving
222
+ // us with some stale values which we should not try to enforce anymore (there probably was a good reason to
223
+ // to disable spork19)
224
+ bestChainLockHash = uint256 ();
225
+ bestChainLock = bestChainLockWithKnownBlock = CChainLockSig ();
226
+ bestChainLockBlockIndex = lastNotifyChainLockBlockIndex = nullptr ;
227
+ }
228
+ }
229
+
202
230
void CChainLocksHandler::TrySignChainTip ()
203
231
{
204
232
Cleanup ();
@@ -215,9 +243,6 @@ void CChainLocksHandler::TrySignChainTip()
215
243
if (!pindex->pprev ) {
216
244
return ;
217
245
}
218
- if (!sporkManager.IsSporkActive (SPORK_19_CHAINLOCKS_ENABLED)) {
219
- return ;
220
- }
221
246
222
247
// DIP8 defines a process called "Signing attempts" which should run before the CLSIG is finalized
223
248
// To simplify the initial implementation, we skip this process and directly try to create a CLSIG
@@ -227,6 +252,10 @@ void CChainLocksHandler::TrySignChainTip()
227
252
{
228
253
LOCK (cs);
229
254
255
+ if (!isSporkActive) {
256
+ return ;
257
+ }
258
+
230
259
if (pindex->nHeight == lastSignedHeight) {
231
260
// already signed this one
232
261
return ;
@@ -348,7 +377,7 @@ void CChainLocksHandler::SyncTransaction(const CTransaction& tx, const CBlockInd
348
377
349
378
bool CChainLocksHandler::IsTxSafeForMining (const uint256& txid)
350
379
{
351
- if (!sporkManager.IsSporkActive (SPORK_19_CHAINLOCKS_ENABLED) || !sporkManager. IsSporkActive ( SPORK_3_INSTANTSEND_BLOCK_FILTERING)) {
380
+ if (!sporkManager.IsSporkActive (SPORK_3_INSTANTSEND_BLOCK_FILTERING)) {
352
381
return true ;
353
382
}
354
383
if (!IsNewInstantSendEnabled ()) {
@@ -358,6 +387,9 @@ bool CChainLocksHandler::IsTxSafeForMining(const uint256& txid)
358
387
int64_t txAge = 0 ;
359
388
{
360
389
LOCK (cs);
390
+ if (!isEnforced) {
391
+ return true ;
392
+ }
361
393
auto it = txFirstSeenTime.find (txid);
362
394
if (it != txFirstSeenTime.end ()) {
363
395
txAge = GetAdjustedTime () - it->second ;
@@ -379,6 +411,11 @@ void CChainLocksHandler::EnforceBestChainLock()
379
411
const CBlockIndex* currentBestChainLockBlockIndex;
380
412
{
381
413
LOCK (cs);
414
+
415
+ if (!isEnforced) {
416
+ return ;
417
+ }
418
+
382
419
clsig = bestChainLockWithKnownBlock;
383
420
pindex = currentBestChainLockBlockIndex = this ->bestChainLockBlockIndex ;
384
421
@@ -442,14 +479,14 @@ void CChainLocksHandler::EnforceBestChainLock()
442
479
443
480
void CChainLocksHandler::HandleNewRecoveredSig (const llmq::CRecoveredSig& recoveredSig)
444
481
{
445
- if (!sporkManager.IsSporkActive (SPORK_19_CHAINLOCKS_ENABLED)) {
446
- return ;
447
- }
448
-
449
482
CChainLockSig clsig;
450
483
{
451
484
LOCK (cs);
452
485
486
+ if (!isSporkActive) {
487
+ return ;
488
+ }
489
+
453
490
if (recoveredSig.id != lastSignedRequestId || recoveredSig.msgHash != lastSignedMsgHash) {
454
491
// this is not what we signed, so lets not create a CLSIG for it
455
492
return ;
@@ -495,10 +532,6 @@ void CChainLocksHandler::DoInvalidateBlock(const CBlockIndex* pindex, bool activ
495
532
496
533
bool CChainLocksHandler::HasChainLock (int nHeight, const uint256& blockHash)
497
534
{
498
- if (!sporkManager.IsSporkActive (SPORK_19_CHAINLOCKS_ENABLED)) {
499
- return false ;
500
- }
501
-
502
535
LOCK (cs);
503
536
return InternalHasChainLock (nHeight, blockHash);
504
537
}
@@ -507,6 +540,10 @@ bool CChainLocksHandler::InternalHasChainLock(int nHeight, const uint256& blockH
507
540
{
508
541
AssertLockHeld (cs);
509
542
543
+ if (!isEnforced) {
544
+ return false ;
545
+ }
546
+
510
547
if (!bestChainLockBlockIndex) {
511
548
return false ;
512
549
}
@@ -525,10 +562,6 @@ bool CChainLocksHandler::InternalHasChainLock(int nHeight, const uint256& blockH
525
562
526
563
bool CChainLocksHandler::HasConflictingChainLock (int nHeight, const uint256& blockHash)
527
564
{
528
- if (!sporkManager.IsSporkActive (SPORK_19_CHAINLOCKS_ENABLED)) {
529
- return false ;
530
- }
531
-
532
565
LOCK (cs);
533
566
return InternalHasConflictingChainLock (nHeight, blockHash);
534
567
}
@@ -537,6 +570,10 @@ bool CChainLocksHandler::InternalHasConflictingChainLock(int nHeight, const uint
537
570
{
538
571
AssertLockHeld (cs);
539
572
573
+ if (!isEnforced) {
574
+ return false ;
575
+ }
576
+
540
577
if (!bestChainLockBlockIndex) {
541
578
return false ;
542
579
}
0 commit comments