Skip to content

feat: implement Platform Ban PoSe DIP-0031 #6613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
7 changes: 7 additions & 0 deletions src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <evo/specialtx.h>
#include <llmq/commitment.h>
#include <llmq/utils.h>
#include <masternode/meta.h>

#include <base58.h>
#include <chainparams.h>
Expand Down Expand Up @@ -838,6 +839,12 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, gsl::no
newState->platformP2PPort = opt_proTx->platformP2PPort;
newState->platformHTTPPort = opt_proTx->platformHTTPPort;
}
if (auto meta_info = m_mn_metaman.GetMetaInfo(opt_proTx->proTxHash, false);
!meta_info || !meta_info->SetPlatformBan(false, nHeight)) {
LogPrintf("CDeterministicMNManager::%s -- MN %s is not Platform revived at height %d\n", __func__,
opt_proTx->proTxHash.ToString(), nHeight);
}

if (newState->IsBanned()) {
// only revive when all keys are set
if (newState->pubKeyOperator != CBLSLazyPublicKey() && !newState->keyIDVoting.IsNull() &&
Expand Down
7 changes: 5 additions & 2 deletions src/evo/deterministicmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CBlock;
class CBlockIndex;
class CCoinsViewCache;
class CEvoDB;
class CMasternodeMetaMan;
class TxValidationState;

extern RecursiveMutex cs_main;
Expand Down Expand Up @@ -563,15 +564,17 @@ class CDeterministicMNManager
std::atomic<int> to_cleanup {0};

CEvoDB& m_evoDb;
CMasternodeMetaMan& m_mn_metaman;

std::unordered_map<uint256, CDeterministicMNList, StaticSaltedHasher> mnListsCache GUARDED_BY(cs);
std::unordered_map<uint256, CDeterministicMNListDiff, StaticSaltedHasher> mnListDiffsCache GUARDED_BY(cs);
const CBlockIndex* tipIndex GUARDED_BY(cs) {nullptr};
const CBlockIndex* m_initial_snapshot_index GUARDED_BY(cs) {nullptr};

public:
explicit CDeterministicMNManager(CEvoDB& evoDb) :
m_evoDb(evoDb)
explicit CDeterministicMNManager(CEvoDB& evoDb, CMasternodeMetaMan& mn_metaman) :
m_evoDb(evoDb),
m_mn_metaman(mn_metaman)
{
}
~CDeterministicMNManager() = default;
Expand Down
8 changes: 6 additions & 2 deletions src/llmq/dkgsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,15 @@ void CDKGSession::VerifyConnectionAndMinProtoVersions(CConnman& connman) const
m->badConnection = true;
logger.Batch("%s does not have min proto version %d (has %d)", m->dmn->proTxHash.ToString(), MIN_MASTERNODE_PROTO_VERSION, it->second);
}

if (m_mn_metaman.GetMetaInfo(m->dmn->proTxHash)->OutboundFailedTooManyTimes()) {
const auto meta_info = m_mn_metaman.GetMetaInfo(m->dmn->proTxHash);
if (meta_info->OutboundFailedTooManyTimes()) {
m->badConnection = true;
logger.Batch("%s failed to connect to it too many times", m->dmn->proTxHash.ToString());
}
if (meta_info->IsPlatformBanned()) {
m->badConnection = true;
logger.Batch("%s is Platform PoSe banned", m->dmn->proTxHash.ToString());
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/masternode/meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ UniValue CMasternodeMetaInfo::ToJson() const
ret.pushKV("lastOutboundAttemptElapsed", now - lastOutboundAttempt);
ret.pushKV("lastOutboundSuccess", lastOutboundSuccess);
ret.pushKV("lastOutboundSuccessElapsed", now - lastOutboundSuccess);
{
LOCK(cs);
ret.pushKV("platform_ban", m_platform_ban);
ret.pushKV("platform_ban_updated", m_platform_ban_height);
}

return ret;
}
Expand Down Expand Up @@ -127,8 +132,31 @@ std::vector<uint256> CMasternodeMetaMan::GetAndClearDirtyGovernanceObjectHashes(
return vecTmp;
}

bool CMasternodeMetaMan::AlreadyHavePlatformBan(const uint256& inv_hash) const
{
LOCK(cs);
return m_seen_platform_bans.contains(inv_hash);
}

std::optional<PlatformBanMessage> CMasternodeMetaMan::GetPlatformBan(const uint256& inv_hash) const
{
LOCK(cs);
auto it = m_seen_platform_bans.find(inv_hash);
if (it == m_seen_platform_bans.end()) return std::nullopt;

return it->second;
}

void CMasternodeMetaMan::RememberPlatformBan(const uint256& inv_hash, PlatformBanMessage& msg)
{
LOCK(cs);
m_seen_platform_bans.insert({inv_hash, msg});
}

std::string MasternodeMetaStore::ToString() const
{
LOCK(cs);
return strprintf("Masternodes: meta infos object count: %d, nDsqCount: %d", metaInfos.size(), nDsqCount);
}

uint256 PlatformBanMessage::GetHash() const { return ::SerializeHash(*this); }
78 changes: 68 additions & 10 deletions src/masternode/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef BITCOIN_MASTERNODE_META_H
#define BITCOIN_MASTERNODE_META_H

#include <bls/bls.h>
#include <serialize.h>
#include <sync.h>
#include <threadsafety.h>
Expand All @@ -13,6 +14,7 @@
#include <atomic>
#include <map>
#include <memory>
#include <optional>
#include <vector>

class CConnman;
Expand Down Expand Up @@ -46,6 +48,9 @@ class CMasternodeMetaInfo
std::atomic<int64_t> lastOutboundAttempt{0};
std::atomic<int64_t> lastOutboundSuccess{0};

bool m_platform_ban GUARDED_BY(cs){false};
int m_platform_ban_height GUARDED_BY(cs){0};

public:
CMasternodeMetaInfo() = default;
explicit CMasternodeMetaInfo(const uint256& _proTxHash) : proTxHash(_proTxHash) {}
Expand All @@ -55,22 +60,18 @@ class CMasternodeMetaInfo
nMixingTxCount(ref.nMixingTxCount.load()),
mapGovernanceObjectsVotedOn(ref.mapGovernanceObjectsVotedOn),
lastOutboundAttempt(ref.lastOutboundAttempt.load()),
lastOutboundSuccess(ref.lastOutboundSuccess.load())
lastOutboundSuccess(ref.lastOutboundSuccess.load()),
m_platform_ban(ref.m_platform_ban),
m_platform_ban_height(ref.m_platform_ban_height)
{
}

SERIALIZE_METHODS(CMasternodeMetaInfo, obj)
{
LOCK(obj.cs);
READWRITE(
obj.proTxHash,
obj.nLastDsq,
obj.nMixingTxCount,
obj.mapGovernanceObjectsVotedOn,
obj.outboundAttemptCount,
obj.lastOutboundAttempt,
obj.lastOutboundSuccess
);
READWRITE(obj.proTxHash, obj.nLastDsq, obj.nMixingTxCount, obj.mapGovernanceObjectsVotedOn,
obj.outboundAttemptCount, obj.lastOutboundAttempt, obj.lastOutboundSuccess, obj.m_platform_ban,
obj.m_platform_ban_height);
}

UniValue ToJson() const;
Expand All @@ -96,6 +97,24 @@ class CMasternodeMetaInfo
int64_t GetLastOutboundAttempt() const { return lastOutboundAttempt; }
void SetLastOutboundSuccess(int64_t t) { lastOutboundSuccess = t; outboundAttemptCount = 0; }
int64_t GetLastOutboundSuccess() const { return lastOutboundSuccess; }
bool SetPlatformBan(bool is_banned, int height)
{
LOCK(cs);
if (height < m_platform_ban_height) {
return false;
}
if (height == m_platform_ban_height && !is_banned) {
return false;
}
m_platform_ban = is_banned;
m_platform_ban_height = height;
return true;
}
bool IsPlatformBanned() const
{
LOCK(cs);
return m_platform_ban;
}
};
using CMasternodeMetaInfoPtr = std::shared_ptr<CMasternodeMetaInfo>;

Expand Down Expand Up @@ -150,6 +169,40 @@ class MasternodeMetaStore
std::string ToString() const;
};

