CCF
Loading...
Searching...
No Matches
hooks.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
10
11#include <stdexcept>
12
13namespace ccf
14{
15 struct NodeAddr
16 {
17 std::string hostname;
18 std::string port;
19 };
20
22 {
23 ccf::kv::Version version;
24 std::map<NodeId, std::optional<NodeAddr>> cfg_delta;
25
26 public:
28 version(version_)
29 {
30 for (const auto& [node_id, opt_ni] : w)
31 {
32 if (!opt_ni.has_value())
33 {
34 // Deleted node will have already been retired
35 continue;
36 }
37
38 const auto& ni = opt_ni.value();
39 const auto [host, port] =
40 split_net_address(ni.node_to_node_interface.published_address);
41 switch (ni.status)
42 {
44 {
45 // Pending nodes are not added to consensus until they are
46 // trusted
47 break;
48 }
50 {
51 cfg_delta.try_emplace(node_id, NodeAddr{host, port});
52 break;
53 }
55 {
56 cfg_delta.try_emplace(node_id, std::nullopt);
57 break;
58 }
59 default:
60 {
61 throw std::logic_error(fmt::format(
62 "Unknown node status {} for node {} in configuration change hook",
63 static_cast<uint8_t>(ni.status),
64 node_id));
65 }
66 }
67 }
68 }
69
71 {
72 auto configuration = consensus->get_latest_configuration_unsafe();
73 for (const auto& [node_id, opt_ni] : cfg_delta)
74 {
75 if (opt_ni.has_value())
76 {
77 configuration.try_emplace(node_id, opt_ni->hostname, opt_ni->port);
78 }
79 else
80 {
81 configuration.erase(node_id);
82 }
83 }
84 if (!cfg_delta.empty())
85 {
86 consensus->add_configuration(version, configuration);
87 }
88 }
89 };
90}
Definition hooks.h:22
void call(ccf::kv::ConfigurableConsensus *consensus) override
Definition hooks.h:70
ConfigurationChangeHook(ccf::kv::Version version_, const Nodes::Write &w)
Definition hooks.h:27
Definition kv_types.h:194
Definition hooks.h:15
std::map< K, std::optional< V > > Write
Definition map.h:40
uint64_t Version
Definition version.h:10
Definition app_interface.h:13
Definition consensus_types.h:23
Definition configuration.h:14
Definition hooks.h:16
std::string hostname
Definition hooks.h:17
std::string port
Definition hooks.h:18