mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-04 11:55:51 +00:00
@@ -37,9 +37,13 @@ public:
|
||||
// response of stream "ledger"
|
||||
// TODO: use better type than json, this type will be used in the stream as well
|
||||
std::optional<boost::json::object> ledger;
|
||||
// books returns nothing by default, if snapshot is true, it returns offers
|
||||
// books returns nothing by default, if snapshot is true and both is false, offers go to offers list
|
||||
// TODO: use better type than json
|
||||
std::optional<boost::json::array> offers;
|
||||
// if snapshot is true and both is true, reversed book' offers go to asks list
|
||||
std::optional<boost::json::array> asks;
|
||||
// if snapshot is true and both is true, original book' offers go to bids list
|
||||
std::optional<boost::json::array> bids;
|
||||
};
|
||||
|
||||
struct OrderBook
|
||||
@@ -128,9 +132,7 @@ public:
|
||||
|
||||
if (input.books)
|
||||
{
|
||||
auto const offers = subscribeToBooks(*(input.books), ctx.session, ctx.yield);
|
||||
if (!offers.empty())
|
||||
output.offers = offers;
|
||||
subscribeToBooks(*(input.books), ctx.session, ctx.yield, output);
|
||||
};
|
||||
|
||||
return output;
|
||||
@@ -188,15 +190,15 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
boost::json::array
|
||||
void
|
||||
subscribeToBooks(
|
||||
std::vector<OrderBook> const& books,
|
||||
std::shared_ptr<Server::ConnectionBase> const& session,
|
||||
boost::asio::yield_context& yield) const
|
||||
boost::asio::yield_context& yield,
|
||||
Output& output) const
|
||||
{
|
||||
static auto constexpr fetchLimit = 200;
|
||||
|
||||
boost::json::array snapshots;
|
||||
std::optional<Backend::LedgerRange> rng;
|
||||
|
||||
for (auto const& internalBook : books)
|
||||
@@ -206,7 +208,7 @@ private:
|
||||
if (!rng)
|
||||
rng = sharedPtrBackend_->fetchLedgerRange();
|
||||
|
||||
auto const getOrderBook = [&](auto const& book) {
|
||||
auto const getOrderBook = [&](auto const& book, auto& snapshots) {
|
||||
auto const bookBase = getBookBase(book);
|
||||
auto const [offers, _] =
|
||||
sharedPtrBackend_->fetchBookOffers(bookBase, rng->maxSequence, fetchLimit, yield);
|
||||
@@ -221,10 +223,21 @@ private:
|
||||
std::copy(orderBook.begin(), orderBook.end(), std::back_inserter(snapshots));
|
||||
};
|
||||
|
||||
getOrderBook(internalBook.book);
|
||||
|
||||
if (internalBook.both)
|
||||
getOrderBook(ripple::reversed(internalBook.book));
|
||||
{
|
||||
if (!output.bids)
|
||||
output.bids = boost::json::array();
|
||||
if (!output.asks)
|
||||
output.asks = boost::json::array();
|
||||
getOrderBook(internalBook.book, *(output.bids));
|
||||
getOrderBook(ripple::reversed(internalBook.book), *(output.asks));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!output.offers)
|
||||
output.offers = boost::json::array();
|
||||
getOrderBook(internalBook.book, *(output.offers));
|
||||
}
|
||||
}
|
||||
|
||||
subscriptions_->subBook(internalBook.book, session);
|
||||
@@ -232,8 +245,6 @@ private:
|
||||
if (internalBook.both)
|
||||
subscriptions_->subBook(ripple::reversed(internalBook.book), session);
|
||||
}
|
||||
|
||||
return snapshots;
|
||||
}
|
||||
|
||||
friend void
|
||||
@@ -243,6 +254,10 @@ private:
|
||||
|
||||
if (output.offers)
|
||||
jv.as_object().emplace(JS(offers), *(output.offers));
|
||||
if (output.asks)
|
||||
jv.as_object().emplace(JS(asks), *(output.asks));
|
||||
if (output.bids)
|
||||
jv.as_object().emplace(JS(bids), *(output.bids));
|
||||
}
|
||||
|
||||
friend Input
|
||||
|
||||
@@ -878,12 +878,151 @@ TEST_F(RPCSubscribeHandlerTest, BooksBothSnapshotSet)
|
||||
auto const handler = AnyHandler{SubscribeHandler{mockBackendPtr, subManager_}};
|
||||
auto const output = handler.process(input, Context{std::ref(yield), session_});
|
||||
ASSERT_TRUE(output);
|
||||
EXPECT_EQ(output->as_object().at("offers").as_array().size(), 20);
|
||||
EXPECT_EQ(output->as_object().at("offers").as_array()[0].as_object(), json::parse(expectedOffer));
|
||||
EXPECT_EQ(output->as_object().at("offers").as_array()[10].as_object(), json::parse(expectedReversedOffer));
|
||||
EXPECT_EQ(output->as_object().at("bids").as_array().size(), 10);
|
||||
EXPECT_EQ(output->as_object().at("asks").as_array().size(), 10);
|
||||
EXPECT_EQ(output->as_object().at("bids").as_array()[0].as_object(), json::parse(expectedOffer));
|
||||
EXPECT_EQ(output->as_object().at("asks").as_array()[0].as_object(), json::parse(expectedReversedOffer));
|
||||
std::this_thread::sleep_for(20ms);
|
||||
auto const report = subManager_->report();
|
||||
// original book + reverse book
|
||||
EXPECT_EQ(report.at("books").as_uint64(), 2);
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(RPCSubscribeHandlerTest, BooksBothUnsetSnapshotSet)
|
||||
{
|
||||
auto const input = json::parse(fmt::format(
|
||||
R"({{
|
||||
"books":
|
||||
[
|
||||
{{
|
||||
"taker_gets":
|
||||
{{
|
||||
"currency": "XRP"
|
||||
}},
|
||||
"taker_pays":
|
||||
{{
|
||||
"currency": "USD",
|
||||
"issuer": "{}"
|
||||
}},
|
||||
"snapshot": true
|
||||
}}
|
||||
]
|
||||
}})",
|
||||
ACCOUNT));
|
||||
mockBackendPtr->updateRange(MINSEQ);
|
||||
mockBackendPtr->updateRange(MAXSEQ);
|
||||
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
|
||||
auto const issuer = GetAccountIDWithString(ACCOUNT);
|
||||
|
||||
auto const getsXRPPaysUSDBook = getBookBase(std::get<ripple::Book>(
|
||||
RPC::parseBook(ripple::to_currency("USD"), issuer, ripple::xrpCurrency(), ripple::xrpAccount())));
|
||||
|
||||
auto const reversedBook = getBookBase(std::get<ripple::Book>(
|
||||
RPC::parseBook(ripple::xrpCurrency(), ripple::xrpAccount(), ripple::to_currency("USD"), issuer)));
|
||||
|
||||
ON_CALL(*rawBackendPtr, doFetchSuccessorKey(getsXRPPaysUSDBook, MAXSEQ, _))
|
||||
.WillByDefault(Return(ripple::uint256{PAYS20USDGETS10XRPBOOKDIR}));
|
||||
|
||||
ON_CALL(*rawBackendPtr, doFetchSuccessorKey(ripple::uint256{PAYS20USDGETS10XRPBOOKDIR}, MAXSEQ, _))
|
||||
.WillByDefault(Return(std::nullopt));
|
||||
|
||||
ON_CALL(*rawBackendPtr, doFetchSuccessorKey(reversedBook, MAXSEQ, _))
|
||||
.WillByDefault(Return(ripple::uint256{PAYS20XRPGETS10USDBOOKDIR}));
|
||||
|
||||
EXPECT_CALL(*rawBackendPtr, doFetchSuccessorKey).Times(2);
|
||||
|
||||
EXPECT_CALL(*rawBackendPtr, doFetchLedgerObject).Times(5);
|
||||
|
||||
auto const indexes = std::vector<ripple::uint256>(10, ripple::uint256{INDEX2});
|
||||
ON_CALL(*rawBackendPtr, doFetchLedgerObject(ripple::uint256{PAYS20USDGETS10XRPBOOKDIR}, MAXSEQ, _))
|
||||
.WillByDefault(Return(CreateOwnerDirLedgerObject(indexes, INDEX1).getSerializer().peekData()));
|
||||
|
||||
// for reverse
|
||||
auto const indexes2 = std::vector<ripple::uint256>(10, ripple::uint256{INDEX1});
|
||||
ON_CALL(*rawBackendPtr, doFetchLedgerObject(ripple::uint256{PAYS20XRPGETS10USDBOOKDIR}, MAXSEQ, _))
|
||||
.WillByDefault(Return(CreateOwnerDirLedgerObject(indexes2, INDEX2).getSerializer().peekData()));
|
||||
|
||||
// offer owner account root
|
||||
ON_CALL(
|
||||
*rawBackendPtr, doFetchLedgerObject(ripple::keylet::account(GetAccountIDWithString(ACCOUNT2)).key, MAXSEQ, _))
|
||||
.WillByDefault(Return(CreateAccountRootObject(ACCOUNT2, 0, 2, 200, 2, INDEX1, 2).getSerializer().peekData()));
|
||||
|
||||
// issuer account root
|
||||
ON_CALL(
|
||||
*rawBackendPtr, doFetchLedgerObject(ripple::keylet::account(GetAccountIDWithString(ACCOUNT)).key, MAXSEQ, _))
|
||||
.WillByDefault(Return(CreateAccountRootObject(ACCOUNT, 0, 2, 200, 2, INDEX1, 2).getSerializer().peekData()));
|
||||
|
||||
// fee
|
||||
auto feeBlob = CreateFeeSettingBlob(1, 2, 3, 4, 0);
|
||||
ON_CALL(*rawBackendPtr, doFetchLedgerObject(ripple::keylet::fees().key, MAXSEQ, _)).WillByDefault(Return(feeBlob));
|
||||
|
||||
auto const gets10XRPPays20USDOffer = CreateOfferLedgerObject(
|
||||
ACCOUNT2,
|
||||
10,
|
||||
20,
|
||||
ripple::to_string(ripple::xrpCurrency()),
|
||||
ripple::to_string(ripple::to_currency("USD")),
|
||||
toBase58(ripple::xrpAccount()),
|
||||
ACCOUNT,
|
||||
PAYS20USDGETS10XRPBOOKDIR);
|
||||
|
||||
// for reverse
|
||||
// offer owner is USD issuer
|
||||
auto const gets10USDPays20XRPOffer = CreateOfferLedgerObject(
|
||||
ACCOUNT,
|
||||
10,
|
||||
20,
|
||||
ripple::to_string(ripple::to_currency("USD")),
|
||||
ripple::to_string(ripple::xrpCurrency()),
|
||||
ACCOUNT,
|
||||
toBase58(ripple::xrpAccount()),
|
||||
PAYS20XRPGETS10USDBOOKDIR);
|
||||
|
||||
std::vector<Blob> bbs(10, gets10XRPPays20USDOffer.getSerializer().peekData());
|
||||
ON_CALL(*rawBackendPtr, doFetchLedgerObjects(indexes, MAXSEQ, _)).WillByDefault(Return(bbs));
|
||||
|
||||
// for reverse
|
||||
std::vector<Blob> bbs2(10, gets10USDPays20XRPOffer.getSerializer().peekData());
|
||||
ON_CALL(*rawBackendPtr, doFetchLedgerObjects(indexes2, MAXSEQ, _)).WillByDefault(Return(bbs2));
|
||||
|
||||
EXPECT_CALL(*rawBackendPtr, doFetchLedgerObjects).Times(1);
|
||||
|
||||
static auto const expectedOffer = fmt::format(
|
||||
R"({{
|
||||
"Account":"{}",
|
||||
"BookDirectory":"{}",
|
||||
"BookNode":"0",
|
||||
"Flags":0,
|
||||
"LedgerEntryType":"Offer",
|
||||
"OwnerNode":"0",
|
||||
"PreviousTxnID":"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
"PreviousTxnLgrSeq":0,
|
||||
"Sequence":0,
|
||||
"TakerGets":"10",
|
||||
"TakerPays":
|
||||
{{
|
||||
"currency":"USD",
|
||||
"issuer":"{}",
|
||||
"value":"20"
|
||||
}},
|
||||
"index":"E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC321",
|
||||
"owner_funds":"193",
|
||||
"quality":"2"
|
||||
}})",
|
||||
ACCOUNT2,
|
||||
PAYS20USDGETS10XRPBOOKDIR,
|
||||
ACCOUNT);
|
||||
|
||||
runSpawn([&, this](auto& yield) {
|
||||
auto const handler = AnyHandler{SubscribeHandler{mockBackendPtr, subManager_}};
|
||||
auto const output = handler.process(input, Context{std::ref(yield), session_});
|
||||
ASSERT_TRUE(output);
|
||||
EXPECT_EQ(output->as_object().at("offers").as_array().size(), 10);
|
||||
EXPECT_EQ(output->as_object().at("offers").as_array()[0].as_object(), json::parse(expectedOffer));
|
||||
std::this_thread::sleep_for(20ms);
|
||||
auto const report = subManager_->report();
|
||||
// original book + reverse book
|
||||
EXPECT_EQ(report.at("books").as_uint64(), 1);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user