Skip to content

Commit 9310eae

Browse files
committed
Add STTx Wrapper
1 parent 372cc5c commit 9310eae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+613
-378
lines changed

include/xrpl/protocol/Permissions.h

+16-12
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,29 @@ namespace ripple {
3434
*/
3535

3636
enum GranularPermissionType : std::uint32_t {
37-
gpTrustlineAuthorize = 65537,
37+
TrustlineAuthorize = 65537,
3838

39-
gpTrustlineFreeze = 65538,
39+
TrustlineFreeze = 65538,
4040

41-
gpTrustlineUnfreeze = 65539,
41+
TrustlineUnfreeze = 65539,
4242

43-
gpAccountDomainSet = 65540,
43+
AccountDomainSet = 65540,
4444

45-
gpAccountEmailHashSet = 65541,
45+
AccountEmailHashSet = 65541,
4646

47-
gpAccountMessageKeySet = 65542,
47+
AccountMessageKeySet = 65542,
4848

49-
gpAccountTransferRateSet = 65543,
49+
AccountTransferRateSet = 65543,
5050

51-
gpAccountTickSizeSet = 65544,
51+
AccountTickSizeSet = 65544,
5252

53-
gpPaymentMint = 65545,
53+
PaymentMint = 65545,
5454

55-
gpPaymentBurn = 65546,
55+
PaymentBurn = 65546,
5656

57-
gpMPTokenIssuanceLock = 65547,
57+
MPTokenIssuanceLock = 65547,
5858

59-
gpMPTokenIssuanceUnlock = 65548,
59+
MPTokenIssuanceUnlock = 65548,
6060
};
6161

6262
class Permission
@@ -73,6 +73,10 @@ class Permission
7373
static Permission const&
7474
getInstance();
7575

76+
Permission(const Permission&) = delete;
77+
Permission&
78+
operator=(const Permission&) = delete;
79+
7680
std::optional<std::uint32_t>
7781
getGranularValue(std::string const& name) const;
7882

include/xrpl/protocol/STTxWr.h

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
//------------------------------------------------------------------------------
2+
/*
3+
This file is part of rippled: https://github.com/ripple/rippled
4+
Copyright (c) 2024 Ripple Labs Inc.
5+
6+
Permission to use, copy, modify, and/or distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
*/
18+
//==============================================================================
19+
20+
#ifndef RIPPLE_PROTOCOL_STTX_WRAPPER_H_INCLUDED
21+
#define RIPPLE_PROTOCOL_STTX_WRAPPER_H_INCLUDED
22+
23+
#include <xrpl/protocol/STAccount.h>
24+
#include <xrpl/protocol/STTx.h>
25+
#include <functional>
26+
27+
namespace ripple {
28+
29+
// This class is a wrapper to deal with delegating in AccountPermission
30+
// amendment. It wraps STTx, and delegates to STTx methods. The key change is
31+
// getAccountID and operator[]. We need to first check if the transaction is
32+
// delegated by another account by checking if the sfOnBehalfOf field is
33+
// present. If it is present, we need to return the sfOnBehalfOf field as the
34+
// account when calling getAccountID(sfAccount) and tx[sfAccount].
35+
class STTxWr
36+
{
37+
private:
38+
STTx tx_; // Wrap an instance of STTx
39+
bool isDelegated_; // if the transaction is delegated by another account
40+
41+
public:
42+
explicit STTxWr(STTx tx, bool isDelegated)
43+
: tx_(tx), isDelegated_(isDelegated)
44+
{
45+
}
46+
47+
STTx
48+
getTx() const
49+
{
50+
return tx_;
51+
}
52+
53+
bool
54+
isDelegated() const
55+
{
56+
return isDelegated_;
57+
}
58+
59+
AccountID
60+
getAccountID(SField const& field) const
61+
{
62+
if (field == sfAccount)
63+
return tx_.isFieldPresent(sfOnBehalfOf) ? *tx_[~sfOnBehalfOf]
64+
: tx_[sfAccount];
65+
return tx_.getAccountID(field);
66+
}
67+
68+
template <class T>
69+
typename std::enable_if<
70+
!std::is_same<TypedField<T>, SF_ACCOUNT>::value,
71+
typename T::value_type>::type
72+
operator[](TypedField<T> const& f) const
73+
{
74+
return tx_[f];
75+
}
76+
77+
// When Type is SF_ACCOUNT and also field name is sfAccount, we need to
78+
// check if the transaction is delegated by another account. If it is,
79+
// return sfOnBehalfOf field instead.
80+
template <class T>
81+
typename std::enable_if<
82+
std::is_same<TypedField<T>, SF_ACCOUNT>::value,
83+
AccountID>::type
84+
operator[](TypedField<T> const& f) const
85+
{
86+
if (f == sfAccount)
87+
return tx_.isFieldPresent(sfOnBehalfOf) ? *tx_[~sfOnBehalfOf]
88+
: tx_[sfAccount];
89+
return tx_[f];
90+
}
91+
92+
template <class T>
93+
std::optional<std::decay_t<typename T::value_type>>
94+
operator[](OptionaledField<T> const& of) const
95+
{
96+
return tx_[of];
97+
}
98+
99+
uint256
100+
getTransactionID() const
101+
{
102+
return tx_.getTransactionID();
103+
}
104+
105+
TxType
106+
getTxnType() const
107+
{
108+
return tx_.getTxnType();
109+
}
110+
111+
std::uint32_t
112+
getFlags() const
113+
{
114+
return tx_.getFlags();
115+
}
116+
117+
bool
118+
isFieldPresent(SField const& field) const
119+
{
120+
return tx_.isFieldPresent(field);
121+
}
122+
123+
Json::Value
124+
getJson(JsonOptions options) const
125+
{
126+
return tx_.getJson(options);
127+
}
128+
129+
void
130+
add(Serializer& s) const
131+
{
132+
tx_.add(s);
133+
}
134+
135+
unsigned char
136+
getFieldU8(SField const& field) const
137+
{
138+
return tx_.getFieldU8(field);
139+
}
140+
141+
std::uint32_t
142+
getFieldU32(SField const& field) const
143+
{
144+
return tx_.getFieldU32(field);
145+
}
146+
147+
uint256
148+
getFieldH256(SField const& field) const
149+
{
150+
return tx_.getFieldH256(field);
151+
}
152+
153+
Blob
154+
getFieldVL(SField const& field) const
155+
{
156+
return tx_.getFieldVL(field);
157+
}
158+
159+
STAmount const&
160+
getFieldAmount(SField const& field) const
161+
{
162+
return tx_.getFieldAmount(field);
163+
}
164+
165+
STPathSet const&
166+
getFieldPathSet(SField const& field) const
167+
{
168+
return tx_.getFieldPathSet(field);
169+
}
170+
171+
const STVector256&
172+
getFieldV256(SField const& field) const
173+
{
174+
return tx_.getFieldV256(field);
175+
}
176+
177+
const STArray&
178+
getFieldArray(SField const& field) const
179+
{
180+
return tx_.getFieldArray(field);
181+
}
182+
183+
Blob
184+
getSigningPubKey() const
185+
{
186+
return tx_.getSigningPubKey();
187+
}
188+
189+
Blob
190+
getSignature() const
191+
{
192+
return tx_.getSignature();
193+
}
194+
195+
bool
196+
isFlag(std::uint32_t f) const
197+
{
198+
return tx_.isFlag(f);
199+
}
200+
201+
SeqProxy
202+
getSeqProxy() const
203+
{
204+
return tx_.getSeqProxy();
205+
}
206+
};
207+
208+
} // namespace ripple
209+
210+
#endif

include/xrpl/protocol/jss.h

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ JSS(attestations);
170170
JSS(attestation_reward_account);
171171
JSS(auction_slot); // out: amm_info
172172
JSS(authorized); // out: AccountLines
173+
JSS(authorize); // out: account_permission
173174
JSS(authorized_credentials); // in: ledger_entry DepositPreauth
174175
JSS(auth_accounts); // out: amm_info
175176
JSS(auth_change); // out: AccountInfo

src/libxrpl/protocol/Indexes.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ enum class LedgerNameSpace : std::uint16_t {
7777
MPTOKEN_ISSUANCE = '~',
7878
MPTOKEN = 't',
7979
CREDENTIAL = 'D',
80-
ACCOUNT_PERMISSION = 'P',
80+
ACCOUNT_PERMISSION = 'E',
8181

8282
// No longer used or supported. Left here to reserve the space
8383
// to avoid accidental reuse.

src/libxrpl/protocol/Permissions.cpp

+24-24
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,32 @@ namespace ripple {
2727
Permission::Permission()
2828
{
2929
granularPermissionMap = {
30-
{"TrustlineAuthorize", gpTrustlineAuthorize},
31-
{"TrustlineFreeze", gpTrustlineFreeze},
32-
{"TrustlineUnfreeze", gpTrustlineUnfreeze},
33-
{"AccountDomainSet", gpAccountDomainSet},
34-
{"AccountEmailHashSet", gpAccountEmailHashSet},
35-
{"AccountMessageKeySet", gpAccountMessageKeySet},
36-
{"AccountTransferRateSet", gpAccountTransferRateSet},
37-
{"AccountTickSizeSet", gpAccountTickSizeSet},
38-
{"PaymentMint", gpPaymentMint},
39-
{"PaymentBurn", gpPaymentBurn},
40-
{"MPTokenIssuanceLock", gpMPTokenIssuanceLock},
41-
{"MPTokenIssuanceUnlock", gpMPTokenIssuanceUnlock}};
30+
{"TrustlineAuthorize", TrustlineAuthorize},
31+
{"TrustlineFreeze", TrustlineFreeze},
32+
{"TrustlineUnfreeze", TrustlineUnfreeze},
33+
{"AccountDomainSet", AccountDomainSet},
34+
{"AccountEmailHashSet", AccountEmailHashSet},
35+
{"AccountMessageKeySet", AccountMessageKeySet},
36+
{"AccountTransferRateSet", AccountTransferRateSet},
37+
{"AccountTickSizeSet", AccountTickSizeSet},
38+
{"PaymentMint", PaymentMint},
39+
{"PaymentBurn", PaymentBurn},
40+
{"MPTokenIssuanceLock", MPTokenIssuanceLock},
41+
{"MPTokenIssuanceUnlock", MPTokenIssuanceUnlock}};
4242

4343
granularTxTypeMap = {
44-
{gpTrustlineAuthorize, ttTRUST_SET},
45-
{gpTrustlineFreeze, ttTRUST_SET},
46-
{gpTrustlineUnfreeze, ttTRUST_SET},
47-
{gpAccountDomainSet, ttACCOUNT_SET},
48-
{gpAccountEmailHashSet, ttACCOUNT_SET},
49-
{gpAccountMessageKeySet, ttACCOUNT_SET},
50-
{gpAccountTransferRateSet, ttACCOUNT_SET},
51-
{gpAccountTickSizeSet, ttACCOUNT_SET},
52-
{gpPaymentMint, ttPAYMENT},
53-
{gpPaymentBurn, ttPAYMENT},
54-
{gpMPTokenIssuanceLock, ttMPTOKEN_ISSUANCE_SET},
55-
{gpMPTokenIssuanceUnlock, ttMPTOKEN_ISSUANCE_SET}};
44+
{TrustlineAuthorize, ttTRUST_SET},
45+
{TrustlineFreeze, ttTRUST_SET},
46+
{TrustlineUnfreeze, ttTRUST_SET},
47+
{AccountDomainSet, ttACCOUNT_SET},
48+
{AccountEmailHashSet, ttACCOUNT_SET},
49+
{AccountMessageKeySet, ttACCOUNT_SET},
50+
{AccountTransferRateSet, ttACCOUNT_SET},
51+
{AccountTickSizeSet, ttACCOUNT_SET},
52+
{PaymentMint, ttPAYMENT},
53+
{PaymentBurn, ttPAYMENT},
54+
{MPTokenIssuanceLock, ttMPTOKEN_ISSUANCE_SET},
55+
{MPTokenIssuanceUnlock, ttMPTOKEN_ISSUANCE_SET}};
5656
}
5757

5858
Permission const&

src/test/app/AMM_test.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -3304,10 +3304,9 @@ struct AMM_test : public jtx::AMMTest
33043304
env.app().config().features.erase(featureAMM);
33053305
PreflightContext pfctx(
33063306
env.app(),
3307-
*jtx.stx,
3307+
STTxWr(*jtx.stx, false),
33083308
env.current()->rules(),
33093309
tapNONE,
3310-
AccountID(0),
33113310
env.journal);
33123311
auto pf = AMMBid::preflight(pfctx);
33133312
BEAST_EXPECT(pf == temDISABLED);
@@ -3320,10 +3319,9 @@ struct AMM_test : public jtx::AMMTest
33203319
jtx.stx = env.ust(jtx);
33213320
PreflightContext pfctx(
33223321
env.app(),
3323-
*jtx.stx,
3322+
STTxWr(*jtx.stx, false),
33243323
env.current()->rules(),
33253324
tapNONE,
3326-
AccountID(0),
33273325
env.journal);
33283326
auto pf = AMMBid::preflight(pfctx);
33293327
BEAST_EXPECT(pf != tesSUCCESS);
@@ -3336,10 +3334,9 @@ struct AMM_test : public jtx::AMMTest
33363334
jtx.stx = env.ust(jtx);
33373335
PreflightContext pfctx(
33383336
env.app(),
3339-
*jtx.stx,
3337+
STTxWr(*jtx.stx, false),
33403338
env.current()->rules(),
33413339
tapNONE,
3342-
AccountID(0),
33433340
env.journal);
33443341
auto pf = AMMBid::preflight(pfctx);
33453342
BEAST_EXPECT(pf == temBAD_AMM_TOKENS);

0 commit comments

Comments
 (0)