Line data Source code
1 : // Copyright (c) 2019-2025 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef BITCOIN_CHAINLOCK_SIGNING_H 6 : #define BITCOIN_CHAINLOCK_SIGNING_H 7 : 8 : #include <chainlock/chainlock.h> 9 : #include <llmq/signing.h> 10 : #include <util/time.h> 11 : #include <validationinterface.h> 12 : 13 : class CScheduler; 14 : class CMasternodeSync; 15 : namespace llmq { 16 : class CInstantSendManager; 17 : class CRecoveredSig; 18 : class CSigningManager; 19 : class CSigSharesManager; 20 : } // namespace llmq 21 : 22 : namespace chainlock { 23 : class ChainlockHandler; 24 : 25 : class ChainLockSigner final : public llmq::CRecoveredSigsListener, public CValidationInterface 26 : { 27 : private: 28 : CChainState& m_chainstate; 29 : const chainlock::Chainlocks& m_chainlocks; 30 : ChainlockHandler& m_clhandler; 31 : const llmq::CInstantSendManager& m_isman; 32 : const llmq::CQuorumManager& m_qman; 33 : llmq::CSigningManager& m_sigman; 34 : llmq::CSigSharesManager& m_shareman; 35 : const CMasternodeSync& m_mn_sync; 36 : 37 : private: 38 : // We keep track of txids from recently received blocks so that we can check if all TXs got islocked 39 : struct BlockHasher { 40 319759 : size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); } 41 : }; 42 : using BlockTxs = std::unordered_map<uint256, std::shared_ptr<Uint256HashSet>, BlockHasher>; 43 : 44 : private: 45 : mutable Mutex cs_try_sign; 46 : mutable Mutex cs_signer; 47 : 48 : std::unique_ptr<CScheduler> m_scheduler; 49 : std::unique_ptr<std::thread> m_scheduler_thread; 50 : CleanupThrottler<NodeClock> m_cleanup_throttler; 51 : 52 : BlockTxs blockTxs GUARDED_BY(cs_signer); 53 : int32_t lastSignedHeight GUARDED_BY(cs_signer){-1}; 54 : uint256 lastSignedRequestId GUARDED_BY(cs_signer); 55 : uint256 lastSignedMsgHash GUARDED_BY(cs_signer); 56 : 57 : public: 58 : ChainLockSigner() = delete; 59 : ChainLockSigner(const ChainLockSigner&) = delete; 60 : ChainLockSigner& operator=(const ChainLockSigner&) = delete; 61 : explicit ChainLockSigner(CChainState& chainstate, const chainlock::Chainlocks& chainlocks, 62 : ChainlockHandler& clhandler, const llmq::CInstantSendManager& isman, 63 : const llmq::CQuorumManager& qman, llmq::CSigningManager& sigman, 64 : llmq::CSigSharesManager& shareman, const CMasternodeSync& mn_sync); 65 : ~ChainLockSigner(); 66 : 67 : void Start(); 68 : void Stop(); 69 : 70 : void RegisterRecoveryInterface(); 71 : void UnregisterRecoveryInterface(); 72 : 73 : // implements validation interface: 74 : void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override 75 : EXCLUSIVE_LOCKS_REQUIRED(!cs_signer); 76 : void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override 77 : EXCLUSIVE_LOCKS_REQUIRED(!cs_signer); 78 : void UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) override 79 : EXCLUSIVE_LOCKS_REQUIRED(!cs_try_sign, !cs_signer); 80 : 81 : [[nodiscard]] llmq::RecoveredSigResult HandleNewRecoveredSig(const llmq::CRecoveredSig& recoveredSig) override 82 : EXCLUSIVE_LOCKS_REQUIRED(!cs_signer); 83 : 84 : void Cleanup() EXCLUSIVE_LOCKS_REQUIRED(!cs_signer); 85 : 86 : private: 87 : void TrySignChainTip() EXCLUSIVE_LOCKS_REQUIRED(!cs_try_sign, !cs_signer); 88 : 89 : [[nodiscard]] BlockTxs::mapped_type GetBlockTxs(const uint256& blockHash) 90 : EXCLUSIVE_LOCKS_REQUIRED(!cs_signer); 91 : }; 92 : } // namespace chainlock 93 : 94 : #endif // BITCOIN_CHAINLOCK_SIGNING_H