Skip to content

refactor: Pass TMTransaction by pointer in Overlay::relay - #8731

Open
FawwazLawal wants to merge 5 commits into
XRPLF:developfrom
FawwazLawal:fix/8607-relay-tmtransaction-no-raw-pointer
Open

FawwazLawal wants to merge 5 commits into
XRPLF:developfrom
FawwazLawal:fix/8607-relay-tmtransaction-no-raw-pointer

Conversation

@FawwazLawal

Copy link
Copy Markdown

High Level Overview of Change

Replace std::optional<std::reference_wrapper<protocol::TMTransaction>> with a raw protocol::TMTransaction const* on Overlay::relay(UInt256 const&, ...) and OverlayImpl::relayChecked. Callers pass nullptr where they previously passed std::nullopt or {}. The pointer type says "maybe a message, or just queue the hash" in one level instead of two, and removes the extra wrapper at every call site. The sibling Overlay::relay(STTx const&, protocol::TMTransaction const&, ...) overload keeps its signature; only its body now forwards &m or nullptr into relayChecked.

Fixes #8607.

Context of Change

Overlay::relay(UInt256 const&, std::optional<std::reference_wrapper<protocol::TMTransaction>>, std::set<Peer::ID> const&) has been on the Overlay interface since well before #8593 (git show 1c41d8d2c~1:src/xrpld/overlay/Overlay.h shows it at lines 184-188). #8593 (merged 2026-11-10, 1c41d8d2c0) added a sibling overload Overlay::relay(STTx const&, protocol::TMTransaction const&, ...) that takes the caller's already-parsed STTx so it is not parsed again (#8593 doc comment and title), and the private helper OverlayImpl::relayChecked. In review of that PR the hash-plus-optional parameter was flagged as awkward: an optional<reference_wrapper<TMTransaction>> is a two-level wrapper for "maybe a reference to a TMTransaction", which a TMTransaction const* expresses in one level with nullptr as the "no transaction" sentinel. #8607 names the pattern and the sites to update.

Lifetime stays the same as before: relayChecked only reads from the pointer within the same call (dereferences it once to build a std::make_shared<Message>(*tx, protocol::mtTRANSACTION)), it does not store it, and all call sites already own the referent for the duration of the call (msg is a local in RCLConsensus::Adaptor::share and in the test, nullptr on the "hash only" branch). OverlayImpl::relay's body still handles pseudo-transactions by reassigning the pointer to nullptr before the call falls through to relayChecked (the pointer analogue of the prior tx.reset() on the optional); relayChecked then takes the queue-only branch and queues the hash (OverlayImpl.cpp:1379, addTxQueue(hash)) for peers that negotiated tx reduce-relay; the announcement itself comes later in a TMHaveTransactions, which tx_reduce_relay_test.cpp:235-239 describes as "a pseudo-transaction, or an explicit nullptr as here. A queued hash is announced later in a TMHaveTransactions".

The sibling Overlay::relay(STTx const&, protocol::TMTransaction const&, ...) overload used by NetworkOPs (src/xrpld/app/misc/NetworkOPs.cpp:1887) and OpenLedger (src/xrpld/app/ledger/detail/OpenLedger.cpp:151) keeps its by-reference signature, since those callers already hold a valid message by reference; only its forwarding body is adjusted.

API Impact

  • Public API: New feature (new methods and/or new fields)
  • Public API: Breaking change (in general, breaking changes should only impact the next api_version)
  • libxrpl change (any change that may affect libxrpl or dependents of libxrpl)
  • Peer protocol change (must be backward compatible or bump the peer protocol version)

src/xrpld/overlay/Overlay.h is a src/xrpld/-private header, not an installed libxrpl header, so this is not a libxrpl change. The wire format is unchanged: the TMTransaction that relayChecked wraps in std::make_shared<Message>(*tx, protocol::mtTRANSACTION) is byte-identical to the prior path. No public JSON-RPC / WebSocket method or field is touched.

