rippled
Loading...
Searching...
No Matches
Permissions.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2025 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#include <xrpl/beast/utility/instrumentation.h>
21#include <xrpl/protocol/Feature.h>
22#include <xrpl/protocol/Permissions.h>
23#include <xrpl/protocol/jss.h>
24
25namespace ripple {
26
28{
30#pragma push_macro("TRANSACTION")
31#undef TRANSACTION
32
33#define TRANSACTION(tag, value, name, delegatable, amendment, ...) \
34 {value, amendment},
35
36#include <xrpl/protocol/detail/transactions.macro>
37
38#undef TRANSACTION
39#pragma pop_macro("TRANSACTION")
40 };
41
43#pragma push_macro("TRANSACTION")
44#undef TRANSACTION
45
46#define TRANSACTION(tag, value, name, delegatable, ...) {value, delegatable},
47
48#include <xrpl/protocol/detail/transactions.macro>
49
50#undef TRANSACTION
51#pragma pop_macro("TRANSACTION")
52 };
53
55#pragma push_macro("PERMISSION")
56#undef PERMISSION
57
58#define PERMISSION(type, txType, value) {#type, type},
59
60#include <xrpl/protocol/detail/permissions.macro>
61
62#undef PERMISSION
63#pragma pop_macro("PERMISSION")
64 };
65
67#pragma push_macro("PERMISSION")
68#undef PERMISSION
69
70#define PERMISSION(type, txType, value) {type, #type},
71
72#include <xrpl/protocol/detail/permissions.macro>
73
74#undef PERMISSION
75#pragma pop_macro("PERMISSION")
76 };
77
79#pragma push_macro("PERMISSION")
80#undef PERMISSION
81
82#define PERMISSION(type, txType, value) {type, txType},
83
84#include <xrpl/protocol/detail/permissions.macro>
85
86#undef PERMISSION
87#pragma pop_macro("PERMISSION")
88 };
89
90 for ([[maybe_unused]] auto const& permission : granularPermissionMap_)
91 XRPL_ASSERT(
92 permission.second > UINT16_MAX,
93 "ripple::Permission::granularPermissionMap_ : granular permission "
94 "value must not exceed the maximum uint16_t value.");
95}
96
97Permission const&
99{
100 static Permission const instance;
101 return instance;
102}
103
106{
107 auto const permissionValue = static_cast<GranularPermissionType>(value);
108 if (auto const granular = getGranularName(permissionValue))
109 return *granular;
110
111 // not a granular permission, check if it maps to a transaction type
112 auto const txType = permissionToTxType(value);
113 if (auto const* item = TxFormats::getInstance().findByType(txType);
114 item != nullptr)
115 return item->getName();
116
117 return std::nullopt;
118}
119
122{
123 auto const it = granularPermissionMap_.find(name);
124 if (it != granularPermissionMap_.end())
125 return static_cast<uint32_t>(it->second);
126
127 return std::nullopt;
128}
129
132{
133 auto const it = granularNameMap_.find(value);
134 if (it != granularNameMap_.end())
135 return it->second;
136
137 return std::nullopt;
138}
139
142{
143 auto const it = granularTxTypeMap_.find(gpType);
144 if (it != granularTxTypeMap_.end())
145 return it->second;
146
147 return std::nullopt;
148}
149
152{
153 auto const txFeaturesIt = txFeatureMap_.find(txType);
154 XRPL_ASSERT(
155 txFeaturesIt != txFeatureMap_.end(),
156 "ripple::Permissions::getTxFeature : tx exists in txFeatureMap_");
157
158 if (txFeaturesIt->second == uint256{})
159 return std::nullopt;
160 return txFeaturesIt->second;
161}
162
163bool
165 std::uint32_t const& permissionValue,
166 Rules const& rules) const
167{
168 auto const granularPermission =
169 getGranularName(static_cast<GranularPermissionType>(permissionValue));
170 if (granularPermission)
171 // granular permissions are always allowed to be delegated
172 return true;
173
174 auto const txType = permissionToTxType(permissionValue);
175 auto const it = delegatableTx_.find(txType);
176
177 if (rules.enabled(fixDelegateV1_1))
178 {
179 if (it == delegatableTx_.end())
180 return false;
181
182 auto const feature = getTxFeature(txType);
183
184 // fixDelegateV1_1: Delegation is only allowed if the required amendment
185 // for the transaction is enabled. For transactions that do not require
186 // an amendment, delegation is always allowed.
187 if (feature && !rules.enabled(*feature))
188 return false;
189 }
190
191 if (it != delegatableTx_.end() && it->second == Delegation::notDelegatable)
192 return false;
193
194 return true;
195}
196
197uint32_t
199{
200 return static_cast<uint32_t>(type) + 1;
201}
202
203TxType
204Permission::permissionToTxType(uint32_t const& value) const
205{
206 return static_cast<TxType>(value - 1);
207}
208
209} // namespace ripple
std::unordered_map< std::uint16_t, Delegation > delegatableTx_
Definition Permissions.h:60
std::optional< std::reference_wrapper< uint256 const > > const getTxFeature(TxType txType) const
TxType permissionToTxType(uint32_t const &value) const
std::optional< std::string > getGranularName(GranularPermissionType const &value) const
std::unordered_map< std::uint16_t, uint256 > txFeatureMap_
Definition Permissions.h:58
std::optional< std::string > getPermissionName(std::uint32_t const value) const
std::unordered_map< GranularPermissionType, std::string > granularNameMap_
Definition Permissions.h:65
std::unordered_map< std::string, GranularPermissionType > granularPermissionMap_
Definition Permissions.h:63
std::optional< std::uint32_t > getGranularValue(std::string const &name) const
std::optional< TxType > getGranularTxType(GranularPermissionType const &gpType) const
uint32_t txToPermissionType(TxType const &type) const
static Permission const & getInstance()
std::unordered_map< GranularPermissionType, TxType > granularTxTypeMap_
Definition Permissions.h:67
bool isDelegatable(std::uint32_t const &permissionValue, Rules const &rules) const
Rules controlling protocol behavior.
Definition Rules.h:38
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:130
static TxFormats const & getInstance()
Definition TxFormats.cpp:71
T end(T... args)
T find(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
TxType
Transaction type identifiers.
Definition TxFormats.h:57
@ notDelegatable
Definition Permissions.h:51
GranularPermissionType
We have both transaction type permissions and granular type permissions.
Definition Permissions.h:39