1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/WSClient.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/domain.h>
6#include <test/jtx/offer.h>
7#include <test/jtx/paths.h>
8#include <test/jtx/pay.h>
9#include <test/jtx/permissioned_dex.h>
10#include <test/jtx/sendmax.h>
12#include <xrpld/rpc/BookChanges.h>
14#include <xrpl/basics/base_uint.h>
15#include <xrpl/basics/chrono.h>
16#include <xrpl/basics/contract.h>
17#include <xrpl/beast/hash/uhash.h>
18#include <xrpl/beast/unit_test/suite.h>
19#include <xrpl/json/json_value.h>
20#include <xrpl/json/to_string.h>
21#include <xrpl/ledger/Ledger.h>
22#include <xrpl/protocol/ErrorCodes.h>
23#include <xrpl/protocol/Feature.h>
24#include <xrpl/protocol/LedgerFormats.h>
25#include <xrpl/protocol/SField.h>
26#include <xrpl/protocol/STArray.h>
27#include <xrpl/protocol/STObject.h>
28#include <xrpl/protocol/STTx.h>
29#include <xrpl/protocol/Serializer.h>
30#include <xrpl/protocol/TxFormats.h>
31#include <xrpl/protocol/jss.h>
50 testcase(
"Specify well-known strings as ledger input");
56 params[
"ledger"] =
"validated";
57 resp = env.
rpc(
"json",
"book_changes",
to_string(params));
58 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
59 BEAST_EXPECT(resp[jss::result][jss::status] ==
"success");
60 BEAST_EXPECT(resp[jss::result][jss::validated] ==
true);
62 params[
"ledger"] =
"current";
63 resp = env.
rpc(
"json",
"book_changes",
to_string(params));
64 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
65 BEAST_EXPECT(resp[jss::result][jss::status] ==
"success");
66 BEAST_EXPECT(resp[jss::result][jss::validated] ==
false);
68 params[
"ledger"] =
"closed";
69 resp = env.
rpc(
"json",
"book_changes",
to_string(params));
70 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
71 BEAST_EXPECT(resp[jss::result][jss::status] ==
"success");
76 BEAST_EXPECT(resp[jss::result][jss::validated] ==
true);
79 params[
"ledger"] =
"non_conventional_ledger_input";
80 resp = env.
rpc(
"json",
"book_changes",
to_string(params));
81 BEAST_EXPECT(resp[jss::result][jss::status] ==
"error");
82 BEAST_EXPECT(resp[jss::result][jss::error] ==
"invalidParams");
85 resp[jss::result][jss::error_message] ==
86 "Invalid field 'ledger', not string or number.");
93 "If ledger_hash or ledger_index is not specified, the behavior "
94 "must default to the `current` ledger");
100 BEAST_EXPECT(!resp[jss::result].isMember(jss::error));
101 BEAST_EXPECT(resp[jss::result][jss::status] ==
"success");
116 featurePermissionedDEX,
121 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] = permDex;
136 auto const ledgerIndex = txResult[jss::ledger_index].
asInt();
139 jvParams[jss::ledger_index] = ledgerIndex;
141 auto jv = wsc->invoke(
"book_changes", jvParams);
142 auto jrr = jv[jss::result];
144 BEAST_EXPECT(jrr[jss::changes].size() == 1);
145 BEAST_EXPECT(jrr[jss::changes][0u][jss::domain].asString() ==
to_string(domainID));
151 testcase(
"book_changes skips overflowing rate");
158 auto const big =
MPT{gw.
id(), 1};
159 auto const usd = iouGw[
"USD"];
168 finalFields.
setFieldAmount(sfTakerGets, big(1'800'000'000'000'000'000ull));
173 previousFields.
setFieldAmount(sfTakerGets, big(3'600'000'000'000'000'000ull));
176 STObject modifiedOffer{sfModifiedNode};
177 modifiedOffer.
setFieldU16(sfLedgerEntryType, ltOFFER);
181 STArray affectedNodes{sfAffectedNodes};
182 affectedNodes.
pushBack(std::move(modifiedOffer));
185 metadata->setFieldArray(sfAffectedNodes, affectedNodes);
197 tx->add(*txSerializer);
200 metadata->add(*metaSerializer);
202 ledger->rawTxInsert(
UInt256{1}, txSerializer, metaSerializer);
203 BEAST_EXPECT(ledger->setImmutable());
204 ledger->setValidated();
210 BEAST_EXPECT(result[jss::type] ==
"bookChanges");
211 BEAST_EXPECT(result[jss::changes].size() == expectedChanges);
215 fail(
"Overflowing book-change rate shouldn't throw");
226 for (
auto const& features : {
231 test(features, tiny, 0);
232 test(features, nine, 1);
250 for (
auto const& [gets, pays] : crossings)
264 STObject modifiedOffer{sfModifiedNode};
265 modifiedOffer.
setFieldU16(sfLedgerEntryType, ltOFFER);
269 STArray affectedNodes{sfAffectedNodes};
270 affectedNodes.
pushBack(std::move(modifiedOffer));
273 metadata->setFieldArray(sfAffectedNodes, affectedNodes);
278 tx.
add(*txSerializer);
281 metadata->add(*metaSerializer);
283 ledger->rawTxInsert(
UInt256{seq}, txSerializer, metaSerializer);
288 if (!ledger->setImmutable())
292 ledger->setValidated();
300 testcase(
"book_changes skips overflowing volume");
316 auto const half = 5'000'000'000'000'000'000ull;
319 bookChangesFor(env, {{mptA(half), mptB(half)}, {mptA(half), mptB(half)}});
321 BEAST_EXPECT(result[jss::type] ==
"bookChanges");
322 if (BEAST_EXPECT(result[jss::changes].size() == 1))
324 auto const& change = result[jss::changes][0u];
327 BEAST_EXPECT(change[jss::volume_a].asString() ==
std::to_string(half));
328 BEAST_EXPECT(change[jss::volume_b].asString() ==
std::to_string(half));
340 STAmount const bigA{gwA[
"USD"].issue(), UINT64_C(9'000'000'000'000'000), 80};
341 STAmount const bigB{gwB[
"EUR"].issue(), UINT64_C(9'000'000'000'000'000), 80};
345 auto const result =
bookChangesFor(env, {{bigA, bigB}, {bigA, bigB}});
346 BEAST_EXPECT(result[jss::type] ==
"bookChanges");
347 BEAST_EXPECT(result[jss::changes].size() == 1);
351 fail(
"Overflowing book-change volume shouldn't throw");
void fail(String const &reason, char const *file, int line)
Record a failure.
TestcaseT testcase
Memberspace for declaring test cases.
virtual Config & config()=0
std::chrono::time_point< NetClock > time_point
Rules controlling protocol behavior.
static constexpr int kMinOffset
static constexpr std::uint64_t kMinValue
void pushBack(STObject const &object)
void setFieldU32(SField const &field, std::uint32_t)
void setFieldAmount(SField const &field, STAmount const &)
void add(Serializer &s) const override
void setFieldU16(SField const &field, std::uint16_t)
static STObject makeInnerObject(SField const &name)
void setFieldObject(SField const &field, STObject const &v)
virtual Family & getNodeFamily()=0
static json::Value bookChangesFor(jtx::Env &env, std::vector< std::pair< STAmount, STAmount > > const &crossings)
void testSkipsOverflowingVolume()
void run() override
Runs the suite.
void testSkipsOverflowingRate()
void testLedgerInputDefaultBehavior()
void testConventionalLedgerInputStrings()
Immutable cryptographic account descriptor.
AccountID id() const
Returns the Account ID.
A transaction testing environment.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
std::shared_ptr< STTx const > tx() const
Return the tx data for the last JTx.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Converts to MPT Issue or STAmount.
Sets the SendMax on a JTx.
The api_version numbers this server serves.
json::Value computeBookChanges(std::shared_ptr< L const > const &lpAccepted)
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
XrpT const XRP
Converts to XRP Issue or STAmount.
FeatureBitset testableAmendments()
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpcVersion, std::unordered_map< std::string, std::string > const &headers)
Returns a client operating through WebSockets/S.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
BaseUInt< 256 > Domain
Domain is a 256-bit hash representing a specific domain.
std::string to_string(BaseUInt< Bits, Tag > const &a)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
T static_pointer_cast(T... args)