forked from libbitcoin/libbitcoin-network
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodel.cpp
More file actions
424 lines (373 loc) · 10 KB
/
model.cpp
File metadata and controls
424 lines (373 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/network/messages/rpc/model.hpp>
#include <variant>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/messages/rpc/enums/version.hpp>
// boost::json parse/seralize is not exception safe.
namespace libbitcoin {
namespace network {
namespace rpc {
// version
// ----------------------------------------------------------------------------
DEFINE_JSON_FROM_TAG(version)
{
switch (instance)
{
case version::v1: value = "1.0"; break;
case version::v2: value = "2.0"; break;
default: value = {}; break;
}
}
DEFINE_JSON_TO_TAG(version)
{
if (value.is_string())
{
const auto& text = value.as_string();
if (text == "1.0") return version::v1;
if (text == "2.0") return version::v2;
}
return version::invalid;
}
// value_t
// ----------------------------------------------------------------------------
BC_PUSH_WARNING(NO_STATIC_CAST)
BC_PUSH_WARNING(NO_CASTS_FOR_ARITHMETIC_CONVERSION)
DEFINE_JSON_FROM_TAG(value_t)
{
// Boost serializes all numbers as double, but varies precision by type.
std::visit(overload
{
[&](null_t) NOEXCEPT
{
value = nullptr;
},
[&](boolean_t visit) NOEXCEPT
{
value = visit;
},
[&](number_t visit) NOEXCEPT
{
value = visit;
},
[&](int8_t visit) THROWS
{
value = visit;
},
[&](int16_t visit) THROWS
{
value = visit;
},
[&](int32_t visit) THROWS
{
value = visit;
},
[&](int64_t visit) THROWS
{
value = visit;
},
[&](uint8_t visit) THROWS
{
value = visit;
},
[&](uint16_t visit) THROWS
{
value = visit;
},
[&](uint32_t visit) THROWS
{
value = visit;
},
[&](uint64_t visit) THROWS
{
value = visit;
},
[&](const string_t& visit) THROWS
{
value = visit;
},
[&](const json_t& visit) THROWS
{
// This is a deep copy.
value = visit;
},
[&](const array_t& visit) THROWS
{
value.emplace_array();
auto& array = value.as_array();
array.reserve(visit.size());
for (const auto& element: visit)
array.push_back(value_from(element));
},
[&](const object_t& visit) THROWS
{
value.emplace_object();
auto& object = value.as_object();
for (const auto& pair: visit)
object.emplace(pair.first, value_from(pair.second));
}
}, instance.value());
}
BC_POP_WARNING()
BC_POP_WARNING()
DEFINE_JSON_TO_TAG(value_t)
{
if (value.is_null())
{
return { std::in_place_type<null_t> };
}
else if (value.is_bool())
{
return { std::in_place_type<boolean_t>, value.as_bool() };
}
else if (value.is_number())
{
// In the general model, all numbers deserialize from double.
return { std::in_place_type<number_t>, value.to_number<number_t>() };
}
else if (value.is_string())
{
return { std::in_place_type<string_t>, value.as_string() };
}
else if (value.is_array())
{
return { std::in_place_type<array_t>, value_to<array_t>(value) };
}
else if (value.is_object())
{
return { std::in_place_type<object_t>, value_to<object_t>(value) };
}
throw ostream_exception{ "value_t" };
}
// identity_t
// ----------------------------------------------------------------------------
DEFINE_JSON_FROM_TAG(identity_t)
{
std::visit(overload
{
[&](null_t) NOEXCEPT
{
value = json_t{};
},
[&](code_t visit) NOEXCEPT
{
value = visit;
},
[&](const string_t& visit) THROWS
{
value = visit;
}
}, instance);
}
DEFINE_JSON_TO_TAG(identity_t)
{
if (value.is_null())
{
return { null_t{} };
}
else if (value.is_number())
{
return { code_t{ value.to_number<code_t>() } };
}
else if (value.is_string())
{
return { string_t{ value.as_string() } };
}
throw ostream_exception{ "identity_t" };
}
// request_t
// ----------------------------------------------------------------------------
DEFINE_JSON_FROM_TAG(request_t)
{
boost::json::object object{};
if (instance.jsonrpc != version::undefined)
{
object["jsonrpc"] = value_from(instance.jsonrpc);
}
if (instance.id.has_value())
{
object["id"] = value_from(instance.id.value());
}
if (!instance.method.empty())
{
object["method"] = value_from(instance.method);
}
if (instance.params.has_value())
{
if (const auto& params = instance.params.value();
std::holds_alternative<array_t>(params))
{
object["params"] = value_from(std::get<array_t>(params));
}
else
{
object["params"] = value_from(std::get<object_t>(params));
}
}
value = object;
}
// Bogus warnings on `it` not checked.
BC_PUSH_WARNING(NO_UNGUARDED_POINTERS)
DEFINE_JSON_TO_TAG(request_t)
{
request_t request{};
const auto& object = value.as_object();
// jsonrpc
if (const auto it = object.find("jsonrpc"); it != object.end())
{
request.jsonrpc = value_to<version>(it->value());
}
// method
if (const auto it = object.find("method"); it != object.end())
{
request.method = it->value().as_string();
}
// id
if (const auto it = object.find("id"); it != object.end())
{
request.id = value_to<identity_t>(it->value());
}
// params
if (const auto it = object.find("params"); it != object.end())
{
if (const auto& params = it->value(); params.is_array())
{
request.params = params_t
{
std::in_place_type<array_t>, value_to<array_t>(params)
};
}
else if (params.is_object())
{
request.params = params_t
{
std::in_place_type<object_t>, value_to<object_t>(params)
};
}
}
return request;
}
// response_t
// ----------------------------------------------------------------------------
DEFINE_JSON_FROM_TAG(response_t)
{
boost::json::object object{};
if (instance.jsonrpc != version::undefined)
{
object["jsonrpc"] = value_from(instance.jsonrpc);
}
if (instance.id.has_value())
{
object["id"] = value_from(instance.id.value());
}
if (instance.error.has_value())
{
const auto& error = instance.error.value();
if (error.data.has_value())
{
object["error"] =
{
{ "code", error.code },
{ "message", error.message },
{ "data", value_from(error.data.value()) }
};
}
else
{
object["error"] =
{
{ "code", error.code },
{ "message", error.message }
};
}
}
else if (instance.jsonrpc == version::v1 ||
instance.jsonrpc == version::undefined)
{
object["error"] = json_t{};
}
if (instance.result.has_value())
{
object["result"] = value_from(instance.result.value());
}
else if (instance.jsonrpc == version::v1 ||
instance.jsonrpc == version::undefined)
{
object["result"] = json_t{};
}
value = object;
}
DEFINE_JSON_TO_TAG(response_t)
{
response_t response{};
const auto& object = value.as_object();
// jsonrpc
if (const auto it = object.find("jsonrpc"); it != object.end())
{
response.jsonrpc = value_to<version>(it->value());
}
// id
if (const auto it = object.find("id"); it != object.end())
{
if (const auto& id = it->value(); id.is_null())
{
response.id = { null_t{} };
}
else if (id.is_number())
{
response.id = { id.to_number<code_t>() };
}
else if (id.is_string())
{
response.id = { string_t{ id.as_string() } };
}
}
// error
if (const auto it = object.find("error"); it != object.end())
{
result_t result{};
const auto& error = it->value().as_object();
if (const auto code_it = error.find("code");
code_it != error.end())
{
result.code = code_it->value().to_number<code_t>();
}
if (const auto message_it = error.find("message");
message_it != error.end())
{
result.message = message_it->value().as_string();
}
if (const auto data_it = error.find("data");
data_it != error.end())
{
result.data = value_to<value_option>(data_it->value());
}
response.error = result;
}
// result
if (const auto it = object.find("result"); it != object.end())
{
response.result = value_to<value_option>(it->value());
}
return response;
}
BC_POP_WARNING()
} // namespace rpc
} // namespace network
} // namespace libbitcoin