Skip to content

Commit

Permalink
Merge pull request #6040 from EOSIO/feature/respect-deterministic-row…
Browse files Browse the repository at this point in the history
…-conversion-in-integrity-hash

respect deterministic row conversion in integrity hash
  • Loading branch information
b1bart authored Oct 17, 2018
2 parents 05d848e + 21f0129 commit 28e498d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 58 deletions.
8 changes: 0 additions & 8 deletions libraries/chain/authorization_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ namespace eosio { namespace chain {
_db.create<permission_object>([](auto&){}); /// reserve perm 0 (used else where)
}

void authorization_manager::calculate_integrity_hash( fc::sha256::encoder& enc ) const {
authorization_index_set::walk_indices([this, &enc]( auto utils ){
decltype(utils)::walk(_db, [&enc]( const auto &row ) {
fc::raw::pack(enc, row);
});
});
}

namespace detail {
template<>
struct snapshot_row_traits<permission_object> {
Expand Down
49 changes: 10 additions & 39 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,23 +419,6 @@ struct controller_impl {
});
}

void calculate_contract_tables_integrity_hash( sha256::encoder& enc ) const {
index_utils<table_id_multi_index>::walk(db, [this, &enc]( const table_id_object& table_row ){
fc::raw::pack(enc, table_row);

contract_database_index_set::walk_indices([this, &enc, &table_row]( auto utils ) {
using value_t = typename decltype(utils)::index_t::value_type;
using by_table_id = object_to_table_id_tag_t<value_t>;

auto tid_key = boost::make_tuple(table_row.id);
auto next_tid_key = boost::make_tuple(table_id_object::id_type(table_row.id._id + 1));
decltype(utils)::template walk_range<by_table_id>(db, tid_key, next_tid_key, [&enc](const auto& row){
fc::raw::pack(enc, row);
});
});
});
}

void add_contract_tables_to_snapshot( const snapshot_writer_ptr& snapshot ) const {
snapshot->write_section("contract_tables", [this]( auto& section ) {
index_utils<table_id_multi_index>::walk(db, [this, &section]( const table_id_object& table_row ){
Expand Down Expand Up @@ -491,28 +474,6 @@ struct controller_impl {
});
}

sha256 calculate_integrity_hash() const {
sha256::encoder enc;
controller_index_set::walk_indices([this, &enc]( auto utils ){
using value_t = typename decltype(utils)::index_t::value_type;

// skip the table_id_object as its inlined with contract tables section
if (std::is_same<value_t, table_id_object>::value) {
return;
}

decltype(utils)::walk(db, [&enc]( const auto &row ) {
fc::raw::pack(enc, row);
});
});

calculate_contract_tables_integrity_hash(enc);

authorization.calculate_integrity_hash(enc);
resource_limits.calculate_integrity_hash(enc);
return enc.result();
}

void add_to_snapshot( const snapshot_writer_ptr& snapshot ) const {
snapshot->write_section<chain_snapshot_header>([this]( auto &section ){
section.add_row(chain_snapshot_header(), db);
Expand Down Expand Up @@ -593,6 +554,16 @@ struct controller_impl {
db.set_revision( head->block_num );
}

sha256 calculate_integrity_hash() const {
sha256::encoder enc;
auto hash_writer = std::make_shared<integrity_hash_snapshot_writer>(enc);
add_to_snapshot(hash_writer);
hash_writer->finalize();

return enc.result();
}


/**
* Sets fork database head to the genesis state.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace eosio { namespace chain {

void add_indices();
void initialize_database();
void calculate_integrity_hash( fc::sha256::encoder& enc ) const;
void add_to_snapshot( const snapshot_writer_ptr& snapshot ) const;
void read_from_snapshot( const snapshot_reader_ptr& snapshot );

Expand Down
1 change: 0 additions & 1 deletion libraries/chain/include/eosio/chain/resource_limits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ namespace eosio { namespace chain { namespace resource_limits {

void add_indices();
void initialize_database();
void calculate_integrity_hash( fc::sha256::encoder& enc ) const;
void add_to_snapshot( const snapshot_writer_ptr& snapshot ) const;
void read_from_snapshot( const snapshot_reader_ptr& snapshot );

Expand Down
26 changes: 25 additions & 1 deletion libraries/chain/include/eosio/chain/snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ namespace eosio { namespace chain {

struct abstract_snapshot_row_writer {
virtual void write(ostream_wrapper& out) const = 0;
virtual void write(fc::sha256::encoder& out) const = 0;
virtual variant to_variant() const = 0;
virtual std::string row_type_name() const = 0;
};
Expand All @@ -83,10 +84,19 @@ namespace eosio { namespace chain {
explicit snapshot_row_writer( const T& data )
:data(data) {}

void write(ostream_wrapper& out) const override {
template<typename DataStream>
void write_stream(DataStream& out) const {
fc::raw::pack(out, data);
}

void write(ostream_wrapper& out) const override {
write_stream(out);
}

void write(fc::sha256::encoder& out) const override {
write_stream(out);
}

fc::variant to_variant() const override {
variant var;
fc::to_variant(data, var);
Expand Down Expand Up @@ -356,4 +366,18 @@ namespace eosio { namespace chain {
uint64_t cur_row;
};

class integrity_hash_snapshot_writer : public snapshot_writer {
public:
explicit integrity_hash_snapshot_writer(fc::sha256::encoder& enc);

void write_start_section( const std::string& section_name ) override;
void write_row( const detail::abstract_snapshot_row_writer& row_writer ) override;
void write_end_section( ) override;
void finalize();

private:
fc::sha256::encoder& enc;

};

}}
8 changes: 0 additions & 8 deletions libraries/chain/resource_limits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ void resource_limits_manager::initialize_database() {
});
}

void resource_limits_manager::calculate_integrity_hash( fc::sha256::encoder& enc ) const {
resource_index_set::walk_indices([this, &enc]( auto utils ){
decltype(utils)::walk(_db, [&enc]( const auto &row ) {
fc::raw::pack(enc, row);
});
});
}

void resource_limits_manager::add_to_snapshot( const snapshot_writer_ptr& snapshot ) const {
resource_index_set::walk_indices([this, &snapshot]( auto utils ){
snapshot->write_section<typename decltype(utils)::index_t::value_type>([this]( auto& section ){
Expand Down
22 changes: 22 additions & 0 deletions libraries/chain/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,26 @@ void istream_snapshot_reader::clear_section() {
cur_row = 0;
}

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
}

}}

0 comments on commit 28e498d

Please sign in to comment.