Skip to content

Commit 0ab7922

Browse files
committed
Code cleanup
1 parent 20ce0e7 commit 0ab7922

File tree

13 files changed

+85
-81
lines changed

13 files changed

+85
-81
lines changed

include/xrpl/protocol/Protocol.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ std::uint8_t constexpr vaultStrategyFirstComeFirstServe = 1;
124124

125125
/** Maximum recursion depth for vault shares being put as an asset inside
126126
* another vault; counted from 0 */
127-
std::uint8_t constexpr maxFreezeCheckDepth = 5;
127+
std::uint8_t constexpr maxAssetCheckDepth = 5;
128128

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

include/xrpl/protocol/STBase.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ struct JsonOptions
9393
};
9494

9595
template <typename T>
96-
requires requires(T const& t) { t.getJson(JsonOptions::none); }
96+
requires requires(T const& t) {
97+
{ t.getJson(JsonOptions::none) } -> std::convertible_to<Json::Value>;
98+
}
9799
Json::Value
98100
to_json(T const& t)
99101
{

include/xrpl/protocol/UintTypes.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ using NodeID = base_uint<160, detail::NodeIDTag>;
6161
/** MPTID is a 192-bit value representing MPT Issuance ID,
6262
* which is a concatenation of a 32-bit sequence (big endian)
6363
* and a 160-bit account */
64-
// TODO - edhennis - Add a tag
65-
using MPTID = uint192;
64+
using MPTID = base_uint<192>;
6665

6766
/** XRP currency. */
6867
Currency const&

include/xrpl/protocol/detail/sfields.macro

+3-3
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ TYPED_SFIELD(sfVaultID, UINT256, 35)
197197

198198
// number (common)
199199
TYPED_SFIELD(sfNumber, NUMBER, 1)
200-
TYPED_SFIELD(sfAssetsAvailable, NUMBER, 2)
201-
TYPED_SFIELD(sfAssetsMaximum, NUMBER, 3)
202-
TYPED_SFIELD(sfAssetsTotal, NUMBER, 4)
200+
TYPED_SFIELD(sfAssetsAvailable, NUMBER, 2)
201+
TYPED_SFIELD(sfAssetsMaximum, NUMBER, 3)
202+
TYPED_SFIELD(sfAssetsTotal, NUMBER, 4)
203203
TYPED_SFIELD(sfLossUnrealized, NUMBER, 5)
204204

205205
// currency amount (common)

include/xrpl/protocol/jss.h

-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ JSS(SettleDelay); // in: TransactionSign
8989
JSS(SendMax); // in: TransactionSign
9090
JSS(Sequence); // in/out: TransactionSign; field.
9191
JSS(SetFlag); // field.
92-
JSS(SharesTotal); // out: Vault
9392
JSS(Signer); // field.
9493
JSS(Signers); // field.
9594
JSS(SigningPubKey); // field.
@@ -101,7 +100,6 @@ JSS(TransactionType); // in: TransactionSign.
101100
JSS(TransferRate); // in: TransferRate.
102101
JSS(TxnSignature); // field.
103102
JSS(URI); // field.
104-
JSS(VaultID); // field.
105103
JSS(VoteSlots); // out: AMM Vote
106104
JSS(aborted); // out: InboundLedger
107105
JSS(accepted); // out: LedgerToJson, OwnerInfo, SubmitTransaction

src/libxrpl/protocol/STLedgerEntry.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <xrpl/protocol/SField.h>
3232
#include <xrpl/protocol/STBase.h>
3333
#include <xrpl/protocol/STLedgerEntry.h>
34-
#include <xrpl/protocol/STNumber.h>
3534
#include <xrpl/protocol/STObject.h>
3635
#include <xrpl/protocol/Serializer.h>
3736
#include <xrpl/protocol/jss.h>

src/test/jtx/impl/vault.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Vault::set(SetArgs const& args)
5050
Json::Value jv;
5151
jv[jss::TransactionType] = jss::VaultSet;
5252
jv[jss::Account] = args.owner.human();
53-
jv[jss::VaultID] = to_string(args.id);
53+
jv[sfVaultID] = to_string(args.id);
5454
return jv;
5555
}
5656

@@ -60,7 +60,7 @@ Vault::del(DeleteArgs const& args)
6060
Json::Value jv;
6161
jv[jss::TransactionType] = jss::VaultDelete;
6262
jv[jss::Account] = args.owner.human();
63-
jv[jss::VaultID] = to_string(args.id);
63+
jv[sfVaultID] = to_string(args.id);
6464
return jv;
6565
}
6666

