This repository was archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathsnapshot.cpp
More file actions
411 lines (319 loc) · 12.2 KB
/
snapshot.cpp
File metadata and controls
411 lines (319 loc) · 12.2 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
#include <eosio/chain/snapshot.hpp>
#include <eosio/chain/exceptions.hpp>
#include <fc/scoped_exit.hpp>
#include <fc/io/json.hpp>
namespace eosio { namespace chain {
variant_snapshot_writer::variant_snapshot_writer(fc::mutable_variant_object& snapshot)
: snapshot(snapshot)
{
snapshot.set("sections", fc::variants());
snapshot.set("version", current_snapshot_version );
}
void variant_snapshot_writer::write_start_section( const std::string& section_name ) {
current_rows.clear();
current_section_name = section_name;
}
void variant_snapshot_writer::write_row( const detail::abstract_snapshot_row_writer& row_writer ) {
current_rows.emplace_back(row_writer.to_variant());
}
void variant_snapshot_writer::write_end_section( ) {
snapshot["sections"].get_array().emplace_back(fc::mutable_variant_object()("name", std::move(current_section_name))("rows", std::move(current_rows)));
}
void variant_snapshot_writer::finalize() {
}
variant_snapshot_reader::variant_snapshot_reader(const fc::variant& snapshot)
:snapshot(snapshot)
,cur_section(nullptr)
,cur_row(0)
{
}
void variant_snapshot_reader::validate() const {
EOS_ASSERT(snapshot.is_object(), snapshot_validation_exception,
"Variant snapshot is not an object");
const fc::variant_object& o = snapshot.get_object();
EOS_ASSERT(o.contains("version"), snapshot_validation_exception,
"Variant snapshot has no version");
const auto& version = o["version"];
EOS_ASSERT(version.is_integer(), snapshot_validation_exception,
"Variant snapshot version is not an integer");
EOS_ASSERT(version.as_uint64() == (uint64_t)current_snapshot_version, snapshot_validation_exception,
"Variant snapshot is an unsuppored version. Expected : ${expected}, Got: ${actual}",
("expected", current_snapshot_version)("actual",o["version"].as_uint64()));
EOS_ASSERT(o.contains("sections"), snapshot_validation_exception,
"Variant snapshot has no sections");
const auto& sections = o["sections"];
EOS_ASSERT(sections.is_array(), snapshot_validation_exception, "Variant snapshot sections is not an array");
const auto& section_array = sections.get_array();
for( const auto& section: section_array ) {
EOS_ASSERT(section.is_object(), snapshot_validation_exception, "Variant snapshot section is not an object");
const auto& so = section.get_object();
EOS_ASSERT(so.contains("name"), snapshot_validation_exception,
"Variant snapshot section has no name");
EOS_ASSERT(so["name"].is_string(), snapshot_validation_exception,
"Variant snapshot section name is not a string");
EOS_ASSERT(so.contains("rows"), snapshot_validation_exception,
"Variant snapshot section has no rows");
EOS_ASSERT(so["rows"].is_array(), snapshot_validation_exception,
"Variant snapshot section rows is not an array");
}
}
bool variant_snapshot_reader::has_section( const string& section_name ) {
const auto& sections = snapshot["sections"].get_array();
for( const auto& section: sections ) {
if (section["name"].as_string() == section_name) {
return true;
}
}
return false;
}
void variant_snapshot_reader::set_section( const string& section_name ) {
const auto& sections = snapshot["sections"].get_array();
for( const auto& section: sections ) {
if (section["name"].as_string() == section_name) {
cur_section = §ion.get_object();
return;
}
}
EOS_THROW(snapshot_exception, "Variant snapshot has no section named ${n}", ("n", section_name));
}
bool variant_snapshot_reader::read_row( detail::abstract_snapshot_row_reader& row_reader ) {
const auto& rows = (*cur_section)["rows"].get_array();
row_reader.provide(rows.at(cur_row++));
return cur_row < rows.size();
}
bool variant_snapshot_reader::empty ( ) {
const auto& rows = (*cur_section)["rows"].get_array();
return rows.empty();
}
void variant_snapshot_reader::clear_section() {
cur_section = nullptr;
cur_row = 0;
}
void variant_snapshot_reader::return_to_header() {
clear_section();
}
ostream_snapshot_writer::ostream_snapshot_writer(std::ostream& snapshot)
:snapshot(snapshot)
,header_pos(snapshot.tellp())
,section_pos(-1)
,row_count(0)
{
// write magic number
auto totem = magic_number;
snapshot.write((char*)&totem, sizeof(totem));
// write version
auto version = current_snapshot_version;
snapshot.write((char*)&version, sizeof(version));
}
void ostream_snapshot_writer::write_start_section( const std::string& section_name )
{
EOS_ASSERT(section_pos == std::streampos(-1), snapshot_exception, "Attempting to write a new section without closing the previous section");
section_pos = snapshot.tellp();
row_count = 0;
uint64_t placeholder = std::numeric_limits<uint64_t>::max();
// write a placeholder for the section size
snapshot.write((char*)&placeholder, sizeof(placeholder));
// write placeholder for row count
snapshot.write((char*)&placeholder, sizeof(placeholder));
// write the section name (null terminated)
snapshot.write(section_name.data(), section_name.size());
snapshot.put(0);
}
void ostream_snapshot_writer::write_row( const detail::abstract_snapshot_row_writer& row_writer ) {
auto restore = snapshot.tellp();
try {
row_writer.write(snapshot);
} catch (...) {
snapshot.seekp(restore);
throw;
}
row_count++;
}
void ostream_snapshot_writer::write_end_section( ) {
auto restore = snapshot.tellp();
uint64_t section_size = restore - section_pos - sizeof(uint64_t);
snapshot.seekp(section_pos);
// write a the section size
snapshot.write((char*)§ion_size, sizeof(section_size));
// write the row count
snapshot.write((char*)&row_count, sizeof(row_count));
snapshot.seekp(restore);
section_pos = std::streampos(-1);
row_count = 0;
}
void ostream_snapshot_writer::finalize() {
uint64_t end_marker = std::numeric_limits<uint64_t>::max();
// write a placeholder for the section size
snapshot.write((char*)&end_marker, sizeof(end_marker));
}
ostream_json_snapshot_writer::ostream_json_snapshot_writer(std::ostream& snapshot)
:snapshot(snapshot)
,row_count(0)
{
snapshot << "{\n";
// write magic number
auto totem = magic_number;
snapshot << "\"magic_number\":" << fc::json::to_string(totem, fc::time_point::maximum()) << "\n";
// write version
auto version = current_snapshot_version;
snapshot << ",\"version\":" << fc::json::to_string(version, fc::time_point::maximum()) << "\n";
}
void ostream_json_snapshot_writer::write_start_section( const std::string& section_name )
{
row_count = 0;
snapshot.inner << "," << fc::json::to_string(section_name, fc::time_point::maximum()) << ":{\n";
}
void ostream_json_snapshot_writer::write_row( const detail::abstract_snapshot_row_writer& row_writer ) {
const auto yield = [&](size_t s) {};
if(row_count != 0) snapshot.inner << ",";
snapshot.inner << "\"row_" << row_count << "\":" << fc::json::to_string(row_writer.to_variant(), yield) << "\n";
++row_count;
}
void ostream_json_snapshot_writer::write_end_section( ) {
snapshot.inner << "}\n";
row_count = 0;
}
void ostream_json_snapshot_writer::finalize() {
snapshot.inner << "}\n";
snapshot.inner.flush();
}
istream_snapshot_reader::istream_snapshot_reader(std::istream& snapshot)
:snapshot(snapshot)
,header_pos(snapshot.tellg())
,num_rows(0)
,cur_row(0)
{
}
void istream_snapshot_reader::validate() const {
// make sure to restore the read pos
auto restore_pos = fc::make_scoped_exit([this,pos=snapshot.tellg(),ex=snapshot.exceptions()](){
snapshot.seekg(pos);
snapshot.exceptions(ex);
});
snapshot.exceptions(std::istream::failbit|std::istream::eofbit);
try {
// validate totem
auto expected_totem = ostream_snapshot_writer::magic_number;
decltype(expected_totem) actual_totem;
snapshot.read((char*)&actual_totem, sizeof(actual_totem));
EOS_ASSERT(actual_totem == expected_totem, snapshot_exception,
"Binary snapshot has unexpected magic number!");
// validate version
auto expected_version = current_snapshot_version;
decltype(expected_version) actual_version;
snapshot.read((char*)&actual_version, sizeof(actual_version));
EOS_ASSERT(actual_version == expected_version, snapshot_exception,
"Binary snapshot is an unsuppored version. Expected : ${expected}, Got: ${actual}",
("expected", expected_version)("actual", actual_version));
while (validate_section()) {}
} catch( const std::exception& e ) { \
snapshot_exception fce(FC_LOG_MESSAGE( warn, "Binary snapshot validation threw IO exception (${what})",("what",e.what())));
throw fce;
}
}
bool istream_snapshot_reader::validate_section() const {
uint64_t section_size = 0;
snapshot.read((char*)§ion_size,sizeof(section_size));
// stop when we see the end marker
if (section_size == std::numeric_limits<uint64_t>::max()) {
return false;
}
// seek past the section
snapshot.seekg(snapshot.tellg() + std::streamoff(section_size));
return true;
}
bool istream_snapshot_reader::has_section( const string& section_name ) {
auto restore_pos = fc::make_scoped_exit([this,pos=snapshot.tellg()](){
snapshot.seekg(pos);
});
const std::streamoff header_size = sizeof(ostream_snapshot_writer::magic_number) + sizeof(current_snapshot_version);
auto next_section_pos = header_pos + header_size;
while (true) {
snapshot.seekg(next_section_pos);
uint64_t section_size = 0;
snapshot.read((char*)§ion_size,sizeof(section_size));
if (section_size == std::numeric_limits<uint64_t>::max()) {
break;
}
next_section_pos = snapshot.tellg() + std::streamoff(section_size);
uint64_t ignore = 0;
snapshot.read((char*)&ignore,sizeof(ignore));
bool match = true;
for(auto c : section_name) {
if(snapshot.get() != c) {
match = false;
break;
}
}
if (match && snapshot.get() == 0) {
return true;
}
}
return false;
}
void istream_snapshot_reader::set_section( const string& section_name ) {
auto restore_pos = fc::make_scoped_exit([this,pos=snapshot.tellg()](){
snapshot.seekg(pos);
});
const std::streamoff header_size = sizeof(ostream_snapshot_writer::magic_number) + sizeof(current_snapshot_version);
auto next_section_pos = header_pos + header_size;
while (true) {
snapshot.seekg(next_section_pos);
uint64_t section_size = 0;
snapshot.read((char*)§ion_size,sizeof(section_size));
if (section_size == std::numeric_limits<uint64_t>::max()) {
break;
}
next_section_pos = snapshot.tellg() + std::streamoff(section_size);
uint64_t row_count = 0;
snapshot.read((char*)&row_count,sizeof(row_count));
bool match = true;
for(auto c : section_name) {
if(snapshot.get() != c) {
match = false;
break;
}
}
if (match && snapshot.get() == 0) {
cur_row = 0;
num_rows = row_count;
// leave the stream at the right point
restore_pos.cancel();
return;
}
}
EOS_THROW(snapshot_exception, "Binary snapshot has no section named ${n}", ("n", section_name));
}
bool istream_snapshot_reader::read_row( detail::abstract_snapshot_row_reader& row_reader ) {
row_reader.provide(snapshot);
return ++cur_row < num_rows;
}
bool istream_snapshot_reader::empty ( ) {
return num_rows == 0;
}
void istream_snapshot_reader::clear_section() {
num_rows = 0;
cur_row = 0;
}
void istream_snapshot_reader::return_to_header() {
snapshot.seekg( header_pos );
clear_section();
}
integrity_hash_snapshot_writer::integrity_hash_snapshot_writer(fc::sha256::encoder& enc)
:enc(enc)
{
}
void integrity_hash_snapshot_writer::write_start_section( const std::string& )
{
// no-op for structural details
}
void integrity_hash_snapshot_writer::write_row( const detail::abstract_snapshot_row_writer& row_writer ) {
row_writer.write(enc);
}
void integrity_hash_snapshot_writer::write_end_section( ) {
// no-op for structural details
}
void integrity_hash_snapshot_writer::finalize() {
// no-op for structural details
}
}}