Skip to content

Commit

Permalink
Merge pull request #591 from evoskuil/master
Browse files Browse the repository at this point in the history
Implement and employ static chaser_check::empty_map().
  • Loading branch information
evoskuil committed Apr 21, 2024
2 parents c8bbe46 + 6d27ab5 commit f82c599
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 54 deletions.
9 changes: 7 additions & 2 deletions include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class BCN_API chaser_check
public:
DELETE_COPY_MOVE_DESTRUCT(chaser_check);

/// Craete empty shared map.
static map_ptr empty_map() NOEXCEPT;

/// Move half of map into returned map.
static map_ptr split(const map_ptr& map) NOEXCEPT;

chaser_check(full_node& node) NOEXCEPT;

/// Initialize chaser state.
Expand All @@ -61,9 +67,8 @@ class BCN_API chaser_check
private:
typedef std::deque<map_ptr> maps;

////size_t count_maps(const maps& table) const NOEXCEPT;
size_t get_unassociated(maps& table, size_t start) const NOEXCEPT;
size_t count_map(const maps& table) const NOEXCEPT;
map_ptr make_map(size_t start, size_t count) const NOEXCEPT;
map_ptr get_map(maps& table) NOEXCEPT;

// These are thread safe.
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/node/protocols/protocol_block_in_31800.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_BLOCK_IN_31800_HPP

#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/protocols/protocol_performer.hpp>

Expand All @@ -40,7 +41,7 @@ class BCN_API protocol_block_in_31800
: protocol_performer(session, channel, performance),
block_type_(session.config().network.witness_node() ?
type_id::witness_block : type_id::block),
map_(std::make_shared<database::associations>())
map_(chaser_check::empty_map())
{
}
BC_POP_WARNING()
Expand Down Expand Up @@ -77,7 +78,6 @@ class BCN_API protocol_block_in_31800
network::messages::get_data create_get_data(
const map_ptr& map) const NOEXCEPT;

static map_ptr split(const map_ptr& map) NOEXCEPT;
void restore(const map_ptr& map) NOEXCEPT;
void handle_put_hashes(const code& ec) NOEXCEPT;
void handle_get_hashes(const code& ec, const map_ptr& map) NOEXCEPT;
Expand Down
62 changes: 37 additions & 25 deletions src/chasers/chaser_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ chaser_check::chaser_check(full_node& node) NOEXCEPT
{
}

// static
map_ptr chaser_check::empty_map() NOEXCEPT
{
return std::make_shared<associations>();
}

// static
map_ptr chaser_check::split(const map_ptr& map) NOEXCEPT
{
const auto half = empty_map();
auto& index = map->get<association::pos>();
const auto end = std::next(index.begin(), to_half(map->size()));
half->merge(index, index.begin(), end);
return half;
}

// start
// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -150,7 +166,7 @@ void chaser_check::do_purge_headers(height_t top) NOEXCEPT
// be purged, it simply means purge all hashes (reset all). All channels
// will get the purge notification before any subsequent download notify.
maps_.clear();
////LOGN("Hashes purged (" << count_map(maps_) << ") remain.");
////LOGN("Hashes purged (" << count_maps(maps_) << ") remain.");
notify(error::success, chase::purge, top);
}

Expand All @@ -177,7 +193,7 @@ void chaser_check::do_get_hashes(const map_handler& handler) NOEXCEPT
BC_ASSERT(stranded());

const auto map = get_map(maps_);
////LOGN("Hashes -" << map->size() << " (" << count_map(maps_) << ") remain.");
////LOGN("Hashes -" << map->size() << " (" << count_maps(maps_) << ") remain.");
handler(error::success, map);
}

Expand All @@ -190,7 +206,7 @@ void chaser_check::do_put_hashes(const map_ptr& map,
{
maps_.push_back(map);
notify(error::success, chase::download, map->size());
////LOGN("Hashes +" << map->size() << " (" << count_map(maps_) << ") remain.");
////LOGN("Hashes +" << map->size() << " (" << count_maps(maps_) << ") remain.");
}

handler(error::success);
Expand All @@ -215,7 +231,7 @@ void chaser_check::do_malleated(header_t link) NOEXCEPT
}

maps_.push_back(std::make_shared<associations>(associations{ out }));
////LOGN("Hashes +1 malleated (" << count_map(maps_) << ") remain.");
////LOGN("Hashes +1 malleated (" << count_maps(maps_) << ") remain.");
notify(error::success, chase::download, one);
}

