Skip to content
Merged

Style. #1189

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
8 changes: 0 additions & 8 deletions include/bitcoin/node/impl/sessions/session_peer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ inline void CLASS::attach_protocols(const channel_ptr& channel) NOEXCEPT

// Ready to relay blocks or block filters.
const auto blocks_out = !delay || this->is_recent();

///////////////////////////////////////////////////////////////////////
// bip152: "Upon receipt of a `sendcmpct` message with the first and
// second integers set to 1, the node SHOULD announce new blocks by
// sending a cmpctblock message." IOW at 70014 bip152 is optional.
// This allows the node to support bip157 without supporting bip152.
///////////////////////////////////////////////////////////////////////

const auto peer = std::dynamic_pointer_cast<channel_t>(channel);

// Node must advertise node_client_filters or no out filters.
Expand Down
14 changes: 8 additions & 6 deletions include/bitcoin/node/protocols/protocol_header_in_70014.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class BCN_API protocol_header_in_70014
void start() NOEXCEPT override;

protected:
using compact_block = network::messages::peer::compact_block;
using compact_transactions = network::messages::peer::compact_transactions;
using compact_block = network::messages::peer::compact_block;
using short_id_t = system::chain::short_id::integer;
using short_ids_t = std::vector<short_id_t>;

/// A block being filled from its compact block.
struct fill
Expand All @@ -56,7 +58,7 @@ class BCN_API protocol_header_in_70014
system::hash_digest hash{};
system::chain::header::cptr header{};
system::siphash_key key{};
std::vector<system::chain::short_id::integer> short_ids{};
short_ids_t short_ids{};
system::chain::transaction_cptrs txs{};
database::tx_links links{};
std::vector<size_t> missing{};
Expand All @@ -80,18 +82,18 @@ class BCN_API protocol_header_in_70014
const system::hash_digest& hash, size_t height) NOEXCEPT;

private:
bool decode(fill& block, const compact_block& message) NOEXCEPT;
bool scan(fill& block) NOEXCEPT;
bool is_malleated64(const fill& block) NOEXCEPT;
bool decode(fill& block, const compact_block& message) NOEXCEPT;
bool to_hashes(system::hashes& txids, system::hashes& wtxids,
const fill& block) NOEXCEPT;
bool is_malleated64(const fill& block) NOEXCEPT;

void collect(const compact_block& message,
const database::header_link& link) NOEXCEPT;
void request() NOEXCEPT;
void identify() NOEXCEPT;
bool resolve(const database::header_link& link) NOEXCEPT;
bool matched(const database::header_link& link) const NOEXCEPT;
void collect(const compact_block& message,
const database::header_link& link) NOEXCEPT;

// These are protected by strand.
bool compact_{};
Expand Down
14 changes: 7 additions & 7 deletions include/bitcoin/node/protocols/protocol_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ class BCN_API protocol_peer
/// Determine if outgoing block or tx was previously announced by peer.
virtual bool was_announced(const system::hash_digest&) const NOEXCEPT;

/// Compact blocks (bip152 version 2).
/// -----------------------------------------------------------------------

/// Compact block of an associated block (coinbase prefilled), or null.
virtual network::messages::peer::compact_block::cptr make_compact_block(
const database::header_link& link) const NOEXCEPT;

/// Currency.
/// -----------------------------------------------------------------------

Expand All @@ -108,6 +101,13 @@ class BCN_API protocol_peer
virtual void notify_one(object_key key, const code& ec,
event_value value) const NOEXCEPT;

/// Compact blocks (bip152 version 2).
/// -----------------------------------------------------------------------

/// Compact block of an associated block (coinbase prefilled), or null.
virtual network::messages::peer::compact_block::cptr make_compact_block(
const database::header_link& link) const NOEXCEPT;

