Skip to content

Commit 3339ba2

Browse files
committed
Make g_enable_bip61 a member variable of PeerLogicValidation
1 parent 6690a28 commit 3339ba2

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ bool AppInitMain()
13171317
g_connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
13181318
CConnman& connman = *g_connman;
13191319

1320-
peerLogic.reset(new PeerLogicValidation(&connman, scheduler));
1320+
peerLogic.reset(new PeerLogicValidation(&connman, scheduler, gArgs.GetBoolArg("-enablebip61", DEFAULT_ENABLE_BIP61)));
13211321
RegisterValidationInterface(peerLogic.get());
13221322

13231323
// sanitize comments per BIP-0014, format user agent and check total size

src/net_processing.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ void Misbehaving(NodeId nodeid, int howmuch, const std::string& message="");
8181

8282
// Internal stuff
8383
namespace {
84-
/** Enable BIP61 (sending reject messages) */
85-
bool g_enable_bip61 = DEFAULT_ENABLE_BIP61;
86-
8784
/** Number of nodes with fSyncStarted. */
8885
int nSyncStarted = 0;
8986

@@ -834,8 +831,8 @@ static bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Para
834831
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, consensusParams) < STALE_RELAY_AGE_LIMIT);
835832
}
836833

837-
PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn, CScheduler &scheduler) : connman(connmanIn), m_stale_tip_check_time(0) {
838-
g_enable_bip61 = gArgs.GetBoolArg("-enablebip61", DEFAULT_ENABLE_BIP61);
834+
PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn, CScheduler &scheduler, bool enable_bip61)
835+
: connman(connmanIn), m_stale_tip_check_time(0), m_enable_bip61(enable_bip61) {
839836

840837
// Initialize global variables that cannot be constructed at startup.
841838
recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
@@ -1564,7 +1561,7 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
15641561
return true;
15651562
}
15661563

1567-
bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman* connman, const std::atomic<bool>& interruptMsgProc)
1564+
bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman* connman, const std::atomic<bool>& interruptMsgProc, bool enable_bip61)
15681565
{
15691566
LogPrint(BCLog::NET, "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->GetId());
15701567
if (gArgs.IsArgSet("-dropmessagestest") && GetRand(gArgs.GetArg("-dropmessagestest", 0)) == 0)
@@ -1618,7 +1615,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
16181615
// Each connection can only send one version message
16191616
if (pfrom->nVersion != 0)
16201617
{
1621-
if (g_enable_bip61) {
1618+
if (enable_bip61) {
16221619
connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_DUPLICATE, std::string("Duplicate version message")));
16231620
}
16241621
LOCK(cs_main);
@@ -1649,7 +1646,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
16491646
if (!pfrom->fInbound && !pfrom->fFeeler && !pfrom->m_manual_connection && !HasAllDesirableServiceFlags(nServices))
16501647
{
16511648
LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->GetId(), nServices, GetDesirableServiceFlags(nServices));
1652-
if (g_enable_bip61) {
1649+
if (enable_bip61) {
16531650
connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_NONSTANDARD,
16541651
strprintf("Expected to offer services %08x", GetDesirableServiceFlags(nServices))));
16551652
}
@@ -1672,7 +1669,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
16721669
{
16731670
// disconnect from peers older than this proto version
16741671
LogPrint(BCLog::NET, "peer=%d using obsolete version %i; disconnecting\n", pfrom->GetId(), nVersion);
1675-
if (g_enable_bip61) {
1672+
if (enable_bip61) {
16761673
connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
16771674
strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION)));
16781675
}
@@ -2373,7 +2370,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
23732370
LogPrint(BCLog::MEMPOOLREJ, "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
23742371
pfrom->GetId(),
23752372
FormatStateMessage(state));
2376-
if (g_enable_bip61 && state.GetRejectCode() > 0 && state.GetRejectCode() < REJECT_INTERNAL) { // Never send AcceptToMemoryPool's internal codes over P2P
2373+
if (enable_bip61 && state.GetRejectCode() > 0 && state.GetRejectCode() < REJECT_INTERNAL) { // Never send AcceptToMemoryPool's internal codes over P2P
23772374
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
23782375
state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash));
23792376
}
@@ -2558,7 +2555,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
25582555
} // cs_main
25592556

25602557
if (fProcessBLOCKTXN)
2561-
return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, nTimeReceived, chainparams, connman, interruptMsgProc);
2558+
return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, nTimeReceived, chainparams, connman, interruptMsgProc, enable_bip61);
25622559

25632560
if (fRevertToHeaderProcessing) {
25642561
// Headers received from HB compact block peers are permitted to be
@@ -2944,12 +2941,12 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
29442941
return true;
29452942
}
29462943

2947-
static bool SendRejectsAndCheckIfBanned(CNode* pnode, CConnman* connman)
2944+
static bool SendRejectsAndCheckIfBanned(CNode* pnode, CConnman* connman, bool enable_bip61)
29482945
{
29492946
AssertLockHeld(cs_main);
29502947
CNodeState &state = *State(pnode->GetId());
29512948

2952-
if (g_enable_bip61) {
2949+
if (enable_bip61) {
29532950
for (const CBlockReject& reject : state.rejects) {
29542951
connman->PushMessage(pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, std::string(NetMsgType::BLOCK), reject.chRejectCode, reject.strRejectReason, reject.hashBlock));
29552952
}
@@ -3051,15 +3048,15 @@ bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& inter
30513048
bool fRet = false;
30523049
try
30533050
{
3054-
fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman, interruptMsgProc);
3051+
fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman, interruptMsgProc, m_enable_bip61);
30553052
if (interruptMsgProc)
30563053
return false;
30573054
if (!pfrom->vRecvGetData.empty())
30583055
fMoreWork = true;
30593056
}
30603057
catch (const std::ios_base::failure& e)
30613058
{
3062-
if (g_enable_bip61) {
3059+
if (m_enable_bip61) {
30633060
connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_MALFORMED, std::string("error parsing message")));
30643061
}
30653062
if (strstr(e.what(), "end of data"))
@@ -3093,7 +3090,7 @@ bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& inter
30933090
}
30943091

30953092
LOCK(cs_main);
3096-
SendRejectsAndCheckIfBanned(pfrom, connman);
3093+
SendRejectsAndCheckIfBanned(pfrom, connman, m_enable_bip61);
30973094

30983095
return fMoreWork;
30993096
}
@@ -3291,7 +3288,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic<bool>& interruptM
32913288
if (!lockMain)
32923289
return true;
32933290

3294-
if (SendRejectsAndCheckIfBanned(pto, connman))
3291+
if (SendRejectsAndCheckIfBanned(pto, connman, m_enable_bip61))
32953292
return true;
32963293
CNodeState &state = *State(pto->GetId());
32973294

src/net_processing.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PeerLogicValidation final : public CValidationInterface, public NetEventsI
2222
CConnman* const connman;
2323

2424
public:
25-
explicit PeerLogicValidation(CConnman* connman, CScheduler &scheduler);
25+
explicit PeerLogicValidation(CConnman* connman, CScheduler &scheduler, bool enable_bip61);
2626

2727
/**
2828
* Overridden from CValidationInterface.
@@ -65,6 +65,9 @@ class PeerLogicValidation final : public CValidationInterface, public NetEventsI
6565

6666
private:
6767
int64_t m_stale_tip_check_time; //! Next time to check for stale tip
68+
69+
/** Enable BIP61 (sending reject messages) */
70+
const bool m_enable_bip61;
6871
};
6972

7073
struct CNodeStateStats {

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
9999
threadGroup.create_thread(&ThreadScriptCheck);
100100
g_connman = std::unique_ptr<CConnman>(new CConnman(0x1337, 0x1337)); // Deterministic randomness for tests.
101101
connman = g_connman.get();
102-
peerLogic.reset(new PeerLogicValidation(connman, scheduler));
102+
peerLogic.reset(new PeerLogicValidation(connman, scheduler, /*enable_bip61=*/true));
103103
}
104104

105105
TestingSetup::~TestingSetup()

0 commit comments

Comments
 (0)