rippled
Loading...
Searching...
No Matches
Permissions.cpp
1#include <xrpl/beast/utility/instrumentation.h>
2#include <xrpl/protocol/Feature.h>
3#include <xrpl/protocol/Permissions.h>
4#include <xrpl/protocol/jss.h>
5
6namespace ripple {
7
9{
11#pragma push_macro("TRANSACTION")
12#undef TRANSACTION
13
14#define TRANSACTION(tag, value, name, delegatable, amendment, ...) \
15 {value, amendment},
16
17#include <xrpl/protocol/detail/transactions.macro>
18
19#undef TRANSACTION
20#pragma pop_macro("TRANSACTION")
21 };
22
24#pragma push_macro("TRANSACTION")
25#undef TRANSACTION
26
27#define TRANSACTION(tag, value, name, delegatable, ...) {value, delegatable},
28
29#include <xrpl/protocol/detail/transactions.macro>
30
31#undef TRANSACTION
32#pragma pop_macro("TRANSACTION")
33 };
34
36#pragma push_macro("PERMISSION")
37#undef PERMISSION
38
39#define PERMISSION(type, txType, value) {#type, type},
40
41#include <xrpl/protocol/detail/permissions.macro>
42
43#undef PERMISSION
44#pragma pop_macro("PERMISSION")
45 };
46
48#pragma push_macro("PERMISSION")
49#undef PERMISSION
50
51#define PERMISSION(type, txType, value) {type, #type},
52
53#include <xrpl/protocol/detail/permissions.macro>
54
55#undef PERMISSION
56#pragma pop_macro("PERMISSION")
57 };
58
60#pragma push_macro("PERMISSION")
61#undef PERMISSION
62
63#define PERMISSION(type, txType, value) {type, txType},
64
65#include <xrpl/protocol/detail/permissions.macro>
66
67#undef PERMISSION
68#pragma pop_macro("PERMISSION")
69 };
70
71 for ([[maybe_unused]] auto const& permission : granularPermissionMap_)
72 XRPL_ASSERT(
73 permission.second > UINT16_MAX,
74 "ripple::Permission::granularPermissionMap_ : granular permission "
75 "value must not exceed the maximum uint16_t value.");
76}
77
78Permission const&
80{
81 static Permission const instance;
82 return instance;
83}
84
87{
88 auto const permissionValue = static_cast<GranularPermissionType>(value);
89 if (auto const granular = getGranularName(permissionValue))
90 return *granular;
91
92 // not a granular permission, check if it maps to a transaction type
93 auto const txType = permissionToTxType(value);
94 if (auto const* item = TxFormats::getInstance().findByType(txType);
95 item != nullptr)
96 return item->getName();
97
98 return std::nullopt;
99}
100
103{
104 auto const it = granularPermissionMap_.find(name);
105 if (it != granularPermissionMap_.end())
106 return static_cast<uint32_t>(it->second);
107
108 return std::nullopt;
109}
110
113{
114 auto const it = granularNameMap_.find(value);
115 if (it != granularNameMap_.end())
116 return it->second;
117
118 return std::nullopt;
119}
120
123{
124 auto const it = granularTxTypeMap_.find(gpType);
125 if (it != granularTxTypeMap_.end())
126 return it->second;
127
128 return std::nullopt;
129}
130
133{
134 auto const txFeaturesIt = txFeatureMap_.find(txType);
135 XRPL_ASSERT(
136 txFeaturesIt != txFeatureMap_.end(),
137 "ripple::Permissions::getTxFeature : tx exists in txFeatureMap_");
138
139 if (txFeaturesIt->second == uint256{})
140 return std::nullopt;
141 return txFeaturesIt->second;
142}
143
144bool
146 std::uint32_t const& permissionValue,
147 Rules const& rules) const
148{
149 auto const granularPermission =
150 getGranularName(static_cast<GranularPermissionType>(permissionValue));
151 if (granularPermission)
152 // granular permissions are always allowed to be delegated
153 return true;
154
155 auto const txType = permissionToTxType(permissionValue);
156 auto const it = delegatableTx_.find(txType);
157
158 if (it == delegatableTx_.end())
159 return false;
160
161 auto const txFeaturesIt = txFeatureMap_.find(txType);
162 XRPL_ASSERT(
163 txFeaturesIt != txFeatureMap_.end(),
164 "ripple::Permissions::isDelegatable : tx exists in txFeatureMap_");
165
166 // Delegation is only allowed if the required amendment for the transaction
167 // is enabled. For transactions that do not require an amendment, delegation
168 // is always allowed.
169 if (txFeaturesIt->second != uint256{} &&
170 !rules.enabled(txFeaturesIt->second))
171 return false;
172
173 if (it->second == Delegation::notDelegatable)
174 return false;
175
176 return true;
177}
178
179uint32_t
181{
182 return static_cast<uint32_t>(type) + 1;
183}
184
185TxType
186Permission::permissionToTxType(uint32_t const& value) const
187{
188 return static_cast<TxType>(value - 1);
189}
190
191} // namespace ripple
std::unordered_map< std::uint16_t, Delegation > delegatableTx_
Definition Permissions.h:41
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:39
std::optional< std::string > getPermissionName(std::uint32_t const value) const
std::unordered_map< GranularPermissionType, std::string > granularNameMap_
Definition Permissions.h:46
std::unordered_map< std::string, GranularPermissionType > granularPermissionMap_
Definition Permissions.h:44
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:48
bool isDelegatable(std::uint32_t const &permissionValue, Rules const &rules) const
Rules controlling protocol behavior.
Definition Rules.h:19
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:111
static TxFormats const & getInstance()
Definition TxFormats.cpp:52
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:6
TxType
Transaction type identifiers.
Definition TxFormats.h:38
@ notDelegatable
Definition Permissions.h:32
GranularPermissionType
We have both transaction type permissions and granular type permissions.
Definition Permissions.h:20