Code cleanup in SetTrust

This commit is contained in:
Bronek Kozicki
2025-05-02 11:38:08 +01:00
parent 1925ceace6
commit a87f58eb22

View File

@@ -179,14 +179,16 @@ SetTrust::preclaim(PreclaimContext const& ctx)
// This might be nullptr // This might be nullptr
auto const sleDst = ctx.view.read(keylet::account(uDstAccountID)); auto const sleDst = ctx.view.read(keylet::account(uDstAccountID));
if ((ctx.view.rules().enabled(featureDisallowIncoming) ||
ammEnabled(ctx.view.rules()) ||
ctx.view.rules().enabled(featureSingleAssetVault)) &&
sleDst == nullptr)
return tecNO_DST;
// If the destination has opted to disallow incoming trustlines // If the destination has opted to disallow incoming trustlines
// then honour that flag // then honour that flag
if (ctx.view.rules().enabled(featureDisallowIncoming)) if (ctx.view.rules().enabled(featureDisallowIncoming))
{ {
if (!sleDst)
return tecNO_DST;
if (sleDst->getFlags() & lsfDisallowIncomingTrustline) if (sleDst->getFlags() & lsfDisallowIncomingTrustline)
{ {
// The original implementation of featureDisallowIncoming was // The original implementation of featureDisallowIncoming was
@@ -204,18 +206,22 @@ SetTrust::preclaim(PreclaimContext const& ctx)
} }
} }
// If destination is AMM and the trustline doesn't exist then only // In general, trust lines to pseudo accounts are not permitted, unless
// allow SetTrust if the asset is AMM LP token and AMM is not // enabled in the code section below, for specific cases. This block is not
// in empty state. // amendment-gated because sleDst will not have a pseudo-account designator
if (ammEnabled(ctx.view.rules())) // field populated, unless the appropriate amendment was already enabled.
if (sleDst && isPseudoAccount(sleDst))
{ {
if (!sleDst) // If destination is AMM and the trustline doesn't exist then only allow
return tecNO_DST; // SetTrust if the asset is AMM LP token and AMM is not in empty state.
if (sleDst->isFieldPresent(sfAMMID))
if (sleDst->isFieldPresent(sfAMMID) &&
!ctx.view.read(keylet::line(id, uDstAccountID, currency)))
{ {
if (auto const ammSle = if (ctx.view.read(keylet::line(id, uDstAccountID, currency)))
{
// pass
}
else if (
auto const ammSle =
ctx.view.read({ltAMM, sleDst->getFieldH256(sfAMMID)})) ctx.view.read({ltAMM, sleDst->getFieldH256(sfAMMID)}))
{ {
if (auto const lpTokens = if (auto const lpTokens =
@@ -226,29 +232,19 @@ SetTrust::preclaim(PreclaimContext const& ctx)
return tecNO_PERMISSION; return tecNO_PERMISSION;
} }
else else
return tecINTERNAL; return tecINTERNAL; // LCOV_EXCL_LINE
} }
} else if (sleDst->isFieldPresent(sfVaultID))
// If destination is Vault, allow changes to the existing trustline.
if (ctx.view.rules().enabled(featureSingleAssetVault))
{
if (!sleDst)
return tecNO_DST;
bool const isVault = sleDst->isFieldPresent(sfVaultID);
if (isVault &&
!ctx.view.read(keylet::line(id, uDstAccountID, currency)))
return tecNO_PERMISSION;
else if (isVault)
{ {
if (ctx.view.read(keylet::vault(sleDst->getFieldH256(sfVaultID)))) if (ctx.view.read(keylet::line(id, uDstAccountID, currency)))
{ {
// Allow updating the state of existing trust line for the vault // pass
} }
else else
return tecINTERNAL; return tecNO_PERMISSION;
} }
else
return tecPSEUDO_ACCOUNT;
} }
// Checking all freeze/deep freeze flag invariants. // Checking all freeze/deep freeze flag invariants.