Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::chain::trim_data Struct Reference

#include <block_log.hpp>

Collaboration diagram for sysio::chain::trim_data:

Public Member Functions

 trim_data (fc::path block_dir)
 
 ~trim_data ()
 
uint64_t block_index (uint32_t n) const
 
uint64_t block_pos (uint32_t n)
 

Public Attributes

fc::path block_file_name
 
fc::path index_file_name
 
uint32_t version = 0
 
uint32_t first_block = 0
 
uint32_t last_block = 0
 
FILE * blk_in = nullptr
 
FILE * ind_in = nullptr
 
uint64_t first_block_pos = 0
 
chain_id_type chain_id
 

Static Public Attributes

static constexpr int blknum_offset {14}
 

Detailed Description

Definition at line 109 of file block_log.hpp.

Constructor & Destructor Documentation

◆ trim_data()

sysio::chain::trim_data::trim_data ( fc::path block_dir)

Definition at line 1308 of file block_log.cpp.

1308 {
1309
1310 // code should follow logic in block_log::repair_log
1311
1312 using namespace std;
1313 block_file_name = block_dir / "blocks.log";
1314 index_file_name = block_dir / "blocks.index";
1315 blk_in = FC_FOPEN(block_file_name.generic_string().c_str(), "rb");
1316 SYS_ASSERT( blk_in != nullptr, block_log_not_found, "cannot read file ${file}", ("file",block_file_name.string()) );
1317 ind_in = FC_FOPEN(index_file_name.generic_string().c_str(), "rb");
1318 SYS_ASSERT( ind_in != nullptr, block_log_not_found, "cannot read file ${file}", ("file",index_file_name.string()) );
1319 auto size = fread((void*)&version,sizeof(version), 1, blk_in);
1320 SYS_ASSERT( size == 1, block_log_unsupported_version, "invalid format for file ${file}", ("file",block_file_name.string()));
1321 ilog("block log version= ${version}",("version",version));
1322 bool is_pruned = detail::is_pruned_log_and_mask_version(version);
1323 SYS_ASSERT( !is_pruned, block_log_unsupported_version, "Block log is currently in pruned format, it must be vacuumed before doing this operation");
1324 SYS_ASSERT( block_log::is_supported_version(version), block_log_unsupported_version, "block log version ${v} is not supported", ("v",version));
1325
1326 detail::fileptr_datastream ds(blk_in, block_file_name.string());
1327 if (version == 1) {
1328 first_block = 1;
1329 genesis_state gs;
1330 fc::raw::unpack(ds, gs);
1331 chain_id = gs.compute_chain_id();
1332 }
1333 else {
1334 size = fread((void *) &first_block, sizeof(first_block), 1, blk_in);
1335 SYS_ASSERT(size == 1, block_log_exception, "invalid format for file ${file}",
1336 ("file", block_file_name.string()));
1338 genesis_state gs;
1339 fc::raw::unpack(ds, gs);
1340 chain_id = gs.compute_chain_id();
1341 }
1343 ds >> chain_id;
1344 }
1345 else {
1346 SYS_THROW( block_log_exception,
1347 "Block log ${file} is not supported. version: ${ver} and first_block: ${first_block} does not contain "
1348 "a genesis_state nor a chain_id.",
1349 ("file", block_file_name.string())("ver", version)("first_block", first_block));
1350 }
1351
1352 const auto expected_totem = block_log::npos;
1353 std::decay_t<decltype(block_log::npos)> actual_totem;
1354 size = fread ( (char*)&actual_totem, sizeof(actual_totem), 1, blk_in);
1355
1356 SYS_ASSERT(size == 1, block_log_exception,
1357 "Expected to read ${size} bytes, but did not read any bytes", ("size", sizeof(actual_totem)));
1358 SYS_ASSERT(actual_totem == expected_totem, block_log_exception,
1359 "Expected separator between block log header and blocks was not found( expected: ${e}, actual: ${a} )",
1360 ("e", fc::to_hex((char*)&expected_totem, sizeof(expected_totem) ))("a", fc::to_hex((char*)&actual_totem, sizeof(actual_totem) )));
1361 }
1362
1363 const uint64_t start_of_blocks = ftell(blk_in);
1364
1365 const auto status = fseek(ind_in, 0, SEEK_END); //get length of blocks.index (gives number of blocks)
1366 SYS_ASSERT( status == 0, block_log_exception, "cannot seek to ${file} end", ("file", index_file_name.string()) );
1367 const uint64_t file_end = ftell(ind_in); //get length of blocks.index (gives number of blocks)
1368 last_block = first_block + file_end/sizeof(uint64_t) - 1;
1369
1371 SYS_ASSERT(start_of_blocks == first_block_pos, block_log_exception,
1372 "Block log ${file} was determined to have its first block at ${determined}, but the block index "
1373 "indicates the first block is at ${index}",
1374 ("file", block_file_name.string())("determined", start_of_blocks)("index",first_block_pos));
1375 ilog("first block= ${first}",("first",first_block));
1376 ilog("last block= ${last}",("last",last_block));
1377 }
#define FC_FOPEN(p, m)
Definition block_log.cpp:16
#define SYS_THROW(exc_type, FORMAT,...)
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
std::string string() const
std::string generic_string() const
static const uint64_t npos
Definition block_log.hpp:73
static bool contains_chain_id(uint32_t version, uint32_t first_block_num)
static bool is_supported_version(uint32_t version)
static bool contains_genesis_state(uint32_t version, uint32_t first_block_num)
#define ilog(FORMAT,...)
Definition logger.hpp:118
static const Segment gs(Segment::gs)
static const Segment ds(Segment::ds)
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
fc::string to_hex(const char *d, uint32_t s)
Definition hex.cpp:17
Definition name.hpp:106
const string block_dir
Definition main.cpp:48
unsigned __int64 uint64_t
Definition stdint.h:136
uint64_t block_pos(uint32_t n)
Here is the call graph for this function:

