mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
p2p, refactor: Switch to enum class for ReconciliationRegisterResult
While doing this, add a new value: ALREADY_REGISTERED.
This commit is contained in:
parent
a60f729e29
commit
bc84e24a4f
4 changed files with 27 additions and 24 deletions
|
@ -3510,24 +3510,21 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_txreconciliation->IsPeerRegistered(pfrom.GetId())) {
|
|
||||||
// A peer is already registered, meaning we already received SENDTXRCNCL from them.
|
|
||||||
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "txreconciliation protocol violation from peer=%d (sendtxrcncl received from already registered peer); disconnecting\n", pfrom.GetId());
|
|
||||||
pfrom.fDisconnect = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t peer_txreconcl_version;
|
uint32_t peer_txreconcl_version;
|
||||||
uint64_t remote_salt;
|
uint64_t remote_salt;
|
||||||
vRecv >> peer_txreconcl_version >> remote_salt;
|
vRecv >> peer_txreconcl_version >> remote_salt;
|
||||||
|
|
||||||
const ReconciliationRegisterResult result = m_txreconciliation->RegisterPeer(pfrom.GetId(), pfrom.IsInboundConn(),
|
const ReconciliationRegisterResult result = m_txreconciliation->RegisterPeer(pfrom.GetId(), pfrom.IsInboundConn(),
|
||||||
peer_txreconcl_version, remote_salt);
|
peer_txreconcl_version, remote_salt);
|
||||||
|
switch (result) {
|
||||||
// If it's a protocol violation, disconnect.
|
case ReconciliationRegisterResult::NOT_FOUND:
|
||||||
// If the peer was not found (but something unexpected happened) or it was registered,
|
case ReconciliationRegisterResult::SUCCESS:
|
||||||
// nothing to be done.
|
break;
|
||||||
if (result == ReconciliationRegisterResult::PROTOCOL_VIOLATION) {
|
case ReconciliationRegisterResult::ALREADY_REGISTERED:
|
||||||
|
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "txreconciliation protocol violation from peer=%d (sendtxrcncl received from already registered peer); disconnecting\n", pfrom.GetId());
|
||||||
|
pfrom.fDisconnect = true;
|
||||||
|
return;
|
||||||
|
case ReconciliationRegisterResult::PROTOCOL_VIOLATION:
|
||||||
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "txreconciliation protocol violation from peer=%d; disconnecting\n", pfrom.GetId());
|
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "txreconciliation protocol violation from peer=%d; disconnecting\n", pfrom.GetId());
|
||||||
pfrom.fDisconnect = true;
|
pfrom.fDisconnect = true;
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -101,11 +101,13 @@ public:
|
||||||
LOCK(m_txreconciliation_mutex);
|
LOCK(m_txreconciliation_mutex);
|
||||||
auto recon_state = m_states.find(peer_id);
|
auto recon_state = m_states.find(peer_id);
|
||||||
|
|
||||||
// A peer should be in the pre-registered state to proceed here.
|
if (recon_state == m_states.end()) return ReconciliationRegisterResult::NOT_FOUND;
|
||||||
if (recon_state == m_states.end()) return NOT_FOUND;
|
|
||||||
uint64_t* local_salt = std::get_if<uint64_t>(&recon_state->second);
|
if (std::holds_alternative<TxReconciliationState>(recon_state->second)) {
|
||||||
// A peer is already registered. This should be checked by the caller.
|
return ReconciliationRegisterResult::ALREADY_REGISTERED;
|
||||||
Assume(local_salt);
|
}
|
||||||
|
|
||||||
|
uint64_t local_salt = *std::get_if<uint64_t>(&recon_state->second);
|
||||||
|
|
||||||
// If the peer supports the version which is lower than ours, we downgrade to the version
|
// If the peer supports the version which is lower than ours, we downgrade to the version
|
||||||
// it supports. For now, this only guarantees that nodes with future reconciliation
|
// it supports. For now, this only guarantees that nodes with future reconciliation
|
||||||
|
@ -114,14 +116,14 @@ public:
|
||||||
// satisfactory (e.g. too low).
|
// satisfactory (e.g. too low).
|
||||||
const uint32_t recon_version{std::min(peer_recon_version, m_recon_version)};
|
const uint32_t recon_version{std::min(peer_recon_version, m_recon_version)};
|
||||||
// v1 is the lowest version, so suggesting something below must be a protocol violation.
|
// v1 is the lowest version, so suggesting something below must be a protocol violation.
|
||||||
if (recon_version < 1) return PROTOCOL_VIOLATION;
|
if (recon_version < 1) return ReconciliationRegisterResult::PROTOCOL_VIOLATION;
|
||||||
|
|
||||||
LogPrintLevel(BCLog::TXRECONCILIATION, BCLog::Level::Debug, "Register peer=%d (inbound=%i)\n",
|
LogPrintLevel(BCLog::TXRECONCILIATION, BCLog::Level::Debug, "Register peer=%d (inbound=%i)\n",
|
||||||
peer_id, is_peer_inbound);
|
peer_id, is_peer_inbound);
|
||||||
|
|
||||||
const uint256 full_salt{ComputeSalt(*local_salt, remote_salt)};
|
const uint256 full_salt{ComputeSalt(local_salt, remote_salt)};
|
||||||
recon_state->second = TxReconciliationState(!is_peer_inbound, full_salt.GetUint64(0), full_salt.GetUint64(1));
|
recon_state->second = TxReconciliationState(!is_peer_inbound, full_salt.GetUint64(0), full_salt.GetUint64(1));
|
||||||
return SUCCESS;
|
return ReconciliationRegisterResult::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ForgetPeer(NodeId peer_id) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex)
|
void ForgetPeer(NodeId peer_id) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex)
|
||||||
|
|
|
@ -16,10 +16,11 @@ static constexpr bool DEFAULT_TXRECONCILIATION_ENABLE{false};
|
||||||
/** Supported transaction reconciliation protocol version */
|
/** Supported transaction reconciliation protocol version */
|
||||||
static constexpr uint32_t TXRECONCILIATION_VERSION{1};
|
static constexpr uint32_t TXRECONCILIATION_VERSION{1};
|
||||||
|
|
||||||
enum ReconciliationRegisterResult {
|
enum class ReconciliationRegisterResult {
|
||||||
NOT_FOUND = 0,
|
NOT_FOUND,
|
||||||
SUCCESS = 1,
|
SUCCESS,
|
||||||
PROTOCOL_VIOLATION = 2,
|
ALREADY_REGISTERED,
|
||||||
|
PROTOCOL_VIOLATION,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -33,6 +33,9 @@ BOOST_AUTO_TEST_CASE(RegisterPeerTest)
|
||||||
BOOST_REQUIRE(tracker.RegisterPeer(1, true, 2, salt) == ReconciliationRegisterResult::SUCCESS);
|
BOOST_REQUIRE(tracker.RegisterPeer(1, true, 2, salt) == ReconciliationRegisterResult::SUCCESS);
|
||||||
BOOST_CHECK(tracker.IsPeerRegistered(1));
|
BOOST_CHECK(tracker.IsPeerRegistered(1));
|
||||||
|
|
||||||
|
// Try registering for the second time.
|
||||||
|
BOOST_REQUIRE(tracker.RegisterPeer(1, false, 1, salt) == ReconciliationRegisterResult::ALREADY_REGISTERED);
|
||||||
|
|
||||||
// Do not register if there were no pre-registration for the peer.
|
// Do not register if there were no pre-registration for the peer.
|
||||||
BOOST_REQUIRE(tracker.RegisterPeer(100, true, 1, salt) == ReconciliationRegisterResult::NOT_FOUND);
|
BOOST_REQUIRE(tracker.RegisterPeer(100, true, 1, salt) == ReconciliationRegisterResult::NOT_FOUND);
|
||||||
BOOST_CHECK(!tracker.IsPeerRegistered(100));
|
BOOST_CHECK(!tracker.IsPeerRegistered(100));
|
||||||
|
|
Loading…
Add table
Reference in a new issue