/**
* Platform PoSe Ban are result in the node voting against the targeted evonode in all future DKG sessions until that targeted
*evonode has been successfully banned. Platform will initiate this ban process by passing relevant information to Core using RPC. See DIP-0031
*
* We use 2 main classes to manage Platform PoSe Ban
*
* PlatformBanMessage
* PlatformBanManager - a higher-level construct which store information about ban status
**/

/**
* PlatformBanMessage - low-level constructs which contain the m_protx_hash, m_requested_height, m_quorum_hash and m_signature
*/
class PlatformBanMessage
{
public:
uint256 m_protx_hash;
int32_t m_requested_height{0};
uint256 m_quorum_hash;
CBLSSignature m_signature;

PlatformBanMessage() = default;

SERIALIZE_METHODS(PlatformBanMessage, obj)
{
READWRITE(obj.m_protx_hash, obj.m_requested_height, obj.m_quorum_hash);
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(CBLSSignatureVersionWrapper(const_cast<CBLSSignature&>(obj.m_signature), false));
}
}

uint256 GetHash() const;
};
Comment on lines +185 to +204
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify signature validation for platform ban messages.

The PlatformBanMessage class handles serialization correctly, but ensure that signature validation is implemented in the corresponding cpp file. The class would benefit from a method to verify the signature against the hashed message data.


