Include hp version number on all required places. (#265)

* Adding HP version header to binary files and adding hp table to ledger.sqlite.

* Updating hp_version in user challenge message.

* Minor improvement.
This commit is contained in:
Savinda Senevirathne
2021-03-10 20:08:04 +05:30
committed by GitHub
parent 1eabe7db4a
commit b45cdb999d
10 changed files with 196 additions and 15 deletions

View File

@@ -199,6 +199,13 @@ namespace ledger
return -1;
}
// Create and update the hp_version table with current hp version.
if (sqlite::create_hp_version_table_and_update(*db, conf::cfg.hp_version) == -1)
{
LOG_ERROR << errno << ": Error creating and updating hp version table, shard: " << std::to_string(shard_seq_no);
return -1;
}
// Creating the ledger table.
if (sqlite::create_ledger_table(*db) == -1)
{
@@ -216,6 +223,15 @@ namespace ledger
return -1;
}
}
// Creating the hp_version header.
uint8_t hp_version_header[util::HP_VERSION_HEADER_SIZE];
if (util::create_hp_version_header(hp_version_header, conf::cfg.hp_version) == -1)
{
LOG_ERROR << "Error creating version header for prev_shard.hash in primary shard " << std::to_string(shard_seq_no);
return -1;
}
// Write the prev_shard.hash to the new folder.
const std::string shard_hash_file_path = shard_path + PREV_SHARD_HASH_FILENAME;
const int fd = open(shard_hash_file_path.data(), O_CREAT | O_RDWR, FILE_PERMS);
@@ -224,7 +240,15 @@ namespace ledger
LOG_ERROR << errno << ": Error creating prev_shard.hash file in shard " << std::to_string(shard_seq_no);
return -1;
}
if (write(fd, &prev_shard_hash, sizeof(util::h32)) == -1)
struct iovec iov_vec[2];
iov_vec[0].iov_base = hp_version_header;
iov_vec[0].iov_len = util::HP_VERSION_HEADER_SIZE;
iov_vec[1].iov_base = &prev_shard_hash;
iov_vec[1].iov_len = sizeof(util::h32);
if (writev(fd, iov_vec, 2) == -1)
{
LOG_ERROR << errno << ": Error writing to " << shard_hash_file_path << ".";
close(fd);
@@ -305,6 +329,14 @@ namespace ledger
should_create_folder = true;
}
// Creating the hp_version header.
uint8_t hp_version_header[util::HP_VERSION_HEADER_SIZE];
if (util::create_hp_version_header(hp_version_header, conf::cfg.hp_version) == -1)
{
LOG_ERROR << "Error creating version header for prev_shard.hash in blob shard " << std::to_string(last_blob_shard_seq_no);
return -1;
}
// Create the required shard folder if not already existing.
if (should_create_folder)
{
@@ -335,7 +367,15 @@ namespace ledger
LOG_ERROR << errno << ": Error creating prev_shard.hash file in blob shard " << std::to_string(last_blob_shard_seq_no);
return -1;
}
if (write(fd, &prev_shard_hash, sizeof(util::h32)) == -1)
struct iovec iov_vec[2];
iov_vec[0].iov_base = hp_version_header;
iov_vec[0].iov_len = util::HP_VERSION_HEADER_SIZE;
iov_vec[1].iov_base = &prev_shard_hash;
iov_vec[1].iov_len = sizeof(util::h32);
if (writev(fd, iov_vec, 2) == -1)
{
LOG_ERROR << errno << ": Error writing to " << shard_hash_file_path << ".";
close(fd);
@@ -387,7 +427,14 @@ namespace ledger
return -1;
}
if (write(fd, builder.GetBufferPointer(), builder.GetSize()) == -1)
struct iovec iov_vec[2];
iov_vec[0].iov_base = hp_version_header;
iov_vec[0].iov_len = util::HP_VERSION_HEADER_SIZE;
iov_vec[1].iov_base = builder.GetBufferPointer();
iov_vec[1].iov_len = builder.GetSize();
if (writev(fd, iov_vec, 2) == -1)
{
LOG_ERROR << errno << ": Error writing to ledger blob file. " << file_path;
close(fd);
@@ -481,9 +528,8 @@ namespace ledger
}
}
uint8_t last_shard_seq_no_buf[8];
if (read(fd, last_shard_seq_no_buf, 8) == -1)
if (pread(fd, last_shard_seq_no_buf, sizeof(last_shard_seq_no_buf), util::HP_VERSION_HEADER_SIZE) == -1)
{
LOG_ERROR << errno << ": Error reading " << last_shard_seq_no_path;
close(fd);
return -1;
@@ -513,7 +559,15 @@ namespace ledger
const std::string last_shard_seq_no_vpath = shard_parent_dir + SHARD_SEQ_NO_FILENAME;
const std::string last_shard_seq_no_path = ledger_fs.physical_path(hpfs::RW_SESSION_NAME, last_shard_seq_no_vpath);
// Write the prev_shard.hash to the new folder.
// Creating the hp_version header.
uint8_t hp_version_header[util::HP_VERSION_HEADER_SIZE];
if (util::create_hp_version_header(hp_version_header, conf::cfg.hp_version) == -1)
{
LOG_ERROR << "Error creating version header for max_shard.seq_no in " << shard_parent_dir;
return -1;
}
// Open max_shard.seq_no in given parent directory.
const int fd = open(last_shard_seq_no_path.data(), O_CREAT | O_RDWR, FILE_PERMS);
if (fd == -1)
{
@@ -522,7 +576,15 @@ namespace ledger
}
uint8_t seq_no_byte_str[8];
util::uint64_to_bytes(seq_no_byte_str, last_shard_seq_no);
if (write(fd, &seq_no_byte_str, 8) == -1)
struct iovec iov_vec[2];
iov_vec[0].iov_base = hp_version_header;
iov_vec[0].iov_len = util::HP_VERSION_HEADER_SIZE;
iov_vec[1].iov_base = seq_no_byte_str;
iov_vec[1].iov_len = sizeof(seq_no_byte_str);
if (writev(fd, iov_vec, 2) == -1)
{
LOG_ERROR << errno << ": Error updating the max_shard.seq_no file for shard " << std::to_string(last_shard_seq_no);
close(fd);

View File

@@ -17,7 +17,8 @@ namespace ledger
}
util::h32 prev_shard_hash_from_file;
const int res = read(fd, &prev_shard_hash_from_file, sizeof(util::h32));
// Start reading hash excluding hp_version header.
const int res = pread(fd, &prev_shard_hash_from_file, sizeof(util::h32), util::HP_VERSION_HEADER_SIZE);
close(fd);
if (res == -1)
{

View File

@@ -4,6 +4,8 @@ namespace ledger::sqlite
{
constexpr const char *LEDGER_TABLE = "ledger";
constexpr const char *LEDGER_COLUMNS = "seq_no, time, ledger_hash, prev_ledger_hash, data_hash, state_hash, patch_hash, user_hash, input_hash, output_hash";
constexpr const char *HP_VERSION_TABLE = "hp";
constexpr const char *HP_VERSION_COLUMN = "hp_version";
constexpr const char *COLUMN_DATA_TYPES[]{"INT", "TEXT"};
constexpr const char *CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";
constexpr const char *INSERT_INTO = "INSERT INTO ";
@@ -233,6 +235,29 @@ namespace ledger::sqlite
return 0;
}
/**
* Create and update the hp table from the hp version when creating a new shard.
* @param db Pointer to the db.
* @param version Hp version.
* @returns returns 0 on success, or -1 on error.
*
*/
int create_hp_version_table_and_update(sqlite3 *db, std::string_view version)
{
const std::vector<table_column_info> column_info{
table_column_info(HP_VERSION_COLUMN, COLUMN_DATA_TYPE::TEXT)};
if (create_table(db, HP_VERSION_TABLE, column_info) == -1)
return -1;
const std::string value_string = "\"" + std::string(version) + "\"";
if (insert_value(db, HP_VERSION_TABLE, HP_VERSION_COLUMN, value_string) == -1)
return -1;
return 0;
}
/**
* Inserts a ledger record.
* @param db Pointer to the db.

View File

@@ -99,6 +99,8 @@ namespace ledger::sqlite
// Ledger specific methdods.
int create_ledger_table(sqlite3 *db);
int create_hp_version_table_and_update(sqlite3 *db, std::string_view version);
int insert_ledger_row(sqlite3 *db, const ledger &ledger);
bool is_ledger_table_exist(sqlite3 *db);