diff --git a/include/xrpl/protocol/Feature.h b/include/xrpl/protocol/Feature.h index 369ec3304e..bff3e57597 100644 --- a/include/xrpl/protocol/Feature.h +++ b/include/xrpl/protocol/Feature.h @@ -80,7 +80,7 @@ namespace detail { // Feature.cpp. Because it's only used to reserve storage, and determine how // large to make the FeatureBitset, it MAY be larger. It MUST NOT be less than // the actual number of amendments. A LogicError on startup will verify this. -static constexpr std::size_t numFeatures = 85; +static constexpr std::size_t numFeatures = 86; /** Amendments that this server supports and the default voting behavior. Whether they are enabled depends on the Rules defined in the validated diff --git a/include/xrpl/protocol/LedgerFormats.h b/include/xrpl/protocol/LedgerFormats.h index 502d66bde6..5f3cca53ac 100644 --- a/include/xrpl/protocol/LedgerFormats.h +++ b/include/xrpl/protocol/LedgerFormats.h @@ -160,10 +160,12 @@ enum LedgerSpecificFlags { lsfHighAuth = 0x00080000, lsfLowNoRipple = 0x00100000, lsfHighNoRipple = 0x00200000, - lsfLowFreeze = 0x00400000, // True, low side has set freeze flag - lsfHighFreeze = 0x00800000, // True, high side has set freeze flag - lsfAMMNode = 0x01000000, // True, trust line to AMM. Used by client - // apps to identify payments via AMM. + lsfLowFreeze = 0x00400000, // True, low side has set freeze flag + lsfHighFreeze = 0x00800000, // True, high side has set freeze flag + lsfLowDeepFreeze = 0x02000000, // True, low side has set deep freeze flag + lsfHighDeepFreeze = 0x04000000, // True, high side has set deep freeze flag + lsfAMMNode = 0x01000000, // True, trust line to AMM. Used by client + // apps to identify payments via AMM. // ltSIGNER_LIST lsfOneOwnerCount = 0x00010000, // True, uses only one OwnerCount diff --git a/include/xrpl/protocol/TxFlags.h b/include/xrpl/protocol/TxFlags.h index 8d6ff09b76..f0f6c7f223 100644 --- a/include/xrpl/protocol/TxFlags.h +++ b/include/xrpl/protocol/TxFlags.h @@ -114,9 +114,11 @@ constexpr std::uint32_t tfSetNoRipple = 0x00020000; constexpr std::uint32_t tfClearNoRipple = 0x00040000; constexpr std::uint32_t tfSetFreeze = 0x00100000; constexpr std::uint32_t tfClearFreeze = 0x00200000; +constexpr std::uint32_t tfSetDeepFreeze = 0x00400000; +constexpr std::uint32_t tfClearDeepFreeze = 0x00800000; constexpr std::uint32_t tfTrustSetMask = ~(tfUniversal | tfSetfAuth | tfSetNoRipple | tfClearNoRipple | tfSetFreeze | - tfClearFreeze); + tfClearFreeze | tfSetDeepFreeze | tfClearDeepFreeze); // EnableAmendment flags: constexpr std::uint32_t tfGotMajority = 0x00010000; diff --git a/include/xrpl/protocol/detail/features.macro b/include/xrpl/protocol/detail/features.macro index d90dc32778..322670c517 100644 --- a/include/xrpl/protocol/detail/features.macro +++ b/include/xrpl/protocol/detail/features.macro @@ -29,6 +29,7 @@ // If you add an amendment here, then do not forget to increment `numFeatures` // in include/xrpl/protocol/Feature.h. +XRPL_FEATURE(DeepFreeze, Supported::yes, VoteBehavior::DefaultNo) XRPL_FEATURE(PermissionedDomains, Supported::no, VoteBehavior::DefaultNo) XRPL_FEATURE(DynamicNFT, Supported::yes, VoteBehavior::DefaultNo) XRPL_FEATURE(Credentials, Supported::yes, VoteBehavior::DefaultNo) @@ -116,3 +117,4 @@ XRPL_FIX (NFTokenNegOffer, Supported::yes, VoteBehavior::Obsolete) XRPL_FIX (NFTokenDirV1, Supported::yes, VoteBehavior::Obsolete) XRPL_FEATURE(NonFungibleTokensV1, Supported::yes, VoteBehavior::Obsolete) XRPL_FEATURE(CryptoConditionsSuite, Supported::yes, VoteBehavior::Obsolete) + diff --git a/include/xrpl/protocol/jss.h b/include/xrpl/protocol/jss.h index f21da5817d..4db8e0e32d 100644 --- a/include/xrpl/protocol/jss.h +++ b/include/xrpl/protocol/jss.h @@ -284,6 +284,8 @@ JSS(flags); // out: AccountOffers, JSS(forward); // in: AccountTx JSS(freeze); // out: AccountLines JSS(freeze_peer); // out: AccountLines +JSS(deep_freeze); // out: AccountLines +JSS(deep_freeze_peer); // out: AccountLines JSS(frozen_balances); // out: GatewayBalances JSS(full); // in: LedgerClearer, handlers/Ledger JSS(full_reply); // out: PathFind diff --git a/src/test/app/Freeze_test.cpp b/src/test/app/Freeze_test.cpp index 0c54f0e1f3..99696c11f6 100644 --- a/src/test/app/Freeze_test.cpp +++ b/src/test/app/Freeze_test.cpp @@ -17,6 +17,7 @@ */ //============================================================================== #include +#include #include #include #include @@ -186,6 +187,193 @@ class Freeze_test : public beast::unit_test::suite } } + void + testDeepFreeze(FeatureBitset features) + { + testcase("Deep Freeze"); + + using namespace test::jtx; + Env env(*this, features); + + Account G1{"G1"}; + Account A1{"A1"}; + + env.fund(XRP(10000), G1, A1); + env.close(); + + env.trust(G1["USD"](1000), A1); + env.close(); + + if (features[featureDeepFreeze]) + { + // test: Issuer deep freezing the trust line in a single + // transaction + env(trust(G1, A1["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + { + auto const flags = getTrustlineFlags(env, 2u, 1u); + BEAST_EXPECT(flags & lsfLowFreeze); + BEAST_EXPECT(flags & lsfLowDeepFreeze); + BEAST_EXPECT(!(flags & (lsfHighFreeze | lsfHighDeepFreeze))); + env.close(); + } + + // test: Issuer clearing deep freeze and normal freeze in a single + // transaction + env(trust(G1, A1["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + { + auto const flags = getTrustlineFlags(env, 2u, 1u); + BEAST_EXPECT(!(flags & (lsfLowFreeze | lsfLowDeepFreeze))); + BEAST_EXPECT(!(flags & (lsfHighFreeze | lsfHighDeepFreeze))); + env.close(); + } + + // test: Issuer deep freezing not already frozen line must fail + env(trust(G1, A1["USD"](0), tfSetDeepFreeze), + ter(tecNO_PERMISSION)); + + env(trust(G1, A1["USD"](0), tfSetFreeze)); + env.close(); + + // test: Issuer deep freezing already frozen trust line + env(trust(G1, A1["USD"](0), tfSetDeepFreeze)); + { + auto const flags = getTrustlineFlags(env, 2u, 1u); + BEAST_EXPECT(flags & lsfLowFreeze); + BEAST_EXPECT(flags & lsfLowDeepFreeze); + BEAST_EXPECT(!(flags & (lsfHighFreeze | lsfHighDeepFreeze))); + env.close(); + } + + // test: Holder clearing freeze flags has no effect. Each sides' + // flags are independent + env(trust(A1, G1["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + { + auto const flags = getTrustlineFlags(env, 2u, 1u); + BEAST_EXPECT(flags & lsfLowFreeze); + BEAST_EXPECT(flags & lsfLowDeepFreeze); + BEAST_EXPECT(!(flags & (lsfHighFreeze | lsfHighDeepFreeze))); + env.close(); + } + + // test: Issuer can't clear normal freeze when line is deep frozen + env(trust(G1, A1["USD"](0), tfClearFreeze), ter(tecNO_PERMISSION)); + + // test: Issuer clearing deep freeze but normal freeze is still in + // effect + env(trust(G1, A1["USD"](0), tfClearDeepFreeze)); + { + auto const flags = getTrustlineFlags(env, 2u, 1u); + BEAST_EXPECT(flags & lsfLowFreeze); + BEAST_EXPECT(!(flags & lsfLowDeepFreeze)); + BEAST_EXPECT(!(flags & (lsfHighFreeze | lsfHighDeepFreeze))); + env.close(); + } + } + else + { + // test: applying deep freeze before amendment fails + env(trust(G1, A1["USD"](0), tfSetDeepFreeze), ter(temINVALID_FLAG)); + + // test: clearing deep freeze before amendment fails + env(trust(G1, A1["USD"](0), tfClearDeepFreeze), + ter(temINVALID_FLAG)); + } + } + + void + testCreateFrozenTrustline(FeatureBitset features) + { + testcase("Create Frozen Trustline"); + + using namespace test::jtx; + Env env(*this, features); + + Account G1{"G1"}; + Account A1{"A1"}; + + env.fund(XRP(10000), G1, A1); + env.close(); + + // test: can create frozen trustline + { + env(trust(G1, A1["USD"](1000), tfSetFreeze)); + auto const flags = getTrustlineFlags(env, 5u, 3u, false); + BEAST_EXPECT(flags & lsfLowFreeze); + env.close(); + env.require(lines(A1, 1)); + } + + // Cleanup + env(trust(G1, A1["USD"](0), tfClearFreeze)); + env.close(); + env.require(lines(G1, 0)); + env.require(lines(A1, 0)); + + // test: cannot create deep frozen trustline without normal freeze + if (features[featureDeepFreeze]) + { + env(trust(G1, A1["USD"](1000), tfSetDeepFreeze), + ter(tecNO_PERMISSION)); + env.close(); + env.require(lines(A1, 0)); + } + + // test: can create deep frozen trustline together with normal freeze + if (features[featureDeepFreeze]) + { + env(trust(G1, A1["USD"](1000), tfSetFreeze | tfSetDeepFreeze)); + auto const flags = getTrustlineFlags(env, 5u, 3u, false); + BEAST_EXPECT(flags & lsfLowFreeze); + BEAST_EXPECT(flags & lsfLowDeepFreeze); + env.close(); + env.require(lines(A1, 1)); + } + } + + void + testSetAndClear(FeatureBitset features) + { + testcase("Freeze Set and Clear"); + + using namespace test::jtx; + Env env(*this, features); + + Account G1{"G1"}; + Account A1{"A1"}; + + env.fund(XRP(10000), G1, A1); + env.close(); + + env.trust(G1["USD"](1000), A1); + env.close(); + + if (features[featureDeepFreeze]) + { + // test: can't have both set and clear flag families in the same + // transaction + env(trust(G1, A1["USD"](0), tfSetFreeze | tfClearFreeze), + ter(tecNO_PERMISSION)); + env(trust(G1, A1["USD"](0), tfSetFreeze | tfClearDeepFreeze), + ter(tecNO_PERMISSION)); + env(trust(G1, A1["USD"](0), tfSetDeepFreeze | tfClearFreeze), + ter(tecNO_PERMISSION)); + env(trust(G1, A1["USD"](0), tfSetDeepFreeze | tfClearDeepFreeze), + ter(tecNO_PERMISSION)); + } + else + { + // test: old behavior, transaction succeed with no effect on a + // trust line + env(trust(G1, A1["USD"](0), tfSetFreeze | tfClearFreeze)); + { + auto affected = env.meta()->getJson( + JsonOptions::none)[sfAffectedNodes.fieldName]; + BEAST_EXPECT(checkArraySize( + affected, 1u)); // means no trustline changes + } + } + } + void testGlobalFreeze(FeatureBitset features) { @@ -354,15 +542,43 @@ class Freeze_test : public beast::unit_test::suite Account G1{"G1"}; Account A1{"A1"}; + Account frozenAcc{"A2"}; + Account deepFrozenAcc{"A3"}; env.fund(XRP(12000), G1); env.fund(XRP(1000), A1); + env.fund(XRP(1000), frozenAcc); + env.fund(XRP(1000), deepFrozenAcc); env.close(); env.trust(G1["USD"](1000), A1); + env.trust(G1["USD"](1000), frozenAcc); + env.trust(G1["USD"](1000), deepFrozenAcc); env.close(); env(pay(G1, A1, G1["USD"](1000))); + env(pay(G1, frozenAcc, G1["USD"](1000))); + env(pay(G1, deepFrozenAcc, G1["USD"](1000))); + + // Freezing and deep freezing some of the trust lines to check deep + // freeze and clearing of freeze separately + env(trust(G1, frozenAcc["USD"](0), tfSetFreeze)); + { + auto const flags = getTrustlineFlags(env, 2u, 1u); + BEAST_EXPECT(flags & lsfLowFreeze); + BEAST_EXPECT(!(flags & lsfHighFreeze)); + } + if (features[featureDeepFreeze]) + { + env(trust( + G1, deepFrozenAcc["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + { + auto const flags = getTrustlineFlags(env, 2u, 1u); + BEAST_EXPECT(!(flags & (lsfLowFreeze | lsfLowDeepFreeze))); + BEAST_EXPECT(flags & lsfHighFreeze); + BEAST_EXPECT(flags & lsfHighDeepFreeze); + } + } env.close(); // TrustSet NoFreeze @@ -387,16 +603,48 @@ class Freeze_test : public beast::unit_test::suite env.require(flags(G1, asfNoFreeze)); env.require(flags(G1, asfGlobalFreeze)); - // test: trustlines can't be frozen - env(trust(G1, A1["USD"](0), tfSetFreeze)); - auto affected = - env.meta()->getJson(JsonOptions::none)[sfAffectedNodes.fieldName]; - if (!BEAST_EXPECT(checkArraySize(affected, 1u))) - return; + // test: trustlines can't be frozen when no freeze enacted + if (features[featureDeepFreeze]) + { + env(trust(G1, A1["USD"](0), tfSetFreeze), ter(tecNO_PERMISSION)); - auto let = - affected[0u][sfModifiedNode.fieldName][sfLedgerEntryType.fieldName]; - BEAST_EXPECT(let == jss::AccountRoot); + // test: cannot deep freeze already frozen line when no freeze + // enacted + env(trust(G1, frozenAcc["USD"](0), tfSetDeepFreeze), + ter(tecNO_PERMISSION)); + } + else + { + // test: previous functionality, checking there's no changes to a + // trust line + env(trust(G1, A1["USD"](0), tfSetFreeze)); + auto affected = env.meta()->getJson( + JsonOptions::none)[sfAffectedNodes.fieldName]; + if (!BEAST_EXPECT(checkArraySize(affected, 1u))) + return; + + auto let = affected[0u][sfModifiedNode.fieldName] + [sfLedgerEntryType.fieldName]; + BEAST_EXPECT(let == jss::AccountRoot); + } + + // test: can clear freeze on account + env(trust(G1, frozenAcc["USD"](0), tfClearFreeze)); + { + auto const flags = getTrustlineFlags(env, 2u, 1u); + BEAST_EXPECT(!(flags & lsfLowFreeze)); + } + + if (features[featureDeepFreeze]) + { + // test: can clear deep freeze on account + env(trust(G1, deepFrozenAcc["USD"](0), tfClearDeepFreeze)); + { + auto const flags = getTrustlineFlags(env, 2u, 1u); + BEAST_EXPECT(flags & lsfHighFreeze); + BEAST_EXPECT(!(flags & lsfHighDeepFreeze)); + } + } } void @@ -506,19 +754,1273 @@ class Freeze_test : public beast::unit_test::suite return; } + void + testOffersWhenDeepFrozen(FeatureBitset features) + { + testcase("Offers on frozen trust lines"); + + using namespace test::jtx; + Env env(*this, features); + + Account G1{"G1"}; + Account A1{"A1"}; + Account A2{"A2"}; + Account A3{"A3"}; + auto const USD{G1["USD"]}; + + env.fund(XRP(10000), G1, A1, A2, A3); + env.close(); + + auto const limit = USD(10000); + env.trust(limit, A1, A2, A3); + env.close(); + + env(pay(G1, A1, USD(1000))); + env(pay(G1, A2, USD(1000))); + env.close(); + + // Making large passive sell offer + // Wants to sell 50 USD for 100 XRP + env(offer(A2, XRP(100), USD(50)), txflags(tfPassive)); + env.close(); + // Making large passive buy offer + // Wants to buy 100 USD for 100 XRP + env(offer(A3, USD(100), XRP(100)), txflags(tfPassive)); + env.close(); + env.require(offers(A2, 1), offers(A3, 1)); + + // Checking A1 can buy from A2 by crossing it's offer + env(offer(A1, USD(1), XRP(2)), txflags(tfFillOrKill)); + env.close(); + env.require(balance(A1, USD(1001)), balance(A2, USD(999))); + + // Checking A1 can sell to A3 by crossing it's offer + env(offer(A1, XRP(1), USD(1)), txflags(tfFillOrKill)); + env.close(); + env.require(balance(A1, USD(1000)), balance(A3, USD(1))); + + // Testing aggressive and passive offer placing, trustline frozen by + // the issuer + { + env(trust(G1, A1["USD"](0), tfSetFreeze)); + env.close(); + + // test: can still make passive buy offer + env(offer(A1, USD(1), XRP(0.5)), txflags(tfPassive)); + env.close(); + env.require(balance(A1, USD(1000)), offers(A1, 1)); + // Cleanup + env(offer_cancel(A1, env.seq(A1) - 1)); + env.require(offers(A1, 0)); + env.close(); + + // test: can still buy from A2 + env(offer(A1, USD(1), XRP(2)), txflags(tfFillOrKill)); + env.close(); + env.require( + balance(A1, USD(1001)), balance(A2, USD(998)), offers(A1, 0)); + + // test: cannot create passive sell offer + env(offer(A1, XRP(2), USD(1)), + txflags(tfPassive), + ter(tecUNFUNDED_OFFER)); + env.close(); + env.require(balance(A1, USD(1001)), offers(A1, 0)); + + // test: cannot sell to A3 + env(offer(A1, XRP(1), USD(1)), + txflags(tfFillOrKill), + ter(tecUNFUNDED_OFFER)); + env.close(); + env.require(balance(A1, USD(1001)), offers(A1, 0)); + + env(trust(G1, A1["USD"](0), tfClearFreeze)); + env.close(); + } + + // Testing aggressive and passive offer placing, trustline deep frozen + // by the issuer + if (features[featureDeepFreeze]) + { + env(trust(G1, A1["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: cannot create passive buy offer + env(offer(A1, USD(1), XRP(0.5)), + txflags(tfPassive), + ter(tecFROZEN)); + env.close(); + + // test: cannot buy from A2 + env(offer(A1, USD(1), XRP(2)), + txflags(tfFillOrKill), + ter(tecFROZEN)); + env.close(); + + // test: cannot create passive sell offer + env(offer(A1, XRP(2), USD(1)), + txflags(tfPassive), + ter(tecUNFUNDED_OFFER)); + env.close(); + + // test: cannot sell to A3 + env(offer(A1, XRP(1), USD(1)), + txflags(tfFillOrKill), + ter(tecUNFUNDED_OFFER)); + env.close(); + + env(trust(G1, A1["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + env.close(); + env.require(balance(A1, USD(1001)), offers(A1, 0)); + } + + // Testing already existing offers behavior after trustline is frozen by + // the issuer + { + env.require(balance(A1, USD(1001))); + env(offer(A1, XRP(1.9), USD(1))); + env(offer(A1, USD(1), XRP(1.1))); + env.close(); + env.require(balance(A1, USD(1001)), offers(A1, 2)); + + env(trust(G1, A1["USD"](0), tfSetFreeze)); + env.close(); + + // test: A2 wants to sell to A1, must succeed + env.require(balance(A1, USD(1001)), balance(A2, USD(998))); + env(offer(A2, XRP(1.1), USD(1)), txflags(tfFillOrKill)); + env.close(); + env.require( + balance(A1, USD(1002)), balance(A2, USD(997)), offers(A1, 1)); + + // test: A3 wants to buy from A1, must fail + env.require( + balance(A1, USD(1002)), balance(A3, USD(1)), offers(A1, 1)); + env(offer(A3, USD(1), XRP(1.9)), + txflags(tfFillOrKill), + ter(tecKILLED)); + env.close(); + env.require( + balance(A1, USD(1002)), balance(A3, USD(1)), offers(A1, 0)); + + env(trust(G1, A1["USD"](0), tfClearFreeze)); + env.close(); + } + + // Testing existing offers behavior after trustline is deep frozen by + // the issuer + if (features[featureDeepFreeze]) + { + env.require(balance(A1, USD(1002))); + env(offer(A1, XRP(1.9), USD(1))); + env(offer(A1, USD(1), XRP(1.1))); + env.close(); + env.require(balance(A1, USD(1002)), offers(A1, 2)); + + env(trust(G1, A1["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: A2 wants to sell to A1, must fail + env.require(balance(A1, USD(1002)), balance(A2, USD(997))); + env(offer(A2, XRP(1.1), USD(1)), + txflags(tfFillOrKill), + ter(tecKILLED)); + env.close(); + env.require( + balance(A1, USD(1002)), balance(A2, USD(997)), offers(A1, 1)); + + // test: A3 wants to buy from A1, must fail + env.require( + balance(A1, USD(1002)), balance(A3, USD(1)), offers(A1, 1)); + env(offer(A3, USD(1), XRP(1.9)), + txflags(tfFillOrKill), + ter(tecKILLED)); + env.close(); + env.require( + balance(A1, USD(1002)), balance(A3, USD(1)), offers(A1, 0)); + + env(trust(G1, A1["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + + // Testing aggressive and passive offer placing, trustline frozen by + // the holder + { + env(trust(A1, limit, tfSetFreeze)); + env.close(); + + // test: A1 can make passive buy offer + env(offer(A1, USD(1), XRP(0.5)), txflags(tfPassive)); + env.close(); + env.require(balance(A1, USD(1002)), offers(A1, 1)); + // Cleanup + env(offer_cancel(A1, env.seq(A1) - 1)); + env.require(offers(A1, 0)); + env.close(); + + // test: A1 wants to buy, must fail + if (features[featureFlowCross]) + { + env(offer(A1, USD(1), XRP(2)), + txflags(tfFillOrKill), + ter(tecKILLED)); + env.close(); + env.require( + balance(A1, USD(1002)), + balance(A2, USD(997)), + offers(A1, 0)); + } + else + { + // The transaction that should be here would succeed. + // I don't want to adjust balances in following tests. Flow + // cross feature flag is not relevant to this particular test + // case so we're not missing out some corner cases checks. + } + + // test: A1 can create passive sell offer + env(offer(A1, XRP(2), USD(1)), txflags(tfPassive)); + env.close(); + env.require(balance(A1, USD(1002)), offers(A1, 1)); + // Cleanup + env(offer_cancel(A1, env.seq(A1) - 1)); + env.require(offers(A1, 0)); + env.close(); + + // test: A1 can sell to A3 + env(offer(A1, XRP(1), USD(1)), txflags(tfFillOrKill)); + env.close(); + env.require(balance(A1, USD(1001)), offers(A1, 0)); + + env(trust(A1, limit, tfClearFreeze)); + env.close(); + } + + // Testing aggressive and passive offer placing, trustline deep frozen + // by the holder + if (features[featureDeepFreeze]) + { + env(trust(A1, limit, tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: A1 cannot create passive buy offer + env(offer(A1, USD(1), XRP(0.5)), + txflags(tfPassive), + ter(tecFROZEN)); + env.close(); + + // test: A1 cannot buy, must fail + env(offer(A1, USD(1), XRP(2)), + txflags(tfFillOrKill), + ter(tecFROZEN)); + env.close(); + + // test: A1 cannot create passive sell offer + env(offer(A1, XRP(2), USD(1)), + txflags(tfPassive), + ter(tecUNFUNDED_OFFER)); + env.close(); + + // test: A1 cannot sell to A3 + env(offer(A1, XRP(1), USD(1)), + txflags(tfFillOrKill), + ter(tecUNFUNDED_OFFER)); + env.close(); + + env(trust(A1, limit, tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + } + + void + testPathsWhenFrozen(FeatureBitset features) + { + testcase("Longer paths payment on frozen trust lines"); + using namespace test::jtx; + using path = test::jtx::path; + + Env env(*this, features); + Account G1{"G1"}; + Account A1{"A1"}; + Account A2{"A2"}; + auto const USD{G1["USD"]}; + + env.fund(XRP(10000), G1, A1, A2); + env.close(); + + auto const limit = USD(10000); + env.trust(limit, A1, A2); + env.close(); + + env(pay(G1, A1, USD(1000))); + env(pay(G1, A2, USD(1000))); + env.close(); + + env(offer(A2, XRP(100), USD(100)), txflags(tfPassive)); + env.close(); + + // Testing payments A1 <-> G1 using offer from A2 frozen by issuer. + { + env(trust(G1, A2["USD"](0), tfSetFreeze)); + env.close(); + + // test: A1 cannot send USD using XRP through A2 offer + env(pay(A1, G1, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_PARTIAL)); + env.close(); + + // test: G1 cannot send USD using XRP through A2 offer + env(pay(G1, A1, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_PARTIAL)); + env.close(); + + env(trust(G1, A2["USD"](0), tfClearFreeze)); + env.close(); + } + + // Testing payments A1 <-> G1 using offer from A2 deep frozen by issuer. + if (features[featureDeepFreeze]) + { + env(trust(G1, A2["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: A1 cannot send USD using XRP through A2 offer + env(pay(A1, G1, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_PARTIAL)); + env.close(); + + // test: G1 cannot send USD using XRP through A2 offer + env(pay(G1, A1, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_PARTIAL)); + env.close(); + + env(trust(G1, A2["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + + // Testing payments A1 <-> G1 using offer from A2 frozen by currency + // holder. + { + env(trust(A2, limit, tfSetFreeze)); + env.close(); + + // test: A1 can send USD using XRP through A2 offer + env(pay(A1, G1, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect)); + env.close(); + + // test: G1 can send USD using XRP through A2 offer + env(pay(G1, A1, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect)); + env.close(); + + env(trust(A2, limit, tfClearFreeze)); + env.close(); + } + + // Testing payments A1 <-> G1 using offer from A2 deep frozen by + // currency holder. + if (features[featureDeepFreeze]) + { + env(trust(A2, limit, tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: A1 cannot send USD using XRP through A2 offer + env(pay(A1, G1, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_PARTIAL)); + env.close(); + + // test: G1 cannot send USD using XRP through A2 offer + env(pay(G1, A1, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_PARTIAL)); + env.close(); + + env(trust(A2, limit, tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + + // Cleanup + env(offer_cancel(A1, env.seq(A1) - 1)); + env.require(offers(A1, 0)); + env.close(); + + env(offer(A2, USD(100), XRP(100)), txflags(tfPassive)); + env.close(); + + // Testing payments A1 <-> G1 using offer from A2 frozen by issuer. + { + env(trust(G1, A2["USD"](0), tfSetFreeze)); + env.close(); + + // test: A1 can send XRP using USD through A2 offer + env(pay(A1, G1, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect)); + env.close(); + + // test: G1 can send XRP using USD through A2 offer + env(pay(G1, A1, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect)); + env.close(); + + env(trust(G1, A2["USD"](0), tfClearFreeze)); + env.close(); + } + + // Testing payments A1 <-> G1 using offer from A2 deep frozen by + // issuer. + if (features[featureDeepFreeze]) + { + env(trust(G1, A2["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: A1 cannot send XRP using USD through A2 offer + env(pay(A1, G1, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_PARTIAL)); + env.close(); + + // test: G1 cannot send XRP using USD through A2 offer + env(pay(G1, A1, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_PARTIAL)); + env.close(); + + env(trust(G1, A2["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + + // Testing payments A1 <-> G1 using offer from A2 frozen by currency + // holder. + { + env(trust(A2, limit, tfSetFreeze)); + env.close(); + + // test: A1 can send XRP using USD through A2 offer + env(pay(A1, G1, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect)); + env.close(); + + // test: G1 can send XRP using USD through A2 offer + env(pay(G1, A1, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect)); + env.close(); + + env(trust(A2, limit, tfClearFreeze)); + env.close(); + } + + // Testing payments A1 <-> G1 using offer from A2 deep frozen by + // currency holder. + if (features[featureDeepFreeze]) + { + env(trust(A2, limit, tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: A1 cannot send XRP using USD through A2 offer + env(pay(A1, G1, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_PARTIAL)); + env.close(); + + // test: G1 cannot send XRP using USD through A2 offer + env(pay(G1, A1, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_PARTIAL)); + env.close(); + + env(trust(A2, limit, tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + + // Cleanup + env(offer_cancel(A1, env.seq(A1) - 1)); + env.require(offers(A1, 0)); + env.close(); + } + + void + testPaymentsWhenDeepFrozen(FeatureBitset features) + { + testcase("Direct payments on frozen trust lines"); + + using namespace test::jtx; + Env env(*this, features); + + Account G1{"G1"}; + Account A1{"A1"}; + Account A2{"A2"}; + auto const USD{G1["USD"]}; + + env.fund(XRP(10000), G1, A1, A2); + env.close(); + + auto const limit = USD(10000); + env.trust(limit, A1, A2); + env.close(); + + env(pay(G1, A1, USD(1000))); + env(pay(G1, A2, USD(1000))); + env.close(); + + // Checking payments before freeze + // To issuer: + env(pay(A1, G1, USD(1))); + env(pay(A2, G1, USD(1))); + env.close(); + + // To each other: + env(pay(A1, A2, USD(1))); + env(pay(A2, A1, USD(1))); + env.close(); + + // Freeze A1 + env(trust(G1, A1["USD"](0), tfSetFreeze)); + env.close(); + + // Issuer and A1 can send payments to each other + env(pay(A1, G1, USD(1))); + env(pay(G1, A1, USD(1))); + env.close(); + + // A1 cannot send tokens to A2 + env(pay(A1, A2, USD(1)), ter(tecPATH_DRY)); + + // A2 can still send to A1 + env(pay(A2, A1, USD(1))); + env.close(); + + if (features[featureDeepFreeze]) + { + // Deep freeze A1 + env(trust(G1, A1["USD"](0), tfSetDeepFreeze)); + env.close(); + + // Issuer and A1 can send payments to each other + env(pay(A1, G1, USD(1))); + env(pay(G1, A1, USD(1))); + env.close(); + + // A1 cannot send tokens to A2 + env(pay(A1, A2, USD(1)), ter(tecPATH_DRY)); + + // A2 cannot send tokens to A1 + env(pay(A2, A1, USD(1)), ter(tecPATH_DRY)); + + // Clear deep freeze on A1 + env(trust(G1, A1["USD"](0), tfClearDeepFreeze)); + env.close(); + } + + // Clear freeze on A1 + env(trust(G1, A1["USD"](0), tfClearFreeze)); + env.close(); + + // A1 freezes trust line + env(trust(A1, limit, tfSetFreeze)); + env.close(); + + // Issuer and A2 must not be affected + env(pay(A2, G1, USD(1))); + env(pay(G1, A2, USD(1))); + env.close(); + + // A1 can send tokens to the issuer + env(pay(A1, G1, USD(1))); + env.close(); + // A1 can send tokens to A2 + env(pay(A1, A2, USD(1))); + env.close(); + + // Issuer can sent tokens to A1 + env(pay(G1, A1, USD(1))); + // A2 cannot send tokens to A1 + env(pay(A2, A1, USD(1)), ter(tecPATH_DRY)); + + if (features[featureDeepFreeze]) + { + // A1 deep freezes trust line + env(trust(A1, limit, tfSetDeepFreeze)); + env.close(); + + // Issuer and A2 must not be affected + env(pay(A2, G1, USD(1))); + env(pay(G1, A2, USD(1))); + env.close(); + + // A1 can still send token to issuer + env(pay(A1, G1, USD(1))); + env.close(); + + // Issuer can send tokens to A1 + env(pay(G1, A1, USD(1))); + // A2 cannot send tokens to A1 + env(pay(A2, A1, USD(1)), ter(tecPATH_DRY)); + // A1 cannot send tokens to A2 + env(pay(A1, A2, USD(1)), ter(tecPATH_DRY)); + } + } + + void + testChecksWhenFrozen(FeatureBitset features) + { + testcase("Checks on frozen trust lines"); + + using namespace test::jtx; + Env env(*this, features); + + Account G1{"G1"}; + Account A1{"A1"}; + Account A2{"A2"}; + auto const USD{G1["USD"]}; + + env.fund(XRP(10000), G1, A1, A2); + env.close(); + + auto const limit = USD(10000); + env.trust(limit, A1, A2); + env.close(); + + env(pay(G1, A1, USD(1000))); + env(pay(G1, A2, USD(1000))); + env.close(); + + // Confirming we can write and cash checks + { + uint256 const checkId{getCheckIndex(G1, env.seq(G1))}; + env(check::create(G1, A1, USD(10))); + env.close(); + env(check::cash(A1, checkId, USD(10))); + env.close(); + } + + { + uint256 const checkId{getCheckIndex(G1, env.seq(G1))}; + env(check::create(G1, A2, USD(10))); + env.close(); + env(check::cash(A2, checkId, USD(10))); + env.close(); + } + + { + uint256 const checkId{getCheckIndex(A1, env.seq(A1))}; + env(check::create(A1, G1, USD(10))); + env.close(); + env(check::cash(G1, checkId, USD(10))); + env.close(); + } + + { + uint256 const checkId{getCheckIndex(A1, env.seq(A1))}; + env(check::create(A1, A2, USD(10))); + env.close(); + env(check::cash(A2, checkId, USD(10))); + env.close(); + } + + { + uint256 const checkId{getCheckIndex(A2, env.seq(A2))}; + env(check::create(A2, G1, USD(10))); + env.close(); + env(check::cash(G1, checkId, USD(10))); + env.close(); + } + + { + uint256 const checkId{getCheckIndex(A2, env.seq(A2))}; + env(check::create(A2, A1, USD(10))); + env.close(); + env(check::cash(A1, checkId, USD(10))); + env.close(); + } + + // Testing creation and cashing of checks on a trustline frozen by + // issuer + { + env(trust(G1, A1["USD"](0), tfSetFreeze)); + env.close(); + + // test: issuer writes check to A1. + { + uint256 const checkId{getCheckIndex(G1, env.seq(G1))}; + env(check::create(G1, A1, USD(10))); + env.close(); + env(check::cash(A1, checkId, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // test: A2 writes check to A1. + { + uint256 const checkId{getCheckIndex(A2, env.seq(A2))}; + env(check::create(A2, A1, USD(10))); + env.close(); + // Same as previous test + env(check::cash(A1, checkId, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // test: A1 writes check to issuer + { + env(check::create(A1, G1, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // test: A1 writes check to A2 + { + // Same as previous test + env(check::create(A1, A2, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // Unfreeze the trustline to create a couple of checks so that we + // could try to cash them later when the trustline is frozen again. + env(trust(G1, A1["USD"](0), tfClearFreeze)); + env.close(); + + uint256 const checkId1{getCheckIndex(A1, env.seq(A1))}; + env(check::create(A1, G1, USD(10))); + env.close(); + uint256 const checkId2{getCheckIndex(A1, env.seq(A1))}; + env(check::create(A1, A2, USD(10))); + env.close(); + + env(trust(G1, A1["USD"](0), tfSetFreeze)); + env.close(); + + // test: issuer tries to cash the check from A1 + { + env(check::cash(G1, checkId1, USD(10)), ter(tecPATH_PARTIAL)); + env.close(); + } + + // test: A2 tries to cash the check from A1 + { + env(check::cash(A2, checkId2, USD(10)), ter(tecPATH_PARTIAL)); + env.close(); + } + + env(trust(G1, A1["USD"](0), tfClearFreeze)); + env.close(); + } + + // Testing creation and cashing of checks on a trustline deep frozen by + // issuer + if (features[featureDeepFreeze]) + { + env(trust(G1, A1["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: issuer writes check to A1. + { + uint256 const checkId{getCheckIndex(G1, env.seq(G1))}; + env(check::create(G1, A1, USD(10))); + env.close(); + + env(check::cash(A1, checkId, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // test: A2 writes check to A1. + { + uint256 const checkId{getCheckIndex(A2, env.seq(A2))}; + env(check::create(A2, A1, USD(10))); + env.close(); + // Same as previous test + env(check::cash(A1, checkId, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // test: A1 writes check to issuer + { + env(check::create(A1, G1, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // test: A1 writes check to A2 + { + // Same as previous test + env(check::create(A1, A2, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // Unfreeze the trustline to create a couple of checks so that we + // could try to cash them later when the trustline is frozen again. + env(trust(G1, A1["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + env.close(); + + uint256 const checkId1{getCheckIndex(A1, env.seq(A1))}; + env(check::create(A1, G1, USD(10))); + env.close(); + uint256 const checkId2{getCheckIndex(A1, env.seq(A1))}; + env(check::create(A1, A2, USD(10))); + env.close(); + + env(trust(G1, A1["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: issuer tries to cash the check from A1 + { + env(check::cash(G1, checkId1, USD(10)), ter(tecPATH_PARTIAL)); + env.close(); + } + + // test: A2 tries to cash the check from A1 + { + env(check::cash(A2, checkId2, USD(10)), ter(tecPATH_PARTIAL)); + env.close(); + } + + env(trust(G1, A1["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + + // Testing creation and cashing of checks on a trustline frozen by + // a currency holder + { + env(trust(A1, limit, tfSetFreeze)); + env.close(); + + // test: issuer writes check to A1. + { + env(check::create(G1, A1, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // test: A2 writes check to A1. + { + env(check::create(A2, A1, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // test: A1 writes check to issuer + { + uint256 const checkId{getCheckIndex(A1, env.seq(A1))}; + env(check::create(A1, G1, USD(10))); + env.close(); + env(check::cash(G1, checkId, USD(10))); + env.close(); + } + + // test: A1 writes check to A2 + { + uint256 const checkId{getCheckIndex(A1, env.seq(A1))}; + env(check::create(A1, A2, USD(10))); + env.close(); + env(check::cash(A2, checkId, USD(10))); + env.close(); + } + + env(trust(A1, limit, tfClearFreeze)); + env.close(); + } + + // Testing creation and cashing of checks on a trustline deep frozen by + // a currency holder + if (features[featureDeepFreeze]) + { + env(trust(A1, limit, tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: issuer writes check to A1. + { + env(check::create(G1, A1, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // test: A2 writes check to A1. + { + env(check::create(A2, A1, USD(10)), ter(tecFROZEN)); + env.close(); + } + + // test: A1 writes check to issuer + { + uint256 const checkId{getCheckIndex(A1, env.seq(A1))}; + env(check::create(A1, G1, USD(10))); + env.close(); + env(check::cash(G1, checkId, USD(10)), ter(tecPATH_PARTIAL)); + env.close(); + } + + // test: A1 writes check to A2 + { + uint256 const checkId{getCheckIndex(A1, env.seq(A1))}; + env(check::create(A1, A2, USD(10))); + env.close(); + env(check::cash(A2, checkId, USD(10)), ter(tecPATH_PARTIAL)); + env.close(); + } + + env(trust(A1, limit, tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + } + + void + testAMMWhenFreeze(FeatureBitset features) + { + testcase("AMM payments on frozen trust lines"); + using namespace test::jtx; + using path = test::jtx::path; + + Env env(*this, features); + Account G1{"G1"}; + Account A1{"A1"}; + Account A2{"A2"}; + auto const USD{G1["USD"]}; + + env.fund(XRP(10000), G1, A1, A2); + env.close(); + + env.trust(G1["USD"](10000), A1, A2); + env.close(); + + env(pay(G1, A1, USD(1000))); + env(pay(G1, A2, USD(1000))); + env.close(); + + AMM ammG1(env, G1, XRP(1'000), USD(1'000)); + env.close(); + + // Testing basic payment using AMM when freezing one of the trust lines. + { + env(trust(G1, A1["USD"](0), tfSetFreeze)); + env.close(); + + // test: can still use XRP to make payment + env(pay(A1, A2, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect)); + env.close(); + + // test: cannot use USD to make payment + env(pay(A1, A2, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_DRY)); + env.close(); + + // test: can still receive USD payments. + env(pay(A2, A1, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect)); + env.close(); + + // test: can still receive XRP payments. + env(pay(A2, A1, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect)); + env.close(); + + env(trust(G1, A1["USD"](0), tfClearFreeze)); + env.close(); + } + + // Testing basic payment using AMM when deep freezing one of the trust + // lines. + if (features[featureDeepFreeze]) + { + env(trust(G1, A1["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: can still use XRP to make payment + env(pay(A1, A2, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect)); + env.close(); + + // test: cannot use USD to make payment + env(pay(A1, A2, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_DRY)); + env.close(); + + // test: cannot receive USD payments. + env(pay(A2, A1, USD(10)), + path(~USD), + sendmax(XRP(11)), + txflags(tfNoRippleDirect), + ter(tecPATH_DRY)); + env.close(); + + // test: can still receive XRP payments. + env(pay(A2, A1, XRP(10)), + path(~XRP), + sendmax(USD(11)), + txflags(tfNoRippleDirect)); + env.close(); + + env(trust(G1, A1["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + } + + void + testNFTOffersWhenFreeze(FeatureBitset features) + { + testcase("NFT offers on frozen trust lines"); + using namespace test::jtx; + + Env env(*this, features); + Account G1{"G1"}; + Account A1{"A1"}; + Account A2{"A2"}; + auto const USD{G1["USD"]}; + + env.fund(XRP(10000), G1, A1, A2); + env.close(); + + auto const limit = USD(10000); + env.trust(limit, A1, A2); + env.close(); + + env(pay(G1, A1, USD(1000))); + env(pay(G1, A2, USD(1000))); + env.close(); + + // Testing A2 nft offer sell when A2 frozen by issuer + { + auto const sellOfferIndex = createNFTSellOffer(env, A2, USD(10)); + env(trust(G1, A2["USD"](0), tfSetFreeze)); + env.close(); + + // test: A2 can still receive USD for his NFT + env(token::acceptSellOffer(A1, sellOfferIndex)); + env.close(); + + env(trust(G1, A2["USD"](0), tfClearFreeze)); + env.close(); + } + + // Testing A2 nft offer sell when A2 deep frozen by issuer + if (features[featureDeepFreeze]) + { + auto const sellOfferIndex = createNFTSellOffer(env, A2, USD(10)); + + env(trust(G1, A2["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: A2 cannot receive USD for his NFT + env(token::acceptSellOffer(A1, sellOfferIndex), ter(tecFROZEN)); + env.close(); + + env(trust(G1, A2["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + + // Testing A1 nft offer sell when A2 frozen by issuer + { + auto const sellOfferIndex = createNFTSellOffer(env, A1, USD(10)); + env(trust(G1, A2["USD"](0), tfSetFreeze)); + env.close(); + + // test: A2 cannot send USD for NFT + env(token::acceptSellOffer(A2, sellOfferIndex), + ter(tecINSUFFICIENT_FUNDS)); + env.close(); + + env(trust(G1, A2["USD"](0), tfClearFreeze)); + env.close(); + } + + // Testing A1 nft offer sell when A2 deep frozen by issuer + if (features[featureDeepFreeze]) + { + auto const sellOfferIndex = createNFTSellOffer(env, A1, USD(10)); + env(trust(G1, A2["USD"](0), tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: A2 cannot send USD for NFT + env(token::acceptSellOffer(A2, sellOfferIndex), + ter(tecINSUFFICIENT_FUNDS)); + env.close(); + + env(trust(G1, A2["USD"](0), tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + + // Testing A2 nft offer sell when A2 frozen by currency holder + { + auto const sellOfferIndex = createNFTSellOffer(env, A2, USD(10)); + env(trust(A2, limit, tfSetFreeze)); + env.close(); + + // test: offer can still be accepted. + env(token::acceptSellOffer(A1, sellOfferIndex)); + env.close(); + + env(trust(A2, limit, tfClearFreeze)); + env.close(); + } + + // Testing A2 nft offer sell when A2 deep frozen by currency holder + if (features[featureDeepFreeze]) + { + auto const sellOfferIndex = createNFTSellOffer(env, A2, USD(10)); + + env(trust(A2, limit, tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: A2 cannot receive USD for his NFT + env(token::acceptSellOffer(A1, sellOfferIndex), ter(tecFROZEN)); + env.close(); + + env(trust(A2, limit, tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + + // Testing A1 nft offer sell when A2 frozen by currency holder + { + auto const sellOfferIndex = createNFTSellOffer(env, A1, USD(10)); + env(trust(A2, limit, tfSetFreeze)); + env.close(); + + // test: A2 cannot send USD for NFT + env(token::acceptSellOffer(A2, sellOfferIndex)); + env.close(); + + env(trust(A2, limit, tfClearFreeze)); + env.close(); + } + + // Testing A1 nft offer sell when A2 deep frozen by currency holder + if (features[featureDeepFreeze]) + { + auto const sellOfferIndex = createNFTSellOffer(env, A1, USD(10)); + env(trust(A2, limit, tfSetFreeze | tfSetDeepFreeze)); + env.close(); + + // test: A2 cannot send USD for NFT + env(token::acceptSellOffer(A2, sellOfferIndex), + ter(tecINSUFFICIENT_FUNDS)); + env.close(); + + env(trust(A2, limit, tfClearFreeze | tfClearDeepFreeze)); + env.close(); + } + } + + // Helper function to extract trustline flags from open ledger + uint32_t + getTrustlineFlags( + test::jtx::Env& env, + size_t expectedArraySize, + size_t expectedArrayIndex, + bool modified = true) + { + using namespace test::jtx; + auto const affected = + env.meta()->getJson(JsonOptions::none)[sfAffectedNodes.fieldName]; + if (!BEAST_EXPECT(checkArraySize(affected, expectedArraySize))) + return 0; + + if (modified) + { + return affected[expectedArrayIndex][sfModifiedNode.fieldName] + [sfFinalFields.fieldName][jss::Flags] + .asUInt(); + } + + return affected[expectedArrayIndex][sfCreatedNode.fieldName] + [sfNewFields.fieldName][jss::Flags] + .asUInt(); + } + + // Helper function that returns the index of the next check on account + uint256 + getCheckIndex(AccountID const& account, std::uint32_t uSequence) + { + return keylet::check(account, uSequence).key; + } + + uint256 + createNFTSellOffer( + test::jtx::Env& env, + test::jtx::Account const& account, + test::jtx::PrettyAmount const& currency) + { + using namespace test::jtx; + uint256 const nftID{token::getNextID(env, account, 0u, tfTransferable)}; + env(token::mint(account, 0), txflags(tfTransferable)); + env.close(); + + uint256 const sellOfferIndex = + keylet::nftoffer(account, env.seq(account)).key; + env(token::createOffer(account, nftID, currency), + txflags(tfSellNFToken)); + env.close(); + + return sellOfferIndex; + } + public: void run() override { auto testAll = [this](FeatureBitset features) { testRippleState(features); + testDeepFreeze(features); + testCreateFrozenTrustline(features); + testSetAndClear(features); testGlobalFreeze(features); testNoFreeze(features); testOffersWhenFrozen(features); + testOffersWhenDeepFrozen(features); + testPaymentsWhenDeepFrozen(features); + testChecksWhenFrozen(features); + testAMMWhenFreeze(features); + testPathsWhenFrozen(features); + testNFTOffersWhenFreeze(features); }; using namespace test::jtx; auto const sa = supported_amendments(); + testAll(sa - featureFlowCross - featureDeepFreeze); testAll(sa - featureFlowCross); + testAll(sa - featureDeepFreeze); testAll(sa); } }; diff --git a/src/test/ledger/Invariants_test.cpp b/src/test/ledger/Invariants_test.cpp index 993104d6ad..ecf1c8e397 100644 --- a/src/test/ledger/Invariants_test.cpp +++ b/src/test/ledger/Invariants_test.cpp @@ -408,6 +408,183 @@ class Invariants_test : public beast::unit_test::suite }); } + void + testNoDeepFreezeTrustLinesWithoutFreeze() + { + using namespace test::jtx; + testcase << "trust lines with deep freeze flag without freeze " + "not allowed"; + doInvariantCheck( + {{"a trust line with deep freeze flag without normal freeze was " + "created"}}, + [](Account const& A1, Account const& A2, ApplyContext& ac) { + auto const sleNew = std::make_shared( + keylet::line(A1, A2, A1["USD"].currency)); + sleNew->setFieldAmount(sfLowLimit, A1["USD"](0)); + sleNew->setFieldAmount(sfHighLimit, A1["USD"](0)); + + std::uint32_t uFlags = 0u; + uFlags |= lsfLowDeepFreeze; + sleNew->setFieldU32(sfFlags, uFlags); + ac.view().insert(sleNew); + return true; + }); + + doInvariantCheck( + {{"a trust line with deep freeze flag without normal freeze was " + "created"}}, + [](Account const& A1, Account const& A2, ApplyContext& ac) { + auto const sleNew = std::make_shared( + keylet::line(A1, A2, A1["USD"].currency)); + sleNew->setFieldAmount(sfLowLimit, A1["USD"](0)); + sleNew->setFieldAmount(sfHighLimit, A1["USD"](0)); + std::uint32_t uFlags = 0u; + uFlags |= lsfHighDeepFreeze; + sleNew->setFieldU32(sfFlags, uFlags); + ac.view().insert(sleNew); + return true; + }); + + doInvariantCheck( + {{"a trust line with deep freeze flag without normal freeze was " + "created"}}, + [](Account const& A1, Account const& A2, ApplyContext& ac) { + auto const sleNew = std::make_shared( + keylet::line(A1, A2, A1["USD"].currency)); + sleNew->setFieldAmount(sfLowLimit, A1["USD"](0)); + sleNew->setFieldAmount(sfHighLimit, A1["USD"](0)); + std::uint32_t uFlags = 0u; + uFlags |= lsfLowDeepFreeze | lsfHighDeepFreeze; + sleNew->setFieldU32(sfFlags, uFlags); + ac.view().insert(sleNew); + return true; + }); + + doInvariantCheck( + {{"a trust line with deep freeze flag without normal freeze was " + "created"}}, + [](Account const& A1, Account const& A2, ApplyContext& ac) { + auto const sleNew = std::make_shared( + keylet::line(A1, A2, A1["USD"].currency)); + sleNew->setFieldAmount(sfLowLimit, A1["USD"](0)); + sleNew->setFieldAmount(sfHighLimit, A1["USD"](0)); + std::uint32_t uFlags = 0u; + uFlags |= lsfLowDeepFreeze | lsfHighFreeze; + sleNew->setFieldU32(sfFlags, uFlags); + ac.view().insert(sleNew); + return true; + }); + + doInvariantCheck( + {{"a trust line with deep freeze flag without normal freeze was " + "created"}}, + [](Account const& A1, Account const& A2, ApplyContext& ac) { + auto const sleNew = std::make_shared( + keylet::line(A1, A2, A1["USD"].currency)); + sleNew->setFieldAmount(sfLowLimit, A1["USD"](0)); + sleNew->setFieldAmount(sfHighLimit, A1["USD"](0)); + std::uint32_t uFlags = 0u; + uFlags |= lsfLowFreeze | lsfHighDeepFreeze; + sleNew->setFieldU32(sfFlags, uFlags); + ac.view().insert(sleNew); + return true; + }); + } + + void + testTransfersNotFrozen() + { + using namespace test::jtx; + testcase << "transfers when frozen"; + + Account G1{"G1"}; + // Helper function to establish the trustlines + auto const createTrustlines = + [&](Account const& A1, Account const& A2, Env& env) { + // Preclose callback to establish trust lines with gateway + env.fund(XRP(1000), G1); + + env.trust(G1["USD"](10000), A1); + env.trust(G1["USD"](10000), A2); + env.close(); + + env(pay(G1, A1, G1["USD"](1000))); + env(pay(G1, A2, G1["USD"](1000))); + env.close(); + + return true; + }; + + auto const A1FrozenByIssuer = + [&](Account const& A1, Account const& A2, Env& env) { + createTrustlines(A1, A2, env); + env(trust(G1, A1["USD"](10000), tfSetFreeze)); + env.close(); + + return true; + }; + + auto const A1DeepFrozenByIssuer = + [&](Account const& A1, Account const& A2, Env& env) { + A1FrozenByIssuer(A1, A2, env); + env(trust(G1, A1["USD"](10000), tfSetDeepFreeze)); + env.close(); + + return true; + }; + + auto const changeBalances = [&](Account const& A1, + Account const& A2, + ApplyContext& ac, + int A1Balance, + int A2Balance) { + auto const sleA1 = ac.view().peek(keylet::line(A1, G1["USD"])); + auto const sleA2 = ac.view().peek(keylet::line(A2, G1["USD"])); + + sleA1->setFieldAmount(sfBalance, G1["USD"](A1Balance)); + sleA2->setFieldAmount(sfBalance, G1["USD"](A2Balance)); + + ac.view().update(sleA1); + ac.view().update(sleA2); + }; + + // test: imitating frozen A1 making a payment to A2. + doInvariantCheck( + {{"Attempting to move frozen funds"}}, + [&](Account const& A1, Account const& A2, ApplyContext& ac) { + changeBalances(A1, A2, ac, -900, -1100); + return true; + }, + XRPAmount{}, + STTx{ttPAYMENT, [](STObject& tx) {}}, + {tecINVARIANT_FAILED, tefINVARIANT_FAILED}, + A1FrozenByIssuer); + + // test: imitating deep frozen A1 making a payment to A2. + doInvariantCheck( + {{"Attempting to move frozen funds"}}, + [&](Account const& A1, Account const& A2, ApplyContext& ac) { + changeBalances(A1, A2, ac, -900, -1100); + return true; + }, + XRPAmount{}, + STTx{ttPAYMENT, [](STObject& tx) {}}, + {tecINVARIANT_FAILED, tefINVARIANT_FAILED}, + A1DeepFrozenByIssuer); + + // test: imitating A2 making a payment to deep frozen A1. + doInvariantCheck( + {{"Attempting to move frozen funds"}}, + [&](Account const& A1, Account const& A2, ApplyContext& ac) { + changeBalances(A1, A2, ac, -1100, -900); + return true; + }, + XRPAmount{}, + STTx{ttPAYMENT, [](STObject& tx) {}}, + {tecINVARIANT_FAILED, tefINVARIANT_FAILED}, + A1DeepFrozenByIssuer); + } + void testXRPBalanceCheck() { @@ -1061,6 +1238,8 @@ public: testAccountRootsDeletedClean(); testTypesMatch(); testNoXRPTrustLine(); + testNoDeepFreezeTrustLinesWithoutFreeze(); + testTransfersNotFrozen(); testXRPBalanceCheck(); testTransactionFeeCheck(); testNoBadOffers(); diff --git a/src/test/rpc/AccountLines_test.cpp b/src/test/rpc/AccountLines_test.cpp index d104ea14b0..bae00d7a76 100644 --- a/src/test/rpc/AccountLines_test.cpp +++ b/src/test/rpc/AccountLines_test.cpp @@ -167,7 +167,11 @@ public: env.close(); // Set flags on gw2 trust lines so we can look for them. - env(trust(alice, gw2Currency(0), gw2, tfSetNoRipple | tfSetFreeze)); + env(trust( + alice, + gw2Currency(0), + gw2, + tfSetNoRipple | tfSetFreeze | tfSetDeepFreeze)); } env.close(); LedgerInfo const ledger58Info = env.closed()->info(); @@ -344,6 +348,7 @@ public: gw2.human() + R"("})"); auto const& line = lines[jss::result][jss::lines][0u]; BEAST_EXPECT(line[jss::freeze].asBool() == true); + BEAST_EXPECT(line[jss::deep_freeze].asBool() == true); BEAST_EXPECT(line[jss::no_ripple].asBool() == true); BEAST_EXPECT(line[jss::peer_authorized].asBool() == true); } @@ -359,6 +364,7 @@ public: alice.human() + R"("})"); auto const& lineA = linesA[jss::result][jss::lines][0u]; BEAST_EXPECT(lineA[jss::freeze_peer].asBool() == true); + BEAST_EXPECT(lineA[jss::deep_freeze_peer].asBool() == true); BEAST_EXPECT(lineA[jss::no_ripple_peer].asBool() == true); BEAST_EXPECT(lineA[jss::authorized].asBool() == true); @@ -981,7 +987,11 @@ public: env.close(); // Set flags on gw2 trust lines so we can look for them. - env(trust(alice, gw2Currency(0), gw2, tfSetNoRipple | tfSetFreeze)); + env(trust( + alice, + gw2Currency(0), + gw2, + tfSetNoRipple | tfSetFreeze | tfSetDeepFreeze)); } env.close(); LedgerInfo const ledger58Info = env.closed()->info(); @@ -1311,6 +1321,7 @@ public: gw2.human() + R"("}})"); auto const& line = lines[jss::result][jss::lines][0u]; BEAST_EXPECT(line[jss::freeze].asBool() == true); + BEAST_EXPECT(line[jss::deep_freeze].asBool() == true); BEAST_EXPECT(line[jss::no_ripple].asBool() == true); BEAST_EXPECT(line[jss::peer_authorized].asBool() == true); BEAST_EXPECT( @@ -1338,6 +1349,7 @@ public: alice.human() + R"("}})"); auto const& lineA = linesA[jss::result][jss::lines][0u]; BEAST_EXPECT(lineA[jss::freeze_peer].asBool() == true); + BEAST_EXPECT(lineA[jss::deep_freeze_peer].asBool() == true); BEAST_EXPECT(lineA[jss::no_ripple_peer].asBool() == true); BEAST_EXPECT(lineA[jss::authorized].asBool() == true); BEAST_EXPECT( diff --git a/src/xrpld/app/paths/TrustLine.h b/src/xrpld/app/paths/TrustLine.h index 381ef47187..4189f7ff48 100644 --- a/src/xrpld/app/paths/TrustLine.h +++ b/src/xrpld/app/paths/TrustLine.h @@ -139,6 +139,13 @@ public: return mFlags & (mViewLowest ? lsfLowFreeze : lsfHighFreeze); } + /** Have we set the deep freeze flag on our peer */ + bool + getDeepFreeze() const + { + return mFlags & (mViewLowest ? lsfLowDeepFreeze : lsfHighDeepFreeze); + } + /** Has the peer set the freeze flag on us */ bool getFreezePeer() const @@ -146,6 +153,13 @@ public: return mFlags & (!mViewLowest ? lsfLowFreeze : lsfHighFreeze); } + /** Has the peer set the deep freeze flag on us */ + bool + getDeepFreezePeer() const + { + return mFlags & (!mViewLowest ? lsfLowDeepFreeze : lsfHighDeepFreeze); + } + STAmount const& getBalance() const { diff --git a/src/xrpld/app/paths/detail/StepChecks.h b/src/xrpld/app/paths/detail/StepChecks.h index 2c2fee91cf..9cbc3ef0f9 100644 --- a/src/xrpld/app/paths/detail/StepChecks.h +++ b/src/xrpld/app/paths/detail/StepChecks.h @@ -52,6 +52,12 @@ checkFreeze( { return terNO_LINE; } + // Unlike normal freeze, a deep frozen trust line acts the same + // regardless of which side froze it + if (sle->isFlag(lsfHighDeepFreeze) || sle->isFlag(lsfLowDeepFreeze)) + { + return terNO_LINE; + } } return tesSUCCESS; diff --git a/src/xrpld/app/tx/detail/CashCheck.cpp b/src/xrpld/app/tx/detail/CashCheck.cpp index 8b5ef79b6d..f6e5f6f3e3 100644 --- a/src/xrpld/app/tx/detail/CashCheck.cpp +++ b/src/xrpld/app/tx/detail/CashCheck.cpp @@ -392,6 +392,7 @@ CashCheck::doApply() false, // authorize account (sleDst->getFlags() & lsfDefaultRipple) == 0, false, // freeze trust line + false, // deep freeze trust line initialBalance, // zero initial balance Issue(currency, account_), // limit of zero 0, // quality in diff --git a/src/xrpld/app/tx/detail/CreateOffer.cpp b/src/xrpld/app/tx/detail/CreateOffer.cpp index 52ca602b95..f1b6646884 100644 --- a/src/xrpld/app/tx/detail/CreateOffer.cpp +++ b/src/xrpld/app/tx/detail/CreateOffer.cpp @@ -259,6 +259,32 @@ CreateOffer::checkAcceptAsset( } } + // An account can not create a trustline to itself, so no line can exist + // to be frozen. Additionally, an issuer can always accept its own + // issuance. + if (issue.account == id) + { + return tesSUCCESS; + } + + auto const trustLine = + view.read(keylet::line(id, issue.account, issue.currency)); + + if (!trustLine) + { + return tesSUCCESS; + } + + // There's no difference which side enacted deep freeze, accepting + // tokens shouldn't be possible. + bool const deepFrozen = + (*trustLine)[sfFlags] & (lsfLowDeepFreeze | lsfHighDeepFreeze); + + if (deepFrozen) + { + return tecFROZEN; + } + return tesSUCCESS; } diff --git a/src/xrpld/app/tx/detail/InvariantCheck.cpp b/src/xrpld/app/tx/detail/InvariantCheck.cpp index 63794023d4..d39492c108 100644 --- a/src/xrpld/app/tx/detail/InvariantCheck.cpp +++ b/src/xrpld/app/tx/detail/InvariantCheck.cpp @@ -556,6 +556,322 @@ NoXRPTrustLines::finalize( //------------------------------------------------------------------------------ +void +NoDeepFreezeTrustLinesWithoutFreeze::visitEntry( + bool, + std::shared_ptr const&, + std::shared_ptr const& after) +{ + if (after && after->getType() == ltRIPPLE_STATE) + { + std::uint32_t const uFlags = after->getFieldU32(sfFlags); + bool const lowFreeze = uFlags & lsfLowFreeze; + bool const lowDeepFreeze = uFlags & lsfLowDeepFreeze; + + bool const highFreeze = uFlags & lsfHighFreeze; + bool const highDeepFreeze = uFlags & lsfHighDeepFreeze; + + deepFreezeWithoutFreeze_ = + (lowDeepFreeze && !lowFreeze) || (highDeepFreeze && !highFreeze); + } +} + +bool +NoDeepFreezeTrustLinesWithoutFreeze::finalize( + STTx const&, + TER const, + XRPAmount const, + ReadView const&, + beast::Journal const& j) +{ + if (!deepFreezeWithoutFreeze_) + return true; + + JLOG(j.fatal()) << "Invariant failed: a trust line with deep freeze flag " + "without normal freeze was created"; + return false; +} + +//------------------------------------------------------------------------------ + +void +TransfersNotFrozen::visitEntry( + bool isDelete, + std::shared_ptr const& before, + std::shared_ptr const& after) +{ + /* + * A trust line freeze state alone doesn't determine if a transfer is + * frozen. The transfer must be examined "end-to-end" because both sides of + * the transfer may have different freeze states and freeze impact depends + * on the transfer direction. This is why first we need to track the + * transfers using IssuerChanges senders/receivers. + * + * Only in validateIssuerChanges, after we collected all changes can we + * determine if the transfer is valid. + */ + if (!isValidEntry(before, after)) + { + return; + } + + auto const balanceChange = calculateBalanceChange(before, after, isDelete); + if (balanceChange.signum() == 0) + { + return; + } + + recordBalanceChanges(after, balanceChange); +} + +bool +TransfersNotFrozen::finalize( + STTx const& tx, + TER const ter, + XRPAmount const fee, + ReadView const& view, + beast::Journal const& j) +{ + /* + * We check this invariant regardless of deep freeze amendment status, + * allowing for detection and logging of potential issues even when the + * amendment is disabled. + * + * If an exploit that allows moving frozen assets is discovered, + * we can alert operators who monitor fatal messages and trigger assert in + * debug builds for an early warning. + * + * In an unlikely event that an exploit is found, this early detection + * enables encouraging the UNL to expedite deep freeze amendment activation + * or deploy hotfixes via new amendments. In case of a new amendment, we'd + * only have to change this line setting 'enforce' variable. + * enforce = view.rules().enabled(featureDeepFreeze) || + * view.rules().enabled(fixFreezeExploit); + */ + [[maybe_unused]] bool const enforce = + view.rules().enabled(featureDeepFreeze); + + for (auto const& [issue, changes] : balanceChanges_) + { + auto const issuerSle = findIssuer(issue.account, view); + // It should be impossible for the issuer to not be found, but check + // just in case so rippled doesn't crash in release. + if (!issuerSle) + { + XRPL_ASSERT( + enforce, + "ripple::TransfersNotFrozen::finalize : enforce " + "invariant."); + if (enforce) + { + return false; + } + continue; + } + + if (!validateIssuerChanges(issuerSle, changes, tx, j, enforce)) + { + return false; + } + } + + return true; +} + +bool +TransfersNotFrozen::isValidEntry( + std::shared_ptr const& before, + std::shared_ptr const& after) +{ + // `after` can never be null, even if the trust line is deleted. + XRPL_ASSERT( + after, "ripple::TransfersNotFrozen::isValidEntry : valid after."); + if (!after) + { + return false; + } + + if (after->getType() == ltACCOUNT_ROOT) + { + possibleIssuers_.emplace(after->at(sfAccount), after); + return false; + } + + /* While LedgerEntryTypesMatch invariant also checks types, all invariants + * are processed regardless of previous failures. + * + * This type check is still necessary here because it prevents potential + * issues in subsequent processing. + */ + return after->getType() == ltRIPPLE_STATE && + (!before || before->getType() == ltRIPPLE_STATE); +} + +STAmount +TransfersNotFrozen::calculateBalanceChange( + std::shared_ptr const& before, + std::shared_ptr const& after, + bool isDelete) +{ + auto const getBalance = [](auto const& line, auto const& other, bool zero) { + STAmount amt = + line ? line->at(sfBalance) : other->at(sfBalance).zeroed(); + return zero ? amt.zeroed() : amt; + }; + + /* Trust lines can be created dynamically by other transactions such as + * Payment and OfferCreate that cross offers. Such trust line won't be + * created frozen, but the sender might be, so the starting balance must be + * treated as zero. + */ + auto const balanceBefore = getBalance(before, after, false); + + /* Same as above, trust lines can be dynamically deleted, and for frozen + * trust lines, payments not involving the issuer must be blocked. This is + * achieved by treating the final balance as zero when isDelete=true to + * ensure frozen line restrictions are enforced even during deletion. + */ + auto const balanceAfter = getBalance(after, before, isDelete); + + return balanceAfter - balanceBefore; +} + +void +TransfersNotFrozen::recordBalance(Issue const& issue, BalanceChange change) +{ + XRPL_ASSERT( + change.balanceChangeSign, + "ripple::TransfersNotFrozen::recordBalance : valid trustline " + "balance sign."); + auto& changes = balanceChanges_[issue]; + if (change.balanceChangeSign < 0) + changes.senders.emplace_back(std::move(change)); + else + changes.receivers.emplace_back(std::move(change)); +} + +void +TransfersNotFrozen::recordBalanceChanges( + std::shared_ptr const& after, + STAmount const& balanceChange) +{ + auto const balanceChangeSign = balanceChange.signum(); + auto const currency = after->at(sfBalance).getCurrency(); + + // Change from low account's perspective, which is trust line default + recordBalance( + {currency, after->at(sfHighLimit).getIssuer()}, + {after, balanceChangeSign}); + + // Change from high account's perspective, which reverses the sign. + recordBalance( + {currency, after->at(sfLowLimit).getIssuer()}, + {after, -balanceChangeSign}); +} + +std::shared_ptr +TransfersNotFrozen::findIssuer(AccountID const& issuerID, ReadView const& view) +{ + if (auto it = possibleIssuers_.find(issuerID); it != possibleIssuers_.end()) + { + return it->second; + } + + return view.read(keylet::account(issuerID)); +} + +bool +TransfersNotFrozen::validateIssuerChanges( + std::shared_ptr const& issuer, + IssuerChanges const& changes, + STTx const& tx, + beast::Journal const& j, + bool enforce) +{ + if (!issuer) + { + return false; + } + + bool const globalFreeze = issuer->isFlag(lsfGlobalFreeze); + if (changes.receivers.empty() || changes.senders.empty()) + { + /* If there are no receivers, then the holder(s) are returning + * their tokens to the issuer. Likewise, if there are no + * senders, then the issuer is issuing tokens to the holder(s). + * This is allowed regardless of the issuer's freeze flags. (The + * holder may have contradicting freeze flags, but that will be + * checked when the holder is treated as issuer.) + */ + return true; + } + + for (auto const& actors : {changes.senders, changes.receivers}) + { + for (auto const& change : actors) + { + bool const high = change.line->at(sfLowLimit).getIssuer() == + issuer->at(sfAccount); + + if (!validateFrozenState( + change, high, tx, j, enforce, globalFreeze)) + { + return false; + } + } + } + return true; +} + +bool +TransfersNotFrozen::validateFrozenState( + BalanceChange const& change, + bool high, + STTx const& tx, + beast::Journal const& j, + bool enforce, + bool globalFreeze) +{ + bool const freeze = change.balanceChangeSign < 0 && + change.line->isFlag(high ? lsfLowFreeze : lsfHighFreeze); + bool const deepFreeze = + change.line->isFlag(high ? lsfLowDeepFreeze : lsfHighDeepFreeze); + bool const frozen = globalFreeze || deepFreeze || freeze; + + bool const isAMMLine = change.line->isFlag(lsfAMMNode); + + if (!frozen) + { + return true; + } + + // AMMClawbacks are allowed to override some freeze rules + if ((!isAMMLine || globalFreeze) && tx.getTxnType() == ttAMM_CLAWBACK) + { + JLOG(j.debug()) << "Invariant check allowing funds to be moved " + << (change.balanceChangeSign > 0 ? "to" : "from") + << " a frozen trustline for AMMClawback " + << tx.getTransactionID(); + return true; + } + + JLOG(j.fatal()) << "Invariant failed: Attempting to move frozen funds for " + << tx.getTransactionID(); + XRPL_ASSERT( + enforce, + "ripple::TransfersNotFrozen::validateFrozenState : enforce " + "invariant."); + + if (enforce) + { + return false; + } + + return true; +} + +//------------------------------------------------------------------------------ + void ValidNewAccountRoot::visitEntry( bool, diff --git a/src/xrpld/app/tx/detail/InvariantCheck.h b/src/xrpld/app/tx/detail/InvariantCheck.h index 19c4ef3e23..cb06b0fb05 100644 --- a/src/xrpld/app/tx/detail/InvariantCheck.h +++ b/src/xrpld/app/tx/detail/InvariantCheck.h @@ -270,6 +270,114 @@ public: beast::Journal const&); }; +/** + * @brief Invariant: Trust lines with deep freeze flag are not allowed if normal + * freeze flag is not set. + * + * We iterate all the trust lines created by this transaction and ensure + * that they don't have deep freeze flag set without normal freeze flag set. + */ +class NoDeepFreezeTrustLinesWithoutFreeze +{ + bool deepFreezeWithoutFreeze_ = false; + +public: + void + visitEntry( + bool, + std::shared_ptr const&, + std::shared_ptr const&); + + bool + finalize( + STTx const&, + TER const, + XRPAmount const, + ReadView const&, + beast::Journal const&); +}; + +/** + * @brief Invariant: frozen trust line balance change is not allowed. + * + * We iterate all affected trust lines and ensure that they don't have + * unexpected change of balance if they're frozen. + */ +class TransfersNotFrozen +{ + struct BalanceChange + { + std::shared_ptr const line; + int const balanceChangeSign; + }; + + struct IssuerChanges + { + std::vector senders; + std::vector receivers; + }; + + using ByIssuer = std::map; + ByIssuer balanceChanges_; + + std::map const> possibleIssuers_; + +public: + void + visitEntry( + bool, + std::shared_ptr const&, + std::shared_ptr const&); + + bool + finalize( + STTx const&, + TER const, + XRPAmount const, + ReadView const&, + beast::Journal const&); + +private: + bool + isValidEntry( + std::shared_ptr const& before, + std::shared_ptr const& after); + + STAmount + calculateBalanceChange( + std::shared_ptr const& before, + std::shared_ptr const& after, + bool isDelete); + + void + recordBalance(Issue const& issue, BalanceChange change); + + void + recordBalanceChanges( + std::shared_ptr const& after, + STAmount const& balanceChange); + + std::shared_ptr + findIssuer(AccountID const& issuerID, ReadView const& view); + + bool + validateIssuerChanges( + std::shared_ptr const& issuer, + IssuerChanges const& changes, + STTx const& tx, + beast::Journal const& j, + bool enforce); + + bool + validateFrozenState( + BalanceChange const& change, + bool high, + STTx const& tx, + beast::Journal const& j, + bool enforce, + bool globalFreeze); +}; + /** * @brief Invariant: offers should be for non-negative amounts and must not * be XRP to XRP. @@ -518,6 +626,8 @@ using InvariantChecks = std::tuple< XRPBalanceChecks, XRPNotCreated, NoXRPTrustLines, + NoDeepFreezeTrustLinesWithoutFreeze, + TransfersNotFrozen, NoBadOffers, NoZeroEscrow, ValidNewAccountRoot, diff --git a/src/xrpld/app/tx/detail/NFTokenAcceptOffer.cpp b/src/xrpld/app/tx/detail/NFTokenAcceptOffer.cpp index b884a791e7..9ae6616e38 100644 --- a/src/xrpld/app/tx/detail/NFTokenAcceptOffer.cpp +++ b/src/xrpld/app/tx/detail/NFTokenAcceptOffer.cpp @@ -268,6 +268,20 @@ NFTokenAcceptOffer::preclaim(PreclaimContext const& ctx) ctx.j) < needed) return tecINSUFFICIENT_FUNDS; } + + // Make sure that we are allowed to hold what the taker will pay us. + // This is a similar approach taken by usual offers. + if (!needed.native()) + { + auto const result = checkAcceptAsset( + ctx.view, + ctx.flags, + (*so)[sfOwner], + ctx.j, + needed.asset().get()); + if (result != tesSUCCESS) + return result; + } } // Fix a bug where the transfer of an NFToken with a transfer fee could @@ -510,4 +524,62 @@ NFTokenAcceptOffer::doApply() return tecINTERNAL; } +TER +NFTokenAcceptOffer::checkAcceptAsset( + ReadView const& view, + ApplyFlags const flags, + AccountID const id, + beast::Journal const j, + Issue const& issue) +{ + // Only valid for custom currencies + + if (!view.rules().enabled(featureDeepFreeze)) + { + return tesSUCCESS; + } + + XRPL_ASSERT( + !isXRP(issue.currency), + "NFTokenAcceptOffer::checkAcceptAsset : valid to check."); + auto const issuerAccount = view.read(keylet::account(issue.account)); + + if (!issuerAccount) + { + JLOG(j.debug()) + << "delay: can't receive IOUs from non-existent issuer: " + << to_string(issue.account); + + return tecNO_ISSUER; + } + + // An account can not create a trustline to itself, so no line can exist + // to be frozen. Additionally, an issuer can always accept its own + // issuance. + if (issue.account == id) + { + return tesSUCCESS; + } + + auto const trustLine = + view.read(keylet::line(id, issue.account, issue.currency)); + + if (!trustLine) + { + return tesSUCCESS; + } + + // There's no difference which side enacted deep freeze, accepting + // tokens shouldn't be possible. + bool const deepFrozen = + (*trustLine)[sfFlags] & (lsfLowDeepFreeze | lsfHighDeepFreeze); + + if (deepFrozen) + { + return tecFROZEN; + } + + return tesSUCCESS; +} + } // namespace ripple diff --git a/src/xrpld/app/tx/detail/NFTokenAcceptOffer.h b/src/xrpld/app/tx/detail/NFTokenAcceptOffer.h index dff3febbb2..6a594e2b2c 100644 --- a/src/xrpld/app/tx/detail/NFTokenAcceptOffer.h +++ b/src/xrpld/app/tx/detail/NFTokenAcceptOffer.h @@ -44,6 +44,14 @@ private: AccountID const& seller, uint256 const& nfTokenID); + static TER + checkAcceptAsset( + ReadView const& view, + ApplyFlags const flags, + AccountID const id, + beast::Journal const j, + Issue const& issue); + public: static constexpr ConsequencesFactoryType ConsequencesFactory{Normal}; diff --git a/src/xrpld/app/tx/detail/OfferStream.cpp b/src/xrpld/app/tx/detail/OfferStream.cpp index ea18306234..4e1cdd9b23 100644 --- a/src/xrpld/app/tx/detail/OfferStream.cpp +++ b/src/xrpld/app/tx/detail/OfferStream.cpp @@ -273,6 +273,20 @@ TOfferStreamBase::step() continue; } + bool const deepFrozen = isDeepFrozen( + view_, + offer_.owner(), + offer_.issueIn().currency, + offer_.issueIn().account); + if (deepFrozen) + { + JLOG(j_.trace()) + << "Removing deep frozen unfunded offer " << entry->key(); + permRmOffer(entry->key()); + offer_ = TOffer{}; + continue; + } + // Calculate owner funds ownerFunds_ = accountFundsHelper( view_, diff --git a/src/xrpld/app/tx/detail/SetTrust.cpp b/src/xrpld/app/tx/detail/SetTrust.cpp index 954fc6543f..b1e0494ba4 100644 --- a/src/xrpld/app/tx/detail/SetTrust.cpp +++ b/src/xrpld/app/tx/detail/SetTrust.cpp @@ -26,6 +26,42 @@ #include #include +namespace { + +uint32_t +computeFreezeFlags( + uint32_t uFlags, + bool bHigh, + bool bNoFreeze, + bool bSetFreeze, + bool bClearFreeze, + bool bSetDeepFreeze, + bool bClearDeepFreeze) +{ + if (bSetFreeze && !bClearFreeze && !bNoFreeze) + { + uFlags |= (bHigh ? ripple::lsfHighFreeze : ripple::lsfLowFreeze); + } + else if (bClearFreeze && !bSetFreeze) + { + uFlags &= ~(bHigh ? ripple::lsfHighFreeze : ripple::lsfLowFreeze); + } + if (bSetDeepFreeze && !bClearDeepFreeze && !bNoFreeze) + { + uFlags |= + (bHigh ? ripple::lsfHighDeepFreeze : ripple::lsfLowDeepFreeze); + } + else if (bClearDeepFreeze && !bSetDeepFreeze) + { + uFlags &= + ~(bHigh ? ripple::lsfHighDeepFreeze : ripple::lsfLowDeepFreeze); + } + + return uFlags; +} + +} // namespace + namespace ripple { NotTEC @@ -45,6 +81,16 @@ SetTrust::preflight(PreflightContext const& ctx) return temINVALID_FLAG; } + if (!ctx.rules.enabled(featureDeepFreeze)) + { + // Even though the deep freeze flags are included in the + // `tfTrustSetMask`, they are not valid if the amendment is not enabled. + if (uTxFlags & (tfSetDeepFreeze | tfClearDeepFreeze)) + { + return temINVALID_FLAG; + } + } + STAmount const saLimitAmount(tx.getFieldAmount(sfLimitAmount)); if (!isLegalNet(saLimitAmount)) @@ -182,6 +228,58 @@ SetTrust::preclaim(PreclaimContext const& ctx) } } + // Checking all freeze/deep freeze flag invariants. + if (ctx.view.rules().enabled(featureDeepFreeze)) + { + bool const bNoFreeze = sle->isFlag(lsfNoFreeze); + bool const bSetFreeze = (uTxFlags & tfSetFreeze); + bool const bSetDeepFreeze = (uTxFlags & tfSetDeepFreeze); + + if (bNoFreeze && (bSetFreeze || bSetDeepFreeze)) + { + // Cannot freeze the trust line if NoFreeze is set + return tecNO_PERMISSION; + } + + bool const bClearFreeze = (uTxFlags & tfClearFreeze); + bool const bClearDeepFreeze = (uTxFlags & tfClearDeepFreeze); + if ((bSetFreeze || bSetDeepFreeze) && + (bClearFreeze || bClearDeepFreeze)) + { + // Freezing and unfreezing in the same transaction should be + // illegal + return tecNO_PERMISSION; + } + + bool const bHigh = id > uDstAccountID; + // Fetching current state of trust line + auto const sleRippleState = + ctx.view.read(keylet::line(id, uDstAccountID, currency)); + std::uint32_t uFlags = + sleRippleState ? sleRippleState->getFieldU32(sfFlags) : 0u; + // Computing expected trust line state + uFlags = computeFreezeFlags( + uFlags, + bHigh, + bNoFreeze, + bSetFreeze, + bClearFreeze, + bSetDeepFreeze, + bClearDeepFreeze); + + auto const frozen = uFlags & (bHigh ? lsfHighFreeze : lsfLowFreeze); + auto const deepFrozen = + uFlags & (bHigh ? lsfHighDeepFreeze : lsfLowDeepFreeze); + + // Trying to set deep freeze on not already frozen trust line must + // fail. This also checks that clearing normal freeze while deep + // frozen must not work + if (deepFrozen && !frozen) + { + return tecNO_PERMISSION; + } + } + return tesSUCCESS; } @@ -197,7 +295,7 @@ SetTrust::doApply() Currency const currency(saLimitAmount.getCurrency()); AccountID uDstAccountID(saLimitAmount.getIssuer()); - // true, iff current is high account. + // true, if current is high account. bool const bHigh = account_ > uDstAccountID; auto const sle = view().peek(keylet::account(account_)); @@ -242,13 +340,15 @@ SetTrust::doApply() bool const bClearNoRipple = (uTxFlags & tfClearNoRipple); bool const bSetFreeze = (uTxFlags & tfSetFreeze); bool const bClearFreeze = (uTxFlags & tfClearFreeze); + bool const bSetDeepFreeze = (uTxFlags & tfSetDeepFreeze); + bool const bClearDeepFreeze = (uTxFlags & tfClearDeepFreeze); auto viewJ = ctx_.app.journal("View"); - // Trust lines to self are impossible but because of the old bug there are - // two on 19-02-2022. This code was here to allow those trust lines to be - // deleted. The fixTrustLinesToSelf fix amendment will remove them when it - // enables so this code will no longer be needed. + // Trust lines to self are impossible but because of the old bug there + // are two on 19-02-2022. This code was here to allow those trust lines + // to be deleted. The fixTrustLinesToSelf fix amendment will remove them + // when it enables so this code will no longer be needed. if (!view().rules().enabled(fixTrustLinesToSelf) && account_ == uDstAccountID) { @@ -408,14 +508,16 @@ SetTrust::doApply() uFlagsOut &= ~(bHigh ? lsfHighNoRipple : lsfLowNoRipple); } - if (bSetFreeze && !bClearFreeze && !sle->isFlag(lsfNoFreeze)) - { - uFlagsOut |= (bHigh ? lsfHighFreeze : lsfLowFreeze); - } - else if (bClearFreeze && !bSetFreeze) - { - uFlagsOut &= ~(bHigh ? lsfHighFreeze : lsfLowFreeze); - } + // Have to use lsfNoFreeze to maintain pre-deep freeze behavior + bool const bNoFreeze = sle->isFlag(lsfNoFreeze); + uFlagsOut = computeFreezeFlags( + uFlagsOut, + bHigh, + bNoFreeze, + bSetFreeze, + bClearFreeze, + bSetDeepFreeze, + bClearDeepFreeze); if (QUALITY_ONE == uLowQualityOut) uLowQualityOut = 0; @@ -498,8 +600,8 @@ SetTrust::doApply() // Reserve is not scaled by load. else if (bReserveIncrease && mPriorBalance < reserveCreate) { - JLOG(j_.trace()) - << "Delay transaction: Insufficent reserve to add trust line."; + JLOG(j_.trace()) << "Delay transaction: Insufficent reserve to " + "add trust line."; // Another transaction could provide XRP to the account and then // this transaction would succeed. @@ -515,17 +617,18 @@ SetTrust::doApply() // Line does not exist. else if ( !saLimitAmount && // Setting default limit. - (!bQualityIn || !uQualityIn) && // Not setting quality in or setting - // default quality in. - (!bQualityOut || !uQualityOut) && // Not setting quality out or setting - // default quality out. + (!bQualityIn || !uQualityIn) && // Not setting quality in or + // setting default quality in. + (!bQualityOut || !uQualityOut) && // Not setting quality out or + // setting default quality out. (!bSetAuth)) { JLOG(j_.trace()) << "Redundant: Setting non-existent ripple line to defaults."; return tecNO_LINE_REDUNDANT; } - else if (mPriorBalance < reserveCreate) // Reserve is not scaled by load. + else if (mPriorBalance < reserveCreate) // Reserve is not scaled by + // load. { JLOG(j_.trace()) << "Delay transaction: Line does not exist. " "Insufficent reserve to create line."; @@ -555,6 +658,7 @@ SetTrust::doApply() bSetAuth, bSetNoRipple && !bClearNoRipple, bSetFreeze && !bClearFreeze, + bSetDeepFreeze, saBalance, saLimitAllow, // Limit for who is being charged. uQualityIn, diff --git a/src/xrpld/ledger/View.h b/src/xrpld/ledger/View.h index 7402775248..b964fc0ee7 100644 --- a/src/xrpld/ledger/View.h +++ b/src/xrpld/ledger/View.h @@ -153,6 +153,13 @@ isFrozen(ReadView const& view, AccountID const& account, Asset const& asset) asset.value()); } +[[nodiscard]] bool +isDeepFrozen( + ReadView const& view, + AccountID const& account, + Currency const& currency, + AccountID const& issuer); + // Returns the amount an account can spend without going into debt. // // <-- saAmount: amount of currency held by account. May be negative. @@ -438,6 +445,7 @@ trustCreate( const bool bAuth, // --> authorize account. const bool bNoRipple, // --> others cannot ripple through const bool bFreeze, // --> funds cannot leave + bool bDeepFreeze, // --> can neither receive nor send funds STAmount const& saBalance, // --> balance of account being set. // Issuer should be noAccount() STAmount const& saLimit, // --> limit for account being set. diff --git a/src/xrpld/ledger/detail/View.cpp b/src/xrpld/ledger/detail/View.cpp index ebf307f153..1422a50a3a 100644 --- a/src/xrpld/ledger/detail/View.cpp +++ b/src/xrpld/ledger/detail/View.cpp @@ -267,6 +267,32 @@ isFrozen( isIndividualFrozen(view, account, mptIssue); } +bool +isDeepFrozen( + ReadView const& view, + AccountID const& account, + Currency const& currency, + AccountID const& issuer) +{ + if (isXRP(currency)) + { + return false; + } + + if (issuer == account) + { + return false; + } + + auto const sle = view.read(keylet::line(account, issuer, currency)); + if (!sle) + { + return false; + } + + return sle->isFlag(lsfHighDeepFreeze) || sle->isFlag(lsfLowDeepFreeze); +} + STAmount accountHolds( ReadView const& view, @@ -284,17 +310,25 @@ accountHolds( // IOU: Return balance on trust line modulo freeze auto const sle = view.read(keylet::line(account, issuer, currency)); - if (!sle) - { - amount.clear(Issue{currency, issuer}); - } - else if ( - (zeroIfFrozen == fhZERO_IF_FROZEN) && - isFrozen(view, account, currency, issuer)) - { - amount.clear(Issue{currency, issuer}); - } - else + auto const allowBalance = [&]() { + if (!sle) + { + return false; + } + + if (zeroIfFrozen == fhZERO_IF_FROZEN) + { + if (isFrozen(view, account, currency, issuer) || + isDeepFrozen(view, account, currency, issuer)) + { + return false; + } + } + + return true; + }(); + + if (allowBalance) { amount = sle->getFieldAmount(sfBalance); if (account > issuer) @@ -304,6 +338,11 @@ accountHolds( } amount.setIssuer(issuer); } + else + { + amount.clear(Issue{currency, issuer}); + } + JLOG(j.trace()) << "accountHolds:" << " account=" << to_string(account) << " amount=" << amount.getFullText(); @@ -863,6 +902,7 @@ trustCreate( const bool bAuth, // --> authorize account. const bool bNoRipple, // --> others cannot ripple through const bool bFreeze, // --> funds cannot leave + bool bDeepFreeze, // --> can neither receive nor send funds STAmount const& saBalance, // --> balance of account being set. // Issuer should be noAccount() STAmount const& saLimit, // --> limit for account being set. @@ -944,7 +984,11 @@ trustCreate( } if (bFreeze) { - uFlags |= (!bSetHigh ? lsfLowFreeze : lsfHighFreeze); + uFlags |= (bSetHigh ? lsfHighFreeze : lsfLowFreeze); + } + if (bDeepFreeze) + { + uFlags |= (bSetHigh ? lsfHighDeepFreeze : lsfLowDeepFreeze); } if ((slePeer->getFlags() & lsfDefaultRipple) == 0) @@ -1189,6 +1233,7 @@ rippleCreditIOU( false, noRipple, false, + false, saBalance, saReceiverLimit, 0, @@ -1688,6 +1733,7 @@ issueIOU( false, noRipple, false, + false, final_balance, limit, 0, diff --git a/src/xrpld/rpc/handlers/AccountLines.cpp b/src/xrpld/rpc/handlers/AccountLines.cpp index e2e6ce19de..5170342eb9 100644 --- a/src/xrpld/rpc/handlers/AccountLines.cpp +++ b/src/xrpld/rpc/handlers/AccountLines.cpp @@ -62,6 +62,10 @@ addLine(Json::Value& jsonLines, RPCTrustLine const& line) jPeer[jss::freeze] = true; if (line.getFreezePeer()) jPeer[jss::freeze_peer] = true; + if (line.getDeepFreeze()) + jPeer[jss::deep_freeze] = true; + if (line.getDeepFreezePeer()) + jPeer[jss::deep_freeze_peer] = true; } // {