🏁 Script executed:

#!/bin/bash
# Check if there's signature validation logic for PlatformBanMessages
echo "Searching for PlatformBanMessage signature validation..."
rg -A 10 "bool.*PlatformBanMessage.*[Vv]alidat" --type cpp

Length of output: 183


Action: Add Signature Verification Logic for PlatformBanMessage

It appears that the current implementation in src/masternode/meta.h only handles serialization and hash generation (via GetHash()), but no signature validation method is present in the corresponding source file. To ensure robust security for platform ban messages, please add a dedicated method (for example, bool ValidateSignature() const) in the corresponding CPP file (likely src/masternode/meta.cpp) that verifies the signature (m_signature) against the message hash.

  • File to update: src/masternode/meta.cpp (or the appropriate implementation file for PlatformBanMessage)
  • Action required: Implement signature validation using the hashed message data (obtained by GetHash()).
  • Recommendation: Confirm that the cryptographic routine used to validate CBLSSignature is correctly integrated.


class CMasternodeMetaMan : public MasternodeMetaStore
{
private:
Expand All @@ -160,6 +213,7 @@ class CMasternodeMetaMan : public MasternodeMetaStore
bool is_valid{false};

std::vector<uint256> vecDirtyGovernanceObjectHashes GUARDED_BY(cs);
std::map<uint256, PlatformBanMessage> m_seen_platform_bans GUARDED_BY(cs);

public:
explicit CMasternodeMetaMan();
Expand All @@ -181,6 +235,10 @@ class CMasternodeMetaMan : public MasternodeMetaStore
void RemoveGovernanceObject(const uint256& nGovernanceObjectHash);

std::vector<uint256> GetAndClearDirtyGovernanceObjectHashes();

bool AlreadyHavePlatformBan(const uint256& inv_hash) const;
std::optional<PlatformBanMessage> GetPlatformBan(const uint256& inv_hash) const;
void RememberPlatformBan(const uint256& inv_hash, PlatformBanMessage& msg);
};

#endif // BITCOIN_MASTERNODE_META_H
83 changes: 82 additions & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <util/check.h>
#include <util/system.h>
#include <util/strencodings.h>
#include <util/underlying.h>
#include <util/trace.h>

#include <algorithm>
Expand Down Expand Up @@ -712,6 +713,9 @@ class PeerManagerImpl final : public PeerManager
const std::vector<CBlockHeader>& headers,
bool via_compact_block)
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex);
PeerMsgRet ProcessPlatformBanMessage(CNode& peer, std::string_view msg_type, CDataStream& vRecv)
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex);

