Skip to content

Commit 38a16a2

Browse files
committed
merge bitcoin#26359: Erlay support signaling follow-ups
1 parent b55a6f7 commit 38a16a2

File tree

8 files changed

+165
-163
lines changed

8 files changed

+165
-163
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ void SetupServerArgs(ArgsManager& argsman)
579579
argsman.AddArg("-v2transport", strprintf("Support v2 transport (default: %u)", DEFAULT_V2_TRANSPORT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
580580
argsman.AddArg("-peerblockfilters", strprintf("Serve compact block filters to peers per BIP 157 (default: %u)", DEFAULT_PEERBLOCKFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
581581
argsman.AddArg("-peerbloomfilters", strprintf("Support filtering of blocks and transaction with bloom filters (default: %u)", DEFAULT_PEERBLOOMFILTERS), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
582+
argsman.AddArg("-txreconciliation", strprintf("Enable transaction reconciliations per BIP 330 (default: %d)", DEFAULT_TXRECONCILIATION_ENABLE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CONNECTION);
582583
argsman.AddArg("-peertimeout=<n>", strprintf("Specify a p2p connection timeout delay in seconds. After connecting to a peer, wait this amount of time before considering disconnection based on inactivity (minimum: 1, default: %d)", DEFAULT_PEER_CONNECT_TIMEOUT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
583584
argsman.AddArg("-permitbaremultisig", strprintf("Relay non-P2SH multisig (default: %u)", DEFAULT_PERMIT_BAREMULTISIG), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
584585
argsman.AddArg("-port=<port>", strprintf("Listen for connections on <port>. Nodes not using the default ports (default: %u, testnet: %u, regtest: %u) are unlikely to get incoming connections. Not relevant for I2P (see doc/i2p.md).", defaultChainParams->GetDefaultPort(), testnetChainParams->GetDefaultPort(), regtestChainParams->GetDefaultPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
@@ -588,7 +589,6 @@ void SetupServerArgs(ArgsManager& argsman)
588589
argsman.AddArg("-socketevents=<mode>", "Socket events mode, which must be one of 'select', 'poll', 'epoll' or 'kqueue', depending on your system (default: Linux - 'epoll', FreeBSD/Apple - 'kqueue', Windows - 'select')", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
589590
argsman.AddArg("-networkactive", "Enable all P2P network activity (default: 1). Can be changed by the setnetworkactive RPC command", ArgsManager::ALLOW_BOOL, OptionsCategory::CONNECTION);
590591
argsman.AddArg("-timeout=<n>", strprintf("Specify socket connection timeout in milliseconds. If an initial attempt to connect is unsuccessful after this amount of time, drop it (minimum: 1, default: %d)", DEFAULT_CONNECT_TIMEOUT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
591-
argsman.AddArg("-txreconciliation", strprintf("Enable transaction reconciliations per BIP 330 (default: %d)", DEFAULT_TXRECONCILIATION_ENABLE), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CONNECTION);
592592
argsman.AddArg("-torcontrol=<ip>:<port>", strprintf("Tor control port to use if onion listening enabled (default: %s)", DEFAULT_TOR_CONTROL), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
593593
argsman.AddArg("-torpassword=<pass>", "Tor control port password (default: empty)", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::CONNECTION);
594594
#ifdef USE_UPNP

src/net_processing.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,17 +3544,14 @@ void PeerManagerImpl::ProcessMessage(
35443544

35453545
if (greatest_common_version >= INCREASE_MAX_HEADERS2_VERSION && m_txreconciliation) {
35463546
// Per BIP-330, we announce txreconciliation support if:
3547-
// - protocol version per the VERSION message supports INCREASE_MAX_HEADERS2_VERSION;
3548-
// - we intended to exchange transactions over this connection while establishing it
3549-
// and the peer indicated support for transaction relay in the VERSION message;
3547+
// - protocol version per the peer's VERSION message supports INCREASE_MAX_HEADERS2_VERSION;
3548+
// - transaction relay is supported per the peer's VERSION message (see m_relays_txs);
3549+
// - this is not a block-relay-only connection and not a feeler (see m_relays_txs);
3550+
// - this is not an addr fetch connection;
35503551
// - we are not in -blocksonly mode.
3551-
if (pfrom.m_relays_txs && !m_ignore_incoming_txs) {
3552+
if (pfrom.m_relays_txs && !pfrom.IsAddrFetchConn() && !m_ignore_incoming_txs) {
35523553
const uint64_t recon_salt = m_txreconciliation->PreRegisterPeer(pfrom.GetId());
3553-
// We suggest our txreconciliation role (initiator/responder) based on
3554-
// the connection direction.
35553554
m_connman.PushMessage(&pfrom, msg_maker.Make(NetMsgType::SENDTXRCNCL,
3556-
!pfrom.IsInboundConn(),
3557-
pfrom.IsInboundConn(),
35583555
TXRECONCILIATION_VERSION, recon_salt));
35593556
}
35603557
}
@@ -3771,41 +3768,44 @@ void PeerManagerImpl::ProcessMessage(
37713768
}
37723769

37733770
if (pfrom.fSuccessfullyConnected) {
3774-
// Disconnect peers that send a SENDTXRCNCL message after VERACK.
37753771
LogPrint(BCLog::NET, "sendtxrcncl received after verack from peer=%d; disconnecting\n", pfrom.GetId());
37763772
pfrom.fDisconnect = true;
37773773
return;
37783774
}
37793775

3780-
if (!peer->GetTxRelay()) {
3781-
// Disconnect peers that send a SENDTXRCNCL message even though we indicated we don't
3782-
// support transaction relay.
3776+
// Peer must not offer us reconciliations if we specified no tx relay support in VERSION.
3777+
if (RejectIncomingTxs(pfrom)) {
37833778
LogPrint(BCLog::NET, "sendtxrcncl received from peer=%d to which we indicated no tx relay; disconnecting\n", pfrom.GetId());
37843779
pfrom.fDisconnect = true;
37853780
return;
37863781
}
37873782

3788-
bool is_peer_initiator, is_peer_responder;
3783+
// Peer must not offer us reconciliations if they specified no tx relay support in VERSION.
3784+
// This flag might also be false in other cases, but the RejectIncomingTxs check above
3785+
// eliminates them, so that this flag fully represents what we are looking for.
3786+
if (!pfrom.m_relays_txs) {
3787+
LogPrint(BCLog::NET, "sendtxrcncl received from peer=%d which indicated no tx relay to us; disconnecting\n", pfrom.GetId());
3788+
pfrom.fDisconnect = true;
3789+
return;
3790+
}
3791+
37893792
uint32_t peer_txreconcl_version;
37903793
uint64_t remote_salt;
3791-
vRecv >> is_peer_initiator >> is_peer_responder >> peer_txreconcl_version >> remote_salt;
3794+
vRecv >> peer_txreconcl_version >> remote_salt;
37923795

3793-
if (m_txreconciliation->IsPeerRegistered(pfrom.GetId())) {
3794-
// A peer is already registered, meaning we already received SENDTXRCNCL from them.
3796+
const ReconciliationRegisterResult result = m_txreconciliation->RegisterPeer(pfrom.GetId(), pfrom.IsInboundConn(),
3797+
peer_txreconcl_version, remote_salt);
3798+
switch (result) {
3799+
case ReconciliationRegisterResult::NOT_FOUND:
3800+
LogPrint(BCLog::NET, "Ignore unexpected txreconciliation signal from peer=%d\n", pfrom.GetId());
3801+
break;
3802+
case ReconciliationRegisterResult::SUCCESS:
3803+
break;
3804+
case ReconciliationRegisterResult::ALREADY_REGISTERED:
37953805
LogPrint(BCLog::NET, "txreconciliation protocol violation from peer=%d (sendtxrcncl received from already registered peer); disconnecting\n", pfrom.GetId());
37963806
pfrom.fDisconnect = true;
37973807
return;
3798-
}
3799-
3800-
const ReconciliationRegisterResult result = m_txreconciliation->RegisterPeer(pfrom.GetId(), pfrom.IsInboundConn(),
3801-
is_peer_initiator, is_peer_responder,
3802-
peer_txreconcl_version,
3803-
remote_salt);
3804-
3805-
// If it's a protocol violation, disconnect.
3806-
// If the peer was not found (but something unexpected happened) or it was registered,
3807-
// nothing to be done.
3808-
if (result == ReconciliationRegisterResult::PROTOCOL_VIOLATION) {
3808+
case ReconciliationRegisterResult::PROTOCOL_VIOLATION:
38093809
LogPrint(BCLog::NET, "txreconciliation protocol violation from peer=%d; disconnecting\n", pfrom.GetId());
38103810
pfrom.fDisconnect = true;
38113811
return;

src/node/txreconciliation.cpp

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class TxReconciliationState
3939
* the following commits.
4040
*
4141
* Reconciliation protocol assumes using one role consistently: either a reconciliation
42-
* initiator (requesting sketches), or responder (sending sketches). This defines our role.
42+
* initiator (requesting sketches), or responder (sending sketches). This defines our role,
43+
* based on the direction of the p2p connection.
4344
*
4445
*/
4546
bool m_we_initiate;
@@ -81,31 +82,30 @@ class TxReconciliationTracker::Impl
8182
{
8283
AssertLockNotHeld(m_txreconciliation_mutex);
8384
LOCK(m_txreconciliation_mutex);
84-
// We do not support txreconciliation salt/version updates.
85-
assert(m_states.find(peer_id) == m_states.end());
8685

8786
LogPrint(BCLog::TXRECONCILIATION, "Pre-register peer=%d\n", peer_id);
8887
const uint64_t local_salt{GetRand(UINT64_MAX)};
8988

9089
// We do this exactly once per peer (which are unique by NodeId, see GetNewNodeId) so it's
9190
// safe to assume we don't have this record yet.
92-
Assert(m_states.emplace(peer_id, local_salt).second);
91+
Assume(m_states.emplace(peer_id, local_salt).second);
9392
return local_salt;
9493
}
9594

96-
ReconciliationRegisterResult RegisterPeer(NodeId peer_id, bool is_peer_inbound, bool is_peer_recon_initiator,
97-
bool is_peer_recon_responder, uint32_t peer_recon_version,
98-
uint64_t remote_salt) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex)
95+
ReconciliationRegisterResult RegisterPeer(NodeId peer_id, bool is_peer_inbound, uint32_t peer_recon_version,
96+
uint64_t remote_salt) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex)
9997
{
10098
AssertLockNotHeld(m_txreconciliation_mutex);
10199
LOCK(m_txreconciliation_mutex);
102100
auto recon_state = m_states.find(peer_id);
103101

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);
102+
if (recon_state == m_states.end()) return ReconciliationRegisterResult::NOT_FOUND;
103+
104+
if (std::holds_alternative<TxReconciliationState>(recon_state->second)) {
105+
return ReconciliationRegisterResult::ALREADY_REGISTERED;
106+
}
107+
108+
uint64_t local_salt = *std::get_if<uint64_t>(&recon_state->second);
109109

110110
// If the peer supports the version which is lower than ours, we downgrade to the version
111111
// it supports. For now, this only guarantees that nodes with future reconciliation
@@ -114,27 +114,13 @@ class TxReconciliationTracker::Impl
114114
// satisfactory (e.g. too low).
115115
const uint32_t recon_version{std::min(peer_recon_version, m_recon_version)};
116116
// v1 is the lowest version, so suggesting something below must be a protocol violation.
117-
if (recon_version < 1) return PROTOCOL_VIOLATION;
118-
119-
// Must match SENDTXRCNCL logic.
120-
const bool they_initiate = is_peer_recon_initiator && is_peer_inbound;
121-
const bool we_initiate = !is_peer_inbound && is_peer_recon_responder;
122-
123-
// If we ever announce support for both requesting and responding, this will need
124-
// tie-breaking. For now, this is mutually exclusive because both are based on the
125-
// inbound flag.
126-
assert(!(they_initiate && we_initiate));
127-
128-
// The peer set both flags to false, we treat it as a protocol violation.
129-
if (!(they_initiate || we_initiate)) return PROTOCOL_VIOLATION;
117+
if (recon_version < 1) return ReconciliationRegisterResult::PROTOCOL_VIOLATION;
130118

131-
LogPrint(BCLog::TXRECONCILIATION, "Register peer=%d with the following params: " /* Continued */
132-
"we_initiate=%i, they_initiate=%i.\n",
133-
peer_id, we_initiate, they_initiate);
119+
LogPrint(BCLog::TXRECONCILIATION, "Register peer=%d (inbound=%i)\n", peer_id, is_peer_inbound);
134120

135-
const uint256 full_salt{ComputeSalt(*local_salt, remote_salt)};
136-
recon_state->second = TxReconciliationState(we_initiate, full_salt.GetUint64(0), full_salt.GetUint64(1));
137-
return SUCCESS;
121+
const uint256 full_salt{ComputeSalt(local_salt, remote_salt)};
122+
recon_state->second = TxReconciliationState(!is_peer_inbound, full_salt.GetUint64(0), full_salt.GetUint64(1));
123+
return ReconciliationRegisterResult::SUCCESS;
138124
}
139125

140126
void ForgetPeer(NodeId peer_id) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex)
@@ -166,11 +152,9 @@ uint64_t TxReconciliationTracker::PreRegisterPeer(NodeId peer_id)
166152
}
167153

168154
ReconciliationRegisterResult TxReconciliationTracker::RegisterPeer(NodeId peer_id, bool is_peer_inbound,
169-
bool is_peer_recon_initiator, bool is_peer_recon_responder,
170155
uint32_t peer_recon_version, uint64_t remote_salt)
171156
{
172-
return m_impl->RegisterPeer(peer_id, is_peer_inbound, is_peer_recon_initiator, is_peer_recon_responder,
173-
peer_recon_version, remote_salt);
157+
return m_impl->RegisterPeer(peer_id, is_peer_inbound, peer_recon_version, remote_salt);
174158
}
175159

176160
void TxReconciliationTracker::ForgetPeer(NodeId peer_id)

src/node/txreconciliation.h

Lines changed: 7 additions & 6 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
/**
@@ -72,8 +73,8 @@ class TxReconciliationTracker
7273
* Step 0. Once the peer agreed to reconcile txs with us, generate the state required to track
7374
* ongoing reconciliations. Must be called only after pre-registering the peer and only once.
7475
*/
75-
ReconciliationRegisterResult RegisterPeer(NodeId peer_id, bool is_peer_inbound, bool is_peer_recon_initiator,
76-
bool is_peer_recon_responder, uint32_t peer_recon_version, uint64_t remote_salt);
76+
ReconciliationRegisterResult RegisterPeer(NodeId peer_id, bool is_peer_inbound,
77+
uint32_t peer_recon_version, uint64_t remote_salt);
7778

7879
/**
7980
* Attempts to forget txreconciliation-related state of the peer (if we previously stored any).

src/protocol.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,7 @@ extern const char* GETCFCHECKPT;
255255
*/
256256
extern const char* CFCHECKPT;
257257
/**
258-
* Contains 2 1-byte bools, a 4-byte version number and an 8-byte salt.
259-
* The 2 booleans indicate that a node is willing to participate in transaction
260-
* reconciliation, respectively as an initiator or as a receiver.
258+
* Contains a 4-byte version number and an 8-byte salt.
261259
* The salt is used to compute short txids needed for efficient
262260
* txreconciliation, as described by BIP 330.
263261
*/

0 commit comments

Comments
 (0)