Skip to content

Commit

Permalink
Add STTx Wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
yinyiqian1 committed Dec 5, 2024
1 parent 372cc5c commit 9310eae
Show file tree
Hide file tree
Showing 63 changed files with 613 additions and 378 deletions.
28 changes: 16 additions & 12 deletions include/xrpl/protocol/Permissions.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,29 @@ namespace ripple {
*/

enum GranularPermissionType : std::uint32_t {
gpTrustlineAuthorize = 65537,
TrustlineAuthorize = 65537,

gpTrustlineFreeze = 65538,
TrustlineFreeze = 65538,

gpTrustlineUnfreeze = 65539,
TrustlineUnfreeze = 65539,

gpAccountDomainSet = 65540,
AccountDomainSet = 65540,

gpAccountEmailHashSet = 65541,
AccountEmailHashSet = 65541,

gpAccountMessageKeySet = 65542,
AccountMessageKeySet = 65542,

gpAccountTransferRateSet = 65543,
AccountTransferRateSet = 65543,

gpAccountTickSizeSet = 65544,
AccountTickSizeSet = 65544,

gpPaymentMint = 65545,
PaymentMint = 65545,

gpPaymentBurn = 65546,
PaymentBurn = 65546,

gpMPTokenIssuanceLock = 65547,
MPTokenIssuanceLock = 65547,

gpMPTokenIssuanceUnlock = 65548,
MPTokenIssuanceUnlock = 65548,
};

class Permission
Expand All @@ -73,6 +73,10 @@ class Permission
static Permission const&
getInstance();

Permission(const Permission&) = delete;
Permission&
operator=(const Permission&) = delete;

std::optional<std::uint32_t>
getGranularValue(std::string const& name) const;

Expand Down
210 changes: 210 additions & 0 deletions include/xrpl/protocol/STTxWr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2024 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#ifndef RIPPLE_PROTOCOL_STTX_WRAPPER_H_INCLUDED
#define RIPPLE_PROTOCOL_STTX_WRAPPER_H_INCLUDED

#include <xrpl/protocol/STAccount.h>
#include <xrpl/protocol/STTx.h>
#include <functional>

namespace ripple {

// This class is a wrapper to deal with delegating in AccountPermission
// amendment. It wraps STTx, and delegates to STTx methods. The key change is
// getAccountID and operator[]. We need to first check if the transaction is
// delegated by another account by checking if the sfOnBehalfOf field is
// present. If it is present, we need to return the sfOnBehalfOf field as the
// account when calling getAccountID(sfAccount) and tx[sfAccount].
class STTxWr
{
private:
STTx tx_; // Wrap an instance of STTx
bool isDelegated_; // if the transaction is delegated by another account

public:
explicit STTxWr(STTx tx, bool isDelegated)
: tx_(tx), isDelegated_(isDelegated)
{
}

STTx
getTx() const
{
return tx_;
}

bool
isDelegated() const
{
return isDelegated_;
}

AccountID
getAccountID(SField const& field) const
{
if (field == sfAccount)
return tx_.isFieldPresent(sfOnBehalfOf) ? *tx_[~sfOnBehalfOf]
: tx_[sfAccount];
return tx_.getAccountID(field);
}

template <class T>
typename std::enable_if<
!std::is_same<TypedField<T>, SF_ACCOUNT>::value,
typename T::value_type>::type
operator[](TypedField<T> const& f) const
{
return tx_[f];
}

// When Type is SF_ACCOUNT and also field name is sfAccount, we need to
// check if the transaction is delegated by another account. If it is,
// return sfOnBehalfOf field instead.
template <class T>
typename std::enable_if<
std::is_same<TypedField<T>, SF_ACCOUNT>::value,
AccountID>::type
operator[](TypedField<T> const& f) const
{
if (f == sfAccount)
return tx_.isFieldPresent(sfOnBehalfOf) ? *tx_[~sfOnBehalfOf]
: tx_[sfAccount];
return tx_[f];
}

template <class T>
std::optional<std::decay_t<typename T::value_type>>
operator[](OptionaledField<T> const& of) const
{
return tx_[of];
}

uint256
getTransactionID() const
{
return tx_.getTransactionID();
}

TxType
getTxnType() const
{
return tx_.getTxnType();
}

std::uint32_t
getFlags() const
{
return tx_.getFlags();
}

bool
isFieldPresent(SField const& field) const
{
return tx_.isFieldPresent(field);
}

Json::Value
getJson(JsonOptions options) const
{
return tx_.getJson(options);
}

void
add(Serializer& s) const
{
tx_.add(s);
}

unsigned char
getFieldU8(SField const& field) const
{
return tx_.getFieldU8(field);
}

std::uint32_t
getFieldU32(SField const& field) const
{
return tx_.getFieldU32(field);
}

uint256
getFieldH256(SField const& field) const
{
return tx_.getFieldH256(field);
}

Blob
getFieldVL(SField const& field) const
{
return tx_.getFieldVL(field);
}

STAmount const&
getFieldAmount(SField const& field) const
{
return tx_.getFieldAmount(field);
}

STPathSet const&
getFieldPathSet(SField const& field) const
{
return tx_.getFieldPathSet(field);
}

const STVector256&
getFieldV256(SField const& field) const
{
return tx_.getFieldV256(field);
}

const STArray&
getFieldArray(SField const& field) const
{
return tx_.getFieldArray(field);
}

Blob
getSigningPubKey() const
{
return tx_.getSigningPubKey();
}

Blob
getSignature() const
{
return tx_.getSignature();
}

bool
isFlag(std::uint32_t f) const
{
return tx_.isFlag(f);
}

SeqProxy
getSeqProxy() const
{
return tx_.getSeqProxy();
}
};

} // namespace ripple

#endif
1 change: 1 addition & 0 deletions include/xrpl/protocol/jss.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ JSS(attestations);
JSS(attestation_reward_account);
JSS(auction_slot); // out: amm_info
JSS(authorized); // out: AccountLines
JSS(authorize); // out: account_permission
JSS(authorized_credentials); // in: ledger_entry DepositPreauth
JSS(auth_accounts); // out: amm_info
JSS(auth_change); // out: AccountInfo
Expand Down
2 changes: 1 addition & 1 deletion src/libxrpl/protocol/Indexes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ enum class LedgerNameSpace : std::uint16_t {
MPTOKEN_ISSUANCE = '~',
MPTOKEN = 't',
CREDENTIAL = 'D',
ACCOUNT_PERMISSION = 'P',
ACCOUNT_PERMISSION = 'E',

// No longer used or supported. Left here to reserve the space
// to avoid accidental reuse.
Expand Down
48 changes: 24 additions & 24 deletions src/libxrpl/protocol/Permissions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,32 @@ namespace ripple {
Permission::Permission()
{
granularPermissionMap = {
{"TrustlineAuthorize", gpTrustlineAuthorize},
{"TrustlineFreeze", gpTrustlineFreeze},
{"TrustlineUnfreeze", gpTrustlineUnfreeze},
{"AccountDomainSet", gpAccountDomainSet},
{"AccountEmailHashSet", gpAccountEmailHashSet},
{"AccountMessageKeySet", gpAccountMessageKeySet},
{"AccountTransferRateSet", gpAccountTransferRateSet},
{"AccountTickSizeSet", gpAccountTickSizeSet},
{"PaymentMint", gpPaymentMint},
{"PaymentBurn", gpPaymentBurn},
{"MPTokenIssuanceLock", gpMPTokenIssuanceLock},
{"MPTokenIssuanceUnlock", gpMPTokenIssuanceUnlock}};
{"TrustlineAuthorize", TrustlineAuthorize},
{"TrustlineFreeze", TrustlineFreeze},
{"TrustlineUnfreeze", TrustlineUnfreeze},
{"AccountDomainSet", AccountDomainSet},
{"AccountEmailHashSet", AccountEmailHashSet},
{"AccountMessageKeySet", AccountMessageKeySet},
{"AccountTransferRateSet", AccountTransferRateSet},
{"AccountTickSizeSet", AccountTickSizeSet},
{"PaymentMint", PaymentMint},
{"PaymentBurn", PaymentBurn},
{"MPTokenIssuanceLock", MPTokenIssuanceLock},
{"MPTokenIssuanceUnlock", MPTokenIssuanceUnlock}};

granularTxTypeMap = {
{gpTrustlineAuthorize, ttTRUST_SET},
{gpTrustlineFreeze, ttTRUST_SET},
{gpTrustlineUnfreeze, ttTRUST_SET},
{gpAccountDomainSet, ttACCOUNT_SET},
{gpAccountEmailHashSet, ttACCOUNT_SET},
{gpAccountMessageKeySet, ttACCOUNT_SET},
{gpAccountTransferRateSet, ttACCOUNT_SET},
{gpAccountTickSizeSet, ttACCOUNT_SET},
{gpPaymentMint, ttPAYMENT},
{gpPaymentBurn, ttPAYMENT},
{gpMPTokenIssuanceLock, ttMPTOKEN_ISSUANCE_SET},
{gpMPTokenIssuanceUnlock, ttMPTOKEN_ISSUANCE_SET}};
{TrustlineAuthorize, ttTRUST_SET},
{TrustlineFreeze, ttTRUST_SET},
{TrustlineUnfreeze, ttTRUST_SET},
{AccountDomainSet, ttACCOUNT_SET},
{AccountEmailHashSet, ttACCOUNT_SET},
{AccountMessageKeySet, ttACCOUNT_SET},
{AccountTransferRateSet, ttACCOUNT_SET},
{AccountTickSizeSet, ttACCOUNT_SET},
{PaymentMint, ttPAYMENT},
{PaymentBurn, ttPAYMENT},
{MPTokenIssuanceLock, ttMPTOKEN_ISSUANCE_SET},
{MPTokenIssuanceUnlock, ttMPTOKEN_ISSUANCE_SET}};
}

Permission const&
Expand Down
9 changes: 3 additions & 6 deletions src/test/app/AMM_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3304,10 +3304,9 @@ struct AMM_test : public jtx::AMMTest
env.app().config().features.erase(featureAMM);
PreflightContext pfctx(
env.app(),
*jtx.stx,
STTxWr(*jtx.stx, false),
env.current()->rules(),
tapNONE,
AccountID(0),
env.journal);
auto pf = AMMBid::preflight(pfctx);
BEAST_EXPECT(pf == temDISABLED);
Expand All @@ -3320,10 +3319,9 @@ struct AMM_test : public jtx::AMMTest
jtx.stx = env.ust(jtx);
PreflightContext pfctx(
env.app(),
*jtx.stx,
STTxWr(*jtx.stx, false),
env.current()->rules(),
tapNONE,
AccountID(0),
env.journal);
auto pf = AMMBid::preflight(pfctx);
BEAST_EXPECT(pf != tesSUCCESS);
Expand All @@ -3336,10 +3334,9 @@ struct AMM_test : public jtx::AMMTest
jtx.stx = env.ust(jtx);
PreflightContext pfctx(
env.app(),
*jtx.stx,
STTxWr(*jtx.stx, false),
env.current()->rules(),
tapNONE,
AccountID(0),
env.journal);
auto pf = AMMBid::preflight(pfctx);
BEAST_EXPECT(pf == temBAD_AMM_TOKENS);
Expand Down
Loading

0 comments on commit 9310eae

Please sign in to comment.