/** Various helpers for headers processing, invoked by ProcessHeadersMessage() */
/** Deal with state tracking and headers sync for peers that send the
* occasional non-connecting header (this can happen due to BIP 130 headers
Expand Down Expand Up @@ -2256,6 +2260,8 @@ bool PeerManagerImpl::AlreadyHave(const CInv& inv)
#else
return m_cj_ctx->server->HasQueue(inv.hash);
#endif
case MSG_PLATFORM_BAN:
return m_mn_metaman.AlreadyHavePlatformBan(inv.hash);
}


Expand Down Expand Up @@ -2878,6 +2884,13 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
push = true;
}
}
if (!push && inv.type == MSG_PLATFORM_BAN) {
auto opt_platform_ban = m_mn_metaman.GetPlatformBan(inv.hash);
if (opt_platform_ban.has_value()) {
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::PLATFORMBAN, *opt_platform_ban));
push = true;
}
}

if (!push) {
vNotFound.push_back(inv);
Expand Down Expand Up @@ -3506,6 +3519,74 @@ void PeerManagerImpl::PostProcessMessage(MessageProcessingResult&& result, NodeI
}
}

PeerMsgRet PeerManagerImpl::ProcessPlatformBanMessage(CNode& pfrom, std::string_view msg_type, CDataStream& vRecv)
{
Comment on lines +3522 to +3523
Copy link
Preview

Copilot AI May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a brief docstring or inline comment for ProcessPlatformBanMessage to clarify its validation logic and the meaning of returned error codes, aiding future maintainers.

Suggested change
PeerMsgRet PeerManagerImpl::ProcessPlatformBanMessage(CNode& pfrom, std::string_view msg_type, CDataStream& vRecv)
{
/**
* Process a PLATFORMBAN message received from a peer.
*
* @param pfrom The peer sending the message.
* @param msg_type The type of the message (should be PLATFORMBAN).
* @param vRecv The serialized message data.
* @return A PeerMsgRet object indicating the result of processing:
* - An empty object if the message is invalid or ignored.
* - An error code if the message is malformed or violates protocol rules.
*
* Validation logic:
* - The function does nothing if the node is not synchronized with the blockchain.
* - The message is deserialized and logged for further processing.
*/
PeerMsgRet PeerManagerImpl::ProcessPlatformBanMessage(CNode& pfrom, std::string_view msg_type, CDataStream& vRecv)

Copilot uses AI. Check for mistakes.

if (msg_type != NetMsgType::PLATFORMBAN) return {};

// Do nothing if node is out of sync
if (!m_mn_sync.IsBlockchainSynced()) {
return {};
}

PlatformBanMessage ban_msg;
vRecv >> ban_msg;

const uint256 hash = ban_msg.GetHash();

LogPrintf("PLATFORMBAN -- hash: %s protx_hash: %s height: %d peer=%d\n", hash.ToString(), ban_msg.m_protx_hash.ToString(), ban_msg.m_requested_height, pfrom.GetId());

const auto list = Assert(m_dmnman)->GetListAtChainTip();
auto dmn = list.GetMN(ban_msg.m_protx_hash);
if (!dmn) {
// small P2P penalty (1), as the evonode may have very recently been removed
return tl::unexpected{1};
}
if (dmn->nType != MnType::Evo) {
// Ban node, P2P penalty (100) if protx_hash is associated with a regular node not an evonode
LogPrintf("PLATFORMBAN -- hash: %s protx_hash: %s unexpected type of node\n", hash.ToString(), ban_msg.m_protx_hash.ToString());
return tl::unexpected{100};
}
const int day_of_blocks = 576;
int tipHeight = WITH_LOCK(cs_main, return m_chainman.ActiveChainstate().m_chain.Height());
if (tipHeight < ban_msg.m_requested_height || tipHeight - day_of_blocks > ban_msg.m_requested_height) {
// m_requested_height is inside the range [TipHeight - 576 - 5, TipHeight + 5]
LogPrintf("PLATFORMBAN -- hash: %s protx_hash: %s unexpected height: %d tip: %d\n", hash.ToString(), ban_msg.m_protx_hash.ToString(), ban_msg.m_requested_height, tipHeight);
if (tipHeight + 5 < ban_msg.m_requested_height || tipHeight - day_of_blocks - 5 > ban_msg.m_requested_height) {
// m_requested_height is outside the range [TipHeight - 576 - 5, TipHeight + 5]
return tl::unexpected{10};
}
return tl::unexpected{1};
}