private:
// This derived channel requires stranded calls, base is thread safe.
const node::channel_peer::ptr channel_;
Expand Down
3 changes: 1 addition & 2 deletions src/protocols/protocol_block_out_70014.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ bool protocol_block_out_70014::handle_receive_get_data(const code& ec,
const auto block = make_compact_block(link);
if (!block)
{
LOGF("Compact block " << encode_hash(item.hash)
<< " not obtained.");
LOGF("Compact block " << encode_hash(item.hash) << " not made.");
continue;
}

Expand Down
57 changes: 37 additions & 20 deletions src/protocols/protocol_header_in_70014.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ void protocol_header_in_70014::complete() NOEXCEPT
{
BC_ASSERT(stranded());

// TODO: this is hardwired in handshake.
constexpr auto version = send_compact::compact_version_2;

if (!compact_ && is_current_chain(true))
{
compact_ = true;
SEND((send_compact{ true, send_compact::compact_version_2 }),
handle_send, _1);
LOGP("Subscribed to compact block announcements at [" << opposite()
<< "].");
SEND((send_compact{ true, version }), handle_send, _1);
LOGP("Subscribed to compact blocks at [" << opposite() << "].");
}

protocol_header_in_70012::complete();
Expand Down Expand Up @@ -100,8 +101,8 @@ bool protocol_header_in_70014::handle_receive_compact_block(const code& ec,

if (link.is_terminal())
{
organize_compact(header, BIND(handle_organize_compact, _1, _2,
message));
organize_compact(header,
BIND(handle_organize_compact, _1, _2, message));
return true;
}

Expand Down Expand Up @@ -169,12 +170,14 @@ void protocol_header_in_70014::collect(const compact_block& message,
return;
}

fill block{};
block.link = link;
block.height = ctx.height;
block.header = message.header_ptr;
block.hash = message.header_ptr->get_hash();
block.key = chain::short_id::to_key(*message.header_ptr, message.nonce);
fill block
{
.link = link,
.height = ctx.height,
.hash = message.header_ptr->get_hash(),
.header = message.header_ptr,
.key = chain::short_id::to_key(*message.header_ptr, message.nonce)
};

if (!decode(block, message))
{
Expand Down Expand Up @@ -206,14 +209,14 @@ bool protocol_header_in_70014::decode(fill& block,
const compact_block& message) NOEXCEPT
{
BC_ASSERT(stranded());
const auto& items = message.transactions;
const auto& ids = message.short_ids;
const auto& items = message.transactions;
const auto count = ids.size() + items.size();
if (is_zero(count) || (count > chain::max_block_size))
return false;

block.short_ids.resize(count);
block.txs.resize(count);
block.short_ids.resize(count);
block.links.assign(count, database::tx_link::terminal);

size_t position{};
Expand Down Expand Up @@ -243,7 +246,7 @@ bool protocol_header_in_70014::decode(fill& block,
bool protocol_header_in_70014::scan(fill& block) NOEXCEPT
{
BC_ASSERT(stranded());
std::vector<chain::short_id::integer> short_ids{};
short_ids_t short_ids{};
std::vector<size_t> positions{};
for (size_t position{}; position < block.txs.size(); ++position)
{
Expand Down Expand Up @@ -328,15 +331,25 @@ bool protocol_header_in_70014::handle_receive_compact_transactions(
{
const auto& tx = txs.at(index);
const auto position = block.missing.at(index);
if (!tx || (chain::short_id::to_id(block.key, tx->is_coinbase() ?
bitcoin_hash(tx->to_data(true)) : tx->get_hash(true)) !=
block.short_ids.at(position)))
if (!tx)
{
LOGR("Invalid compact transaction from [" << opposite() << "].");
stop(network::error::protocol_violation);
return false;
}

// TODO: use fast streaming hash to avoid allocation.
const auto tx_hash = tx->is_coinbase() ?
bitcoin_hash(tx->to_data(true)) : tx->get_hash(true);

if (chain::short_id::to_id(block.key, tx_hash) !=
block.short_ids.at(position))
{
LOGR("Invalid compact short id from [" << opposite() << "].");
stop(network::error::protocol_violation);
return false;
}

block.txs.at(position) = tx;
}

Expand All @@ -351,10 +364,11 @@ bool protocol_header_in_70014::handle_receive_compact_transactions(
void protocol_header_in_70014::identify() NOEXCEPT
{
BC_ASSERT(stranded());
auto& block = *fill_;

hashes txids{}, wtxids{};
auto& block = *fill_;
chain::context ctx{};
hashes txids{}, wtxids{};

if (!to_hashes(txids, wtxids, block) ||
!archive().get_context(ctx, block.link))
{
Expand All @@ -369,6 +383,7 @@ void protocol_header_in_70014::identify() NOEXCEPT
const auto& root = block.header->merkle_root();
const auto segregated = !std::equal(txids.cbegin(), txids.cend(),
wtxids.cbegin());

const auto witness_root = sha256::merkle_root(std::move(wtxids));
if ((ec = chain::block::identify(root, txids, is_malleated64(block))) ||
(ec = chain::block::identify(ctx, first, witness_root, segregated)))
Expand Down Expand Up @@ -398,6 +413,7 @@ bool protocol_header_in_70014::to_hashes(hashes& txids, hashes& wtxids,
const fill& block) NOEXCEPT
{
BC_ASSERT(stranded());

const auto& query = archive();
const auto count = block.txs.size();
if (!block.txs.front())
Expand Down Expand Up @@ -429,6 +445,7 @@ bool protocol_header_in_70014::to_hashes(hashes& txids, hashes& wtxids,
bool protocol_header_in_70014::is_malleated64(const fill& block) NOEXCEPT
{
BC_ASSERT(stranded());

const auto& query = archive();
auto malleated = !block.txs.front()->is_coinbase();
for (size_t at{}; malleated && at < block.txs.size(); ++at)
Expand Down
7 changes: 5 additions & 2 deletions src/protocols/protocol_header_out_70014.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ bool protocol_header_out_70014::do_announce(header_t link) NOEXCEPT

if (const auto message = make_compact_block(link))
{
LOGN("Announce compact ..." << encode_hash(hash).substr(
hash_size - 8, 8) << " to [" << opposite() << "].");
LOGN("Announce compact ..."
<< encode_hash(hash).substr(hash_size - 8, 8) << " to ["
<< opposite() << "].");
NOTIFY(*message, handle_send, _1);
return true;
}
Expand Down Expand Up @@ -95,6 +96,8 @@ bool protocol_header_out_70014::handle_receive_send_compact(const code& ec,
announce();
}

// TODO: drop peer on non-negotiated version.

return true;
}

Expand Down
66 changes: 33 additions & 33 deletions src/protocols/protocol_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ code protocol_peer::fault(const code& ec) NOEXCEPT
return ec;
}

// Announcements.
// ----------------------------------------------------------------------------

void protocol_peer::set_announced(const system::hash_digest& hash) NOEXCEPT
{
channel_->set_announced(hash);
}

void protocol_peer::set_current(bool value) NOEXCEPT
{
channel_->set_current(value);
}

bool protocol_peer::was_announced(const system::hash_digest& hash) const NOEXCEPT
{
return channel_->was_announced(hash);
}

// Events notification.
// ----------------------------------------------------------------------------

void protocol_peer::notify(const code& ec, event_value value) const NOEXCEPT
{
session_->notify(ec, value);
}

void protocol_peer::notify_one(object_key key, const code& ec,
event_value value) const NOEXCEPT
{
session_->notify_one(key, ec, value);
}

// Compact blocks (bip152 version 2).
// ----------------------------------------------------------------------------

Expand All @@ -83,7 +115,7 @@ network::messages::peer::compact_block::cptr protocol_peer::make_compact_block(

compact_block::short_id_list ids{};
ids.reserve(sub1(wtxids.size()));
for (auto it = std::next(wtxids.begin()); it != wtxids.end(); ++it)
for (auto it = std::next(wtxids.cbegin()); it != wtxids.cend(); ++it)
{
if (*it == null_hash)
return {};
Expand All @@ -101,37 +133,5 @@ network::messages::peer::compact_block::cptr protocol_peer::make_compact_block(
});
}

// Announcements.
// ----------------------------------------------------------------------------

void protocol_peer::set_announced(const system::hash_digest& hash) NOEXCEPT
{
channel_->set_announced(hash);
}

void protocol_peer::set_current(bool value) NOEXCEPT
{
channel_->set_current(value);
}

bool protocol_peer::was_announced(const system::hash_digest& hash) const NOEXCEPT
{
return channel_->was_announced(hash);
}

// Events notification.
// ----------------------------------------------------------------------------

void protocol_peer::notify(const code& ec, event_value value) const NOEXCEPT
{
session_->notify(ec, value);
}

void protocol_peer::notify_one(object_key key, const code& ec,
event_value value) const NOEXCEPT
{
session_->notify_one(key, ec, value);
}

} // namespace node
} // namespace libbitcoin
Loading