Skip to content

Commit 7c899a2

Browse files
committed
refactor: drop dependency of spork manager on network code
1 parent bd2bff1 commit 7c899a2

4 files changed

Lines changed: 41 additions & 45 deletions

File tree

src/net_processing.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5462,6 +5462,23 @@ void PeerManagerImpl::ProcessMessage(
54625462
return;
54635463
}
54645464

5465+
if (msg_type == NetMsgType::SPORK) {
5466+
CSporkMessage spork;
5467+
vRecv >> spork;
5468+
5469+
uint256 hash = spork.GetHash();
5470+
CInv spork_inv{MSG_SPORK, hash};
5471+
WITH_LOCK(::cs_main, EraseObjectRequest(pfrom.GetId(), spork_inv));
5472+
if (!m_sporkman.IsSporkValid(spork)) {
5473+
Misbehaving(pfrom.GetId(), 100);
5474+
return;
5475+
}
5476+
if (m_sporkman.ProcessSpork(spork)) {
5477+
RelayInv(spork_inv);
5478+
}
5479+
return;
5480+
}
5481+
54655482
if (msg_type == NetMsgType::GETSPORKS) {
54665483
// For 'getsporks', active sporks is sent to the requesting peer.
54675484
auto active_sporks = m_sporkman.ActiveSporks();
@@ -5470,6 +5487,7 @@ void PeerManagerImpl::ProcessMessage(
54705487
m_connman.PushMessage(&pfrom, CNetMsgMaker(pfrom.GetCommonVersion()).Make(NetMsgType::SPORK, signerSporkPair.second));
54715488
}
54725489
}
5490+
return;
54735491
}
54745492

54755493
if (msg_type == NetMsgType::QUORUMROTATIONINFO) {
@@ -5518,7 +5536,6 @@ void PeerManagerImpl::ProcessMessage(
55185536
PostProcessMessage(m_cj_walletman->processMessage(pfrom, m_chainman.ActiveChainstate(), m_connman, m_mempool, msg_type, vRecv), pfrom.GetId());
55195537
}
55205538
PostProcessMessage(DKGSessionProcessMessage(pfrom, is_masternode, msg_type, vRecv), pfrom.GetId());
5521-
PostProcessMessage(m_sporkman.ProcessMessage(pfrom, m_connman, msg_type, vRecv), pfrom.GetId());
55225539
PostProcessMessage(CMNAuth::ProcessMessage(pfrom, peer->m_their_services, m_connman, m_mn_metaman, (m_active_ctx ? m_active_ctx->nodeman.get() : nullptr), m_mn_sync, m_dmnman->GetListAtChainTip(), msg_type, vRecv), pfrom.GetId());
55235540
PostProcessMessage(m_llmq_ctx->quorum_block_processor->ProcessMessage(pfrom, msg_type, vRecv), pfrom.GetId());
55245541
PostProcessMessage(ProcessPlatformBanMessage(pfrom.GetId(), msg_type, vRecv), pfrom.GetId());

src/spork.cpp

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44

55
#include <spork.h>
66

7-
#include <flat-database.h>
8-
#include <util/helpers.h>
9-
107
#include <chainparams.h>
8+
#include <flat-database.h>
119
#include <key_io.h>
1210
#include <logging.h>
1311
#include <messagesigner.h>
14-
#include <net.h>
1512
#include <protocol.h>
1613
#include <script/standard.h>
1714
#include <timedata.h>
15+
#include <util/helpers.h>
1816
#include <util/message.h> // for MESSAGE_MAGIC
1917
#include <util/string.h>
2018

@@ -125,49 +123,37 @@ void CSporkManager::CheckAndRemove()
125123
}
126124
}
127125

128-
MessageProcessingResult CSporkManager::ProcessMessage(CNode& peer, CConnman& connman, std::string_view msg_type, CDataStream& vRecv)
126+
bool CSporkManager::IsSporkValid(const CSporkMessage& spork)
129127
{
130-
if (msg_type == NetMsgType::SPORK) {
131-
return ProcessSpork(peer.GetId(), vRecv);
132-
}
133-
return {};
134-
}
135-
136-
MessageProcessingResult CSporkManager::ProcessSpork(NodeId from, CDataStream& vRecv)
137-
{
138-
CSporkMessage spork;
139-
vRecv >> spork;
140-
141-
uint256 hash = spork.GetHash();
142-
143-
MessageProcessingResult ret{};
144-
ret.m_to_erase = CInv{MSG_SPORK, hash};
145-
146128
if (spork.nTimeSigned > GetAdjustedTime() + 2 * 60 * 60) {
147129
LogPrint(BCLog::SPORK, "CSporkManager::ProcessSpork -- ERROR: too far into the future\n");
148-
ret.m_error = MisbehavingError{100};
149-
return ret;
130+
return false;
150131
}
151132

152133
auto opt_keyIDSigner = spork.GetSignerKeyID();
153134

154135
if (opt_keyIDSigner == std::nullopt || WITH_LOCK(cs, return !setSporkPubKeyIDs.count(*opt_keyIDSigner))) {
155136
LogPrint(BCLog::SPORK, "CSporkManager::ProcessSpork -- ERROR: invalid signature\n");
156-
ret.m_error = MisbehavingError{100};
157-
return ret;
137+
return false;
158138
}
139+
return true;
140+
}
159141