@@ -70,7 +70,7 @@ Vault::deposit(DepositArgs const& args)
7070
Json::Value jv;
7171
jv[jss::TransactionType] = jss::VaultDeposit;
7272
jv[jss::Account] = args.depositor.human();
73-
jv[jss::VaultID] = to_string(args.id);
73+
jv[sfVaultID] = to_string(args.id);
7474
jv[jss::Amount] = to_json(args.amount);
7575
return jv;
7676
}
@@ -81,7 +81,7 @@ Vault::withdraw(WithdrawArgs const& args)
8181
Json::Value jv;
8282
jv[jss::TransactionType] = jss::VaultWithdraw;
8383
jv[jss::Account] = args.depositor.human();
84-
jv[jss::VaultID] = to_string(args.id);
84+
jv[sfVaultID] = to_string(args.id);
8585
jv[jss::Amount] = to_json(args.amount);
8686
return jv;
8787
}
@@ -92,7 +92,7 @@ Vault::clawback(ClawbackArgs const& args)
9292
Json::Value jv;
9393
jv[jss::TransactionType] = jss::VaultClawback;
9494
jv[jss::Account] = args.issuer.human();
95-
jv[jss::VaultID] = to_string(args.id);
95+
jv[sfVaultID] = to_string(args.id);
9696
jv[jss::Holder] = args.holder.human();
9797
if (args.amount)
9898
jv[jss::Amount] = to_json(*args.amount);

src/xrpld/app/tx/detail/AMMCreate.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ applyCreate(
227227
auto const ammKeylet = keylet::amm(amount.issue(), amount2.issue());
228228

229229
// Mitigate same account exists possibility
230-
auto const maybeAccount =
231-
createPseudoAccount(sb, ammKeylet.key, PseudoAccountOwnerType::AMM);
230+
auto const maybeAccount = createPseudoAccount(sb, ammKeylet.key, sfAMMID);
232231
// AMM account already exists (should not happen)
233232
if (!maybeAccount)
234233
{

src/xrpld/app/tx/detail/VaultClawback.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ VaultClawback::preclaim(PreclaimContext const& ctx)
9797
return tecWRONG_ASSET;
9898

9999
std::uint32_t const issueFlags = mptIssue->getFieldU32(sfFlags);
100-
if (!(issueFlags & lsfMPTCanClawback) || !(issueFlags & lsfMPTCanLock))
100+
if (!(issueFlags & lsfMPTCanClawback))
101101
return tecNO_PERMISSION;
102102
}
103103
else if (asset.holds<Issue>())

src/xrpld/app/tx/detail/VaultCreate.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ VaultCreate::preflight(PreflightContext const& ctx)
5656
return temMALFORMED;
5757
}
5858

59-
if (auto const data = ctx.tx[~sfWithdrawalPolicy])
59+
if (auto const withdrawalPolicy = ctx.tx[~sfWithdrawalPolicy])
6060
{
6161
// Enforce valid withdrawal policy
62-
if (*data != vaultStrategyFirstComeFirstServe)
62+
if (*withdrawalPolicy != vaultStrategyFirstComeFirstServe)
6363
return temMALFORMED;
6464
}
6565

@@ -151,22 +151,21 @@ VaultCreate::doApply()
151151
// we can consider downgrading them to `tef` or `tem`.
152152

153153
auto const& tx = ctx_.tx;
154-
auto const& ownerId = account_;
155154
auto sequence = tx.getSeqValue();
155+
auto owner = view().peek(keylet::account(account_));
156+
if (owner == nullptr)
157+
return tefINTERNAL;
156158

157-
auto owner = view().peek(keylet::account(ownerId));
158-
auto vault = std::make_shared<SLE>(keylet::vault(ownerId, sequence));
159+
auto vault = std::make_shared<SLE>(keylet::vault(account_, sequence));
159160

160-
if (auto ter = dirLink(view(), ownerId, vault))
161+
if (auto ter = dirLink(view(), account_, vault))
161162
return ter;
162-
// Should the next 3 lines be folded into `dirLink`?
163163
adjustOwnerCount(view(), owner, 1, j_);
164164
auto ownerCount = owner->at(sfOwnerCount);
165165
if (mPriorBalance < view().fees().accountReserve(ownerCount))
166166
return tecINSUFFICIENT_RESERVE;
167167

168-
auto maybePseudo = createPseudoAccount(
169-
view(), vault->key(), PseudoAccountOwnerType::Vault);
168+
auto maybePseudo = createPseudoAccount(view(), vault->key(), sfVaultID);
170169
if (!maybePseudo)
171170
return maybePseudo.error();
172171
auto& pseudo = *maybePseudo;
@@ -206,7 +205,7 @@ VaultCreate::doApply()
206205

