Skip to content

Commit

Permalink
Merge pull request #615 from evoskuil/master
Browse files Browse the repository at this point in the history
Create store on bn run if directory missing.
  • Loading branch information
evoskuil committed May 12, 2024
2 parents b73308f + c91075e commit a174987
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 68 deletions.
90 changes: 44 additions & 46 deletions console/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1831,12 +1831,15 @@ bool executor::check_store_path(bool create) const
{
const auto& configuration = metadata_.configured.file;
if (configuration.empty())
{
logger(BN_USING_DEFAULT_CONFIG);
}
else
{
logger(format(BN_USING_CONFIG_FILE) % configuration);
}

const auto& store = metadata_.configured.database.path;

if (create)
{
logger(format(BN_INITIALIZING_CHAIN) % store);
Expand Down Expand Up @@ -1872,13 +1875,21 @@ bool executor::create_store(bool details)
return false;
}

// Create and confirm genesis block (store invalid without it).
logger(BN_INITCHAIN_DATABASE_INITIALIZE);
if (!query_.initialize(metadata_.configured.bitcoin.genesis_block))
{
logger(BN_INITCHAIN_DATABASE_INITIALIZE_FAILURE);
return false;
}

const auto span = duration_cast<seconds>(logger::now() - start);
logger(format(BN_INITCHAIN_CREATED) % span.count());
return true;
}

// not timed or announced (generally fast)
code executor::open_store_code(bool details)
code executor::open_store_coded(bool details)
{
////logger(BN_DATABASE_STARTING);
if (const auto ec = store_.open([&](auto event_, auto table)
Expand All @@ -1897,7 +1908,7 @@ code executor::open_store_code(bool details)

bool executor::open_store(bool details)
{
return !open_store_code(details);
return !open_store_coded(details);
}

bool executor::close_store(bool details)
Expand Down Expand Up @@ -2043,27 +2054,14 @@ bool executor::do_initchain()
{
log_.stop();
if (!check_store_path(true) ||
!create_store(true) ||
!open_store())
!create_store(true))
return false;

// Create and confirm genesis block (store invalid without it).
logger(BN_INITCHAIN_DATABASE_INITIALIZE);
if (!query_.initialize(metadata_.configured.bitcoin.genesis_block))
{
logger(BN_INITCHAIN_DATABASE_INITIALIZE_FAILURE);
return false;
}

// Records and sizes reflect genesis block only.
dump_sizes();
dump_records();
dump_buckets();

// This one can take a few seconds on cold iron.
logger(BN_MEASURE_PROGRESS_START);
dump_progress();

if (!close_store())
return false;

Expand Down Expand Up @@ -2556,6 +2554,23 @@ void executor::subscribe_close()
});
}

void executor::dump_options() const
{
logger(BN_NODE_INTERRUPT);
logger(BN_LOG_TABLE_HEADER);
logger(format("Application.. " BN_LOG_TABLE) % levels::application_defined % toggle_.at(levels::application));
logger(format("News......... " BN_LOG_TABLE) % levels::news_defined % toggle_.at(levels::news));
logger(format("Session...... " BN_LOG_TABLE) % levels::session_defined % toggle_.at(levels::session));
logger(format("Protocol..... " BN_LOG_TABLE) % levels::protocol_defined % toggle_.at(levels::protocol));
logger(format("ProXy........ " BN_LOG_TABLE) % levels::proxy_defined % toggle_.at(levels::proxy));
logger(format("Wire......... " BN_LOG_TABLE) % levels::wire_defined % toggle_.at(levels::wire));
logger(format("Remote....... " BN_LOG_TABLE) % levels::remote_defined % toggle_.at(levels::remote));
logger(format("Fault........ " BN_LOG_TABLE) % levels::fault_defined % toggle_.at(levels::fault));
logger(format("Quit......... " BN_LOG_TABLE) % levels::quit_defined % toggle_.at(levels::quit));
logger(format("Object....... " BN_LOG_TABLE) % levels::objects_defined % toggle_.at(levels::objects));
logger(format("Verbose...... " BN_LOG_TABLE) % levels::verbose_defined % toggle_.at(levels::verbose));
}

// ----------------------------------------------------------------------------

bool executor::do_run()
Expand All @@ -2575,38 +2590,21 @@ bool executor::do_run()
subscribe_log(log);
subscribe_events(events);
subscribe_capture();