Before / After

Interface src/xrpld/overlay/Overlay.h:

// Before
virtual void
relay(
    UInt256 const& hash,
    std::optional<std::reference_wrapper<protocol::TMTransaction>> m,
    std::set<Peer::ID> const& toSkip) = 0;

// After
virtual void
relay(
    UInt256 const& hash,
    protocol::TMTransaction const* m,
    std::set<Peer::ID> const& toSkip) = 0;

Private helper src/xrpld/overlay/detail/OverlayImpl.h:

// Before
void
relayChecked(
    UInt256 const& hash,
    std::optional<std::reference_wrapper<protocol::TMTransaction const>> tx,
    std::set<Peer::ID> const& toSkip);

// After
void
relayChecked(
    UInt256 const& hash,
    protocol::TMTransaction const* tx,
    std::set<Peer::ID> const& toSkip);

Call-sites that change (all 5):

// src/xrpld/app/consensus/RCLConsensus.cpp:218
-app_.getOverlay().relay(tx.id(), msg, kSkip);
+app_.getOverlay().relay(tx.id(), &msg, kSkip);

// src/xrpld/overlay/detail/PeerImp.cpp:3092
-app_.getOverlay().relay(tx->getID(), {}, *toSkip);
+app_.getOverlay().relay(tx->getID(), nullptr, *toSkip);

// src/test/overlay/tx_reduce_relay_test.cpp:222
-env.app().getOverlay().relay(UInt256{0}, m, toSkip);
+env.app().getOverlay().relay(UInt256{0}, &m, toSkip);

// src/test/overlay/tx_reduce_relay_test.cpp:267
-env.app().getOverlay().relay(uint256{0}, std::nullopt, {});
+env.app().getOverlay().relay(uint256{0}, nullptr, {});

// src/test/overlay/tx_reduce_relay_test.cpp:327
-env.app().getOverlay().relay(UInt256{0}, m, {});
+env.app().getOverlay().relay(UInt256{0}, &m, {});

Inside OverlayImpl::relay, the pseudo-transaction reset becomes a null assignment:

