Move OrderBookDB to libxrpl

Signed-off-by: JCW <a1q123456@users.noreply.github.com>
This commit is contained in:
JCW
2026-01-13 11:51:47 +00:00
parent a95f58187b
commit 1316293878
22 changed files with 278 additions and 139 deletions

View File

@@ -234,6 +234,10 @@ public:
virtual std::optional<uint256> const&
trapTxID() const = 0;
/** Retrieve the "wallet database" */
virtual DatabaseCon&
getWalletDB() = 0;
// Temporary: Get the underlying Application for functions that haven't
// been migrated yet. This should be removed once all code is migrated.
virtual Application&

View File

@@ -0,0 +1,90 @@
#ifndef XRPL_LEDGER_ACCEPTEDLEDGERTX_H_INCLUDED
#define XRPL_LEDGER_ACCEPTEDLEDGERTX_H_INCLUDED
#include <xrpl/basics/CountedObject.h>
#include <xrpl/ledger/ReadView.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/STTx.h>
#include <xrpl/protocol/TxMeta.h>
#include <boost/container/flat_set.hpp>
namespace xrpl {
/**
A transaction that is in a closed ledger.
Description
An accepted ledger transaction contains additional information that the
server needs to tell clients about the transaction. For example,
- The transaction in JSON form
- Which accounts are affected
* This is used by InfoSub to report to clients
- Cached stuff
*/
class AcceptedLedgerTx : public CountedObject<AcceptedLedgerTx>
{
public:
AcceptedLedgerTx(
std::shared_ptr<ReadView const> const& ledger,
std::shared_ptr<STTx const> const&,
std::shared_ptr<STObject const> const&);
std::shared_ptr<STTx const> const&
getTxn() const
{
return mTxn;
}
TxMeta const&
getMeta() const
{
return mMeta;
}
boost::container::flat_set<AccountID> const&
getAffected() const
{
return mAffected;
}
TxID
getTransactionID() const
{
return mTxn->getTransactionID();
}
TxType
getTxnType() const
{
return mTxn->getTxnType();
}
TER
getResult() const
{
return mMeta.getResultTER();
}
std::uint32_t
getTxnSeq() const
{
return mMeta.getIndex();
}
std::string
getEscMeta() const;
Json::Value const&
getJson() const
{
return mJson;
}
private:
std::shared_ptr<STTx const> mTxn;
TxMeta mMeta;
boost::container::flat_set<AccountID> mAffected;
Blob mRawMeta;
Json::Value mJson;
};
} // namespace xrpl
#endif

View File

@@ -0,0 +1,55 @@
#ifndef XRPL_LEDGER_BOOKLISTENERS_H_INCLUDED
#define XRPL_LEDGER_BOOKLISTENERS_H_INCLUDED
#include <xrpl/basics/UnorderedContainers.h>
#include <xrpl/protocol/MultiApiJson.h>
#include <xrpl/server/InfoSub.h>
#include <memory>
#include <mutex>
namespace xrpl {
/** Listen to public/subscribe messages from a book. */
class BookListeners
{
public:
using pointer = std::shared_ptr<BookListeners>;
BookListeners()
{
}
/** Add a new subscription for this book
*/
void
addSubscriber(InfoSub::ref sub);
/** Stop publishing to a subscriber
*/
void
removeSubscriber(std::uint64_t sub);
/** Publish a transaction to subscribers
Publish a transaction to clients subscribed to changes on this book.
Uses havePublished to prevent sending duplicate transactions to clients
that have subscribed to multiple books.
@param jvObj JSON transaction data to publish
@param havePublished InfoSub sequence numbers that have already
published this transaction.
*/
void
publish(MultiApiJson const& jvObj, hash_set<std::uint64_t>& havePublished);
private:
std::recursive_mutex mLock;
hash_map<std::uint64_t, InfoSub::wptr> mListeners;
};
} // namespace xrpl
#endif

View File

@@ -0,0 +1,102 @@
#ifndef XRPL_LEDGER_ORDERBOOKDB_H_INCLUDED
#define XRPL_LEDGER_ORDERBOOKDB_H_INCLUDED
#include <xrpl/ledger/AcceptedLedgerTx.h>
#include <xrpl/ledger/BookListeners.h>
#include <xrpl/ledger/ReadView.h>
#include <xrpl/protocol/Book.h>
#include <xrpl/protocol/Issue.h>
#include <xrpl/protocol/MultiApiJson.h>
#include <xrpl/protocol/UintTypes.h>
#include <memory>
#include <optional>
#include <vector>
namespace xrpl {
/** Tracks order books in the ledger.
This interface provides access to order book information, including:
- Which order books exist in the ledger
- Querying order books by issue
- Managing order book subscriptions
The order book database is updated as ledgers are accepted and provides
efficient lookup of order book information for pathfinding and client
subscriptions.
*/
class OrderBookDB
{
public:
virtual ~OrderBookDB() = default;
/** Initialize or update the order book database with a new ledger.
This method should be called when a new ledger is accepted to update
the order book database with the current state of all order books.
@param ledger The ledger to scan for order books
*/
virtual void
setup(std::shared_ptr<ReadView const> const& ledger) = 0;
/** Add an order book to track.
@param book The order book to add
*/
virtual void
addOrderBook(Book const& book) = 0;
/** Get all order books that want a specific issue.
Returns a list of all order books where the taker pays the specified
issue. This is useful for pathfinding to find all possible next hops
from a given currency.
@param issue The issue to search for
@param domain Optional domain restriction for the order book
@return Vector of books that want this issue
*/
virtual std::vector<Book>
getBooksByTakerPays(
Issue const& issue,
std::optional<Domain> const& domain = std::nullopt) = 0;
/** Get the count of order books that want a specific issue.
@param issue The issue to search for
@param domain Optional domain restriction for the order book
@return Number of books that want this issue
*/
virtual int
getBookSize(
Issue const& issue,
std::optional<Domain> const& domain = std::nullopt) = 0;
/** Check if an order book to XRP exists for the given issue.
@param issue The issue to check
@param domain Optional domain restriction for the order book
@return true if a book from this issue to XRP exists
*/
virtual bool
isBookToXRP(
Issue const& issue,
std::optional<Domain> domain = std::nullopt) = 0;
virtual void
processTxn(
std::shared_ptr<ReadView const> const& ledger,
AcceptedLedgerTx const& alTx,
MultiApiJson const& jvObj) = 0;
virtual BookListeners::pointer
getBookListeners(Book const&) = 0;
virtual BookListeners::pointer
makeBookListeners(Book const&) = 0;
};
} // namespace xrpl
#endif