Expand All @@ -224,40 +240,36 @@ void chaser_check::do_malleated(header_t link) NOEXCEPT

size_t chaser_check::get_unassociated(maps& table, size_t start) const NOEXCEPT
{
const auto& query = archive();
size_t added{};
while (true)
{
const auto map = make_map(start, inventory_);
if (map->empty()) break;
const auto map = std::make_shared<associations>(
query.get_unassociated_above(start, inventory_));

if (map->empty())
return added;

table.push_back(map);
start = map->top().height;
added += map->size();
}

return added;
}

size_t chaser_check::count_map(const maps& table) const NOEXCEPT
{
return std::accumulate(table.begin(), table.end(), zero,
[](size_t sum, const map_ptr& map) NOEXCEPT
{
return sum + map->size();
});
}

map_ptr chaser_check::make_map(size_t start, size_t count) const NOEXCEPT
{
// Known malleated blocks are disassociated and therefore appear here.
return std::make_shared<associations>(
archive().get_unassociated_above(start, count));
}

map_ptr chaser_check::get_map(maps& table) NOEXCEPT
{
return table.empty() ? std::make_shared<associations>() : pop_front(table);
return table.empty() ? empty_map() : pop_front(table);
}

////size_t chaser_check::count_maps(const maps& table) const NOEXCEPT
////{
//// return std::accumulate(table.begin(), table.end(), zero,
//// [](size_t sum, const map_ptr& map) NOEXCEPT
//// {
//// return sum + map->size();
//// });
////}

BC_POP_WARNING()
BC_POP_WARNING()
BC_POP_WARNING()
Expand Down
34 changes: 9 additions & 25 deletions src/protocols/protocol_block_in_31800.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <algorithm>
#include <bitcoin/database.hpp>
#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/define.hpp>

namespace libbitcoin {
Expand Down Expand Up @@ -65,7 +66,7 @@ void protocol_block_in_31800::stopping(const code& ec) NOEXCEPT
BC_ASSERT(stranded());

restore(map_);
map_ = std::make_shared<database::associations>();
map_ = chaser_check::empty_map();
stop_performance();

protocol::stopping(ec);
Expand Down Expand Up @@ -222,24 +223,12 @@ void protocol_block_in_31800::do_split(channel_t) NOEXCEPT

LOGP("Divide work (" << map_->size() << ") from [" << authority() << "].");

restore(split(map_));
restore(chaser_check::split(map_));
restore(map_);
map_ = std::make_shared<database::associations>();
map_ = chaser_check::empty_map();
stop(error::sacrificed_channel);
}

// static
map_ptr protocol_block_in_31800::split(const map_ptr& map) NOEXCEPT
{
// Move half of map into new half, map is mutable (only pointer is const).
const auto half = std::make_shared<database::associations>();
auto& index = map->get<database::association::pos>();
const auto begin = index.begin();
const auto end = std::next(begin, to_half(map->size()));
half->merge(index, begin, end);
return half;
}

// request hashes
// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -270,11 +259,11 @@ void protocol_block_in_31800::send_get_data(const map_ptr& map) NOEXCEPT
get_data protocol_block_in_31800::create_get_data(
const map_ptr& map) const NOEXCEPT
{
// clang emplace_back bug (no matching constructor), using push_back.
// bip144: get_data uses witness constant but inventory does not.

get_data getter{};
getter.items.reserve(map->size());

// bip144: get_data uses witness constant but inventory does not.
// clang emplace_back bug (no matching constructor), using push_back.
std::for_each(map->pos_begin(), map->pos_end(),
[&](const database::association& item) NOEXCEPT
{
Expand Down Expand Up @@ -329,13 +318,8 @@ bool protocol_block_in_31800::handle_receive_block(const code& ec,
if (const auto code = check(block, ctx))
{
// Both forms of malleabilty are possible here.
if (block.is_malleable())
{
// Block has not been associated, so just drop peer and continue.
// A malleable block with no valid variant (i.e. mined invalid)
// will cause a candidate stall until a stronger chain is found.
}
else
// Malleable has not been associated, so just drop peer and continue.
if (!block.is_malleable())
{
if (!query.set_block_unconfirmable(link))
{
Expand Down

0 comments on commit f82c599

Please sign in to comment.