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 it = granularPermissionMap_.find(name);
108 if (it != granularPermissionMap_.end())
109 return static_cast<uint32_t>(it->second);
110
111 return std::nullopt;
112}
113
116{
117 auto const it = granularNameMap_.find(value);
118 if (it != granularNameMap_.end())
119 return it->second;
120
121 return std::nullopt;
122}
123
126{
127 auto const it = granularTxTypeMap_.find(gpType);
128 if (it != granularTxTypeMap_.end())
129 return it->second;
130
131 return std::nullopt;
132}
133
134bool
136 std::uint32_t const& permissionValue,
137 Rules const& rules) const
138{
139 auto const granularPermission =
140 getGranularName(static_cast<GranularPermissionType>(permissionValue));
141 if (granularPermission)
142 // granular permissions are always allowed to be delegated
143 return true;
144
145 auto const txType = permissionToTxType(permissionValue);
146 auto const it = delegatableTx_.find(txType);
147
148 if (rules.enabled(fixDelegateV1_1))
149 {
150 if (it == delegatableTx_.end())
151 return false;
152
153 auto const txFeaturesIt = txFeatureMap_.find(txType);
154 XRPL_ASSERT(
155 txFeaturesIt != txFeatureMap_.end(),
156 "ripple::Permissions::isDelegatable : tx exists in txFeatureMap_");
157
158 // fixDelegateV1_1: Delegation is only allowed if the required amendment
159 // for the transaction is enabled. For transactions that do not require
160 // an amendment, delegation is always allowed.
161 if (txFeaturesIt->second != uint256{} &&
162 !rules.enabled(txFeaturesIt->second))
163 return false;
164 }
165
166 if (it != delegatableTx_.end() && it->second == Delegation::notDelegatable)
167 return false;
168
169 return true;
170}
171
172uint32_t
174{
175 return static_cast<uint32_t>(type) + 1;
176}
177
178TxType
179Permission::permissionToTxType(uint32_t const& value) const
180{
181 return static_cast<TxType>(value - 1);
182}
183
184} // namespace ripple
std::unordered_map< std::uint16_t, Delegation > delegatableTx_
Definition Permissions.h:60
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::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
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