logger(BN_LOG_HEADER);
logger(BN_NODE_INTERRUPT);
logger(BN_LOG_TABLE_HEADER);
logger(format("Application.. " BN_LOG_TABLE) % levels::application_defined % toggle_.at(levels::application));
logger(format("News......... " BN_LOG_TABLE) % levels::news_defined % toggle_.at(levels::news));
logger(format("Session...... " BN_LOG_TABLE) % levels::session_defined % toggle_.at(levels::session));
logger(format("Protocol..... " BN_LOG_TABLE) % levels::protocol_defined % toggle_.at(levels::protocol));
logger(format("ProXy........ " BN_LOG_TABLE) % levels::proxy_defined % toggle_.at(levels::proxy));
logger(format("Wire......... " BN_LOG_TABLE) % levels::wire_defined % toggle_.at(levels::wire));
logger(format("Remote....... " BN_LOG_TABLE) % levels::remote_defined % toggle_.at(levels::remote));
logger(format("Fault........ " BN_LOG_TABLE) % levels::fault_defined % toggle_.at(levels::fault));
logger(format("Quit......... " BN_LOG_TABLE) % levels::quit_defined % toggle_.at(levels::quit));
logger(format("Object....... " BN_LOG_TABLE) % levels::objects_defined % toggle_.at(levels::objects));
logger(format("Verbose...... " BN_LOG_TABLE) % levels::verbose_defined % toggle_.at(levels::verbose));

if (!check_store_path())
return false;

// stopped by stopper.
capture_.start();