◆ ~trim_data()

sysio::chain::trim_data::~trim_data ( )

Definition at line 1379 of file block_log.cpp.

1379 {
1380 if (blk_in != nullptr)
1381 fclose(blk_in);
1382 if (ind_in != nullptr)
1383 fclose(ind_in);
1384 }

Member Function Documentation

◆ block_index()

uint64_t sysio::chain::trim_data::block_index ( uint32_t n) const

Definition at line 1386 of file block_log.cpp.

1386 {
1387 using namespace std;
1388 SYS_ASSERT( first_block <= n, block_log_exception,
1389 "cannot seek in ${file} to block number ${b}, block number ${first} is the first block",
1390 ("file", index_file_name.string())("b",n)("first",first_block) );
1391 SYS_ASSERT( n <= last_block, block_log_exception,
1392 "cannot seek in ${file} to block number ${b}, block number ${last} is the last block",
1393 ("file", index_file_name.string())("b",n)("last",last_block) );
1394 return sizeof(uint64_t) * (n - first_block);
1395 }
Here is the caller graph for this function:

◆ block_pos()

uint64_t sysio::chain::trim_data::block_pos ( uint32_t n)

Definition at line 1397 of file block_log.cpp.

1397 {
1398 using namespace std;
1399 // can indicate the location of the block after the last block
1400 if (n == last_block + 1) {
1401 return ftell(blk_in);
1402 }
1403 const uint64_t index_pos = block_index(n);
1404 auto status = fseek(ind_in, index_pos, SEEK_SET);
1405 SYS_ASSERT( status == 0, block_log_exception, "cannot seek to ${file} ${pos} from beginning of file for block ${b}", ("file", index_file_name.string())("pos", index_pos)("b",n) );
1406 const uint64_t pos = ftell(ind_in);
1407 SYS_ASSERT( pos == index_pos, block_log_exception, "cannot seek to ${file} entry for block ${b}", ("file", index_file_name.string())("b",n) );
1408 uint64_t block_n_pos;
1409 auto size = fread((void*)&block_n_pos, sizeof(block_n_pos), 1, ind_in); //filepos of block n
1410 SYS_ASSERT( size == 1, block_log_exception, "cannot read ${file} entry for block ${b}", ("file", index_file_name.string())("b",n) );
1411
1412 //read blocks.log and verify block number n is found at the determined file position
1413 const auto calc_blknum_pos = block_n_pos + blknum_offset;
1414 status = fseek(blk_in, calc_blknum_pos, SEEK_SET);
1415 SYS_ASSERT( status == 0, block_log_exception, "cannot seek to ${file} ${pos} from beginning of file", ("file", block_file_name.string())("pos", calc_blknum_pos) );
1416 const uint64_t block_offset_pos = ftell(blk_in);
1417 SYS_ASSERT( block_offset_pos == calc_blknum_pos, block_log_exception, "cannot seek to ${file} ${pos} from beginning of file", ("file", block_file_name.string())("pos", calc_blknum_pos) );
1418 uint32_t prior_blknum;
1419 size = fread((void*)&prior_blknum, sizeof(prior_blknum), 1, blk_in); //read bigendian block number of prior block
1420 SYS_ASSERT( size == 1, block_log_exception, "cannot read prior block");
1421 const uint32_t bnum = fc::endian_reverse_u32(prior_blknum) + 1; //convert to little endian, add 1 since prior block
1422 SYS_ASSERT( bnum == n, block_log_exception,
1423 "At position ${pos} in ${file} expected to find ${exp_bnum} but found ${act_bnum}",
1424 ("pos",block_offset_pos)("file", block_file_name.string())("exp_bnum",n)("act_bnum",bnum) );
1425
1426 return block_n_pos;
1427 }
uint32_t endian_reverse_u32(uint32_t x)
Definition bitutil.hpp:19
unsigned int uint32_t
Definition stdint.h:126
uint64_t block_index(uint32_t n) const
static constexpr int blknum_offset
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ blk_in

FILE* sysio::chain::trim_data::blk_in = nullptr

Definition at line 118 of file block_log.hpp.

◆ blknum_offset

int sysio::chain::trim_data::blknum_offset {14}
staticconstexpr

Definition at line 124 of file block_log.hpp.

124{14}; //offset from start of block to 4 byte block number, valid for the only allowed versions

◆ block_file_name

fc::path sysio::chain::trim_data::block_file_name

Definition at line 114 of file block_log.hpp.

◆ chain_id

chain_id_type sysio::chain::trim_data::chain_id

Definition at line 122 of file block_log.hpp.

◆ first_block

uint32_t sysio::chain::trim_data::first_block = 0

Definition at line 116 of file block_log.hpp.

◆ first_block_pos

uint64_t sysio::chain::trim_data::first_block_pos = 0

Definition at line 121 of file block_log.hpp.

◆ ind_in

FILE* sysio::chain::trim_data::ind_in = nullptr

Definition at line 119 of file block_log.hpp.

◆ index_file_name

fc::path sysio::chain::trim_data::index_file_name

Definition at line 114 of file block_log.hpp.

◆ last_block

uint32_t sysio::chain::trim_data::last_block = 0

Definition at line 117 of file block_log.hpp.

◆ version

uint32_t sysio::chain::trim_data::version = 0

Definition at line 115 of file block_log.hpp.


The documentation for this struct was generated from the following files: