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

Commit

Permalink
compression
Browse files Browse the repository at this point in the history
  • Loading branch information
tbfleming committed Mar 4, 2020
1 parent a0ed1b1 commit 29233b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
1 change: 1 addition & 0 deletions libraries/state_history/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ file(GLOB HEADERS "include/eosio/state-history/*.hpp")

add_library( state_history
abi.cpp
compression.cpp
create_deltas.cpp
trace_converter.cpp
${HEADERS}
Expand Down
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
1 change: 0 additions & 1 deletion libraries/state_history/create_deltas.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <eosio/state_history/create_deltas.hpp>

#include <eosio/state_history/serialization.hpp>

namespace eosio {
Expand Down
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
31 changes: 4 additions & 27 deletions plugins/state_history_plugin/state_history_plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <eosio/chain/config.hpp>
#include <eosio/state_history/compression.hpp>
#include <eosio/state_history/create_deltas.hpp>
#include <eosio/state_history/log.hpp>
#include <eosio/state_history/serialization.hpp>
Expand All @@ -11,9 +12,6 @@
#include <boost/asio/strand.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/signals2/connection.hpp>

using tcp = boost::asio::ip::tcp;
Expand Down Expand Up @@ -41,27 +39,6 @@ auto catch_and_log(F f) {
}
}

namespace bio = boost::iostreams;
static bytes zlib_compress_bytes(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;
}

static 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;
}

struct state_history_plugin_impl : std::enable_shared_from_this<state_history_plugin_impl> {
chain_plugin* chain_plug = nullptr;
fc::optional<state_history_log> trace_log;
Expand All @@ -85,7 +62,7 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
bytes compressed(s);
if (s)
stream.read(compressed.data(), s);
result = zlib_decompress(compressed);
result = state_history::zlib_decompress(compressed);
}

void get_block(uint32_t block_num, fc::optional<bytes>& result) {
Expand Down Expand Up @@ -367,7 +344,7 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
if (!trace_log)
return;
auto traces_bin =
zlib_compress_bytes(trace_converter.pack(chain_plug->chain().db(), trace_debug_mode, block_state));
state_history::zlib_compress_bytes(trace_converter.pack(chain_plug->chain().db(), trace_debug_mode, block_state));
EOS_ASSERT(traces_bin.size() == (uint32_t)traces_bin.size(), plugin_exception, "traces is too big");

state_history_log_header header{.magic = ship_magic(ship_current_version),
Expand All @@ -389,7 +366,7 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
ilog("Placing initial state in block ${n}", ("n", block_state->block->block_num()));

std::vector<table_delta> deltas = state_history::create_deltas(chain_plug->chain().db(), fresh);
auto deltas_bin = zlib_compress_bytes(fc::raw::pack(deltas));
auto deltas_bin = state_history::zlib_compress_bytes(fc::raw::pack(deltas));
EOS_ASSERT(deltas_bin.size() == (uint32_t)deltas_bin.size(), plugin_exception, "deltas is too big");
state_history_log_header header{.magic = ship_magic(ship_current_version),
.block_id = block_state->block->id(),
Expand Down

0 comments on commit 29233b8

Please sign in to comment.