// Before
if (tx)
{
    auto const& txn = tx->get();
    SerialIter sit(makeSlice(txn.rawtransaction()));
    try
    {
        if (isPseudoTx(STTx{sit}))
            tx.reset();
    }
    ...

// After
if (tx != nullptr)
{
    SerialIter sit(makeSlice(tx->rawtransaction()));
    try
    {
        if (isPseudoTx(STTx{sit}))
            tx = nullptr;
    }
    ...

The sibling Overlay::relay(STTx const&, TMTransaction const&, ...) overload forwards without the wrapper:

// Before
relayChecked(tx.getTransactionID(), std::nullopt, toSkip);
relayChecked(tx.getTransactionID(), m, toSkip);

// After
relayChecked(tx.getTransactionID(), nullptr, toSkip);
relayChecked(tx.getTransactionID(), &m, toSkip);

Three now-unused <functional> includes drop from src/xrpld/overlay/Overlay.h:22, src/xrpld/overlay/detail/OverlayImpl.h:47 and src/xrpld/overlay/detail/OverlayImpl.cpp:82: the only use in each of those files was the std::reference_wrapper in the old signature.

Test Plan

The change is representation-only. Correctness is covered by the existing xrpl.overlay unit suite, which exercises both the nullptr branch (hash-only relay, pseudo-transactions, and the tx_reduce_relay_test branch at line 267) and the &msg branch (normal relay in RCLConsensus::Adaptor::share and the tx_reduce_relay_test branches at lines 222 and 327).

Ran locally in the ghcr.io/xrplf/xrpld/nix-ubuntu:sha-d3a71b2 docker image (the tag .github/scripts/strategy-matrix/linux.json pins for the matching CI leg), Debug build with -Dxrpld=ON -Dtests=ON -Dwerr=ON -Dverify_headers=ON -Drust=ON:

$ cmake --build build/build/Debug --parallel -- xrpld
...
[100%] Linking CXX executable xrpld
[100%] Built target xrpld

$ ./xrpld --unittest=xrpl.overlay --unittest-jobs=9
...
8> xrpl.overlay.tx_reduce_relay had 0 failures.
Longest suite times:
    2.8s xrpl.overlay.tx_reduce_relay
    1.3s xrpl.overlay.TMGetLedger
    1.2s xrpl.overlay.TMGetObjectByHash
    1.1s xrpl.overlay.TMTransactions
    1.0s xrpl.overlay.OverlayLimit
4.4s, 14 suites, 74 cases, 2722 tests total, 0 failures

Exit 0.

Also ran, on the same image:

  • pre-commit run clang-format --all-files: PASS, no file diffs after the run.
  • run-clang-tidy -j18 -p . -quiet -allow-no-checks -extra-arg=-Wno-unknown-warning-option -extra-arg=-Wno-error -extra-arg=-Wno-deprecated-declarations on all 6 changed files: all 6 translation units completed, 0 Error-while-processing, 0 unique diagnostics via bin/filter-clang-tidy.py.
  • python3 .github/scripts/levelization/generate.py: EXIT=0, .github/scripts/levelization/results/loops.txt and ordering.txt byte-identical to the worktree copies (no committed-result drift).
  • The 8 rename scripts under .github/scripts/rename/ (binary.sh, cmake.sh, config.sh, copyright.sh, definitions.sh, docs.sh, include.sh, namespace.sh): all EXIT=0; diff -r --brief of the rename output against the worktree returns zero source-file differences.

git diff upstream/develop --shortstat: 6 files changed, 19 insertions(+), 26 deletions(-).

Replace std::optional<std::reference_wrapper<protocol::TMTransaction>>
with protocol::TMTransaction const* on Overlay::relay(UInt256, ...)
and OverlayImpl::relayChecked. Callers pass nullptr where they
previously passed std::nullopt or {}; dereferencing the pointer
replaces the prior tx->get() call on the reference wrapper. No
behaviour change: the two branches ("hash only" and "with message")
map one to one.

The sibling Overlay::relay(STTx, TMTransaction const&, ...) overload
keeps its signature; its body now forwards &m or nullptr into
relayChecked instead of wrapping m in an optional reference wrapper.

Drop three now-unused <functional> includes from Overlay.h,
OverlayImpl.h and OverlayImpl.cpp.

Fixes XRPLF#8607.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the transaction-relay pointer refactor.

No open issues 🎉

What changed in this PR

Simplifies transaction relay in xrpld’s internal overlay by replacing optional reference wrappers with nullable TMTransaction const* pointers, addressing #8607. Relay behavior and wire format remain unchanged.

Changes:

  • Updates the hash-based relay interface and relayChecked helper to accept pointers.
  • Converts callers and forwarding logic to use message addresses or nullptr, preserving the parsed-transaction overload’s signature.
  • Adjusts existing tests and explanatory comments, and removes unused <functional> includes.
File Description
src/​xrpld/​overlay/​Overlay.h Changes the hash-based relay interface to a nullable const pointer.
src/​xrpld/​overlay/​detail/​PeerImp.cpp Passes nullptr for hash-only pseudo-transaction relay.
src/​xrpld/​overlay/​detail/​OverlayImpl.h Aligns relay and helper declarations with the pointer interface.
src/​xrpld/​overlay/​detail/​OverlayImpl.cpp Uses pointer checks, forwarding, and dereferencing while preserving relay logic.
src/​xrpld/​app/​consensus/​RCLConsensus.cpp Passes the local transaction message by address.
src/​test/​overlay/​tx_reduce_relay_test.cpp Updates relay calls and the hash-only test comment for pointer semantics.

Review effort: Balanced

@nbougalis nbougalis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! 👍

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use a pointer instead of std::optional<std::reference_wrapper<TMTransaction>> in Overlay::relay

3 participants