160-
std::string strLogMsg{strprintf("SPORK -- hash: %s id: %d value: %10d peer=%d", hash.ToString(), spork.nSporkID,
161-
spork.nValue, from)};
162-
auto keyIDSigner = *opt_keyIDSigner;
142+
bool CSporkManager::ProcessSpork(const CSporkMessage& spork)
143+
{
144+
uint256 hash = spork.GetHash();
145+
std::string strLogMsg{strprintf("SPORK -- hash: %s id: %d value: %10d", hash.ToString(), spork.nSporkID,
146+
spork.nValue)};
147+
assert(spork.GetSignerKeyID().has_value());
148+
auto keyIDSigner = spork.GetSignerKeyID().value();
163149

164150
{
165151
LOCK(cs); // make sure to not lock this together with cs_main
166152
if (mapSporksActive.count(spork.nSporkID)) {
167153
if (mapSporksActive[spork.nSporkID].count(keyIDSigner)) {
168154
if (mapSporksActive[spork.nSporkID][keyIDSigner].nTimeSigned >= spork.nTimeSigned) {
169155
LogPrint(BCLog::SPORK, "%s seen\n", strLogMsg);
170-
return ret;
156+
return false;
171157
} else {
172158
LogPrintf("%s updated\n", strLogMsg);
173159
}
@@ -188,8 +174,7 @@ MessageProcessingResult CSporkManager::ProcessSpork(NodeId from, CDataStream& vR
188174
}
189175
}
190176

191-
ret.m_inventory.emplace_back(MSG_SPORK, hash);
192-
return ret;
177+
return true;
193178
}
194179

195180
std::unordered_map<SporkId, std::map<CKeyID, CSporkMessage>> CSporkManager::ActiveSporks() const

src/spork.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,23 @@
55
#ifndef BITCOIN_SPORK_H
66
#define BITCOIN_SPORK_H
77

8-
#include <msg_result.h>
9-
108
#include <hash.h>
119
#include <key.h>
12-
#include <net.h>
13-
#include <net_types.h>
1410
#include <pubkey.h>
1511
#include <saltedhasher.h>
1612
#include <sync.h>
17-
#include <uint256.h>
1813

1914
#include <array>
2015
#include <optional>
2116
#include <string_view>
2217
#include <unordered_map>
2318
#include <vector>
2419

25-
class CConnman;
2620
template<typename T>
2721
class CFlatDB;
28-
class CNode;
2922
class CDataStream;
23+
class uint256;
24+
class CInv;
3025

3126
class CSporkMessage;
3227
class CSporkManager;
@@ -249,18 +244,18 @@ class CSporkManager : public SporkStore
249244
void CheckAndRemove() EXCLUSIVE_LOCKS_REQUIRED(!cs);
250245

251246
/**
252-
* ProcessMessage is used to call ProcessSpork and ProcessGetSporks. See below
247+
* IsSporkValid validate signed time and pubkey
248+
* If these values mismatch function returns false [spork is invalid]
249+
* If spork validation failed, peer should be punished
253250
*/
254-
[[nodiscard]] MessageProcessingResult ProcessMessage(CNode& peer, CConnman& connman, std::string_view msg_type,
255-
CDataStream& vRecv) EXCLUSIVE_LOCKS_REQUIRED(!cs_cache);
256-
251+
[[nodiscard]] bool IsSporkValid(const CSporkMessage& spork) EXCLUSIVE_LOCKS_REQUIRED(!cs);
257252
/**
258253
* ProcessSpork is used to handle the 'spork' p2p message.
259254
*
260255
* For 'spork', it validates the spork and adds it to the internal spork storage and
261256
* performs any necessary processing.
262257
*/
263-
[[nodiscard]] MessageProcessingResult ProcessSpork(NodeId from, CDataStream& vRecv)
258+
[[nodiscard]] bool ProcessSpork(const CSporkMessage& spork)
264259
EXCLUSIVE_LOCKS_REQUIRED(!cs, !cs_cache);
265260

266261
/**

test/lint/lint-circular-dependencies.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# Dash
2424
"active/context -> active/dkgsessionhandler -> llmq/dkgsessionhandler -> net_processing -> active/context",
2525
"banman -> common/bloom -> evo/assetlocktx -> llmq/quorumsman -> llmq/blockprocessor -> net -> banman",
26-
"chainlock/chainlock -> spork -> net -> evo/deterministicmns -> evo/providertx -> validation -> chainlock/chainlock",
2726
"coinjoin/client -> coinjoin/util -> wallet/wallet -> psbt -> node/transaction -> net_processing -> coinjoin/walletman -> coinjoin/client",
2827
"common/bloom -> evo/assetlocktx -> llmq/commitment -> evo/deterministicmns -> evo/simplifiedmns -> merkleblock -> common/bloom",
2928
"common/bloom -> evo/assetlocktx -> llmq/quorumsman -> llmq/blockprocessor -> net -> common/bloom",

0 commit comments

Comments
 (0)