From 65e7acfee0362224f132b038516db7b2f569217e Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sat, 11 May 2024 00:59:31 -0400 Subject: [PATCH] Stub in chaser_snapshot. --- Makefile.am | 2 + builds/cmake/CMakeLists.txt | 1 + .../libbitcoin-node/libbitcoin-node.vcxproj | 2 + .../libbitcoin-node.vcxproj.filters | 6 ++ include/bitcoin/node.hpp | 1 + include/bitcoin/node/chase.hpp | 4 + include/bitcoin/node/chasers/chaser.hpp | 5 +- .../bitcoin/node/chasers/chaser_snapshot.hpp | 55 ++++++++++ include/bitcoin/node/chasers/chasers.hpp | 1 + include/bitcoin/node/full_node.hpp | 1 + src/chasers/chaser.cpp | 7 +- src/chasers/chaser_confirm.cpp | 3 + src/chasers/chaser_snapshot.cpp | 100 ++++++++++++++++++ src/full_node.cpp | 4 +- 14 files changed, 189 insertions(+), 3 deletions(-) create mode 100644 include/bitcoin/node/chasers/chaser_snapshot.hpp create mode 100644 src/chasers/chaser_snapshot.cpp diff --git a/Makefile.am b/Makefile.am index 3aab583f..62899de9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ @@ -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 diff --git a/builds/cmake/CMakeLists.txt b/builds/cmake/CMakeLists.txt index 2687de43..bdbf5adb 100644 --- a/builds/cmake/CMakeLists.txt +++ b/builds/cmake/CMakeLists.txt @@ -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" diff --git a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj index db5d0d9b..08b9781c 100644 --- a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj +++ b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj @@ -79,6 +79,7 @@ + @@ -113,6 +114,7 @@ + diff --git a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters index 15c98286..e18dc741 100644 --- a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters +++ b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters @@ -66,6 +66,9 @@ src\chasers + + src\chasers + src\chasers @@ -164,6 +167,9 @@ include\bitcoin\node\chasers + + include\bitcoin\node\chasers + include\bitcoin\node\chasers diff --git a/include/bitcoin/node.hpp b/include/bitcoin/node.hpp index cdd209c0..8d1b5f10 100644 --- a/include/bitcoin/node.hpp +++ b/include/bitcoin/node.hpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include diff --git a/include/bitcoin/node/chase.hpp b/include/bitcoin/node/chase.hpp index 0e2051bd..c35a8e1c 100644 --- a/include/bitcoin/node/chase.hpp +++ b/include/bitcoin/node/chase.hpp @@ -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, diff --git a/include/bitcoin/node/chasers/chaser.hpp b/include/bitcoin/node/chasers/chaser.hpp index b03e544b..99158e47 100644 --- a/include/bitcoin/node/chasers/chaser.hpp +++ b/include/bitcoin/node/chasers/chaser.hpp @@ -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. @@ -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. /// ----------------------------------------------------------------------- diff --git a/include/bitcoin/node/chasers/chaser_snapshot.hpp b/include/bitcoin/node/chasers/chaser_snapshot.hpp new file mode 100644 index 00000000..9622f90f --- /dev/null +++ b/include/bitcoin/node/chasers/chaser_snapshot.hpp @@ -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 . + */ +#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_SNAPSHOT_HPP +#define LIBBITCOIN_NODE_CHASERS_CHASER_SNAPSHOT_HPP + +#include +#include +#include + +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 diff --git a/include/bitcoin/node/chasers/chasers.hpp b/include/bitcoin/node/chasers/chasers.hpp index 61e66018..3f415a00 100644 --- a/include/bitcoin/node/chasers/chasers.hpp +++ b/include/bitcoin/node/chasers/chasers.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include diff --git a/include/bitcoin/node/full_node.hpp b/include/bitcoin/node/full_node.hpp index 5e7d46cc..58506621 100644 --- a/include/bitcoin/node/full_node.hpp +++ b/include/bitcoin/node/full_node.hpp @@ -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_{}; }; diff --git a/src/chasers/chaser.cpp b/src/chasers/chaser.cpp index 43bd9fc4..536f46f6 100644 --- a/src/chasers/chaser.cpp +++ b/src/chasers/chaser.cpp @@ -40,7 +40,7 @@ chaser::chaser(full_node& node) NOEXCEPT { } -// Close/suspend. +// Methods. // ---------------------------------------------------------------------------- bool chaser::closed() const NOEXCEPT @@ -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. // ---------------------------------------------------------------------------- diff --git a/src/chasers/chaser_confirm.cpp b/src/chasers/chaser_confirm.cpp index f0aab38d..d00db8f7 100644 --- a/src/chasers/chaser_confirm.cpp +++ b/src/chasers/chaser_confirm.cpp @@ -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; diff --git a/src/chasers/chaser_snapshot.cpp b/src/chasers/chaser_snapshot.cpp new file mode 100644 index 00000000..51ca527d --- /dev/null +++ b/src/chasers/chaser_snapshot.cpp @@ -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 . + */ +#include + +#include +#include +#include +#include + +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(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 diff --git a/src/full_node.cpp b/src/full_node.cpp index 063e3088..a64f49a0 100644 --- a/src/full_node.cpp +++ b/src/full_node.cpp @@ -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()) { } @@ -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;