Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
main.cpp File Reference
#include <memory>
#include <sysio/chain/abi_serializer.hpp>
#include <sysio/chain/block_log.hpp>
#include <sysio/chain/fork_database.hpp>
#include <sysio/chain/config.hpp>
#include <fc/io/json.hpp>
#include <fc/filesystem.hpp>
#include <fc/variant.hpp>
#include <fc/bitutil.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include <chrono>
Include dependency graph for main.cpp:

Go to the source code of this file.

Classes

struct  blocklog
 
struct  report_time
 

Macros

#define FOPEN(p, m)
 

Functions

int trim_blocklog_end (bfs::path block_dir, uint32_t n)
 
bool trim_blocklog_front (bfs::path block_dir, uint32_t n)
 
void smoke_test (bfs::path block_dir)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ FOPEN

#define FOPEN ( p,
m )
Value:
fopen(p, m)
const mie::Vuint & p
Definition bn.cpp:27

Definition at line 20 of file main.cpp.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 292 of file main.cpp.

292 {
293 std::ios::sync_with_stdio(false); // for potential performance boost for large block log files
294 options_description cli ("sysio-blocklog command line options");
295 try {
296 blocklog blog;
298 variables_map vmap;
299 bpo::store(bpo::parse_command_line(argc, argv, cli), vmap);
300 bpo::notify(vmap);
301 if (blog.help) {
302 cli.print(std::cerr);
303 return 0;
304 }
305 if (blog.smoke_test) {
306 smoke_test(vmap.at("blocks-dir").as<bfs::path>());
307 return 0;
308 }
309 if (blog.trim_log) {
310 if (blog.first_block == 0 && blog.last_block == std::numeric_limits<uint32_t>::max()) {
311 std::cerr << "trim-blocklog does nothing unless specify first and/or last block.";
312 return -1;
313 }
314 if (blog.last_block != std::numeric_limits<uint32_t>::max()) {
315 if (trim_blocklog_end(vmap.at("blocks-dir").as<bfs::path>(), blog.last_block) != 0)
316 return -1;
317 }
318 if (blog.first_block != 0) {
319 if (!trim_blocklog_front(vmap.at("blocks-dir").as<bfs::path>(), blog.first_block))
320 return -1;
321 }
322 return 0;
323 }
324 if (blog.vacuum) {
325 blog.initialize(vmap);
326 blog.do_vacuum();
327 return 0;
328 }
329 if (blog.make_index) {
330 const bfs::path blocks_dir = vmap.at("blocks-dir").as<bfs::path>();
331 bfs::path out_file = blocks_dir / "blocks.index";
332 const bfs::path block_file = blocks_dir / "blocks.log";
333
334 if (vmap.count("output-file") > 0)
335 out_file = vmap.at("output-file").as<bfs::path>();
336
337 report_time rt("making index");
340 block_log::construct_index(block_file.generic_string(), out_file.generic_string());
342 rt.report();
343 return 0;
344 }
345 //else print blocks.log as JSON
346 blog.initialize(vmap);
347 blog.read_log();
348 } catch( const fc::exception& e ) {
349 elog( "${e}", ("e", e.to_detail_string()));
350 return -1;
351 } catch( const boost::exception& e ) {
352 elog("${e}", ("e",boost::diagnostic_information(e)));
353 return -1;
354 } catch( const std::exception& e ) {
355 elog("${e}", ("e",e.what()));
356 return -1;
357 } catch( ... ) {
358 elog("unknown exception");
359 return -1;
360 }
361
362 return 0;
363}
Used to generate a useful error report when an exception is thrown.
Definition exception.hpp:58
std::string to_detail_string(log_level ll=log_level::all) const
logger & set_log_level(log_level e)
Definition logger.cpp:100
static logger get(const fc::string &name=DEFAULT_LOGGER)
Definition logger.cpp:88
log_level get_log_level() const
Definition logger.cpp:99
char ** argv
#define DEFAULT_LOGGER
Definition logger.hpp:7
#define elog(FORMAT,...)
Definition logger.hpp:130
void smoke_test(bfs::path block_dir)
Definition main.cpp:262
int trim_blocklog_end(bfs::path block_dir, uint32_t n)
Definition main.cpp:231
bool trim_blocklog_front(bfs::path block_dir, uint32_t n)
Definition main.cpp:254
void do_vacuum()
Definition main.cpp:72
void set_program_options(options_description &cli)
Definition main.cpp:178
bool help
Definition main.cpp:52
bool make_index
Definition main.cpp:48
uint32_t last_block
Definition main.cpp:45
bool smoke_test
Definition main.cpp:50
bool trim_log
Definition main.cpp:49
void initialize(const variables_map &options)
Definition main.cpp:205
void read_log()
Definition main.cpp:78
bool vacuum
Definition main.cpp:51
uint32_t first_block
Definition main.cpp:44
void cli()
Here is the call graph for this function:

