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 XRPL_ASSERT(
72 permission.second > UINT16_MAX,
73 "xrpl::Permission::granularPermissionMap_ : granular permission "
74 "value must not exceed the maximum uint16_t value.");
75}
76
77Permission const&
79{
80 static Permission const instance;
81 return instance;
82}
83
86{
87 auto const permissionValue = static_cast<GranularPermissionType>(value);
88 if (auto const granular = getGranularName(permissionValue))
89 return *granular;
90
91 // not a granular permission, check if it maps to a transaction type
92 auto const txType = permissionToTxType(value);
93 if (auto const* item = TxFormats::getInstance().findByType(txType); item != nullptr)
94 return item->getName();
95
96 return std::nullopt;
97}
98
101{
102 auto const it = granularPermissionMap_.find(name);
103 if (it != granularPermissionMap_.end())
104 return static_cast<uint32_t>(it->second);
105
106 return std::nullopt;
107}
108
111{
112 auto const it = granularNameMap_.find(value);
113 if (it != granularNameMap_.end())
114 return it->second;
115
116 return std::nullopt;
117}
118
121{
122 auto const it = granularTxTypeMap_.find(gpType);
123 if (it != granularTxTypeMap_.end())
124 return it->second;
125
126 return std::nullopt;
127}
128
131{
132 auto const txFeaturesIt = txFeatureMap_.find(txType);
133 XRPL_ASSERT(txFeaturesIt != txFeatureMap_.end(), "xrpl::Permissions::getTxFeature : tx exists in txFeatureMap_");
134
135 if (txFeaturesIt->second == uint256{})
136 return std::nullopt;
137 return txFeaturesIt->second;
138}
139
140bool
141Permission::isDelegable(std::uint32_t const& permissionValue, Rules const& rules) const
142{
143 auto const granularPermission = getGranularName(static_cast<GranularPermissionType>(permissionValue));
144 if (granularPermission)
145 // granular permissions are always allowed to be delegated
146 return true;
147
148 auto const txType = permissionToTxType(permissionValue);
149 auto const it = delegableTx_.find(txType);
150
151 if (it == delegableTx_.end())
152 return false;
153
154 auto const txFeaturesIt = txFeatureMap_.find(txType);
155 XRPL_ASSERT(txFeaturesIt != txFeatureMap_.end(), "xrpl::Permissions::isDelegable : tx exists in txFeatureMap_");
156
157 // Delegation is only allowed if the required amendment for the transaction
158 // is enabled. For transactions that do not require an amendment, delegation
159 // is always allowed.
160 if (txFeaturesIt->second != uint256{} && !rules.enabled(txFeaturesIt->second))
161 return false;
162
163 if (it->second == Delegation::notDelegable)
164 return false;
165
166 return true;
167}
168
169uint32_t
171{
172 return static_cast<uint32_t>(type) + 1;
173}
174
175TxType
176Permission::permissionToTxType(uint32_t const& value) const
177{
178 return static_cast<TxType>(value - 1);
179}
180
181} // 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
TxType permissionToTxType(uint32_t const &value) const
std::optional< TxType > getGranularTxType(GranularPermissionType const &gpType) const
std::optional< std::string > getPermissionName(std::uint32_t const value) const
uint32_t txToPermissionType(TxType const &type) const
bool isDelegable(std::uint32_t const &permissionValue, Rules const &rules) const
std::optional< std::reference_wrapper< uint256 const > > const getTxFeature(TxType txType) const
std::unordered_map< GranularPermissionType, std::string > granularNameMap_
Definition Permissions.h:44
std::unordered_map< std::uint16_t, uint256 > txFeatureMap_
Definition Permissions.h:38
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:118
static TxFormats const & getInstance()
Definition TxFormats.cpp:51
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:37
GranularPermissionType
We have both transaction type permissions and granular type permissions.
Definition Permissions.h:19
@ notDelegable
Definition Permissions.h:31