Repository: bitcoin/bitcoin
File: src/private_broadcast.h (C/C++)

Code before:
        std::optional<FinalState> final_state;
    };

    /**
     * Add a transaction to the storage.
     * @param[in] tx The transaction to add.
     * @retval true The transaction was added, or was previously stopped and is now scheduled again.
     * @retval false The transaction was already present and pending.
     */
    bool Add(const CTransactionRef& tx)
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);

    /**
     * Stop broadcasting a transaction and mark as received back from the network.
     * Updates a previously aborted transaction with the received_from address.
     *
     * @param[in] tx Transaction to stop broadcasting.
     * @param[in] received_from The peer address from which the transaction was received.
     * @retval !nullopt The number of times the transaction was sent and confirmed
     * by the recipient (if the transaction existed and broadcasting was stopped).
     * @retval nullopt The transaction was not in the storage, or was already stopped.
     */
    std::optional<size_t> StopBroadcasting(const CTransactionRef& tx, const CService& received_from)
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
    {
        return StopBroadcasting(tx, util::Expected<CService, std::string>{received_from});
    }

    /**
     * Stop broadcasting a transaction and mark as aborted.
     *
     * @param[in] tx Transaction to stop broadcasting.
     * @param[in] aborted Message describing why private broadcasting was aborted.
     * @retval !nullopt The number of times the transaction was sent and confirmed
     * by the recipient (if the transaction existed and broadcasting was stopped).
     * @retval nullopt The transaction was not in the storage, or was already stopped.
     */
    std::optional<size_t> StopBroadcasting(const CTransactionRef& tx, std::string aborted)
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
    {
        return StopBroadcasting(tx, util::Unexpected<std::string>{std::move(aborted)});
    }

    /**
     * Pick the transaction with the fewest send attempts, and confirmations,
     * and oldest send/confirm times.
     * @param[in] will_send_to_nodeid Will remember that the returned transaction
     * was picked for sending to this node.
     * @param[in] will_send_to_address Address of the peer to which this transaction
     * will be sent.
     * @return Most urgent transaction or nullopt if there are no transactions.

Review comment (refactor):
I find this usage of `util::Expected` unusual. Is not [`std::variant`](https://en.cppreference.com/w/cpp/utility/variant.html) more suitable for this?

Code after:
     */
    bool Add(const CTransactionRef& tx)
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);

    /**
     * Forget a transaction.
     * @param[in] tx Transaction to forget.
     * @retval !nullopt The number of times the transaction was sent and confirmed
     * by the recipient (if the transaction existed and was removed).
     * @retval nullopt The transaction was not in the storage.
     */
    std::optional<size_t> Remove(const CTransactionRef& tx)
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);

    /**
     * Pick the transaction with the fewest send attempts, and confirmations,
     * and oldest send/confirm times.
     * @param[in] will_send_to_nodeid Will remember that the returned transaction
     * was picked for sending to this node.
     * @param[in] will_send_to_address Address of the peer to which this transaction
     * will be sent.
     * @return Most urgent transaction or nullopt if there are no transactions.
     */
    std::optional<CTransactionRef> PickTxForSend(const NodeId& will_send_to_nodeid, const CService& will_send_to_address)
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);

    /**
     * Get the transaction that was picked for sending to a given node by PickTxForSend().
     * @param[in] nodeid Node to which a transaction is being (or was) sent.
     * @return Transaction or nullopt if the nodeid is unknown.
     */
    std::optional<CTransactionRef> GetTxForNode(const NodeId& nodeid)
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);

    /**
     * Mark that the node has confirmed reception of the transaction we sent it by
     * responding with `PONG` to our `PING` message.
     * @param[in] nodeid Node that we sent a transaction to.
     */
    void NodeConfirmedReception(const NodeId& nodeid)
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);

    /**
     * Check if the node has confirmed reception of the transaction.
     * @retval true Node has confirmed, `NodeConfirmedReception()` has been called.
     * @retval false Node has not confirmed, `NodeConfirmedReception()` has not been called.
     */
    bool DidNodeConfirmReception(const NodeId& nodeid)
        EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);

    /**