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
1 change: 0 additions & 1 deletion console/executor_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <boost/format.hpp>
#include <bitcoin/node.hpp>

Expand Down
1 change: 0 additions & 1 deletion console/executor_dumps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <boost/format.hpp>
#include <bitcoin/node.hpp>

Expand Down
1 change: 0 additions & 1 deletion console/executor_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <boost/format.hpp>
#include <bitcoin/node.hpp>

Expand Down
1 change: 0 additions & 1 deletion console/executor_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <boost/format.hpp>
#include <bitcoin/node.hpp>

Expand Down
1 change: 0 additions & 1 deletion console/executor_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <boost/format.hpp>
#include <bitcoin/node.hpp>

Expand Down
1 change: 0 additions & 1 deletion console/executor_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <boost/format.hpp>
#include <bitcoin/node.hpp>

Expand Down
1 change: 0 additions & 1 deletion console/executor_scans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <boost/format.hpp>
#include <bitcoin/node.hpp>

Expand Down
1 change: 0 additions & 1 deletion console/executor_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <boost/format.hpp>
#include <bitcoin/node.hpp>

Expand Down
1 change: 0 additions & 1 deletion console/executor_test_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <boost/format.hpp>
#include <bitcoin/node.hpp>

Expand Down
1 change: 0 additions & 1 deletion console/executor_test_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <future>
#include <iostream>
#include <map>
#include <mutex>
#include <boost/format.hpp>
#include <bitcoin/node.hpp>
namespace libbitcoin {
Expand Down
1 change: 0 additions & 1 deletion include/bitcoin/node/block_arena.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#ifndef LIBBITCOIN_NODE_BLOCK_ARENA_HPP
#define LIBBITCOIN_NODE_BLOCK_ARENA_HPP

#include <shared_mutex>
#include <bitcoin/system.hpp>
#include <bitcoin/node/define.hpp>

Expand Down
3 changes: 3 additions & 0 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class BCN_API chaser
/// Reset store disk full condition.
virtual code reload(const store::event_handler& handler) NOEXCEPT;

/// Get reorganization lock.
virtual lock get_reorganization_lock() NOEXCEPT;

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

Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/node/define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/// Standard includes (do not include directly).
#include <functional>
#include <memory>
#include <mutex>
#include <utility>
#include <variant>

Expand Down Expand Up @@ -57,6 +58,7 @@ typedef std::error_code code;
typedef std::function<void(const code&, size_t)> organize_handler;
typedef database::store<database::map> store;
typedef database::query<store> query;
typedef std::unique_lock<std::mutex> lock;

/// Work types.
typedef network::race_all<const code&> job;
Expand Down
5 changes: 5 additions & 0 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ class BCN_API full_node
/// Get the memory resource.
virtual network::memory& get_memory() NOEXCEPT;

/// Get reorganization lock.
/// Used to prevent candidate chain reorganization during confirmation.
virtual lock get_reorganization_lock() NOEXCEPT;

protected:
/// Session attachments.
/// -----------------------------------------------------------------------
Expand All @@ -161,6 +165,7 @@ class BCN_API full_node

// These are thread safe.
const configuration& config_;
std::mutex reorganization_mutex_{};
memory_controller memory_;
query& query_;

Expand Down
27 changes: 17 additions & 10 deletions include/bitcoin/node/impl/chasers/chaser_organize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ void CLASS::do_organize(typename Block::cptr block,
{
BC_ASSERT(stranded());

// shared_ptr copy keeps block ref in scope until completion of set_code.
const auto& hash = block->get_hash();
const auto& header = get_header(*block);

Expand Down Expand Up @@ -235,12 +234,16 @@ void CLASS::do_organize(typename Block::cptr block,

// Pop down to the branch point.
auto index = top_candidate;
while (index > branch_point)
if (top_candidate > branch_point)
{
if (!set_reorganized(index--))
get_reorganization_lock();
while (index > branch_point)
{
handler(fault(error::organize5), height);
return;
if (!set_reorganized(index--))
{
handler(fault(error::organize5), height);
return;
}
}
}

Expand Down Expand Up @@ -356,6 +359,7 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
// Height and above excluded from tree since they are unconfirmable blocks.
// ........................................................................
// Forward order is required to advance chain state for tree.
// Can't pop in loop because that requires reverse order.

typename Block::cptr block{};
for (auto index = add1(fork_point); index < height; ++index)
Expand All @@ -374,15 +378,18 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT

// Pop candidates from top candidate down to above fork point.
// ........................................................................
// Can't pop in loop above because state chaining requires forward order.

const auto top_candidate = state_->height();
for (auto index = top_candidate; index > fork_point; --index)
if (top_candidate > fork_point)
{
if (!set_reorganized(index))
get_reorganization_lock();
for (auto index = top_candidate; index > fork_point; --index)
{
fault(error::organize11);
return;
if (!set_reorganized(index))
{
fault(error::organize11);
return;
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/block_arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <bitcoin/node/block_arena.hpp>

#include <algorithm>
#include <shared_mutex>
#include <bitcoin/system.hpp>

namespace libbitcoin {
Expand Down
6 changes: 6 additions & 0 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
#include <bitcoin/node/chasers/chaser.hpp>

#include <mutex>
#include <bitcoin/database.hpp>
#include <bitcoin/network.hpp>
#include <bitcoin/node/configuration.hpp>
Expand Down Expand Up @@ -82,6 +83,11 @@ code chaser::reload(const store::event_handler& handler) NOEXCEPT
return node_.reload(handler);
}

lock chaser::get_reorganization_lock() NOEXCEPT
{
return node_.get_reorganization_lock();
}

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

Expand Down
4 changes: 4 additions & 0 deletions src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ void chaser_confirm::do_bumped(height_t height) NOEXCEPT
if (closed())
return;

// Prevents organizer from popping candidates (does not block push).
get_reorganization_lock();

// Scan from candidate height to first confirmed, return links and work.
uint256_t work{};
header_links fork{};
Expand Down Expand Up @@ -291,6 +294,7 @@ void chaser_confirm::organize(header_links& fork, const header_links& popped,
}

// Prevent stall by posting internal event, avoiding external handlers.
// Posts new work, preventing recursion and releasing reorganization lock.
handle_event(error::success, chase::bump, height_t{});
}

Expand Down
5 changes: 5 additions & 0 deletions src/full_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ network::memory& full_node::get_memory() NOEXCEPT
return memory_;
}

lock full_node::get_reorganization_lock() NOEXCEPT
{
return lock{ reorganization_mutex_ };
}

// Session attachments.
// ----------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ parser::parser(system::chain::selection context) NOEXCEPT
configured.database.prevout_rate = 5;

configured.database.validated_bk_buckets = 850'001;
configured.database.validated_bk_size = 3'700'000;
configured.database.validated_bk_size = 1'700'000;
configured.database.validated_bk_rate = 5;

configured.database.validated_tx_buckets = 1;
Expand Down Expand Up @@ -863,7 +863,7 @@ options_metadata parser::load_settings() THROWS
(
"database.validated_bk_size",
value<uint64_t>(&configured.database.validated_bk_size),
"The minimum allocation of the validated_bk table body, defaults to '3400000'."
"The minimum allocation of the validated_bk table body, defaults to '1700000'."
)
(
"database.validated_bk_rate",
Expand Down
Loading