Consensus::LLMQType llmq_type = Params().GetConsensus().llmqTypePlatform;
auto quorum = m_llmq_ctx->qman->GetQuorum(llmq_type, ban_msg.m_quorum_hash);
if (!quorum) {
LogPrintf("PLATFORMBAN -- hash: %s protx_hash: %s missing quorum_hash: %s llmq_type: %d\n", hash.ToString(), ban_msg.m_protx_hash.ToString(), ban_msg.m_quorum_hash.ToString(), ToUnderlying(llmq_type));
return tl::unexpected{100};
}

const std::string PLATFORM_BAN_REQUESTID_PREFIX = "PlatformPoSeBan";
const auto data = std::make_pair(ban_msg.m_protx_hash, ban_msg.m_requested_height);
const uint256 request_id = ::SerializeHash(std::make_pair(PLATFORM_BAN_REQUESTID_PREFIX, data));
const uint256 msg_hash = ::SerializeHash(data);

auto ret = llmq::VerifyRecoveredSig(llmq_type, m_chainman.ActiveChainstate().m_chain, *m_llmq_ctx->qman, ban_msg.m_requested_height, request_id, msg_hash, ban_msg.m_signature);
if (ret != llmq::VerifyRecSigStatus::Valid) {
LogPrintf("PLATFORMBAN -- hash: %s protx_hash: %s request_id: %s msg_hash: %s sig validation failed: %d\n", hash.ToString(), ban_msg.m_protx_hash.ToString(), request_id.ToString(), msg_hash.ToString(), ToUnderlying(ret));
return tl::unexpected{100};
}

// At this point, the outgoing message serialization version can't change.
const auto meta_info = m_mn_metaman.GetMetaInfo(ban_msg.m_protx_hash);
if (meta_info->SetPlatformBan(true, ban_msg.m_requested_height)) {
LogPrintf("PLATFORMBAN -- forward message to other nodes\n");
m_mn_metaman.RememberPlatformBan(hash, ban_msg);
CInv platform_ban_inv{MSG_PLATFORM_BAN, hash};
RelayInv(platform_ban_inv);
}
return {};
}

void PeerManagerImpl::ProcessMessage(
CNode& pfrom,
const std::string& msg_type,
Expand Down Expand Up @@ -5219,7 +5300,6 @@ void PeerManagerImpl::ProcessMessage(
Misbehaving(pfrom.GetId(), 100, strprintf("received not-requested quorumrotationinfo. peer=%d", pfrom.GetId()));
return;
}

if (msg_type == NetMsgType::NOTFOUND) {
// Remove the NOTFOUND transactions from the peer
LOCK(cs_main);
Expand Down Expand Up @@ -5273,6 +5353,7 @@ void PeerManagerImpl::ProcessMessage(
ProcessPeerMsgRet(m_llmq_ctx->qman->ProcessMessage(pfrom, m_connman, msg_type, vRecv), pfrom);
m_llmq_ctx->shareman->ProcessMessage(pfrom, *this, m_sporkman, msg_type, vRecv);
ProcessPeerMsgRet(m_llmq_ctx->sigman->ProcessMessage(pfrom, *this, msg_type, vRecv), pfrom);
ProcessPeerMsgRet(ProcessPlatformBanMessage(pfrom, msg_type, vRecv), pfrom);

if (msg_type == NetMsgType::CLSIG) {
if (llmq::AreChainLocksEnabled(m_sporkman)) {
Expand Down
2 changes: 1 addition & 1 deletion src/node/chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void DashChainstateSetup(ChainstateManager& chainman,
{
// Same logic as pblocktree
dmnman.reset();
dmnman = std::make_unique<CDeterministicMNManager>(*evodb);
dmnman = std::make_unique<CDeterministicMNManager>(*evodb, mn_metaman);

cpoolman.reset();
cpoolman = std::make_unique<CCreditPoolManager>(*evodb);
Expand Down
Loading
Loading