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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class BCN_API protocol_header_in_70014
system::hash_digest hash{};
system::chain::header::cptr header{};
system::siphash_key key{};
std::vector<uint64_t> short_ids{};
std::vector<system::chain::short_id::integer> short_ids{};
system::chain::transaction_cptrs txs{};
database::tx_links links{};
std::vector<size_t> missing{};
Expand Down
12 changes: 0 additions & 12 deletions include/bitcoin/node/protocols/protocol_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,6 @@ class BCN_API protocol_peer
/// Compact blocks (bip152 version 2).
/// -----------------------------------------------------------------------

/// Short id siphash key of the header and nonce.
static system::siphash_key to_compact_key(
const system::chain::header& header, uint64_t nonce) NOEXCEPT;

/// Short id of a witness hash (low 48 bits of its siphash).
static uint64_t to_short_id(const system::siphash_key& key,
const system::hash_digest& wtxid) NOEXCEPT;

/// Short id wire encoding (six bytes little endian).
static uint64_t from_mini(const system::mini_hash& id) NOEXCEPT;
static system::mini_hash to_mini(uint64_t id) NOEXCEPT;

/// 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;
Expand Down
8 changes: 4 additions & 4 deletions src/protocols/protocol_header_in_70014.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void protocol_header_in_70014::collect(const compact_block& message,
block.height = ctx.height;
block.header = message.header_ptr;
block.hash = message.header_ptr->get_hash();
block.key = to_compact_key(*message.header_ptr, message.nonce);
block.key = chain::short_id::to_key(*message.header_ptr, message.nonce);

if (!decode(block, message))
{
Expand Down Expand Up @@ -232,7 +232,7 @@ bool protocol_header_in_70014::decode(fill& block,
auto id = ids.begin();
for (position = zero; position < count; ++position)
if (!block.txs.at(position))
block.short_ids.at(position) = from_mini(*id++);
block.short_ids.at(position) = chain::short_id::from_mini(*id++);

return true;
}
Expand All @@ -241,7 +241,7 @@ bool protocol_header_in_70014::decode(fill& block,
bool protocol_header_in_70014::scan(fill& block) NOEXCEPT
{
BC_ASSERT(stranded());
std::vector<uint64_t> short_ids{};
std::vector<chain::short_id::integer> short_ids{};
std::vector<size_t> positions{};
for (size_t position{}; position < block.txs.size(); ++position)
{
Expand Down Expand Up @@ -326,7 +326,7 @@ bool protocol_header_in_70014::handle_receive_compact_transactions(
{
const auto& tx = txs.at(index);
const auto position = block.missing.at(index);
if (!tx || (to_short_id(block.key, tx->is_coinbase() ?
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)))
{
Expand Down
39 changes: 3 additions & 36 deletions src/protocols/protocol_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,40 +63,6 @@ code protocol_peer::fault(const code& ec) NOEXCEPT
// Compact blocks (bip152 version 2).
// ----------------------------------------------------------------------------

// static
siphash_key protocol_peer::to_compact_key(const chain::header& header,
uint64_t nonce) NOEXCEPT
{
auto data = header.to_data();
extend(data, to_little_endian(nonce));
return to_siphash_key(split(sha256_hash(data)).first);
}

// static
uint64_t protocol_peer::to_short_id(const siphash_key& key,
const hash_digest& wtxid) NOEXCEPT
{
constexpr auto mask = unmask_right<uint64_t>(to_bits(mini_hash_size));
return bit_and(siphash(key, wtxid), mask);
}

// static
uint64_t protocol_peer::from_mini(const mini_hash& id) NOEXCEPT
{
data_array<sizeof(uint64_t)> bytes{};
std::copy(id.begin(), id.end(), bytes.begin());
return from_little_endian<uint64_t>(bytes);
}

// static
mini_hash protocol_peer::to_mini(uint64_t id) NOEXCEPT
{
mini_hash out{};
const auto bytes = to_little_endian(id);
std::copy_n(bytes.begin(), out.size(), out.begin());
return out;
}

network::messages::peer::compact_block::cptr protocol_peer::make_compact_block(
const database::header_link& link) const NOEXCEPT
{
Expand All @@ -113,7 +79,7 @@ network::messages::peer::compact_block::cptr protocol_peer::make_compact_block(
return {};

const auto nonce = maybe_random::next<uint64_t>(0, max_uint64);
const auto key = to_compact_key(*header, nonce);
const auto key = chain::short_id::to_key(*header, nonce);

compact_block::short_id_list ids{};
ids.reserve(sub1(wtxids.size()));
Expand All @@ -122,7 +88,8 @@ network::messages::peer::compact_block::cptr protocol_peer::make_compact_block(
if (*it == null_hash)
return {};

ids.push_back(to_mini(to_short_id(key, *it)));
const auto id = chain::short_id::to_id(key, *it);
ids.push_back(chain::short_id::to_mini(id));
}

return to_shared(compact_block
Expand Down
14 changes: 3 additions & 11 deletions test/functional/p2p_compact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ using namespace network::messages::peer;

constexpr uint64_t full_node = service::node_network | service::node_witness;

struct compact_accessor
: node::protocol_peer
{
using protocol_peer::to_compact_key;
using protocol_peer::to_short_id;
using protocol_peer::to_mini;
};

static compact_block prefilled(const chain::block& block,
const chain::transaction::cptr& coinbase)
{
Expand All @@ -41,11 +33,11 @@ static compact_block prefilled(const chain::block& block,

static compact_block shortened(const chain::block& block)
{
const auto key = compact_accessor::to_compact_key(block.header(), 42);
const auto key = chain::short_id::to_key(block.header(), 42);
const auto& coinbase = *block.transactions_ptr()->front();
const auto hash = bitcoin_hash(coinbase.to_data(true));
const auto short_id = compact_accessor::to_short_id(key, hash);
const auto id = compact_accessor::to_mini(short_id);
const auto integer = chain::short_id::to_id(key, hash);
const auto id = chain::short_id::to_mini(integer);
return { block.header_ptr(), 42, { id }, {} };
}

Expand Down
Loading