diff --git a/src/evo/mnhftx.cpp b/src/evo/mnhftx.cpp index 94db62b1f66253..dfe88a9efd53b5 100644 --- a/src/evo/mnhftx.cpp +++ b/src/evo/mnhftx.cpp @@ -22,6 +22,21 @@ const std::string MNEHF_REQUESTID_PREFIX = "mnhf"; static const std::string DB_SIGNALS = "mnhf_s"; +uint256 MNHFTxPayload::GetRequestId() const +{ + return ::SerializeHash(std::make_pair(MNEHF_REQUESTID_PREFIX, int64_t{signal.versionBit})); +} + +CMutableTransaction MNHFTxPayload::PrepareTx() const +{ + CMutableTransaction tx; + tx.nVersion = 3; + tx.nType = SPECIALTX_TYPE; + SetTxPayload(tx, *this); + + return tx; +} + CMNHFManager::Signals CMNHFManager::GetSignalsStage(const CBlockIndex* const pindexPrev) { Signals signals = GetFromCache(pindexPrev); @@ -53,12 +68,7 @@ CMNHFManager::Signals CMNHFManager::GetSignalsStage(const CBlockIndex* const pin return signals; } -const uint256 MNHFTx::GetRequestId() const -{ - return ::SerializeHash(std::make_pair(MNEHF_REQUESTID_PREFIX, int64_t{versionBit})); -} - -bool MNHFTx::Verify(const uint256& quorumHash, const uint256& msgHash, TxValidationState& state) const +bool MNHFTx::Verify(const uint256& quorumHash, const uint256& requestId, const uint256& msgHash, TxValidationState& state) const { if (versionBit >= VERSIONBITS_NUM_BITS) { return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-nbit-out-of-bounds"); @@ -67,8 +77,8 @@ bool MNHFTx::Verify(const uint256& quorumHash, const uint256& msgHash, TxValidat const Consensus::LLMQType& llmqType = Params().GetConsensus().llmqTypeMnhf; const auto quorum = llmq::quorumManager->GetQuorum(llmqType, quorumHash); - const uint256 signHash = llmq::utils::BuildSignHash(llmqType, quorum->qc->quorumHash, GetRequestId(), msgHash); - LogPrintf("validation: quorum:%s requestId:%s msgHash:%s signHash:%s\n", quorum->qc->quorumHash.ToString(), GetRequestId().ToString(), msgHash.ToString(), signHash.ToString()); + const uint256 signHash = llmq::utils::BuildSignHash(llmqType, quorum->qc->quorumHash, requestId, msgHash); + LogPrintf("validation: quorum:%s requestId:%s msgHash:%s signHash:%s\n", quorum->qc->quorumHash.ToString(), requestId.ToString(), msgHash.ToString(), signHash.ToString()); if (!sig.VerifyInsecure(quorum->qc->quorumPublicKey, signHash)) { LogPrintf("validation: indeed invalid!\n"); return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-mnhf-invalid"); @@ -113,7 +123,7 @@ bool CheckMNHFTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValida LogPrintf("knst verify signal: tx hash:%s - %s\n", tx.GetHash().ToString(), tx.ToString()); - if (!mnhfTx.signal.Verify(mnhfTx.signal.quorumHash, msgHash, state)) { + if (!mnhfTx.signal.Verify(mnhfTx.signal.quorumHash, mnhfTx.GetRequestId(), msgHash, state)) { // set up inside Verify return false; } diff --git a/src/evo/mnhftx.h b/src/evo/mnhftx.h index c1227c63309b89..2fe33588ecc4bb 100644 --- a/src/evo/mnhftx.h +++ b/src/evo/mnhftx.h @@ -34,9 +34,7 @@ class MNHFTx CBLSSignature sig{}; MNHFTx() = default; - bool Verify(const uint256& quorumHash, const uint256& msgHash, TxValidationState& state) const; - - const uint256 GetRequestId() const; + bool Verify(const uint256& quorumHash, const uint256& requestId, const uint256& msgHash, TxValidationState& state) const; SERIALIZE_METHODS(MNHFTx, obj) { @@ -67,6 +65,17 @@ class MNHFTxPayload uint8_t nVersion{CURRENT_VERSION}; MNHFTx signal; +public: + /** + * helper function to calculare Request ID used for signing + */ + uint256 GetRequestId() const; + + /** + * helper function to prepare special transaction for signing + */ + CMutableTransaction PrepareTx() const; + SERIALIZE_METHODS(MNHFTxPayload, obj) { READWRITE(obj.nVersion, obj.signal); diff --git a/src/llmq/ehf_signals.cpp b/src/llmq/ehf_signals.cpp index 30bf55f2a55d7e..95bd66fa4bf3a4 100644 --- a/src/llmq/ehf_signals.cpp +++ b/src/llmq/ehf_signals.cpp @@ -54,6 +54,7 @@ void CEHFSignalsHandler::UpdatedBlockTip(const CBlockIndex* const pindexNew) return; } + trySignEHFSignal(Params().GetConsensus().vDeployments[Consensus::DEPLOYMENT_MN_RR].bit, pindexNew); } @@ -77,7 +78,7 @@ void CEHFSignalsHandler::trySignEHFSignal(int bit, const CBlockIndex* const pind LogPrintf("knst try sign - llmq params doesn't exist\n"); return; } - auto quorum = sigman.SelectQuorumForSigning(llmq_params_opt.value(), qman, requestId); + const auto quorum = sigman.SelectQuorumForSigning(llmq_params_opt.value(), qman, requestId); if (!quorum) { LogPrintf("knst EHF - No active quorum\n"); return; @@ -86,45 +87,36 @@ void CEHFSignalsHandler::trySignEHFSignal(int bit, const CBlockIndex* const pind LogPrintf("knst quorum: %lld\n", quorum.get()); LogPrintf("knst quorum->qc: %lld\n", quorum->qc.get()); LogPrintf("knst quorum hash: %s\n", quorum->qc->quorumHash.ToString()); + MNHFTxPayload mnhfPayload; mnhfPayload.signal.versionBit = bit; mnhfPayload.signal.quorumHash = quorum->qc->quorumHash; + const uint256 msgHash = mnhfPayload.PrepareTx().GetHash(); - CMutableTransaction tx; - tx.nVersion = 3; - tx.nType = TRANSACTION_MNHF_SIGNAL; - SetTxPayload(tx, mnhfPayload); - const uint256 msgHash = tx.GetHash(); - + ids.insert(requestId); sigman.AsyncSignIfMember(llmqType, shareman, requestId, msgHash); } void CEHFSignalsHandler::HandleNewRecoveredSig(const CRecoveredSig& recoveredSig) { - LogPrintf("knst straight in HandleNewRecoveredSig! as expected\n"); if (g_txindex) { g_txindex->BlockUntilSyncedToCurrentChain(); } LogPrintf("knst Handle new recovered sig: %s!\n", recoveredSig.ToJson().write()); + MNHFTxPayload mnhfPayload; + mnhfPayload.signal.versionBit = Params().GetConsensus().vDeployments[Consensus::DEPLOYMENT_MN_RR].bit; - const int bit = Params().GetConsensus().vDeployments[Consensus::DEPLOYMENT_MN_RR].bit; - const uint256 requestId = ::SerializeHash(std::make_pair(MNEHF_REQUESTID_PREFIX, int64_t{bit})); - - LogPrintf("knst while expecting: request: %s bit=%d\n", requestId.ToString(), bit); - if (requestId != recoveredSig.getId()) return; + if (ids.find(recoveredSig.getId()) == ids.end()) { + // there's nothing interesting for CEHFSignalsHandler + return; + } - MNHFTxPayload mnhfPayload; - mnhfPayload.signal.versionBit = bit; mnhfPayload.signal.quorumHash = recoveredSig.getQuorumHash(); mnhfPayload.signal.sig = recoveredSig.sig.Get(); - CMutableTransaction tx; - tx.nVersion = 3; - tx.nType = TRANSACTION_MNHF_SIGNAL; - SetTxPayload(tx, mnhfPayload); - + CMutableTransaction tx = mnhfPayload.PrepareTx(); LogPrintf("knst send tx: %s - %s\n", tx.GetHash().ToString(), tx.ToString()); { diff --git a/src/llmq/ehf_signals.h b/src/llmq/ehf_signals.h index 9d9b3766447a31..11e9435f47f5b8 100644 --- a/src/llmq/ehf_signals.h +++ b/src/llmq/ehf_signals.h @@ -10,6 +10,8 @@ #include #include +#include + class CChainState; class CConnman; class CTxMemPool; @@ -30,6 +32,8 @@ class CEHFSignalsHandler : public CRecoveredSigsListener CQuorumManager& qman; CTxMemPool& mempool; CMNHFManager& mnhfManager; + + std::set ids; public: explicit CEHFSignalsHandler(CChainState& chainstate, CConnman& connman, CSigningManager& sigman, CSigSharesManager& shareman, @@ -52,6 +56,7 @@ class CEHFSignalsHandler : public CRecoveredSigsListener private: void trySignEHFSignal(int bit, const CBlockIndex* const pindex); + }; } // namespace llmq