Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 30 additions & 79 deletions include/bitcoin/database/impl/query/wire.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -33,162 +33,113 @@ namespace database {
// This normalized approach is also the most efficient.

TEMPLATE
bool CLASS::get_wire_header(byteflipper& flipper,
bool CLASS::get_wire_header(bytewriter& sink,
const header_link& link) const NOEXCEPT
{
const auto start = flipper.get_write_position();
table::header::wire_header header{ {}, flipper };
if (!store_.header.get(link, header))
{
flipper.invalidate();
return false;
}

// Genesis header parent is defaulted.
if (header.parent_fk == schema::header::link::terminal)
return true;

flipper.set_position(start);
table::header::wire_key key{ {}, flipper };
if (!store_.header.get(header.parent_fk, key))
{
flipper.invalidate();
return false;
}

return true;
// Double read of header table is small, prevents need for writer rewind.
const auto parent = to_parent(link);
table::header::wire_header header{ {}, sink, get_header_key(parent) };
return store_.header.get(link, header);
}

TEMPLATE
bool CLASS::get_wire_input(byteflipper& flipper,
bool CLASS::get_wire_input(bytewriter& sink,
const point_link& link) const NOEXCEPT
{
// [point]
table::point::wire_point point{ {}, flipper };
table::point::wire_point point{ {}, sink };
if (!store_.point.get(link, point))
{
flipper.invalidate();
return false;
}

// [[size]script]
table::ins::get_input ins{};
table::input::wire_script script{ {}, flipper };
table::input::wire_script script{ {}, sink };
if (!store_.ins.get(link, ins) ||
!store_.input.get(ins.input_fk, script))
{
flipper.invalidate();
return false;
}

// [sequence]
flipper.write_4_bytes_little_endian(ins.sequence);
sink.write_4_bytes_little_endian(ins.sequence);
return true;
}

TEMPLATE
bool CLASS::get_wire_output(byteflipper& flipper,
bool CLASS::get_wire_output(bytewriter& sink,
const output_link& link) const NOEXCEPT
{
// [value][[[size]script]]
table::output::wire_script out{ {}, flipper };
if (!store_.output.get(link, out))
{
flipper.invalidate();
return false;
}

return true;
table::output::wire_script out{ {}, sink };
return store_.output.get(link, out);
}

TEMPLATE
bool CLASS::get_wire_witness(byteflipper& flipper,
bool CLASS::get_wire_witness(bytewriter& sink,
const point_link& link) const NOEXCEPT
{
// [count][[[size]element]]
table::ins::get_input ins{};
table::input::wire_witness wire{ {}, flipper };
if (!store_.ins.get(link, ins) ||
!store_.input.get(ins.input_fk, wire))
{
flipper.invalidate();
return false;
}

return true;
table::input::wire_witness wire{ {}, sink };
return store_.ins.get(link, ins)
&& store_.input.get(ins.input_fk, wire);
}

TEMPLATE
bool CLASS::get_wire_tx(byteflipper& flipper, const tx_link& link,
bool CLASS::get_wire_tx(bytewriter& sink, const tx_link& link,
bool witness) const NOEXCEPT
{
table::transaction::record tx{};
if (!store_.tx.get(link, tx))
{
flipper.invalidate();
return false;
}

table::outs::record outs{};
outs.out_fks.resize(tx.outs_count);
if (!store_.outs.get(tx.outs_fk, outs))
{
flipper.invalidate();
return false;
}

// Point links are contiguous (computed).
const auto ins_begin = tx.point_fk;
const auto ins_count = tx.ins_count;
const auto ins_final = ins_begin + ins_count;
const auto witnessed = witness && (tx.heavy != tx.light);

flipper.write_4_bytes_little_endian(tx.version);
sink.write_4_bytes_little_endian(tx.version);

if (witnessed)
{
flipper.write_byte(system::chain::witness_marker);
flipper.write_byte(system::chain::witness_enabled);
sink.write_byte(system::chain::witness_marker);
sink.write_byte(system::chain::witness_enabled);
}

flipper.write_variable(ins_count);
sink.write_variable(ins_count);
for (auto fk = ins_begin; fk < ins_final; ++fk)
if (!get_wire_input(flipper, fk))
if (!get_wire_input(sink, fk))
return false;

flipper.write_variable(outs.out_fks.size());
sink.write_variable(outs.out_fks.size());
for (const auto& fk: outs.out_fks)
if (!get_wire_output(flipper, fk))
if (!get_wire_output(sink, fk))
return false;

if (witnessed)
{
for (auto fk = ins_begin; fk < ins_final; ++fk)
if (!get_wire_witness(flipper, fk))
if (!get_wire_witness(sink, fk))
return false;
}

flipper.write_4_bytes_little_endian(tx.locktime);
sink.write_4_bytes_little_endian(tx.locktime);
return true;
}

TEMPLATE
bool CLASS::get_wire_block(byteflipper& flipper, const header_link& link,
bool CLASS::get_wire_block(bytewriter& sink, const header_link& link,
bool witness) const NOEXCEPT
{
if (!get_wire_header(flipper, link))
if (!get_wire_header(sink, link))
return false;

const auto txs = to_transactions(link);
if (txs.empty())
{
flipper.invalidate();
return false;
}

flipper.write_variable(txs.size());
sink.write_variable(txs.size());
for (const auto& tx_link: txs)
if (!get_wire_tx(flipper, tx_link, witness))
if (!get_wire_tx(sink, tx_link, witness))
return false;

return true;
Expand Down
4 changes: 3 additions & 1 deletion include/bitcoin/database/memory/streamers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ namespace libbitcoin {
namespace database {

/// These all operate over a system::iostream.

using reader = system::byte_reader<system::iostream<>>;
using writer = system::byte_writer<system::iostream<>>;
using flipper = system::byte_flipper<system::iostream<>>;

/// Interfaces.
using bytewriter = system::bytewriter;

} // namespace database
} // namespace libbitcoin

Expand Down
13 changes: 7 additions & 6 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,13 @@ class query
/// Wire.
/// -----------------------------------------------------------------------

bool get_wire_input(byteflipper& flipper, const point_link& link) const NOEXCEPT;
bool get_wire_output(byteflipper& flipper, const output_link& link) const NOEXCEPT;
bool get_wire_witness(byteflipper& flipper, const point_link& link) const NOEXCEPT;
bool get_wire_header(byteflipper& flipper, const header_link& link) const NOEXCEPT;
bool get_wire_tx(byteflipper& flipper, const tx_link& link, bool witness) const NOEXCEPT;
bool get_wire_block(byteflipper& flipper, const header_link& link,
bool get_wire_input(bytewriter& sink, const point_link& link) const NOEXCEPT;
bool get_wire_output(bytewriter& sink, const output_link& link) const NOEXCEPT;
bool get_wire_witness(bytewriter& sink, const point_link& link) const NOEXCEPT;
bool get_wire_header(bytewriter& sink, const header_link& link) const NOEXCEPT;
bool get_wire_tx(bytewriter& sink, const tx_link& link,
bool witness) const NOEXCEPT;
bool get_wire_block(bytewriter& sink, const header_link& link,
bool witness) const NOEXCEPT;

data_chunk get_wire_header(const header_link& link) const NOEXCEPT;
Expand Down
32 changes: 7 additions & 25 deletions include/bitcoin/database/tables/archives/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,43 +322,25 @@ struct header
uint32_t timestamp{};
};

struct wire_key
: public schema::header
{
inline bool from_data(reader& source) NOEXCEPT
{
const auto time_bits_nonce_size = 3u * sizeof(uint32_t);
const auto version_size = sizeof(uint32_t);
source.rewind_bytes(sk);
flipper.skip_bytes(version_size);
flipper.write_bytes(source.read_hash());
flipper.skip_bytes(schema::hash + time_bits_nonce_size);
return source;
}

system::byteflipper& flipper;
};

struct wire_header
: public schema::header
{
inline bool from_data(reader& source) NOEXCEPT
{
const auto time_bits_nonce_size = 3u * sizeof(uint32_t);
const auto version_size = sizeof(uint32_t);
source.skip_bytes(skip_to_parent);
parent_fk = to_parent(source.read_little_endian<link::integer, link::size>());
flipper.write_bytes(source.read_bytes(version_size));
flipper.write_bytes(system::null_hash);
source.skip_bytes(skip_to_version);
sink.write_bytes(source.read_bytes(version_size));
sink.write_bytes(parent_hash);
source.skip_bytes(time_bits_nonce_size);
flipper.write_bytes(source.read_hash());
sink.write_bytes(source.read_hash());
source.rewind_bytes(time_bits_nonce_size + schema::hash);
flipper.write_bytes(source.read_bytes(time_bits_nonce_size));
sink.write_bytes(source.read_bytes(time_bits_nonce_size));
return source;
}

system::byteflipper& flipper;
link::integer parent_fk{};
bytewriter& sink;
hash_digest parent_hash{};
};
};

Expand Down
14 changes: 7 additions & 7 deletions include/bitcoin/database/tables/archives/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ struct input
{
// script (prefixed)
const auto length = source.read_size();
flipper.write_variable(length);
flipper.write_bytes(source.read_bytes(length));
sink.write_variable(length);
sink.write_bytes(source.read_bytes(length));
return source;
}

system::byteflipper& flipper;
bytewriter& sink;
};

struct wire_witness
Expand All @@ -201,20 +201,20 @@ struct input

// witness (count)
const auto count = source.read_size();
flipper.write_variable(count);
sink.write_variable(count);

// witness (prefixed)
for (size_t element{}; element < count; ++element)
{
const auto length = source.read_size();
flipper.write_variable(length);
flipper.write_bytes(source.read_bytes(length));
sink.write_variable(length);
sink.write_bytes(source.read_bytes(length));
}

return source;
}

system::byteflipper& flipper;
bytewriter& sink;
};
};

Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/database/tables/archives/ins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ struct ins
inline bool from_data(reader& source) NOEXCEPT
{
const auto sequence_size = sizeof(uint32_t);
flipper.write_bytes(source.read_bytes(sequence_size));
sink.write_bytes(source.read_bytes(sequence_size));
return source;
}

system::byteflipper& flipper;
bytewriter& sink;
};
};

Expand Down
8 changes: 4 additions & 4 deletions include/bitcoin/database/tables/archives/output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,16 @@ struct output
source.skip_bytes(tx::size);

// value (translates from variable to fixed width)
flipper.write_8_bytes_little_endian(source.read_variable());
sink.write_8_bytes_little_endian(source.read_variable());

// script (prefixed)
const auto length = source.read_size();
flipper.write_variable(length);
flipper.write_bytes(source.read_bytes(length));
sink.write_variable(length);
sink.write_bytes(source.read_bytes(length));
return source;
}

system::byteflipper& flipper;
bytewriter& sink;
};
};

Expand Down
6 changes: 3 additions & 3 deletions include/bitcoin/database/tables/archives/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ struct point
inline bool from_data(reader& source) NOEXCEPT
{
source.rewind_bytes(schema::point::sk);
flipper.write_bytes(source.read_hash());
sink.write_bytes(source.read_hash());
const auto value = source.read_little_endian<ix::integer, ix::size>();
flipper.write_4_bytes_little_endian(to_index(value));
sink.write_4_bytes_little_endian(to_index(value));
return source;
}

system::byteflipper& flipper;
bytewriter& sink;
};
};

Expand Down
3 changes: 0 additions & 3 deletions include/bitcoin/database/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ using outpoint = system::chain::outpoint;
using outpoints = std::set<outpoint>;

using data_chunk = system::data_chunk;
using bytereader = system::bytereader;
using bytewriter = system::bytewriter;
using byteflipper = system::byteflipper;

struct header_state { header_link link; code ec; };
using header_states = std::vector<header_state>;
Expand Down