From 181ffadd162a84551b3518de77b5dcc08c712425 Mon Sep 17 00:00:00 2001 From: Suhas Daftuar Date: Thu, 30 Jan 2020 10:10:50 -0500 Subject: Add p2p message "wtxidrelay" When sent to and received from a given peer, enables using wtxid's for announcing and fetching transactions with that peer. --- src/net_processing.cpp | 16 ++++++++++++++++ src/protocol.cpp | 2 ++ src/protocol.h | 8 +++++++- src/version.h | 5 ++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 50aea0fd0b..835095f4d0 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -2055,6 +2055,10 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec if (pfrom->fInbound) PushNodeVersion(pfrom, connman, GetAdjustedTime()); + if (nVersion >= WTXID_RELAY_VERSION) { + connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::WTXIDRELAY)); + } + connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERACK)); pfrom->nServices = nServices; @@ -2194,6 +2198,18 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec return true; } + // Feature negotiation of wtxidrelay should happen between VERSION and + // VERACK, to avoid relay problems from switching after a connection is up + if (msg_type == NetMsgType::WTXIDRELAY) { + if (pfrom->nVersion >= WTXID_RELAY_VERSION) { + LOCK(cs_main); + if (!State(pfrom->GetId())->m_wtxid_relay) { + State(pfrom->GetId())->m_wtxid_relay = true; + } + } + return false; + } + if (!pfrom->fSuccessfullyConnected) { // Must have a verack message before anything else LOCK(cs_main); diff --git a/src/protocol.cpp b/src/protocol.cpp index f5cf9e6de8..8b2043fda4 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -40,6 +40,7 @@ const char *SENDCMPCT="sendcmpct"; const char *CMPCTBLOCK="cmpctblock"; const char *GETBLOCKTXN="getblocktxn"; const char *BLOCKTXN="blocktxn"; +const char *WTXIDRELAY="wtxidrelay"; } // namespace NetMsgType /** All known message types. Keep this in the same order as the list of @@ -71,6 +72,7 @@ const static std::string allNetMessageTypes[] = { NetMsgType::CMPCTBLOCK, NetMsgType::GETBLOCKTXN, NetMsgType::BLOCKTXN, + NetMsgType::WTXIDRELAY, }; const static std::vector allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes)); diff --git a/src/protocol.h b/src/protocol.h index 1d14585f97..a27ca56495 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -234,6 +234,12 @@ extern const char *GETBLOCKTXN; * @since protocol version 70014 as described by BIP 152 */ extern const char *BLOCKTXN; +/** + * Indicates that a node prefers to relay transactions via wtxid, rather than + * txid. + * @since protocol version 70016 as described by BIP 339. + */ +extern const char *WTXIDRELAY; }; /* Get a vector of all valid message types (see above) */ @@ -367,7 +373,7 @@ enum GetDataMsg : uint32_t { MSG_TX = 1, MSG_BLOCK = 2, MSG_WTX = 5, //!< Defined in BIP 339 - // The following can only occur in getdata. Invs always use TX or BLOCK. + // The following can only occur in getdata. Invs always use TX/WTX or BLOCK. MSG_FILTERED_BLOCK = 3, //!< Defined in BIP37 MSG_CMPCT_BLOCK = 4, //!< Defined in BIP152 MSG_WITNESS_BLOCK = MSG_BLOCK | MSG_WITNESS_FLAG, //!< Defined in BIP144 diff --git a/src/version.h b/src/version.h index d932b512d4..e55871fc41 100644 --- a/src/version.h +++ b/src/version.h @@ -9,7 +9,7 @@ * network protocol versioning */ -static const int PROTOCOL_VERSION = 70015; +static const int PROTOCOL_VERSION = 70016; //! initial proto version, to be increased after version/verack negotiation static const int INIT_PROTO_VERSION = 209; @@ -42,4 +42,7 @@ static const int SHORT_IDS_BLOCKS_VERSION = 70014; //! not banning for invalid compact blocks starts with this version static const int INVALID_CB_NO_BAN_VERSION = 70015; +//! "wtxidrelay" command for wtxid-based relay starts with this version +static const int WTXID_RELAY_VERSION = 70016; + #endif // BITCOIN_VERSION_H -- cgit v1.2.3