const auto ec = open_store_code(true);
if (ec == database::error::flush_lock)
if (check_store_path())
{
if (!restore_store(true))
auto ec = open_store_coded(true);
if ((ec == database::error::flush_lock) && !restore_store(true))
ec = error::store_integrity;

if (ec)
{
stopper(BN_NODE_STOPPED);
return false;
}
}
else if (ec)
else if (!check_store_path(true) || !create_store(true))
{
stopper(BN_NODE_STOPPED);
return false;
Expand All @@ -2615,11 +2613,13 @@ bool executor::do_run()
dump_sizes();
dump_records();
dump_buckets();

// This one can take a few seconds on cold iron.
logger(BN_MEASURE_PROGRESS_START);
dump_progress();

// stopped by stopper.
capture_.start();
dump_options();

// Create node.
metadata_.configured.network.initialize();
node_ = std::make_shared<full_node>(query_, metadata_.configured, log_);
Expand All @@ -2640,11 +2640,9 @@ bool executor::do_run()
// Stop network (if not already stopped by self).
node_->close();

// All measures can change except buckets.
// Sizes and records change, buckets don't.
dump_sizes();
dump_records();

// This one can take a few seconds on cold iron.
logger(BN_MEASURE_PROGRESS_START);
dump_progress();

Expand Down
5 changes: 3 additions & 2 deletions console/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ class executor
void dump_buckets() const;
void dump_progress() const;
void dump_collisions() const;
void dump_options() const;

// Store functions.
code open_store_code(bool details=false);
code open_store_coded(bool details=false);
bool open_store(bool details=false);
bool close_store(bool details=false);
bool create_store(bool details=false);
bool restore_store(bool details=false);
bool backup_store(bool details=false);
bool check_store_path(bool create = false) const;
bool check_store_path(bool create=false) const;

// Command line options.
bool do_help();
Expand Down
4 changes: 2 additions & 2 deletions console/localize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ namespace node {
"Node is running."

#define BN_UNINITIALIZED_DATABASE \
"The %1% database directory does not exist, run: bn --initchain"
"The %1% database directory does not exist."
#define BN_UNINITIALIZED_CHAIN \
"The %1% database is not initialized, delete and run: bn --initchain"
"The %1% database is not initialized, delete and retry."
#define BN_DATABASE_START_FAIL \
"Database failed to start with error '%1%'."
#define BN_DATABASE_STOPPING \
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/node/chase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enum class chase
start,

/// It's time to snapshot the store, relayed to independent chaser thread.
/// Issued by 'xxxxxx' and handled by 'snapshot'.
/// Issued by [various] and handled by 'snapshot'.
snapshot,

/// Chaser is directed to start when there are no downloads (height_t).
Expand All @@ -49,7 +49,7 @@ enum class chase
/// Issued by 'block_in_31800' and handled by 'session_outbound'.
starved,

/// Channel (slow) directed to split work and stop (channel_t).
/// Channel (slow) directed to split work and stop (object_t).
/// Issued by 'session_outbound' and handled by 'block_in_31800'.
split,

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class BCN_API protocol
/// -----------------------------------------------------------------------

/// Report performance, handler may direct self-terminate.
virtual void performance(uint64_t channel, uint64_t speed,
virtual void performance(uint64_t speed,
network::result_handler&& handler) const NOEXCEPT;

/// Suspend all existing and future network connections.
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class BCN_API session
/// -----------------------------------------------------------------------

/// Handle performance, base returns false (implied terminate).
virtual void performance(uint64_t channel, uint64_t speed,
virtual void performance(object_key channel, uint64_t speed,
network::result_handler&& handler) NOEXCEPT;

/// Suspensions.
Expand Down
11 changes: 8 additions & 3 deletions src/chasers/chaser_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ bool chaser_snapshot::handle_event(const code&, chase event_,
{
case chase::snapshot:
{
// Either from confirmed or disk full.
POST(do_snapshot, possible_narrow_cast<height_t>(value));
break;
}
case chase::stop:
{
// From full_node.stop().
POST(do_snapshot, height_t{});
return false;
}
default:
Expand All @@ -85,12 +88,14 @@ void chaser_snapshot::do_snapshot(size_t height) NOEXCEPT
if (closed())
return;

if (const auto ec = snapshot([&](auto, auto) NOEXCEPT
if (const auto ec = snapshot([&](auto event_, auto table) NOEXCEPT
{
LOGA("SNAPSHOT PROGRESS: " << height);
LOGN("Snapshot at (" << height << ") event ["
<< static_cast<size_t>(event_) << ", "
<< static_cast<size_t>(table) << "].");
}))
{
LOGA("SNAPSHOT ERROR: " << ec.message());
LOGN("Snapshot failed, " << ec.message());
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/full_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ code full_node::suspend(const code& ec) NOEXCEPT
LOGS("Suspending network connections: " << ec.message());
}

// Multiple messages will be absorbed by snapshot chaser.
if (ec == database::error::disk_full)
notify(error::success, chase::snapshot, {});

// Do these even if suspended was true, since there are multiple levels.
notify(error::success, chase::suspend, ec.value());
return p2p::suspend(ec);
Expand Down
4 changes: 2 additions & 2 deletions src/protocols/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ object_key protocol::events_key() const NOEXCEPT
// Methods.
// ----------------------------------------------------------------------------

void protocol::performance(uint64_t channel, uint64_t speed,
void protocol::performance(uint64_t speed,
network::result_handler&& handler) const NOEXCEPT
{
session_->performance(channel, speed, std::move(handler));
session_->performance(events_key(), speed, std::move(handler));
}

code protocol::suspend(const code& ec) NOEXCEPT
Expand Down
4 changes: 2 additions & 2 deletions src/protocols/protocol_block_in_31800.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ bool protocol_block_in_31800::handle_event(const code& ec,
case chase::split:
{
// chase::split is posted by notify_one() using subscription key.
// That key is not the channel identifier, but it's this channel.
POST(do_split, identifier());
// 'value' is the channel that requested a split to obtain work.
POST(do_split, channel_t{});
break;
}
case chase::stall:
Expand Down
3 changes: 1 addition & 2 deletions src/protocols/protocol_performer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ void protocol_performer::send_performance(uint64_t rate) NOEXCEPT
if (deviation_)
{
performance_timer_->stop();
node::protocol::performance(identifier(), rate,
BIND(handle_send_performance, _1));
performance(rate, BIND(handle_send_performance, _1));
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/sessions/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ void session::unsubscribe_events(object_key key) NOEXCEPT
// Methods.
// ----------------------------------------------------------------------------

void session::performance(uint64_t, uint64_t, result_handler&& handler) NOEXCEPT
void session::performance(object_key, uint64_t,
result_handler&& handler) NOEXCEPT
{
// This session type does not implement performance, handler error.
// The handler captures the protocol shared pointer for its lifetime.
Expand Down
9 changes: 5 additions & 4 deletions src/sessions/session_outbound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ void session_outbound::split(object_t self) NOEXCEPT
if (slowest != speeds_.end())
{
// Erase entry so less likely to be claimed again before stopping.
const auto channel = slowest->first;
const auto slow = slowest->first;
speeds_.erase(slowest);

node::session::notify_one(self, error::success, chase::split, channel);
// Notify slow channel to split itself (in favor of 'self' channel).
node::session::notify_one(slow, error::success, chase::split, self);
return;
}

Expand All @@ -133,7 +134,7 @@ void session_outbound::split(object_t self) NOEXCEPT
// performance
// ----------------------------------------------------------------------------

void session_outbound::performance(uint64_t channel, uint64_t speed,
void session_outbound::performance(object_key channel, uint64_t speed,
network::result_handler&& handler) NOEXCEPT
{
if (stopped())
Expand All @@ -146,7 +147,7 @@ void session_outbound::performance(uint64_t channel, uint64_t speed,
BIND(do_performance, channel, speed, handler));
}

void session_outbound::do_performance(uint64_t channel, uint64_t speed,
void session_outbound::do_performance(object_key channel, uint64_t speed,
const network::result_handler& handler) NOEXCEPT
{
BC_ASSERT(stranded());
Expand Down

0 comments on commit a174987

Please sign in to comment.