-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathledger.cpp
332 lines (295 loc) · 8.79 KB
/
ledger.cpp
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
#include <string.h>
#include <unistd.h>
#include <fstream>
#include <sstream>
#include <curl/curl.h>
#include <json/json.h>
// This is very quick and dirty code to retrieve a ledger
// by sequence number from the S2 full history cluster
char *out_buf;
bool pretty = true;
std::string space = " ";
std::string eol = "\n";
std::string indent = "\t";
// Switch from pretty-printing to compact JSON
void setCompact()
{
pretty = false;
space = "";
eol = "";
indent = "";
}
// Structure to pass to the CURL write callback
struct user_data
{
char *buf;
int so_far;
};
// The CURL code needs this callback
size_t write_callback (char *ptr, size_t size, size_t len, void *userdata)
{
struct user_data* ud = (struct user_data*) userdata;
memcpy(ud->buf + ud->so_far, ptr, size * len);
ud->so_far += size * len;
return size * len;
}
// Write a JSON object to a stream
void writeJson(std::ofstream& f, int step, Json::Value const &v)
{
std::string pre;
std::string str;
if (! pretty)
{
Json::FastWriter w;
str = w.write (v);
}
else
{
for (int i = 0; i < step; ++i)
pre += indent;
Json::StyledStreamWriter w (indent);
std::ostringstream out;
w.write (out, v);
std::istringstream in(out.str());
str = out.str();
}
// Read the output line-by-line, apply
// formatting as needed, and write to output
std::string l;
bool first = true;
std::istringstream in (str);
while (std::getline (in, l))
{
if ((l == indent) || (l == ""))
continue;
if (first)
first = false;
else
f << eol;
f << pre << l;
}
}
// Execute a query against the S2 cluster of full history XRP Ledger notes.
// Note that this is a best-effort service that does not guarantee
// any particular level of reliability.
bool do_query (std::string const& method, Json::Value const& params, Json::Value& reply)
{
std::string q;
{
Json::Value query = Json::objectValue;
query["method"] = method;
Json::Value& p = (query["params"] = Json::arrayValue);
p.append(params);
Json::FastWriter w;
q = w.write (query);
}
CURL* c = curl_easy_init();
if (c == NULL)
return false;
struct user_data ud;
ud.buf = out_buf;
ud.so_far = 0;
CURLcode code;
code = curl_easy_setopt (c, CURLOPT_URL, "http://s2.ripple.com:51234");
code = curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, q.size());
code = curl_easy_setopt (c, CURLOPT_POSTFIELDS, q.c_str());
code = curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, write_callback);
code = curl_easy_setopt (c, CURLOPT_WRITEDATA, &ud);
code = curl_easy_perform (c);
if (code != CURLE_OK)
{
curl_easy_cleanup (c);
return false;
}
ud.buf[ud.so_far] = 0;
curl_easy_cleanup (c);
Json::Reader reader;
Json::Value root;
if (!reader.parse(out_buf, root))
{
fprintf(stderr, "%s\n", reader.getFormatedErrorMessages().c_str());
return false;
}
Json::Value& result = root["result"];
if (! result.isObject())
{
fprintf(stderr, "Result is not object\n");
return false;
}
Json::Value& status = result["status"];
if (!status.isString() || (status.asString() != "success"))
{
fprintf(stderr, "Result is '%s', not success\n", status.asString().c_str());
reply = result;
return false;
}
reply = std::move (root);
return true;
}
// Get the header of a ledger given its sequence number
bool getHeader (unsigned ledger_seq, Json::Value& header)
{
Json::Value reply;
Json::Value params = Json::objectValue;
params["ledger_index"] = ledger_seq;
if (! do_query ("ledger", params, reply))
{
header = reply;
return false;
}
header = reply["result"]["ledger"];
if (header.isObject() && !header.isNull())
return true;
header = reply;
return false;
}
// Get the transactions from a ledger given its sequence number
// (with metadata)
bool getTxns (unsigned ledger_seq, Json::Value& txns)
{
Json::Value reply;
Json::Value params = Json::objectValue;
params["ledger_index"] = ledger_seq;
params["transactions"] = true;
params["expand"] = true;
if (! do_query ("ledger", params, reply))
return false;
txns = std::move (reply["result"]["ledger"]);
if (txns.isNull() || !txns.isObject())
return false;
txns = std::move (txns["transactions"]);
return txns.isArray() && !txns.isNull();
}
// Get a chunk of a ledger's state tree
bool getState (unsigned ledger_seq, Json::Value& entries, Json::Value& marker)
{
Json::Value reply;
Json::Value params = Json::objectValue;
params["ledger_index"] = ledger_seq;
params["binary"] = false;
if (! marker.isNull())
params["marker"] = marker;
if (! do_query ("ledger_data", params, reply))
return false;
reply = std::move (reply["result"]);
marker = reply["marker"];
entries = std::move(reply["state"]);
return true;
}
// Old school hacky progress indicator
// Sorry, not sorry
void do_progress(int max, Json::Value const& marker)
{
static int so_far = 0;
static int phase = 0;
char phases[4] = { '\\', '|', '/', '-' };
int progress = max;
if (marker.isString())
{
// Hashes are randomly distributed, so we can tell how much
// progress we made by where we are in the hash space.
// This code only looks at the first two characters, 00-FF.
std::string m = marker.asString();
progress = 0;
if (m[0] >= '0' && m[0] <= '9') progress += 16 * (m[0] - '0');
if (m[0] >= 'A' && m[0] <= 'Z') progress += 16 * (m[0] + 10 - 'A');
if (m[1] >= '0' && m[1] <= '9') progress += m[1] - '0';
if (m[1] >= 'A' && m[1] <= 'Z') progress += m[1] + 10 - 'A';
progress *= max;
progress /= 255;
}
std::cout << "\x8 \x8";
while (so_far < progress)
{
std::cout << "*";
++so_far;
}
std::cout << phases[++phase % 4] << std::flush;
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
fprintf (stderr, "You must specify a ledger by index\n");
return -1;
}
// When compiling, define "COMPACT" if you want your JSON
// to be as short as possible instead of being neatly
// spaced and indented.
#ifdef COMPACT
setCompact();
#endif
out_buf = (char *) malloc (100000000);
if (out_buf == NULL)
return -1;
unsigned seq = (unsigned) atoll(argv[1]);
printf("Seq = %u\n", seq);
char fbuf[512];
sprintf(fbuf, "ledger.%u", seq);
std::ofstream f (fbuf, std::ofstream::out | std::ofstream::trunc);
if (! f.is_open())
return -1;
f << "{" << eol;
Json::Value reply;
if (! getHeader(seq, reply))
{
fprintf(stderr, "Query failed");
std::cerr << reply;
return -1;
}
f << indent << "\"ledger\"" << space << ":" << eol;;
writeJson (f, 1, reply);
f << "," << eol;
printf("Got header\n");
// If transaction_hash is all zeroes, there are no transactions to get
if (reply["transaction_hash"].asString() !=
"0000000000000000000000000000000000000000000000000000000000000000")
{
if (! getTxns(seq, reply))
{
fprintf(stderr, "Query failed\n");
return -1;
}
f << indent << "\"transactions\":" << eol;
writeJson (f, 1, reply);
f << indent << "," << eol;;
printf("Got %d transaction(s)\n", reply.size());
}
else printf("No transactions\n");
printf("Getting state tree:\n");
int e = 0;
std::string const progress = "0%--10%--20%--30%--40%--50%--60%--70%--80%--90%--100%";
std::cout << progress << std::endl;
bool first = true;
f << indent << "\"state\"" << space << ":" << eol << indent << "[" << eol;
Json::Value marker = Json::nullValue;
do
{
if (! getState (seq, reply, marker))
{
fprintf(stderr, "Query failed\n");
return -1;
}
for (unsigned j = 0u; j < reply.size(); ++j)
{
// We need to write out each state entry
// separately so we don't get start of object
// and end of object stuff from the chunk
if (first)
first = false;
else
f << "," << eol;
writeJson (f, 2, reply[j]);
++e;
}
do_progress (progress.size(), marker);
// empty returned marker indicates last chunk of data
} while (! marker.isNull());
std::cout << "\x8 " << std::endl;
printf("%d state entries\n", e);
f << eol << indent << "]" << eol;
f << "}" << eol;
f.close();
printf("File \"%s\" written\n", fbuf);
return 0;
}