Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thejohnfreeman committed Oct 17, 2024
1 parent 041f849 commit 846d0e7
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 23 deletions.
3 changes: 3 additions & 0 deletions include/xrpl/protocol/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ std::size_t constexpr maxMPTokenMetadataLength = 1024;
/** The maximum amount of MPTokenIssuance */
std::uint64_t constexpr maxMPTokenAmount = 0x7FFF'FFFF'FFFF'FFFFull;

/** The maximum length of MPTokenMetadata */
std::size_t constexpr maxVaultDataLength = 256;

/** A ledger index. */
using LedgerIndex = std::uint32_t;

Expand Down
30 changes: 15 additions & 15 deletions src/test/app/Vault_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class Vault_test : public beast::unit_test::suite
Asset asset = issuer["IOU"];
auto tx = vault::create({.owner = owner, .asset = asset});

SUBCASE("happy path")
{
env(tx, fee);
env.close();
}

SUBCASE("global freeze")
{
env(fset(issuer, asfGlobalFreeze));
Expand All @@ -55,14 +61,8 @@ class Vault_test : public beast::unit_test::suite

SUBCASE("data too large")
{
// tx[sfData] = blob(260)
// env(tx, ter(tecFROZEN));
env.close();
}

SUBCASE("success")
{
env(tx, fee);
tx[sfData] = blob257;
env(tx, fee, ter(temMALFORMED));
env.close();
}
}
Expand All @@ -74,17 +74,17 @@ class Vault_test : public beast::unit_test::suite
Asset asset = mptt.issuanceID();
auto tx = vault::create({.owner = owner, .asset = asset});

SUBCASE("metadata too large")
SUBCASE("happy path")
{
// tx[sfMPTokenMetadata] = blob(1100);
// env(tx, ter(???));
// env.close();
env(tx, fee);
env.close();
}

SUBCASE("create")
SUBCASE("metadata too large")
{
// env(tx);
// env.close();
// tx[sfMPTokenMetadata] = blob1025;
// env(tx, fee, ter(temMALFORMED));
env.close();
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/jtx/Env.h
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,9 @@ Env::rpc(std::string const& cmd, Args&&... args)
std::forward<Args>(args)...);
}

extern std::string blob257;
extern std::string blob1025;

} // namespace jtx
} // namespace test
} // namespace ripple
Expand Down
5 changes: 4 additions & 1 deletion src/test/jtx/impl/Env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,10 @@ Env::disableFeature(uint256 const feature)
app().config().features.erase(feature);
}

} // namespace jtx
// The strings are hexadecimal. 1 byte = 2 hexadecimal characters.
std::string blob257(514, 'A');
std::string blob1025(2050, 'B');

} // namespace jtx
} // namespace test
} // namespace ripple
20 changes: 16 additions & 4 deletions src/xrpld/app/tx/detail/VaultCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ VaultCreate::preflight(PreflightContext const& ctx)
if (!ctx.rules.enabled(featureSingleAssetVault))
return temDISABLED;

if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
return ret;
if (auto const ter = preflight1(ctx))
return ter;

if (auto const data = ctx.tx[~sfData])
{
if (data->length() > maxVaultDataLength)
return temMALFORMED;
}

return preflight2(ctx);
}
Expand Down Expand Up @@ -63,20 +69,25 @@ VaultCreate::doApply()
auto sequence = tx.getSequence();

auto vault = std::make_shared<SLE>(keylet::vault(owner, sequence));
if (auto ter = dirLink(view(), owner, vault); !isTesSuccess(ter))
if (auto ter = dirLink(view(), owner, vault))
return ter;

auto maybePseudo = createPseudoAccount(view(), vault->key());
if (!maybePseudo)
return maybePseudo.error();
auto& pseudo = *maybePseudo;
// TODO: create empty MPToken or RippleState for Asset.
auto pseudoId = pseudo->at(sfAccount);

if (auto ter = enableHolding(view(), pseudoId, tx[sfAsset], j_))
return ter;

auto txFlags = tx.getFlags();
std::uint32_t mptFlags = 0;
if (!(txFlags & tfVaultShareNonTransferable))
mptFlags |= (lsfMPTCanEscrow | lsfMPTCanTrade | lsfMPTCanTransfer);
if (txFlags & tfVaultPrivate)
mptFlags |= lsfMPTRequireAuth;

auto maybeShare = MPTokenIssuanceCreate::create(
view(),
j_,
Expand All @@ -89,6 +100,7 @@ VaultCreate::doApply()
if (!maybeShare)
return maybeShare.error();
auto& share = *maybeShare;

vault->at(sfFlags) = txFlags & tfVaultPrivate;
vault->at(sfSequence) = sequence;
vault->at(sfOwner) = owner;
Expand Down
6 changes: 5 additions & 1 deletion src/xrpld/ledger/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,11 @@ trustDelete(
* and create an MPToken.
*/
[[nodiscard]] TER
enableHolding(ApplyView& view, AccountID const& account, Asset const& asset);
enableHolding(
ApplyView& view,
AccountID const& account,
Asset const& asset,
beast::Journal journal);

/** Delete an offer.
Expand Down
35 changes: 33 additions & 2 deletions src/xrpld/ledger/detail/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,40 @@ trustDelete(
}

[[nodiscard]] TER
enableHolding(ApplyView& view, AccountID const& account, Asset const& asset)
enableHolding(
ApplyView& view,
AccountID const& account,
Asset const& asset,
beast::Journal journal)
{
return tesSUCCESS;
// Every account can hold XRP by default.
if (asset.native())
return tesSUCCESS;

if (asset.holds<Issue>())
{
auto const& issue = asset.get<Issue>();
auto const& issuer = issue.getIssuer();
if (isGlobalFrozen(view, issuer))
return tecFROZEN;
return rippleCredit(
view,
issuer,
account,
STAmount{asset},
/*bCheckIssuer=*/false,
journal);
}

if (asset.holds<MPTIssue>())
{
auto const& mptIssue = asset.get<MPTIssue>();
return rippleCreditMPT(
view, mptIssue.getIssuer(), account, STAmount{asset}, journal);
}

// Should be unreachable.
return tecINTERNAL;
}

TER
Expand Down

0 comments on commit 846d0e7

Please sign in to comment.