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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
#include <evo/evodb.h>
#include <evo/providertx.h>
#include <evo/specialtx.h>
#include <index/txindex.h>

#include <base58.h>
#include <chainparams.h>
#include <coins.h>
#include <consensus/validation.h>
#include <deploymentstatus.h>
#include <index/txindex.h>
#include <masternode/meta.h>
#include <messagesigner.h>
#include <script/standard.h>
#include <stats/client.h>
Expand Down Expand Up @@ -609,6 +610,23 @@ bool CDeterministicMNManager::ProcessBlock(const CBlock& block, gsl::not_null<co
oldList = GetListForBlockInternal(pindex->pprev);
diff = oldList.BuildDiff(newList);

// apply platform unban for platform revive too
for (int i = 1; i < (int)block.vtx.size(); i++) {
const CTransaction& tx = *block.vtx[i];
if (!tx.IsSpecialTxVersion() || tx.nType != TRANSACTION_PROVIDER_UPDATE_SERVICE) {
// only interested in revive transactions
continue;
}
const auto opt_proTx = GetTxPayload<CProUpServTx>(tx);
if (!opt_proTx) continue; // should not happen but does not matter

if (auto meta_info = m_mn_metaman.GetMetaInfo(opt_proTx->proTxHash, false);
!meta_info || !meta_info->SetPlatformBan(false, nHeight)) {
LogPrint(BCLog::LLMQ, "%s -- MN %s is failed to Platform revived at height %d\n", __func__,
opt_proTx->proTxHash.ToString(), nHeight);
}
}

m_evoDb.Write(std::make_pair(DB_LIST_DIFF, newList.GetBlockHash()), diff);
if ((nHeight % DISK_SNAPSHOT_PERIOD) == 0 || pindex->pprev == m_initial_snapshot_index) {
m_evoDb.Write(std::make_pair(DB_LIST_SNAPSHOT, newList.GetBlockHash()), newList);
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 @@ -544,15 +545,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
2 changes: 2 additions & 0 deletions src/evo/specialtxman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <llmq/commitment.h>
#include <llmq/quorums.h>
#include <llmq/utils.h>
#include <masternode/meta.h>
#include <primitives/block.h>
#include <util/irange.h>
#include <validation.h>
Expand Down Expand Up @@ -328,6 +329,7 @@ bool CSpecialTxProcessor::BuildNewListFromBlock(const CBlock& block, gsl::not_nu
newState->platformP2PPort = opt_proTx->platformP2PPort;
newState->platformHTTPPort = opt_proTx->platformHTTPPort;
}

if (newState->IsBanned()) {
// only revive when all keys are set
if (newState->pubKeyOperator != CBLSLazyPublicKey() && !newState->keyIDVoting.IsNull() &&
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
Loading
Loading