Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8750 from EOSIO/ship-library
Browse files Browse the repository at this point in the history
Move parts of state-history-plugin to libraries/state_history
  • Loading branch information
tbfleming authored May 4, 2020
2 parents 74c020a + e19f0a4 commit cef7bd3
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 350 deletions.
1 change: 1 addition & 0 deletions libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_subdirectory( appbase )
add_subdirectory( chain )
add_subdirectory( testing )
add_subdirectory( version )
add_subdirectory( state_history )

set(USE_EXISTING_SOFTFLOAT ON CACHE BOOL "use pre-exisiting softfloat lib")
set(ENABLE_TOOLS OFF CACHE BOOL "Build tools")
Expand Down
8 changes: 8 additions & 0 deletions libraries/state_history/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BasedOnStyle: LLVM
IndentWidth: 3
ColumnLimit: 120
PointerAlignment: Left
AlwaysBreakTemplateDeclarations: true
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
BreakConstructorInitializers: BeforeComma
17 changes: 17 additions & 0 deletions libraries/state_history/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
file(GLOB HEADERS "include/eosio/state-history/*.hpp")

add_library( state_history
abi.cpp
compression.cpp
create_deltas.cpp
trace_converter.cpp
${HEADERS}
)

target_link_libraries( state_history
PUBLIC eosio_chain fc chainbase softfloat
)

target_include_directories( state_history
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include"
)
32 changes: 32 additions & 0 deletions libraries/state_history/compression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <eosio/state_history/compression.hpp>

#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <boost/iostreams/filtering_stream.hpp>

namespace eosio {
namespace state_history {

namespace bio = boost::iostreams;
bytes zlib_compress_bytes(const bytes& in) {
bytes out;
bio::filtering_ostream comp;
comp.push(bio::zlib_compressor(bio::zlib::default_compression));
comp.push(bio::back_inserter(out));
bio::write(comp, in.data(), in.size());
bio::close(comp);
return out;
}

bytes zlib_decompress(const bytes& in) {
bytes out;
bio::filtering_ostream decomp;
decomp.push(bio::zlib_decompressor());
decomp.push(bio::back_inserter(out));
bio::write(decomp, in.data(), in.size());
bio::close(decomp);
return out;
}

} // namespace state_history
} // namespace eosio
140 changes: 140 additions & 0 deletions libraries/state_history/create_deltas.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include <eosio/state_history/create_deltas.hpp>
#include <eosio/state_history/serialization.hpp>