◆ smoke_test()

void smoke_test ( bfs::path block_dir)

Definition at line 262 of file main.cpp.

262 {
263 using namespace std;
264 cout << "\nSmoke test of blocks.log and blocks.index in directory " << block_dir << '\n';
266 auto status = fseek(td.blk_in, -sizeof(uint64_t), SEEK_END); //get last_block from blocks.log, compare to from blocks.index
267 SYS_ASSERT( status == 0, block_log_exception, "cannot seek to ${file} ${pos} from beginning of file", ("file", td.block_file_name.string())("pos", sizeof(uint64_t)) );
268 uint64_t file_pos;
269 auto size = fread((void*)&file_pos, sizeof(uint64_t), 1, td.blk_in);
270 SYS_ASSERT( size == 1, block_log_exception, "${file} read fails", ("file", td.block_file_name.string()) );
271 status = fseek(td.blk_in, file_pos + trim_data::blknum_offset, SEEK_SET);
272 SYS_ASSERT( status == 0, block_log_exception, "cannot seek to ${file} ${pos} from beginning of file", ("file", td.block_file_name.string())("pos", file_pos + trim_data::blknum_offset) );
273 uint32_t bnum;
274 size = fread((void*)&bnum, sizeof(uint32_t), 1, td.blk_in);
275 SYS_ASSERT( size == 1, block_log_exception, "${file} read fails", ("file", td.block_file_name.string()) );
276 bnum = endian_reverse_u32(bnum) + 1; //convert from big endian to little endian and add 1
277 SYS_ASSERT( td.last_block == bnum, block_log_exception, "blocks.log says last block is ${lb} which disagrees with blocks.index", ("lb", bnum) );
278 cout << "blocks.log and blocks.index agree on number of blocks\n";
279 uint32_t delta = (td.last_block + 8 - td.first_block) >> 3;
280 if (delta < 1)
281 delta = 1;
282 for (uint32_t n = td.first_block; ; n += delta) {
283 if (n > td.last_block)
284 n = td.last_block;
285 td.block_pos(n); //check block 'n' is where blocks.index says
286 if (n == td.last_block)
287 break;
288 }
289 cout << "\nno problems found\n"; //if get here there were no exceptions
290}
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
uint32_t endian_reverse_u32(uint32_t x)
Definition bitutil.hpp:19
Definition name.hpp:106
const string block_dir
Definition main.cpp:48
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
static constexpr int blknum_offset
Here is the call graph for this function:
Here is the caller graph for this function:

◆ trim_blocklog_end()

int trim_blocklog_end ( bfs::path block_dir,
uint32_t n )

Definition at line 231 of file main.cpp.

231 { //n is last block to keep (remove later blocks)
232 report_time rt("trimming blocklog end");
233 using namespace std;
235 cout << "\nIn directory " << block_dir << " will trim all blocks after block " << n << " from "
236 << td.block_file_name.generic_string() << " and " << td.index_file_name.generic_string() << ".\n";
237 if (n < td.first_block) {
238 cerr << "All blocks are after block " << n << " so do nothing (trim_end would delete entire blocks.log)\n";
239 return 1;
240 }
241 if (n >= td.last_block) {
242 cerr << "There are no blocks after block " << n << " so do nothing\n";
243 return 2;
244 }
245 const uint64_t end_of_new_file = td.block_pos(n + 1);
246 bfs::resize_file(td.block_file_name, end_of_new_file);
247 const uint64_t index_end= td.block_index(n) + sizeof(uint64_t); //advance past record for block n
248 bfs::resize_file(td.index_file_name, index_end);
249 cout << "blocks.index has been trimmed to " << index_end << " bytes\n";
250 rt.report();
251 return 0;
252}
std::ostream & cerr()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ trim_blocklog_front()

bool trim_blocklog_front ( bfs::path block_dir,
uint32_t n )

Definition at line 254 of file main.cpp.

254 { //n is first block to keep (remove prior blocks)
255 report_time rt("trimming blocklog start");
256 const bool status = block_log::trim_blocklog_front(block_dir, block_dir / "old", n);
257 rt.report();
258 return status;
259}
static bool trim_blocklog_front(const fc::path &block_dir, const fc::path &temp_dir, uint32_t truncate_at_block)
Here is the call graph for this function:
Here is the caller graph for this function: