Skip to content

Commit bc84e24

Browse files
committed
p2p, refactor: Switch to enum class for ReconciliationRegisterResult
While doing this, add a new value: ALREADY_REGISTERED.
1 parent a60f729 commit bc84e24

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

src/net_processing.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,24 +3510,21 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
35103510
return;
35113511
}
35123512

3513-
if (m_txreconciliation->IsPeerRegistered(pfrom.GetId())) {
3514-
// A peer is already registered, meaning we already received SENDTXRCNCL from them.
3515-
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "txreconciliation protocol violation from peer=%d (sendtxrcncl received from already registered peer); disconnecting\n", pfrom.GetId());
3516-
pfrom.fDisconnect = true;
3517-
return;
3518-
}
3519-
35203513
uint32_t peer_txreconcl_version;
35213514
uint64_t remote_salt;
35223515
vRecv >> peer_txreconcl_version >> remote_salt;
35233516

35243517
const ReconciliationRegisterResult result = m_txreconciliation->RegisterPeer(pfrom.GetId(), pfrom.IsInboundConn(),
35253518
peer_txreconcl_version, remote_salt);
3526-
3527-
// If it's a protocol violation, disconnect.
3528-
// If the peer was not found (but something unexpected happened) or it was registered,
3529-
// nothing to be done.
3530-
if (result == ReconciliationRegisterResult::PROTOCOL_VIOLATION) {
3519+
switch (result) {
3520+
case ReconciliationRegisterResult::NOT_FOUND:
3521+
case ReconciliationRegisterResult::SUCCESS:
3522+
break;
3523+
case ReconciliationRegisterResult::ALREADY_REGISTERED:
3524+
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "txreconciliation protocol violation from peer=%d (sendtxrcncl received from already registered peer); disconnecting\n", pfrom.GetId());
3525+
pfrom.fDisconnect = true;
3526+
return;
3527+
case ReconciliationRegisterResult::PROTOCOL_VIOLATION:
35313528
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "txreconciliation protocol violation from peer=%d; disconnecting\n", pfrom.GetId());
35323529
pfrom.fDisconnect = true;
35333530
return;

src/node/txreconciliation.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ class TxReconciliationTracker::Impl
101101
LOCK(m_txreconciliation_mutex);
102102
auto recon_state = m_states.find(peer_id);
103103

104-
// A peer should be in the pre-registered state to proceed here.
105-
if (recon_state == m_states.end()) return NOT_FOUND;
106-
uint64_t* local_salt = std::get_if<uint64_t>(&recon_state->second);
107-
// A peer is already registered. This should be checked by the caller.
108-
Assume(local_salt);
104+
if (recon_state == m_states.end()) return ReconciliationRegisterResult::NOT_FOUND;
105+
106+
if (std::holds_alternative<TxReconciliationState>(recon_state->second)) {
107+
return ReconciliationRegisterResult::ALREADY_REGISTERED;
108+
}
109+
110+
uint64_t local_salt = *std::get_if<uint64_t>(&recon_state->second);
109111

110112
// If the peer supports the version which is lower than ours, we downgrade to the version
111113
// it supports. For now, this only guarantees that nodes with future reconciliation
@@ -114,14 +116,14 @@ class TxReconciliationTracker::Impl
114116
// satisfactory (e.g. too low).
115117
const uint32_t recon_version{std::min(peer_recon_version, m_recon_version)};
116118
// v1 is the lowest version, so suggesting something below must be a protocol violation.
117-
if (recon_version < 1) return PROTOCOL_VIOLATION;
119+
if (recon_version < 1) return ReconciliationRegisterResult::PROTOCOL_VIOLATION;
118120

119121
LogPrintLevel(BCLog::TXRECONCILIATION, BCLog::Level::Debug, "Register peer=%d (inbound=%i)\n",
120122
peer_id, is_peer_inbound);
121123

122-
const uint256 full_salt{ComputeSalt(*local_salt, remote_salt)};
124+
const uint256 full_salt{ComputeSalt(local_salt, remote_salt)};
123125
recon_state->second = TxReconciliationState(!is_peer_inbound, full_salt.GetUint64(0), full_salt.GetUint64(1));
124-
return SUCCESS;
126+
return ReconciliationRegisterResult::SUCCESS;
125127
}
126128

127129
void ForgetPeer(NodeId peer_id) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex)

src/node/txreconciliation.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false};
1616
/** Supported transaction reconciliation protocol version */
1717
static constexpr uint32_t TXRECONCILIATION_VERSION{1};
1818

19-
enum ReconciliationRegisterResult {
20-
NOT_FOUND = 0,
21-
SUCCESS = 1,
22-
PROTOCOL_VIOLATION = 2,
19+
enum class ReconciliationRegisterResult {
20+
NOT_FOUND,
21+
SUCCESS,
22+
ALREADY_REGISTERED,
23+
PROTOCOL_VIOLATION,
2324
};
2425

2526
/**

src/test/txreconciliation_tests.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ BOOST_AUTO_TEST_CASE(RegisterPeerTest)
3333
BOOST_REQUIRE(tracker.RegisterPeer(1, true, 2, salt) == ReconciliationRegisterResult::SUCCESS);
3434
BOOST_CHECK(tracker.IsPeerRegistered(1));
3535

36+
// Try registering for the second time.
37+
BOOST_REQUIRE(tracker.RegisterPeer(1, false, 1, salt) == ReconciliationRegisterResult::ALREADY_REGISTERED);
38+
3639
// Do not register if there were no pre-registration for the peer.
3740
BOOST_REQUIRE(tracker.RegisterPeer(100, true, 1, salt) == ReconciliationRegisterResult::NOT_FOUND);
3841
BOOST_CHECK(!tracker.IsPeerRegistered(100));

0 commit comments

Comments
 (0)