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 #9895 from EOSIO/huangminghuang/fix-ship-unpack
Browse files Browse the repository at this point in the history
Fix state_history zlib_unpack bug
  • Loading branch information
huangminghuang authored Jan 14, 2021
2 parents f700e13 + 42c8ecf commit 2be00e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ template <typename STREAM, typename T>
void zlib_unpack(STREAM& strm, T& obj) {
uint32_t len;
fc::raw::unpack(strm, len);
auto pos = strm.tellp();
if (len > 0) {
fc::datastream<bio::filtering_istreambuf> decompress_strm(bio::zlib_decompressor() | bio::restrict(fc::to_source(strm), 0, len));
fc::raw::unpack(decompress_strm, obj);
// zlib may add some padding at the end of the uncompressed data so that the position of `strm` wont be at the start of next entry,
// we need to use `seek()` to adjust the position of `strm`.
strm.seekp(pos + len);
}
}

Expand Down
40 changes: 30 additions & 10 deletions libraries/state_history/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,27 +294,47 @@ state_history_traces_log::state_history_traces_log(const state_history_config& c

chain::bytes state_history_traces_log::get_log_entry(block_num_type block_num) {

auto get_traces_bin = [](auto& ds, uint32_t version) {
if (version == 0) {
return state_history::zlib_decompress(ds);
}
else {
std::vector<state_history::transaction_trace> traces;
state_history::trace_converter::unpack(ds, traces);
return fc::raw::pack(traces);
auto get_traces_bin = [block_num](auto& ds, uint32_t version, std::size_t size) {
auto start_pos = ds.tellp();
try {
if (version == 0) {
return state_history::zlib_decompress(ds);
}
else {
std::vector<state_history::transaction_trace> traces;
state_history::trace_converter::unpack(ds, traces);
return fc::raw::pack(traces);
}
} catch (fc::exception& ex) {
std::vector<char> trace_data(size);
ds.seekp(start_pos);
ds.read(trace_data.data(), size);

fc::cfile output;
char filename[PATH_MAX];
snprintf(filename, PATH_MAX, "invalid_trace_%u_v%u.bin", block_num, version);
output.set_file_path(filename);
output.open("w");
output.write(trace_data.data(), size);

ex.append_log(FC_LOG_MESSAGE(error,
"trace data for block ${block_num} has been written to ${filename} for debugging",
("block_num", block_num)("filename", filename)));

throw ex;
}
};

auto [ds, version] = catalog.ro_stream_for_block(block_num);
if (ds.remaining()) {
return get_traces_bin(ds, version);
return get_traces_bin(ds, version, ds.remaining());
}

if (block_num < begin_block() || block_num >= end_block())
return {};
state_history_log_header header;
get_entry_header(block_num, header);
return get_traces_bin(read_log, get_ship_version(header.magic));
return get_traces_bin(read_log, get_ship_version(header.magic), header.payload_size);
}


Expand Down

0 comments on commit 2be00e6

Please sign in to comment.