Skip to content

Commit

Permalink
Stub in chaser_snapshot.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed May 11, 2024
1 parent c965eed commit 65e7acf
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ src_libbitcoin_node_la_SOURCES = \
src/chasers/chaser_confirm.cpp \
src/chasers/chaser_header.cpp \
src/chasers/chaser_preconfirm.cpp \
src/chasers/chaser_snapshot.cpp \
src/chasers/chaser_template.cpp \
src/chasers/chaser_transaction.cpp \
src/protocols/protocol.cpp \
Expand Down Expand Up @@ -139,6 +140,7 @@ include_bitcoin_node_chasers_HEADERS = \
include/bitcoin/node/chasers/chaser_header.hpp \
include/bitcoin/node/chasers/chaser_organize.hpp \
include/bitcoin/node/chasers/chaser_preconfirm.hpp \
include/bitcoin/node/chasers/chaser_snapshot.hpp \
include/bitcoin/node/chasers/chaser_template.hpp \
include/bitcoin/node/chasers/chaser_transaction.hpp \
include/bitcoin/node/chasers/chasers.hpp
Expand Down
1 change: 1 addition & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/chasers/chaser_confirm.cpp"
"../../src/chasers/chaser_header.cpp"
"../../src/chasers/chaser_preconfirm.cpp"
"../../src/chasers/chaser_snapshot.cpp"
"../../src/chasers/chaser_template.cpp"
"../../src/chasers/chaser_transaction.cpp"
"../../src/protocols/protocol.cpp"
Expand Down
2 changes: 2 additions & 0 deletions builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<ClCompile Include="..\..\..\..\src\chasers\chaser_confirm.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_header.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_preconfirm.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_snapshot.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_template.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_transaction.cpp" />
<ClCompile Include="..\..\..\..\src\configuration.cpp" />
Expand Down Expand Up @@ -113,6 +114,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_header.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_organize.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_preconfirm.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_snapshot.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_template.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_transaction.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chasers.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
<ClCompile Include="..\..\..\..\src\chasers\chaser_preconfirm.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\chasers\chaser_snapshot.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\chasers\chaser_template.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
Expand Down Expand Up @@ -164,6 +167,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_preconfirm.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_snapshot.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_template.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <bitcoin/node/chasers/chaser_header.hpp>
#include <bitcoin/node/chasers/chaser_organize.hpp>
#include <bitcoin/node/chasers/chaser_preconfirm.hpp>
#include <bitcoin/node/chasers/chaser_snapshot.hpp>
#include <bitcoin/node/chasers/chaser_template.hpp>
#include <bitcoin/node/chasers/chaser_transaction.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
Expand Down
4 changes: 4 additions & 0 deletions include/bitcoin/node/chase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ enum class chase
/// Issued by 'full_node' and handled by 'preconfirm'.
start,

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

/// Chaser is directed to start when there are no downloads (height_t).
/// Issued by 'organize' and handled by 'preconfirm'.
bump,
Expand Down
5 changes: 4 additions & 1 deletion include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class BCN_API chaser
return boost::asio::post(strand(), BIND_THIS(method, args));
}

/// Close.
/// Methods.
/// -----------------------------------------------------------------------

/// Node threadpool is stopped and may still be joining.
Expand All @@ -74,6 +74,9 @@ class BCN_API chaser
/// A race condition could result in an unsuspended connection.
virtual code suspend(const code& ec) const NOEXCEPT;

/// Snapshot the store, suspends and resumes network.
code snapshot(const store::event_handler& handler) NOEXCEPT;

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

Expand Down
55 changes: 55 additions & 0 deletions include/bitcoin/node/chasers/chaser_snapshot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_SNAPSHOT_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_SNAPSHOT_HPP

#include <bitcoin/system.hpp>
#include <bitcoin/node/chasers/chaser.hpp>
#include <bitcoin/node/define.hpp>

namespace libbitcoin {
namespace node {

class full_node;

/// Perform automated snapshots.
class BCN_API chaser_snapshot
: public chaser
{
public:
DELETE_COPY_MOVE_DESTRUCT(chaser_snapshot);

chaser_snapshot(full_node& node) NOEXCEPT;

code start() NOEXCEPT override;

protected:
virtual bool handle_event(const code& ec, chase event_,
event_value value) NOEXCEPT;

virtual void do_snapshot(height_t height) NOEXCEPT;

private:
size_t snapshot_interval_;
};

} // namespace node
} // namespace libbitcoin