207206
vault->at(sfFlags) = txFlags & tfVaultPrivate;
208207
vault->at(sfSequence) = sequence;
209-
vault->at(sfOwner) = ownerId;
208+
vault->at(sfOwner) = account_;
210209
vault->at(sfAccount) = pseudoId;
211210
vault->at(sfAsset) = asset;
212211
vault->at(sfAssetsTotal) = Number(0);

src/xrpld/app/tx/detail/VaultDeposit.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ VaultDeposit::preclaim(PreclaimContext const& ctx)
8383
if (isFrozen(ctx.view, account, share))
8484
return tecLOCKED;
8585

86-
if ((vault->getFlags() & tfVaultPrivate) && account != vault->at(sfOwner))
86+
if (vault->isFlag(tfVaultPrivate) && account != vault->at(sfOwner))
8787
{
8888
auto const maybeDomainID = sleIssuance->at(~sfDomainID);
8989
// Since this is a private vault and the account is not its owner, we
@@ -176,7 +176,7 @@ VaultDeposit::doApply()
176176
}
177177

178178
// If the vault is private, set the authorized flag for the vault owner
179-
if (vault->getFlags() & tfVaultPrivate)
179+
if (vault->isFlag(tfVaultPrivate))
180180
{
181181
if (auto const err = MPTokenAuthorize::authorize(
182182
view(),

src/xrpld/ledger/View.h

+23-11
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ isFrozen(
155155
MPTIssue const& mptIssue,
156156
int depth = 0);
157157

158+
/**
159+
* isFrozen check is recursive for MPT shares in a vault, descending to assets
160+
* in the vault. These assets could be themselves MPT shares in another vault.
161+
* For this reason we limit depth of check, up to maxAssetCheckDepth. If this
162+
* depth is exceeded, we return true (i.e. the asset is considered frozen).
163+
*/
158164
[[nodiscard]] inline bool
159165
isFrozen(
160166
ReadView const& view,
@@ -502,14 +508,19 @@ dirLink(ApplyView& view, AccountID const& owner, std::shared_ptr<SLE>& object);
502508
AccountID
503509
pseudoAccountAddress(ReadView const& view, uint256 const& pseudoOwnerKey);
504510

505-
// Which of the owner-object fields should we set: sfAMMID, sfVaultID
506-
enum class PseudoAccountOwnerType : int { AMM, Vault };
507-
511+
/**
512+
*
513+
* Create pseudo-account, storing pseudoOwnerKey into ownerField.
514+
*
515+
* The list of valid ownerField is maintained in View.cpp and the caller to
516+
* this function must perform necessary amendment check(s) before using a
517+
* field. The amendment check is **not** performed in createPseudoAccount.
518+
*/
508519
[[nodiscard]] Expected<std::shared_ptr<SLE>, TER>
509520
createPseudoAccount(
510521
ApplyView& view,
511522
uint256 const& pseudoOwnerKey,
512-
PseudoAccountOwnerType type);
523+
SField const& ownerField);
513524

514525
// Returns true iff sleAcct is a pseudo-account.
515526
//
@@ -691,12 +702,6 @@ transferXRP(
691702
STAmount const& amount,
692703
beast::Journal j);
693704

694-
struct TokenDescriptor
695-
{
696-
std::shared_ptr<SLE const> token;
697-
std::shared_ptr<SLE const> issuance;
698-
};
699-
700705
/** Check if the account lacks required authorization.
701706
*
702707
* Return tecNO_AUTH or tecNO_LINE if it does
@@ -711,6 +716,13 @@ requireAuth(ReadView const& view, Issue const& issue, AccountID const& account);
711716
* from preclaim, the user should convert result tecEXPIRED to tesSUCCESS and
712717
* proceed to also check permissions with enforceMPTokenAuthorization inside
713718
* doApply. This will ensure that any expired credentials are deleted.
719+
*
720+
* requireAuth check is recursive for MPT shares in a vault, descending to
721+
* assets in the vault. These assets could be themselves MPT shares in another
722+
* vault. For this reason we limit depth of check, up to maxAssetCheckDepth.
723+
* This function will return tecKILLED if maximum depth is exceeded.
724+
* Transaction VaultCreate checks for tecKILLED to prevent such vaults being
725+
* created.
714726
*/
715727
[[nodiscard]] TER
716728
requireAuth(
@@ -728,7 +740,7 @@ requireAuth(
728740
* in order for the transactor to proceed to doApply.
729741
*
730742
* This function will create MPToken (if needed) on the basis of any
731-
* non-expired credentals and will delete any expired credentials, indirectly
743+
* non-expired credentials and will delete any expired credentials, indirectly
732744
* via verifyValidDomain, as per DomainID (if set in MPTokenIssuance).
733745
*
734746
* The caller does NOT need to ensure that DomainID is actually set - this

0 commit comments

Comments
 (0)