Skip to content

Commit 12e6fcc

Browse files
authored
1 parent f9d9879 commit 12e6fcc

File tree

3 files changed

+113
-53
lines changed

3 files changed

+113
-53
lines changed

src/rpc/handlers/GatewayBalances.hpp

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -108,44 +108,51 @@ class GatewayBalancesHandler {
108108
static RpcSpecConstRef
109109
spec([[maybe_unused]] uint32_t apiVersion)
110110
{
111-
static auto const kHOT_WALLET_VALIDATOR =
112-
validation::CustomValidator{[](boost::json::value const& value, std::string_view key) -> MaybeError {
113-
if (!value.is_string() && !value.is_array())
114-
return Error{Status{RippledError::rpcINVALID_PARAMS, std::string(key) + "NotStringOrArray"}};
115-
116-
// wallet needs to be an valid accountID or public key
117-
auto const wallets = value.is_array() ? value.as_array() : boost::json::array{value};
118-
auto const getAccountID = [](auto const& j) -> std::optional<ripple::AccountID> {
119-
if (j.is_string()) {
120-
auto const pk = util::parseBase58Wrapper<ripple::PublicKey>(
121-
ripple::TokenType::AccountPublic, boost::json::value_to<std::string>(j)
122-
);
123-
124-
if (pk)
125-
return ripple::calcAccountID(*pk);
126-
127-
return util::parseBase58Wrapper<ripple::AccountID>(boost::json::value_to<std::string>(j));
111+
auto const getHotWalletValidator = [](RippledError errCode) {
112+
return validation::CustomValidator{
113+
[errCode](boost::json::value const& value, std::string_view key) -> MaybeError {
114+
if (!value.is_string() && !value.is_array())
115+
return Error{Status{errCode, std::string(key) + "NotStringOrArray"}};
116+
117+
// wallet needs to be an valid accountID or public key
118+
auto const wallets = value.is_array() ? value.as_array() : boost::json::array{value};
119+
auto const getAccountID = [](auto const& j) -> std::optional<ripple::AccountID> {
120+
if (j.is_string()) {
121+
auto const pk = util::parseBase58Wrapper<ripple::PublicKey>(
122+
ripple::TokenType::AccountPublic, boost::json::value_to<std::string>(j)
123+
);
124+
125+
if (pk)
126+
return ripple::calcAccountID(*pk);
127+
128+
return util::parseBase58Wrapper<ripple::AccountID>(boost::json::value_to<std::string>(j));
129+
}
130+
131+
return {};
132+
};
133+
134+
for (auto const& wallet : wallets) {
135+
if (!getAccountID(wallet))
136+
return Error{Status{errCode, std::string(key) + "Malformed"}};
128137
}
129138

130-
return {};
131-
};
132-
133-
for (auto const& wallet : wallets) {
134-
if (!getAccountID(wallet))
135-
return Error{Status{RippledError::rpcINVALID_PARAMS, std::string(key) + "Malformed"}};
139+
return MaybeError{};
136140
}
141+
};
142+
};
137143

138-
return MaybeError{};
139-
}};
140-
141-
static auto const kRPC_SPEC = RpcSpec{
144+
static auto const kSPEC_COMMON = RpcSpec{
142145
{JS(account), validation::Required{}, validation::CustomValidators::accountValidator},
143146
{JS(ledger_hash), validation::CustomValidators::uint256HexStringValidator},
144-
{JS(ledger_index), validation::CustomValidators::ledgerIndexValidator},
145-
{JS(hotwallet), kHOT_WALLET_VALIDATOR}
147+
{JS(ledger_index), validation::CustomValidators::ledgerIndexValidator}
146148
};
147149

148-
return kRPC_SPEC;
150+
auto static const kSPEC_V1 =
151+
RpcSpec{kSPEC_COMMON, {{JS(hotwallet), getHotWalletValidator(ripple::rpcINVALID_HOTWALLET)}}};
152+
auto static const kSPEC_V2 =
153+
RpcSpec{kSPEC_COMMON, {{JS(hotwallet), getHotWalletValidator(ripple::rpcINVALID_PARAMS)}}};
154+
155+
return apiVersion == 1 ? kSPEC_V1 : kSPEC_V2;
149156
}
150157

151158
/**

tests/unit/rpc/handlers/BookOffersTests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ constexpr auto kPAYS20_XRP_GETS10_USD_BOOK_DIR = "7B1767D41DBCE79D9585CF9D0262A5
6868
// transfer rate x2
6969
constexpr auto kTRANSFER_RATE_X2 = 2000000000;
7070

71+
struct ParameterTestBundle {
72+
std::string testName;
73+
std::string testJson;
74+
std::string expectedError;
75+
std::string expectedErrorMessage;
76+
};
77+
7178
} // namespace
7279

7380
using namespace rpc;
@@ -81,13 +88,6 @@ struct RPCBookOffersHandlerTest : HandlerBaseTest {
8188
}
8289
};
8390

84-
struct ParameterTestBundle {
85-
std::string testName;
86-
std::string testJson;
87-
std::string expectedError;
88-
std::string expectedErrorMessage;
89-
};
90-
9191
struct RPCBookOffersParameterTest : RPCBookOffersHandlerTest, WithParamInterface<ParameterTestBundle> {};
9292

9393
TEST_P(RPCBookOffersParameterTest, CheckError)

tests/unit/rpc/handlers/GatewayBalancesTests.cpp

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ constexpr auto kINDEX1 = "1B8590C01B0006EDFA9ED60296DD052DC5E90F99659B25014D08E1
6060
constexpr auto kINDEX2 = "E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC321";
6161
constexpr auto kTXN_ID = "E3FE6EA3D48F0C2B639448020EA4F03D4F4F8FFDB243A852A0F59177921B4879";
6262

63+
struct ParameterTestBundle {
64+
std::string testName;
65+
std::string testJson;
66+
std::string expectedError;
67+
std::string expectedErrorMessage;
68+
std::uint32_t apiVersion = 1u;
69+
};
6370
} // namespace
6471

6572
struct RPCGatewayBalancesHandlerTest : HandlerBaseTest {
@@ -69,21 +76,15 @@ struct RPCGatewayBalancesHandlerTest : HandlerBaseTest {
6976
}
7077
};
7178

72-
struct ParameterTestBundle {
73-
std::string testName;
74-
std::string testJson;
75-
std::string expectedError;
76-
std::string expectedErrorMessage;
77-
};
78-
7979
struct ParameterTest : public RPCGatewayBalancesHandlerTest, public WithParamInterface<ParameterTestBundle> {};
8080

8181
TEST_P(ParameterTest, CheckError)
8282
{
8383
auto bundle = GetParam();
8484
auto const handler = AnyHandler{GatewayBalancesHandler{backend_}};
8585
runSpawn([&](auto yield) {
86-
auto const output = handler.process(json::parse(bundle.testJson), Context{yield});
86+
auto const output =
87+
handler.process(json::parse(bundle.testJson), Context{.yield = yield, .apiVersion = bundle.apiVersion});
8788
ASSERT_FALSE(output);
8889
auto const err = rpc::makeError(output.result.error());
8990
EXPECT_EQ(err.at("error").as_string(), bundle.expectedError);
@@ -155,53 +156,105 @@ generateParameterTestBundles()
155156
.expectedErrorMessage = "ledger_hashNotString"
156157
},
157158
ParameterTestBundle{
158-
.testName = "WalletsNotStringOrArray",
159+
.testName = "WalletsNotStringOrArrayV1",
159160
.testJson = fmt::format(
160161
R"({{
161162
"account": "{}",
162163
"hotwallet": 12
163164
}})",
164165
kACCOUNT
165166
),
166-
.expectedError = "invalidParams",
167+
.expectedError = "invalidHotWallet",
167168
.expectedErrorMessage = "hotwalletNotStringOrArray"
168169
},
169170
ParameterTestBundle{
170-
.testName = "WalletsNotStringAccount",
171+
.testName = "WalletsNotStringAccountV1",
171172
.testJson = fmt::format(
172173
R"({{
173174
"account": "{}",
174175
"hotwallet": [12]
175176
}})",
176177
kACCOUNT
177178
),
178-
.expectedError = "invalidParams",
179+
.expectedError = "invalidHotWallet",
179180
.expectedErrorMessage = "hotwalletMalformed"
180181
},
181182
ParameterTestBundle{
182-
.testName = "WalletsInvalidAccount",
183+
.testName = "WalletsInvalidAccountV1",
183184
.testJson = fmt::format(
184185
R"({{
185186
"account": "{}",
186187
"hotwallet": ["12"]
187188
}})",
188189
kACCOUNT
189190
),
190-
.expectedError = "invalidParams",
191+
.expectedError = "invalidHotWallet",
191192
.expectedErrorMessage = "hotwalletMalformed"
192193
},
193194
ParameterTestBundle{
194-
.testName = "WalletInvalidAccount",
195+
.testName = "WalletInvalidAccountV1",
195196
.testJson = fmt::format(
196197
R"({{
197198
"account": "{}",
198199
"hotwallet": "12"
199200
}})",
200201
kACCOUNT
201202
),
202-
.expectedError = "invalidParams",
203+
.expectedError = "invalidHotWallet",
203204
.expectedErrorMessage = "hotwalletMalformed"
204205
},
206+
ParameterTestBundle{
207+
.testName = "WalletsNotStringOrArrayV2",
208+
.testJson = fmt::format(
209+
R"({{
210+
"account": "{}",
211+
"hotwallet": 12
212+
}})",
213+
kACCOUNT
214+
),
215+
.expectedError = "invalidParams",
216+
.expectedErrorMessage = "hotwalletNotStringOrArray",
217+
.apiVersion = 2u
218+
},
219+
ParameterTestBundle{
220+
.testName = "WalletsNotStringAccountV2",
221+
.testJson = fmt::format(
222+
R"({{
223+
"account": "{}",
224+
"hotwallet": [12]
225+
}})",
226+
kACCOUNT
227+
),
228+
.expectedError = "invalidParams",
229+
.expectedErrorMessage = "hotwalletMalformed",
230+
.apiVersion = 2u
231+
},
232+
ParameterTestBundle{
233+
.testName = "WalletsInvalidAccountV2",
234+
.testJson = fmt::format(
235+
R"({{
236+
"account": "{}",
237+
"hotwallet": ["12"]
238+
}})",
239+
kACCOUNT
240+
),
241+
.expectedError = "invalidParams",
242+
.expectedErrorMessage = "hotwalletMalformed",
243+
.apiVersion = 2u
244+
},
245+
ParameterTestBundle{
246+
.testName = "WalletInvalidAccountV2",
247+
.testJson = fmt::format(
248+
R"({{
249+
"account": "{}",
250+
"hotwallet": "12"
251+
}})",
252+
kACCOUNT
253+
),
254+
.expectedError = "invalidParams",
255+
.expectedErrorMessage = "hotwalletMalformed",
256+
.apiVersion = 2u
257+
},
205258
};
206259
}
207260

0 commit comments

Comments
 (0)