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 xrpl {
7
9{
11#pragma push_macro("TRANSACTION")
12#undef TRANSACTION
13
14#define TRANSACTION(tag, value, name, delegable, amendment, ...) {value, amendment},
15
16#include <xrpl/protocol/detail/transactions.macro>
17
18#undef TRANSACTION
19#pragma pop_macro("TRANSACTION")
20 };
21
22 delegableTx_ = {
23#pragma push_macro("TRANSACTION")
24#undef TRANSACTION
25
26#define TRANSACTION(tag, value, name, delegable, ...) {value, delegable},
27
28#include <xrpl/protocol/detail/transactions.macro>
29
30#undef TRANSACTION
31#pragma pop_macro("TRANSACTION")
32 };
33
35#pragma push_macro("PERMISSION")
36#undef PERMISSION
37
38#define PERMISSION(type, txType, value) {#type, type},
39
40#include <xrpl/protocol/detail/permissions.macro>
41
42#undef PERMISSION
43#pragma pop_macro("PERMISSION")
44 };
45
47#pragma push_macro("PERMISSION")
48#undef PERMISSION
49
50#define PERMISSION(type, txType, value) {type, #type},
51
52#include <xrpl/protocol/detail/permissions.macro>
53
54#undef PERMISSION
55#pragma pop_macro("PERMISSION")
56 };
57
59#pragma push_macro("PERMISSION")
60#undef PERMISSION
61
62#define PERMISSION(type, txType, value) {type, txType},
63
64#include <xrpl/protocol/detail/permissions.macro>
65
66#undef PERMISSION
67#pragma pop_macro("PERMISSION")
68 };
69
70 for ([[maybe_unused]] auto const& permission : granularPermissionMap_)
71 {
72 XRPL_ASSERT(
73 permission.second > UINT16_MAX,
74 "xrpl::Permission::granularPermissionMap_ : granular permission "
75 "value must not exceed the maximum uint16_t value.");
76 }
77}
78
79Permission const&
81{
82 static Permission const instance;
83 return instance;
84}
85
88{
89 auto const permissionValue = static_cast<GranularPermissionType>(value);
90 if (auto const granular = getGranularName(permissionValue))
91 return granular;
92
93 // not a granular permission, check if it maps to a transaction type
94 auto const txType = permissionToTxType(value);
95 if (auto const* item = TxFormats::getInstance().findByType(txType); 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 "xrpl::Permissions::getTxFeature : tx exists in txFeatureMap_");
138
139 if (txFeaturesIt->second == uint256{})
140 return std::nullopt;
141 return txFeaturesIt->second;
142}
143
144bool
145Permission::isDelegable(std::uint32_t const& permissionValue, Rules const& rules) const
146{
147 auto const granularPermission =
148 getGranularName(static_cast<GranularPermissionType>(permissionValue));
149 if (granularPermission)
150 {
151 // granular permissions are always allowed to be delegated
152 return true;
153 }
154
155 auto const txType = permissionToTxType(permissionValue);
156 auto const it = delegableTx_.find(txType);
157
158 if (it == delegableTx_.end())
159 return false;
160
161 auto const txFeaturesIt = txFeatureMap_.find(txType);
162 XRPL_ASSERT(
163 txFeaturesIt != txFeatureMap_.end(),
164 "xrpl::Permissions::isDelegable : 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{} && !rules.enabled(txFeaturesIt->second))
170 return false;
171
172 if (it->second == Delegation::notDelegable)
173 return false;
174
175 return true;
176}
177
178uint32_t
180{
181 return static_cast<uint32_t>(type) + 1;
182}
183
184TxType
185Permission::permissionToTxType(uint32_t const& value)
186{
187 return static_cast<TxType>(value - 1);
188}
189
190} // namespace xrpl
std::optional< std::string > getGranularName(GranularPermissionType const &value) const
std::unordered_map< std::uint16_t, Delegation > delegableTx_
Definition Permissions.h:40
std::optional< std::uint32_t > getGranularValue(std::string const &name) const
std::optional< TxType > getGranularTxType(GranularPermissionType const &gpType) const
std::optional< std::string > getPermissionName(std::uint32_t const value) const
bool isDelegable(std::uint32_t const &permissionValue, Rules const &rules) const
std::optional< std::reference_wrapper< uint256 const > > getTxFeature(TxType txType) const
std::unordered_map< GranularPermissionType, std::string > granularNameMap_
Definition Permissions.h:44
static uint32_t txToPermissionType(TxType const &type)
std::unordered_map< std::uint16_t, uint256 > txFeatureMap_
Definition Permissions.h:38
static TxType permissionToTxType(uint32_t const &value)
std::unordered_map< std::string, GranularPermissionType > granularPermissionMap_
Definition Permissions.h:42
std::unordered_map< GranularPermissionType, TxType > granularTxTypeMap_
Definition Permissions.h:46
static Permission const & getInstance()
Rules controlling protocol behavior.
Definition Rules.h:18
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:120
static TxFormats const & getInstance()
Definition TxFormats.cpp:55
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:5
TxType
Transaction type identifiers.
Definition TxFormats.h:39
GranularPermissionType
We have both transaction type permissions and granular type permissions.
Definition Permissions.h:19
@ notDelegable
Definition Permissions.h:31