Skip to content

Commit

Permalink
more cleanups for EHF Signals
Browse files Browse the repository at this point in the history
  • Loading branch information
knst committed Oct 5, 2023
1 parent eaf582c commit dc8b298
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
28 changes: 19 additions & 9 deletions src/evo/mnhftx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 12 additions & 3 deletions src/evo/mnhftx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 12 additions & 20 deletions src/llmq/ehf_signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void CEHFSignalsHandler::UpdatedBlockTip(const CBlockIndex* const pindexNew)
return;
}


trySignEHFSignal(Params().GetConsensus().vDeployments[Consensus::DEPLOYMENT_MN_RR].bit, pindexNew);
}

Expand All @@ -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;
Expand All @@ -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());
{
Expand Down
5 changes: 5 additions & 0 deletions src/llmq/ehf_signals.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <crypto/common.h>
#include <llmq/signing.h>

#include <set>

class CChainState;
class CConnman;
class CTxMemPool;
Expand All @@ -30,6 +32,8 @@ class CEHFSignalsHandler : public CRecoveredSigsListener
CQuorumManager& qman;
CTxMemPool& mempool;
CMNHFManager& mnhfManager;

std::set<uint256> ids;
public:
explicit CEHFSignalsHandler(CChainState& chainstate, CConnman& connman,
CSigningManager& sigman, CSigSharesManager& shareman,
Expand All @@ -52,6 +56,7 @@ class CEHFSignalsHandler : public CRecoveredSigsListener

private:
void trySignEHFSignal(int bit, const CBlockIndex* const pindex);

};

} // namespace llmq
Expand Down

0 comments on commit dc8b298

Please sign in to comment.