@@ -30,13 +30,23 @@ ChainLockSigner::ChainLockSigner(CChainState& chainstate, llmq::CChainLocksHandl
3030
3131ChainLockSigner::~ChainLockSigner () = default ;
3232
33+ void ChainLockSigner::Start ()
34+ {
35+ m_sigman.RegisterRecoveredSigsListener (this );
36+ }
37+
38+ void ChainLockSigner::Stop ()
39+ {
40+ m_sigman.UnregisterRecoveredSigsListener (this );
41+ }
42+
3343void ChainLockSigner::TrySignChainTip (const llmq::CInstantSendManager& isman)
3444{
3545 if (!m_mn_sync.IsBlockchainSynced ()) {
3646 return ;
3747 }
3848
39- if (!m_clhandler.isEnabled ) {
49+ if (!m_clhandler.IsEnabled () ) {
4050 return ;
4151 }
4252
@@ -57,23 +67,23 @@ void ChainLockSigner::TrySignChainTip(const llmq::CInstantSendManager& isman)
5767 // Later, we'll add the multiple attempts process.
5868
5969 {
60- LOCK2 (m_clhandler. cs , cs_signer);
70+ LOCK ( cs_signer);
6171
6272 if (pindex->nHeight == lastSignedHeight) {
6373 // already signed this one
6474 return ;
6575 }
76+ }
6677
67- if (m_clhandler.bestChainLock . getHeight () >= pindex->nHeight ) {
68- // already got the same CLSIG or a better one
69- return ;
70- }
78+ if (m_clhandler.GetBestChainLockHeight () >= pindex->nHeight ) {
79+ // already got the same CLSIG or a better one
80+ return ;
81+ }
7182
72- if (m_clhandler.InternalHasConflictingChainLock (pindex->nHeight , pindex->GetBlockHash ())) {
73- // don't sign if another conflicting CLSIG is already present. EnforceBestChainLock will later enforce
74- // the correct chain.
75- return ;
76- }
83+ if (m_clhandler.HasConflictingChainLock (pindex->nHeight , pindex->GetBlockHash ())) {
84+ // don't sign if another conflicting CLSIG is already present. EnforceBestChainLock will later enforce
85+ // the correct chain.
86+ return ;
7787 }
7888
7989 LogPrint (BCLog::CHAINLOCKS, " %s -- trying to sign %s, height=%d\n " , __func__, pindex->GetBlockHash ().ToString (), pindex->nHeight );
@@ -104,18 +114,9 @@ void ChainLockSigner::TrySignChainTip(const llmq::CInstantSendManager& isman)
104114 }
105115
106116 for (const auto & txid : *txids) {
107- int64_t txAge = 0 ;
108- {
109- LOCK (m_clhandler.cs );
110- auto it = m_clhandler.txFirstSeenTime .find (txid);
111- if (it != m_clhandler.txFirstSeenTime .end ()) {
112- txAge = GetTime<std::chrono::seconds>().count () - it->second ;
113- }
114- }
115-
116- if (txAge < m_clhandler.WAIT_FOR_ISLOCK_TIMEOUT && !isman.IsLocked (txid)) {
117- LogPrint (BCLog::CHAINLOCKS, " %s -- not signing block %s due to TX %s not being islocked and not old enough. age=%d\n " , __func__,
118- pindexWalk->GetBlockHash ().ToString (), txid.ToString (), txAge);
117+ if (!m_clhandler.IsTxSafeForMining (txid) && !isman.IsLocked (txid)) {
118+ LogPrint (BCLog::CHAINLOCKS, " %s -- not signing block %s due to TX %s not being islocked and not old enough.\n " , __func__,
119+ pindexWalk->GetBlockHash ().ToString (), txid.ToString ());
119120 return ;
120121 }
121122 }
@@ -127,12 +128,9 @@ void ChainLockSigner::TrySignChainTip(const llmq::CInstantSendManager& isman)
127128 uint256 requestId = ::SerializeHash (std::make_pair (CLSIG_REQUESTID_PREFIX, pindex->nHeight ));
128129 uint256 msgHash = pindex->GetBlockHash ();
129130
130- {
131- LOCK (m_clhandler.cs );
132- if (m_clhandler.bestChainLock .getHeight () >= pindex->nHeight ) {
133- // might have happened while we didn't hold cs
134- return ;
135- }
131+ if (m_clhandler.GetBestChainLockHeight () >= pindex->nHeight ) {
132+ // might have happened while we didn't hold cs
133+ return ;
136134 }
137135 {
138136 LOCK (cs_signer);
@@ -144,10 +142,34 @@ void ChainLockSigner::TrySignChainTip(const llmq::CInstantSendManager& isman)
144142 m_sigman.AsyncSignIfMember (Params ().GetConsensus ().llmqTypeChainLocks , m_shareman, requestId, msgHash);
145143}
146144
145+ void ChainLockSigner::EraseFromBlockHashTxidMap (const uint256& hash)
146+ {
147+ AssertLockNotHeld (cs_signer);
148+ LOCK (cs_signer);
149+ blockTxs.erase (hash);
150+ }
151+
152+ void ChainLockSigner::UpdateBlockHashTxidMap (const uint256& hash, const std::vector<CTransactionRef>& vtx)
153+ {
154+ AssertLockNotHeld (cs_signer);
155+ LOCK (cs_signer);
156+ auto it = blockTxs.find (hash);
157+ if (it == blockTxs.end ()) {
158+ // We must create this entry even if there are no lockable transactions in the block, so that TrySignChainTip
159+ // later knows about this block
160+ it = blockTxs.emplace (hash, std::make_shared<std::unordered_set<uint256, StaticSaltedHasher>>()).first ;
161+ }
162+ auto & txids = *it->second ;
163+ for (const auto & tx : vtx) {
164+ if (!tx->IsCoinBase () && !tx->vin .empty ()) {
165+ txids.emplace (tx->GetHash ());
166+ }
167+ }
168+ }
169+
147170ChainLockSigner::BlockTxs::mapped_type ChainLockSigner::GetBlockTxs (const uint256& blockHash)
148171{
149172 AssertLockNotHeld (cs_signer);
150- AssertLockNotHeld (m_clhandler.cs );
151173 AssertLockNotHeld (::cs_main);
152174
153175 ChainLockSigner::BlockTxs::mapped_type ret;
@@ -188,31 +210,26 @@ ChainLockSigner::BlockTxs::mapped_type ChainLockSigner::GetBlockTxs(const uint25
188210 LOCK (cs_signer);
189211 blockTxs.emplace (blockHash, ret);
190212 }
191- {
192- LOCK (m_clhandler.cs );
193- for (const auto & txid : *ret) {
194- m_clhandler.txFirstSeenTime .emplace (txid, blockTime);
195- }
196- }
213+ m_clhandler.UpdateTxFirstSeenMap (*ret, blockTime);
197214 }
198215 return ret;
199216}
200217
201218MessageProcessingResult ChainLockSigner::HandleNewRecoveredSig (const llmq::CRecoveredSig& recoveredSig)
202219{
203- if (!m_clhandler.isEnabled ) {
220+ if (!m_clhandler.IsEnabled () ) {
204221 return {};
205222 }
206223
207224 ChainLockSig clsig;
208225 {
209- LOCK2 (m_clhandler. cs , cs_signer);
226+ LOCK ( cs_signer);
210227
211228 if (recoveredSig.getId () != lastSignedRequestId || recoveredSig.getMsgHash () != lastSignedMsgHash) {
212229 // this is not what we signed, so lets not create a CLSIG for it
213230 return {};
214231 }
215- if (m_clhandler.bestChainLock . getHeight () >= lastSignedHeight) {
232+ if (m_clhandler.GetBestChainLockHeight () >= lastSignedHeight) {
216233 // already got the same or a better CLSIG through the CLSIG message
217234 return {};
218235 }
0 commit comments