#endif
1 change: 1 addition & 0 deletions include/bitcoin/node/chasers/chasers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <bitcoin/node/chasers/chaser_header.hpp>
#include <bitcoin/node/chasers/chaser_organize.hpp>
#include <bitcoin/node/chasers/chaser_preconfirm.hpp>
#include <bitcoin/node/chasers/chaser_snapshot.hpp>
#include <bitcoin/node/chasers/chaser_template.hpp>
#include <bitcoin/node/chasers/chaser_transaction.hpp>

Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class BCN_API full_node
chaser_confirm chaser_confirm_;
chaser_transaction chaser_transaction_;
chaser_template chaser_template_;
chaser_snapshot chaser_snapshot_;
event_subscriber event_subscriber_;
object_key keys_{};
};
Expand Down
7 changes: 6 additions & 1 deletion src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ chaser::chaser(full_node& node) NOEXCEPT
{
}

// Close/suspend.
// Methods.
// ----------------------------------------------------------------------------

bool chaser::closed() const NOEXCEPT
Expand All @@ -53,6 +53,11 @@ code chaser::suspend(const code& ec) const NOEXCEPT
return node_.suspend(ec);
}

code chaser::snapshot(const store::event_handler& handler) NOEXCEPT
{
return node_.snapshot(handler);
}

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

Expand Down
3 changes: 3 additions & 0 deletions src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ bool chaser_confirm::set_confirmed(header_t link, height_t height) NOEXCEPT
if (!query.push_confirmed(link))
return false;

if (is_zero(height % config().node.snapshot_interval_()))
notify(error::success, chase::snapshot, height);

notify(error::success, chase::organized, link);
fire(events::block_organized, height);
return true;
Expand Down
100 changes: 100 additions & 0 deletions src/chasers/chaser_snapshot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/node/chasers/chaser_snapshot.hpp>

#include <bitcoin/system.hpp>
#include <bitcoin/node/chasers/chaser.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/full_node.hpp>

namespace libbitcoin {
namespace node {

#define CLASS chaser_snapshot

using namespace system;
using namespace std::placeholders;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

chaser_snapshot::chaser_snapshot(full_node& node) NOEXCEPT
: chaser(node),
snapshot_interval_(node.config().node.snapshot_interval_())
{
}

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

// initialize snapshot tracking state.
code chaser_snapshot::start() NOEXCEPT
{
SUBSCRIBE_EVENTS(handle_event, _1, _2, _3);
return error::success;
}

// event handlers
// ----------------------------------------------------------------------------

bool chaser_snapshot::handle_event(const code&, chase event_,
event_value value) NOEXCEPT
{
if (closed())
return false;

switch (event_)
{
case chase::snapshot:
{
POST(do_snapshot, possible_narrow_cast<height_t>(value));
break;
}
case chase::stop:
{
return false;
}
default:
{
break;
}
}

return true;
}

void chaser_snapshot::do_snapshot(size_t height) NOEXCEPT
{
BC_ASSERT(stranded());

if (closed())
return;

if (const auto ec = snapshot([&](auto, auto) NOEXCEPT
{
LOGN("SNAPSHOT PROGRESS: " << height);
}))
{
LOGN("SNAPSHOT ERROR: " << ec.message());
}
}

BC_POP_WARNING()

} // namespace database
} // namespace libbitcoin
4 changes: 3 additions & 1 deletion src/full_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ full_node::full_node(query& query, const configuration& configuration,
chaser_confirm_(*this),
chaser_transaction_(*this),
chaser_template_(*this),
chaser_snapshot_(*this),
event_subscriber_(strand())
{
}
Expand Down Expand Up @@ -79,7 +80,8 @@ void full_node::do_start(const result_handler& handler) NOEXCEPT
((ec = chaser_preconfirm_.start())) ||
((ec = chaser_confirm_.start())) ||
((ec = chaser_transaction_.start())) ||
((ec = chaser_template_.start())))
((ec = chaser_template_.start())) ||
((ec = chaser_snapshot_.start())))
{
handler(ec);
return;
Expand Down

0 comments on commit 65e7acf

Please sign in to comment.