feat: LPT freeze (#1840)

Fixes #1827
This commit is contained in:
Shawn Xie
2025-02-24 10:39:11 -05:00
committed by GitHub
parent f577139f70
commit c9e8330e0a
30 changed files with 765 additions and 139 deletions

View File

@@ -17,12 +17,14 @@
*/
//==============================================================================
#include "data/AmendmentCenter.hpp"
#include "data/Types.hpp"
#include "rpc/Errors.hpp"
#include "rpc/JS.hpp"
#include "rpc/RPCHelpers.hpp"
#include "rpc/common/Types.hpp"
#include "util/AsioContextTestFixture.hpp"
#include "util/MockAmendmentCenter.hpp"
#include "util/MockBackendTestFixture.hpp"
#include "util/MockPrometheus.hpp"
#include "util/NameGenerator.hpp"
@@ -38,6 +40,7 @@
#include <xrpl/basics/base_uint.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/LedgerFormats.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/protocol/STObject.h>
#include <xrpl/protocol/UintTypes.h>
@@ -63,6 +66,10 @@ constexpr auto kACCOUNT2 = "rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun";
constexpr auto kINDEX1 = "E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC321";
constexpr auto kINDEX2 = "E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC322";
constexpr auto kTXN_ID = "E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC321";
constexpr auto kAMM_ACCOUNT = "rnW8FAPgpQgA6VoESnVrUVJHBdq9QAtRZs";
constexpr auto kISSUER = "rK9DrarGKnVEo2nYp5MfVRXRYf5yRX3mwD";
constexpr auto kLPTOKEN_CURRENCY = "037C35306B24AAB7FF90848206E003279AA47090";
constexpr auto kAMM_ID = 54321;
} // namespace
@@ -77,6 +84,9 @@ class RPCHelpersTest : public util::prometheus::WithPrometheus, public MockBacke
{
SyncAsioContextTest::TearDown();
}
protected:
StrictMockAmendmentCenterSharedPtr mockAmendmentCenterPtr_;
};
TEST_F(RPCHelpersTest, TraverseOwnedNodesMarkerInvalidIndexNotHex)
@@ -547,6 +557,247 @@ TEST_F(RPCHelpersTest, ParseIssue)
);
}
TEST_F(RPCHelpersTest, AccountHoldsFixLPTAmendmentDisabled)
{
auto ammAccount = getAccountIdWithString(kAMM_ACCOUNT);
auto account = getAccountIdWithString(kACCOUNT);
auto const lptRippleState = createRippleStateLedgerObject(
kLPTOKEN_CURRENCY, kAMM_ACCOUNT, 100, kACCOUNT, 100, kAMM_ACCOUNT, 100, kTXN_ID, 3
);
auto const lptRippleStateKk = ripple::keylet::line(ammAccount, account, ripple::to_currency(kLPTOKEN_CURRENCY)).key;
// trustline fetched twice. once in accountHolds and once in isFrozen
EXPECT_CALL(*backend_, doFetchLedgerObject(lptRippleStateKk, testing::_, testing::_))
.Times(2)
.WillRepeatedly(Return(lptRippleState.getSerializer().peekData()));
auto const ammID = ripple::uint256{kAMM_ID};
auto const ammAccountKk = ripple::keylet::account(ammAccount).key;
auto const ammAccountRoot = createAccountRootObject(kAMM_ACCOUNT, 0, 2, 200, 2, kINDEX1, 2, 0, ammID);
EXPECT_CALL(*backend_, doFetchLedgerObject(ammAccountKk, testing::_, testing::_))
.WillOnce(Return(ammAccountRoot.getSerializer().peekData()));
EXPECT_CALL(*mockAmendmentCenterPtr_, isEnabled(testing::_, Amendments::fixFrozenLPTokenTransfer, testing::_))
.WillOnce(Return(false));
boost::asio::spawn(ctx_, [&, this](boost::asio::yield_context yield) {
auto ret = accountHolds(
*backend_,
*mockAmendmentCenterPtr_,
0,
account,
ripple::to_currency(kLPTOKEN_CURRENCY),
ammAccount,
true,
yield
);
EXPECT_EQ(ret.mantissa(), 1000000000000000);
});
ctx_.run();
}
TEST_F(RPCHelpersTest, AccountHoldsLPTokenNotAMMAccount)
{
auto account = getAccountIdWithString(kACCOUNT);
auto account2 = getAccountIdWithString(kACCOUNT2);
auto const usdRippleState =
createRippleStateLedgerObject("USD", kACCOUNT2, 100, kACCOUNT, 100, kACCOUNT2, 100, kTXN_ID, 3);
auto const usdRippleStateKk = ripple::keylet::line(account2, account, ripple::to_currency("USD")).key;
// trustline fetched twice. once in accountHolds and once in isFrozen
EXPECT_CALL(*backend_, doFetchLedgerObject(usdRippleStateKk, testing::_, testing::_))
.Times(2)
.WillRepeatedly(Return(usdRippleState.getSerializer().peekData()));
EXPECT_CALL(*mockAmendmentCenterPtr_, isEnabled(testing::_, Amendments::fixFrozenLPTokenTransfer, testing::_))
.WillOnce(Return(true));
auto const account2Kk = ripple::keylet::account(account2).key;
auto const account2Root = createAccountRootObject(kACCOUNT2, 0, 2, 200, 2, kINDEX1, 2, 0);
EXPECT_CALL(*backend_, doFetchLedgerObject(account2Kk, testing::_, testing::_))
.Times(2)
.WillRepeatedly(Return(account2Root.getSerializer().peekData()));
boost::asio::spawn(ctx_, [&, this](boost::asio::yield_context yield) {
auto ret = accountHolds(
*backend_, *mockAmendmentCenterPtr_, 0, account, ripple::to_currency("USD"), account2, true, yield
);
EXPECT_EQ(ret.mantissa(), 1000000000000000);
});
ctx_.run();
}
TEST_F(RPCHelpersTest, AccountHoldsLPTokenAsset1Frozen)
{
auto ammAccount = getAccountIdWithString(kAMM_ACCOUNT);
auto account = getAccountIdWithString(kACCOUNT);
auto issuer = getAccountIdWithString(kISSUER);
auto const lptRippleState = createRippleStateLedgerObject(
kLPTOKEN_CURRENCY, kAMM_ACCOUNT, 100, kACCOUNT, 100, kAMM_ACCOUNT, 100, kTXN_ID, 3
);
auto const lptRippleStateKk = ripple::keylet::line(ammAccount, account, ripple::to_currency(kLPTOKEN_CURRENCY)).key;
// trustline fetched twice. once in accountHolds and once in isFrozen
EXPECT_CALL(*backend_, doFetchLedgerObject(lptRippleStateKk, testing::_, testing::_))
.Times(2)
.WillRepeatedly(Return(lptRippleState.getSerializer().peekData()));
EXPECT_CALL(*mockAmendmentCenterPtr_, isEnabled(testing::_, Amendments::fixFrozenLPTokenTransfer, testing::_))
.WillOnce(Return(true));
auto const ammID = ripple::uint256{kAMM_ID};
auto const ammAccountKk = ripple::keylet::account(ammAccount).key;
auto const ammAccountRoot = createAccountRootObject(kAMM_ACCOUNT, 0, 2, 200, 2, kINDEX1, 2, 0, ammID);
// accountroot fetched twice, once in isFrozen, once in accountHolds
EXPECT_CALL(*backend_, doFetchLedgerObject(ammAccountKk, testing::_, testing::_))
.Times(2)
.WillRepeatedly(Return(ammAccountRoot.getSerializer().peekData()));
auto const amm = createAmmObject(kAMM_ACCOUNT, "USD", kISSUER, "XRP", ripple::toBase58(ripple::xrpAccount()));
EXPECT_CALL(*backend_, doFetchLedgerObject(ripple::keylet::amm(ammID).key, testing::_, testing::_))
.Times(1)
.WillOnce(Return(amm.getSerializer().peekData()));
auto const issuerKk = ripple::keylet::account(issuer).key;
auto const issuerAccountRoot = createAccountRootObject(kISSUER, ripple::lsfGlobalFreeze, 2, 200, 2, kINDEX1, 2, 0);
EXPECT_CALL(*backend_, doFetchLedgerObject(issuerKk, testing::_, testing::_))
.WillOnce(Return(issuerAccountRoot.getSerializer().peekData()));
boost::asio::spawn(ctx_, [&, this](boost::asio::yield_context yield) {
auto ret = accountHolds(
*backend_,
*mockAmendmentCenterPtr_,
0,
account,
ripple::to_currency(kLPTOKEN_CURRENCY),
ammAccount,
true,
yield
);
EXPECT_EQ(ret.mantissa(), 0);
});
ctx_.run();
}
TEST_F(RPCHelpersTest, AccountHoldsLPTokenAsset2Frozen)
{
auto ammAccount = getAccountIdWithString(kAMM_ACCOUNT);
auto account = getAccountIdWithString(kACCOUNT);
auto issuer = getAccountIdWithString(kISSUER);
auto const lptRippleState = createRippleStateLedgerObject(
kLPTOKEN_CURRENCY, kAMM_ACCOUNT, 100, kACCOUNT, 100, kAMM_ACCOUNT, 100, kTXN_ID, 3
);
auto const lptRippleStateKk = ripple::keylet::line(ammAccount, account, ripple::to_currency(kLPTOKEN_CURRENCY)).key;
// trustline fetched twice. once in accountHolds and once in isFrozen
EXPECT_CALL(*backend_, doFetchLedgerObject(lptRippleStateKk, testing::_, testing::_)).Times(2);
ON_CALL(*backend_, doFetchLedgerObject(lptRippleStateKk, testing::_, testing::_))
.WillByDefault(testing::Return(lptRippleState.getSerializer().peekData()));
EXPECT_CALL(*mockAmendmentCenterPtr_, isEnabled(testing::_, Amendments::fixFrozenLPTokenTransfer, testing::_))
.WillOnce(testing::Return(true));
auto const ammID = ripple::uint256{kAMM_ID};
auto const ammAccountKk = ripple::keylet::account(ammAccount).key;
auto const ammAccountRoot = createAccountRootObject(kAMM_ACCOUNT, 0, 2, 200, 2, kINDEX1, 2, 0, ammID);
// accountroot fetched twice, once in isFrozen, once in accountHolds
EXPECT_CALL(*backend_, doFetchLedgerObject(ammAccountKk, testing::_, testing::_))
.Times(2)
.WillRepeatedly(Return(ammAccountRoot.getSerializer().peekData()));
auto const amm = createAmmObject(kAMM_ACCOUNT, "XRP", ripple::toBase58(ripple::xrpAccount()), "USD", kISSUER);
EXPECT_CALL(*backend_, doFetchLedgerObject(ripple::keylet::amm(ammID).key, testing::_, testing::_))
.WillOnce(Return(amm.getSerializer().peekData()));
auto const issuerKk = ripple::keylet::account(issuer).key;
auto const issuerAccountRoot = createAccountRootObject(kISSUER, ripple::lsfGlobalFreeze, 2, 200, 2, kINDEX1, 2, 0);
EXPECT_CALL(*backend_, doFetchLedgerObject(issuerKk, testing::_, testing::_))
.WillOnce(Return(issuerAccountRoot.getSerializer().peekData()));
boost::asio::spawn(ctx_, [&, this](boost::asio::yield_context yield) {
auto ret = accountHolds(
*backend_,
*mockAmendmentCenterPtr_,
0,
account,
ripple::to_currency(kLPTOKEN_CURRENCY),
ammAccount,
true,
yield
);
EXPECT_EQ(ret.mantissa(), 0);
});
ctx_.run();
}
TEST_F(RPCHelpersTest, AccountHoldsLPTokenUnfrozen)
{
auto ammAccount = getAccountIdWithString(kAMM_ACCOUNT);
auto account = getAccountIdWithString(kACCOUNT);
auto issuer = getAccountIdWithString(kISSUER);
auto const lptRippleState = createRippleStateLedgerObject(
kLPTOKEN_CURRENCY, kAMM_ACCOUNT, 100, kACCOUNT, 100, kAMM_ACCOUNT, 100, kTXN_ID, 3
);
auto const lptRippleStateKk = ripple::keylet::line(ammAccount, account, ripple::to_currency(kLPTOKEN_CURRENCY)).key;
// trustline fetched twice. once in accountHolds and once in isFrozen
EXPECT_CALL(*backend_, doFetchLedgerObject(lptRippleStateKk, testing::_, testing::_))
.Times(2)
.WillRepeatedly(Return(lptRippleState.getSerializer().peekData()));
EXPECT_CALL(*mockAmendmentCenterPtr_, isEnabled(testing::_, Amendments::fixFrozenLPTokenTransfer, testing::_))
.WillOnce(Return(true));
auto const ammID = ripple::uint256{kAMM_ID};
auto const ammAccountKk = ripple::keylet::account(ammAccount).key;
auto const ammAccountRoot = createAccountRootObject(kAMM_ACCOUNT, 0, 2, 200, 2, kINDEX1, 2, 0, ammID);
// accountroot fetched twice, once in isFrozen, once in accountHolds
EXPECT_CALL(*backend_, doFetchLedgerObject(ammAccountKk, testing::_, testing::_))
.Times(2)
.WillRepeatedly(Return(ammAccountRoot.getSerializer().peekData()));
auto const amm = createAmmObject(kAMM_ACCOUNT, "XRP", ripple::toBase58(ripple::xrpAccount()), "USD", kISSUER);
EXPECT_CALL(*backend_, doFetchLedgerObject(ripple::keylet::amm(ammID).key, testing::_, testing::_))
.WillOnce(Return(amm.getSerializer().peekData()));
auto const issuerKk = ripple::keylet::account(issuer).key;
auto const issuerAccountRoot = createAccountRootObject(kISSUER, 0, 2, 200, 2, kINDEX1, 2, 0);
EXPECT_CALL(*backend_, doFetchLedgerObject(issuerKk, testing::_, testing::_))
.WillOnce(Return(issuerAccountRoot.getSerializer().peekData()));
auto const usdRippleState =
createRippleStateLedgerObject("USD", kISSUER, 100, kACCOUNT, 100, kISSUER, 100, kTXN_ID, 3);
auto const usdRippleStateKk = ripple::keylet::line(issuer, account, ripple::to_currency("USD")).key;
EXPECT_CALL(*backend_, doFetchLedgerObject(usdRippleStateKk, testing::_, testing::_))
.WillOnce(Return(usdRippleState.getSerializer().peekData()));
boost::asio::spawn(ctx_, [&, this](boost::asio::yield_context yield) {
auto ret = accountHolds(
*backend_,
*mockAmendmentCenterPtr_,
0,
account,
ripple::to_currency(kLPTOKEN_CURRENCY),
ammAccount,
true,
yield
);
EXPECT_EQ(ret.mantissa(), 1000000000000000);
});
ctx_.run();
}
struct IsAdminCmdParamTestCaseBundle {
std::string testName;
std::string method;