namespace eosio {
namespace state_history {

template <typename T>
bool include_delta(const T& old, const T& curr) {
return true;
}

bool include_delta(const chain::table_id_object& old, const chain::table_id_object& curr) {
return old.payer != curr.payer;
}

bool include_delta(const chain::resource_limits::resource_limits_object& old,
const chain::resource_limits::resource_limits_object& curr) {
return //
old.net_weight != curr.net_weight || //
old.cpu_weight != curr.cpu_weight || //
old.ram_bytes != curr.ram_bytes;
}

bool include_delta(const chain::resource_limits::resource_limits_state_object& old,
const chain::resource_limits::resource_limits_state_object& curr) {
return //
old.average_block_net_usage.last_ordinal != curr.average_block_net_usage.last_ordinal || //
old.average_block_net_usage.value_ex != curr.average_block_net_usage.value_ex || //
old.average_block_net_usage.consumed != curr.average_block_net_usage.consumed || //
old.average_block_cpu_usage.last_ordinal != curr.average_block_cpu_usage.last_ordinal || //
old.average_block_cpu_usage.value_ex != curr.average_block_cpu_usage.value_ex || //
old.average_block_cpu_usage.consumed != curr.average_block_cpu_usage.consumed || //
old.total_net_weight != curr.total_net_weight || //
old.total_cpu_weight != curr.total_cpu_weight || //
old.total_ram_bytes != curr.total_ram_bytes || //
old.virtual_net_limit != curr.virtual_net_limit || //
old.virtual_cpu_limit != curr.virtual_cpu_limit;
}

bool include_delta(const chain::account_metadata_object& old, const chain::account_metadata_object& curr) {
return //
old.name != curr.name || //
old.is_privileged() != curr.is_privileged() || //
old.last_code_update != curr.last_code_update || //
old.vm_type != curr.vm_type || //
old.vm_version != curr.vm_version || //
old.code_hash != curr.code_hash;
}

bool include_delta(const chain::code_object& old, const chain::code_object& curr) { //
return false;
}

bool include_delta(const chain::protocol_state_object& old, const chain::protocol_state_object& curr) {
return old.activated_protocol_features != curr.activated_protocol_features;
}

std::vector<table_delta> create_deltas(const chainbase::database& db, bool full_snapshot) {
std::vector<table_delta> deltas;
const auto& table_id_index = db.get_index<chain::table_id_multi_index>();
std::map<uint64_t, const chain::table_id_object*> removed_table_id;
for (auto& rem : table_id_index.last_undo_session().removed_values)
removed_table_id[rem.id._id] = &rem;

auto get_table_id = [&](uint64_t tid) -> const chain::table_id_object& {
auto obj = table_id_index.find(tid);
if (obj)
return *obj;
auto it = removed_table_id.find(tid);
EOS_ASSERT(it != removed_table_id.end(), chain::plugin_exception, "can not found table id ${tid}", ("tid", tid));
return *it->second;
};

auto pack_row = [&](auto& row) { return fc::raw::pack(make_history_serial_wrapper(db, row)); };
auto pack_contract_row = [&](auto& row) {
return fc::raw::pack(make_history_context_wrapper(db, get_table_id(row.t_id._id), row));
};

auto process_table = [&](auto* name, auto& index, auto& pack_row) {
if (full_snapshot) {
if (index.indices().empty())
return;
deltas.push_back({});
auto& delta = deltas.back();
delta.name = name;
for (auto& row : index.indices())
delta.rows.obj.emplace_back(true, pack_row(row));
} else {
auto undo = index.last_undo_session();
if (undo.old_values.empty() && undo.new_values.empty() && undo.removed_values.empty())
return;
deltas.push_back({});
auto& delta = deltas.back();
delta.name = name;
for (auto& old : undo.old_values) {
auto& row = index.get(old.id);
if (include_delta(old, row))
delta.rows.obj.emplace_back(true, pack_row(row));
}
for (auto& old : undo.removed_values)
delta.rows.obj.emplace_back(false, pack_row(old));
for (auto& row : undo.new_values) {
delta.rows.obj.emplace_back(true, pack_row(row));
}
}
};

process_table("account", db.get_index<chain::account_index>(), pack_row);
process_table("account_metadata", db.get_index<chain::account_metadata_index>(), pack_row);
process_table("code", db.get_index<chain::code_index>(), pack_row);

process_table("contract_table", db.get_index<chain::table_id_multi_index>(), pack_row);
process_table("contract_row", db.get_index<chain::key_value_index>(), pack_contract_row);
process_table("contract_index64", db.get_index<chain::index64_index>(), pack_contract_row);
process_table("contract_index128", db.get_index<chain::index128_index>(), pack_contract_row);
process_table("contract_index256", db.get_index<chain::index256_index>(), pack_contract_row);
process_table("contract_index_double", db.get_index<chain::index_double_index>(), pack_contract_row);
process_table("contract_index_long_double", db.get_index<chain::index_long_double_index>(), pack_contract_row);

process_table("key_value", db.get_index<chain::kv_index>(), pack_row);

process_table("global_property", db.get_index<chain::global_property_multi_index>(), pack_row);
process_table("generated_transaction", db.get_index<chain::generated_transaction_multi_index>(), pack_row);
process_table("protocol_state", db.get_index<chain::protocol_state_multi_index>(), pack_row);

process_table("permission", db.get_index<chain::permission_index>(), pack_row);
process_table("permission_link", db.get_index<chain::permission_link_index>(), pack_row);

process_table("resource_limits", db.get_index<chain::resource_limits::resource_limits_index>(), pack_row);
process_table("resource_usage", db.get_index<chain::resource_limits::resource_usage_index>(), pack_row);
process_table("resource_limits_state", db.get_index<chain::resource_limits::resource_limits_state_index>(),
pack_row);
process_table("resource_limits_config", db.get_index<chain::resource_limits::resource_limits_config_index>(),
pack_row);

return deltas;
}

} // namespace state_history
} // namespace eosio
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <eosio/chain/types.hpp>

namespace eosio {
namespace state_history {

using chain::bytes;

bytes zlib_compress_bytes(const bytes& in);
bytes zlib_decompress(const bytes& in);

} // namespace state_history
} // namespace eosio
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <eosio/state_history/types.hpp>

namespace eosio {
namespace state_history {

std::vector<table_delta> create_deltas(const chainbase::database& db, bool full_snapshot);

} // namespace state_history
} // namespace eosio
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <eosio/chain/block_header.hpp>
#include <eosio/chain/exceptions.hpp>
#include <eosio/chain/types.hpp>
#include <fc/log/logger.hpp>
#include <fc/io/cfile.hpp>
#include <fc/log/logger.hpp>

namespace eosio {

Expand Down Expand Up @@ -197,8 +197,8 @@ class state_history_log {
}

void open_log() {
log.set_file_path( log_filename );
log.open( "a+b" ); // std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app
log.set_file_path(log_filename);
log.open("a+b"); // std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app
log.seek_end(0);
uint64_t size = log.tellp();
if (size >= state_history_log_header_serial_size) {
Expand All @@ -220,14 +220,14 @@ class state_history_log {
}

void open_index() {
index.set_file_path( index_filename );
index.open( "a+b" ); // std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app
index.set_file_path(index_filename);
index.open("a+b"); // std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::app
index.seek_end(0);
if (index.tellp() == (static_cast<int>(_end_block) - _begin_block) * sizeof(uint64_t))
return;
ilog("Regenerate ${name}.index", ("name", name));
index.close();
index.open( "w+b" ); // std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc
index.open("w+b"); // std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc

log.seek_end(0);
uint64_t size = log.tellp();
Expand Down
Loading

0 comments on commit cef7bd3

Please sign in to comment.