xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
jtx
impl
permissioned_domains.cpp
1
#include <test/jtx/permissioned_domains.h>
2
3
#include <test/jtx/Account.h>
4
#include <test/jtx/Env.h>
5
6
#include <xrpl/basics/StringUtilities.h>
7
#include <xrpl/basics/base_uint.h>
8
#include <xrpl/basics/contract.h>
9
#include <xrpl/json/json_value.h>
10
#include <xrpl/json/to_string.h>
11
#include <xrpl/protocol/AccountID.h>
12
#include <xrpl/protocol/SField.h>
13
#include <xrpl/protocol/STObject.h>
14
#include <xrpl/protocol/jss.h>
15
16
#include <
map
>
17
#include <
memory
>
18
#include <
optional
>
19
#include <
set
>
20
#include <
stdexcept
>
21
#include <
tuple
>
22
#include <
unordered_map
>
23
#include <
utility
>
24
25
namespace
xrpl::test::jtx::pdomain
{
26
27
// helpers
28
// Make json for PermissionedDomainSet transaction
29
json::Value
30
setTx
(
AccountID
const
& account,
Credentials
const
&
credentials
,
std::optional<UInt256>
domain)
31
{
32
json::Value
jv;
33
jv[sfTransactionType] = jss::PermissionedDomainSet;
34
jv[sfAccount] =
to_string
(account);
35
if
(domain)
36
jv[sfDomainID] =
to_string
(*domain);
37
38
json::Value
acceptedCredentials(
json::ValueType::Array
);
39
for
(
auto
const
& credential :
credentials
)
40
{
41
json::Value
object(
json::ValueType::Object
);
42
object
[sfCredential] = credential.toJson();
43
acceptedCredentials.
append
(std::move(
object
));
44
}
45
46
jv[sfAcceptedCredentials] = acceptedCredentials;
47
return
jv;
48
}
49
50
// Make json for PermissionedDomainDelete transaction
51
json::Value
52
deleteTx
(
AccountID
const
& account,
UInt256
const
& domain)
53
{
54
json::Value
jv{
json::ValueType::Object
};
55
jv[sfTransactionType] = jss::PermissionedDomainDelete;
56
jv[sfAccount] =
to_string
(account);
57
jv[sfDomainID] =
to_string
(domain);
58
return
jv;
59
}
60
61
// Get PermissionedDomain objects by type from account_objects rpc call
62
std::map<UInt256, json::Value>
63
getObjects
(
Account
const
& account,
Env
& env,
bool
withType)
64
{
65
std::map<UInt256, json::Value>
ret;
66
json::Value
params;
67
params[jss::account] = account.human();
68
if
(withType)
69
params[jss::type] = jss::permissioned_domain;
70
71
auto
const
& resp = env.
rpc
(
"json"
,
"account_objects"
,
to_string
(params));
72
json::Value
objects(
json::ValueType::Array
);
73
objects = resp[jss::result][jss::account_objects];
74
for
(
auto
const
&
object
: objects)
75
{
76
if
(
object
[
"LedgerEntryType"
] !=
"PermissionedDomain"
)
77
{
78
if
(withType)
79
{
// impossible to get there
80
Throw<std::runtime_error>
(
81
"Invalid object type: "
+
82
object
[
"LedgerEntryType"
].asString());
// LCOV_EXCL_LINE
83
}
84
continue
;
85
}
86
87
UInt256
index;
88
std::ignore = index.parseHex(
object
[jss::index].asString());
89
ret[index] = object;
90
}
91
92
return
ret;
93
}
94
95
// Check if ledger object is there
96
bool
97
objectExists
(
UInt256
const
& objID,
Env
& env)
98
{
99
json::Value
params;
100
params[jss::index] =
to_string
(objID);
101
102
auto
const
result = env.
rpc
(
"json"
,
"ledger_entry"
,
to_string
(params))[
"result"
];
103
104
if
((result[
"status"
] ==
"error"
) && (result[
"error"
] ==
"entryNotFound"
))
105
return
false
;
106
107
if
((result[
"node"
][
"LedgerEntryType"
] != jss::PermissionedDomain))
108
return
false
;
109
110
if
(result[
"status"
] ==
"success"
)
111
return
true
;
112
113
throw
std::runtime_error
(
"Error getting ledger_entry RPC result."
);
114
}
115
116
// Extract credentials from account_object object
117
Credentials
118
credentialsFromJson
(
119
json::Value
const
&
object
,
120
std::unordered_map<std::string, Account>
const
& human2Acc)
121
{
122
Credentials
ret;
123
json::Value
credentials
(
json::ValueType::Array
);
124
credentials
=
object
[
"AcceptedCredentials"
];
125
for
(
auto
const
& credential :
credentials
)
126
{
127
json::Value
obj(
json::ValueType::Object
);
128
obj = credential[jss::Credential];
129
auto
const
& issuer = obj[jss::Issuer];
130
auto
const
& credentialType = obj[
"CredentialType"
];
131
// NOLINTNEXTLINE(bugprone-unchecked-optional-access): used only in tests
132
auto
blob =
strUnHex
(credentialType.asString()).value();
133
ret.
push_back
(
134
{.issuer = human2Acc.
at
(issuer.asString()),
135
.credType =
std::string
(blob.begin(), blob.end())});
136
}
137
return
ret;
138
}
139
140
// Sort credentials the same way as PermissionedDomainSet. Silently
141
// remove duplicates.
142
Credentials
143
sortCredentials
(
Credentials
const
& input)
144
{
145
std::set<Credential>
credentialsSet;
146
for
(
auto
const
& credential : input)
147
credentialsSet.
insert
(credential);
148
return
{credentialsSet.
begin
(), credentialsSet.
end
()};
149
}
150
151
UInt256
152
getNewDomain
(
std::shared_ptr<STObject const>
const
& meta)
153
{
154
UInt256
ret;
155
auto
metaJson = meta->getJson(
JsonOptions::Values::None
);
156
json::Value
a(
json::ValueType::Array
);
157
a = metaJson[
"AffectedNodes"
];
158
159
for
(
auto
const
& node : a)
160
{
161
if
(!node.isMember(
"CreatedNode"
) ||
162
node[
"CreatedNode"
][
"LedgerEntryType"
] !=
"PermissionedDomain"
)
163
{
164
continue
;
165
}
166
std::ignore = ret.
parseHex
(node[
"CreatedNode"
][
"LedgerIndex"
].asString());
167
break
;
168
}
169
170
return
ret;
171
}
172
173
}
// namespace xrpl::test::jtx::pdomain
std::unordered_map::at
T at(T... args)
std::string
std::set::begin
T begin(T... args)
json::Value
Represents a JSON value.
Definition
json_value.h:117
json::Value::append
Value & append(Value const &value)
Append value to array at the end.
Definition
json_value.cpp:997
xrpl::BaseUInt::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition
base_uint.h:525
xrpl::test::jtx::Account
Immutable cryptographic account descriptor.
Definition
jtx/Account.h:21
xrpl::test::jtx::Env
A transaction testing environment.
Definition
Env.h:161
xrpl::test::jtx::Env::rpc
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition
Env.h:1058
std::set::end
T end(T... args)
std::set::insert
T insert(T... args)
map
memory
json::ValueType::Array
@ Array
array value (ordered list)
Definition
json_value.h:28
json::ValueType::Object
@ Object
object value (collection of name/value pairs).
Definition
json_value.h:29
xrpl::test::jtx::credentials
Definition
credentials.h:18
xrpl::test::jtx::pdomain
Definition
permissioned_domains.cpp:25
xrpl::test::jtx::pdomain::getObjects
std::map< UInt256, json::Value > getObjects(Account const &account, Env &env, bool withType)
Definition
permissioned_domains.cpp:63
xrpl::test::jtx::pdomain::Credentials
std::vector< Credential > Credentials
Definition
permissioned_domains.h:23
xrpl::test::jtx::pdomain::credentialsFromJson
Credentials credentialsFromJson(json::Value const &object, std::unordered_map< std::string, Account > const &human2Acc)
Definition
permissioned_domains.cpp:118
xrpl::test::jtx::pdomain::deleteTx
json::Value deleteTx(AccountID const &account, UInt256 const &domain)
Definition
permissioned_domains.cpp:52
xrpl::test::jtx::pdomain::getNewDomain
UInt256 getNewDomain(std::shared_ptr< STObject const > const &meta)
Definition
permissioned_domains.cpp:152
xrpl::test::jtx::pdomain::setTx
json::Value setTx(AccountID const &account, Credentials const &credentials, std::optional< UInt256 > domain)
Definition
permissioned_domains.cpp:30
xrpl::test::jtx::pdomain::objectExists
bool objectExists(UInt256 const &objID, Env &env)
Definition
permissioned_domains.cpp:97
xrpl::test::jtx::pdomain::sortCredentials
Credentials sortCredentials(Credentials const &input)
Definition
permissioned_domains.cpp:143
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::UInt256
BaseUInt< 256 > UInt256
Definition
base_uint.h:580
xrpl::strUnHex
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Definition
StringUtilities.h:64
xrpl::AccountID
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition
AccountID.h:34
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
optional
std::vector::push_back
T push_back(T... args)
std::runtime_error
set
std::shared_ptr
stdexcept
xrpl::JsonOptions::Values::None
@ None
Definition
STBase.h:27
tuple
unordered_map
utility
Generated by
1.17.0