OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
sentineld_2pc.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include "controller.hpp"
7
8#include <csignal>
9#include <unordered_map>
10
11// LCOV_EXCL_START
12auto main(int argc, char** argv) -> int {
13 auto args = cbdc::config::get_args(argc, argv);
14 if(args.size() < 3) {
15 std::cerr << "Usage: " << args[0] << " <config file> <sentinel id>"
16 << std::endl;
17 return -1;
18 }
19
20 const auto sentinel_id = std::stoull(args[2]);
21
22 auto cfg_or_err = cbdc::config::load_options(args[1]);
23 if(std::holds_alternative<std::string>(cfg_or_err)) {
24 std::cerr << "Error loading config file: "
25 << std::get<std::string>(cfg_or_err) << std::endl;
26 return -1;
27 }
28 auto opts = std::get<cbdc::config::options>(cfg_or_err);
29
30 if(opts.m_sentinel_endpoints.size() <= sentinel_id) {
31 std::cerr << "Sentinel ID not in config file" << std::endl;
32 return -1;
33 }
34
35 auto logger = std::make_shared<cbdc::logging::log>(
36 opts.m_sentinel_loglevels[sentinel_id]);
37
38 std::string sha2_impl(SHA256AutoDetect());
39 logger->info("using sha2:", sha2_impl);
40
41 static std::atomic_bool running{true};
42
43 auto ctl
44 = cbdc::sentinel_2pc::controller{static_cast<uint32_t>(sentinel_id),
45 opts,
46 logger};
47 if(!ctl.init()) {
48 return -1;
49 }
50
51 // Wait for CTRL+C etc
52 std::signal(SIGINT, [](int /* sig */) {
53 running = false;
54 });
55
56 logger->info("Sentinel running...");
57
58 while(running) {
59 static constexpr auto running_check_delay
60 = std::chrono::milliseconds(1000);
61 std::this_thread::sleep_for(running_check_delay);
62 }
63
64 logger->info("Shutting down...");
65
66 return 0;
67}
68// LCOV_EXCL_STOP
Manages a sentinel server for the two-phase commit architecture.
auto load_options(const std::string &config_file) -> std::variant< options, std::string >
Loads options from the given config file and check for invariants.
Definition config.cpp:668
auto get_args(int argc, char **argv) -> std::vector< std::string >
Converts c-args from an executable's main function into a vector of strings.
Definition config.cpp:751
auto main(int argc, char **argv) -> int