Kagome
Polkadot Runtime Engine in C++17
app_configuration_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_APP_CONFIGURATION_IMPL_HPP
7 #define KAGOME_APP_CONFIGURATION_IMPL_HPP
8 
10 
11 #define RAPIDJSON_NO_SIZETYPEDEFINE
12 namespace rapidjson {
13  using SizeType = ::std::size_t;
14 }
15 #include <rapidjson/document.h>
16 #undef RAPIDJSON_NO_SIZETYPEDEFINE
17 
18 #include <array>
19 #include <cstdio>
20 #include <memory>
21 
22 #include "log/logger.hpp"
23 
24 #ifdef DECLARE_PROPERTY
25 #error DECLARE_PROPERTY already defined!
26 #endif // DECLARE_PROPERTY
27 #define DECLARE_PROPERTY(T, N) \
28  private: \
29  T N##_; \
30  \
31  public: \
32  std::conditional<std::is_trivial<T>::value && (sizeof(T) <= sizeof(size_t)), \
33  T, \
34  const T &>::type \
35  N() const override { \
36  return N##_; \
37  }
38 
39 namespace kagome::application {
40 
41  // clang-format off
51  // clang-format on
52 
53  class AppConfigurationImpl final : public AppConfiguration {
54  using FilePtr = std::unique_ptr<std::FILE, decltype(&std::fclose)>;
55 
56  public:
58  ~AppConfigurationImpl() override = default;
59 
61  AppConfigurationImpl &operator=(const AppConfigurationImpl &) = delete;
62 
64  AppConfigurationImpl &operator=(AppConfigurationImpl &&) = default;
65 
66  [[nodiscard]] bool initializeFromArgs(int argc, const char **argv);
67 
68  network::Roles roles() const override {
69  return roles_;
70  }
71  boost::filesystem::path chainSpecPath() const override;
72  boost::filesystem::path runtimeCacheDirPath() const override;
73  boost::filesystem::path runtimeCachePath(
74  std::string runtime_hash) const override;
75  boost::filesystem::path chainPath(std::string chain_id) const override;
76  boost::filesystem::path databasePath(std::string chain_id) const override;
77  boost::filesystem::path keystorePath(std::string chain_id) const override;
78 
79  const std::optional<crypto::Ed25519PrivateKey> &nodeKey() const override {
80  return node_key_;
81  }
82 
83  const std::optional<std::string> &nodeKeyFile() const override {
84  return node_key_file_;
85  }
86 
87  bool shouldSaveNodeKey() const override {
88  return save_node_key_;
89  }
90 
91  const std::vector<libp2p::multi::Multiaddress> &listenAddresses()
92  const override {
93  return listen_addresses_;
94  }
95 
96  const std::vector<libp2p::multi::Multiaddress> &publicAddresses()
97  const override {
98  return public_addresses_;
99  }
100 
101  const std::vector<libp2p::multi::Multiaddress> &bootNodes() const override {
102  return boot_nodes_;
103  }
104 
105  uint16_t p2pPort() const override {
106  return p2p_port_;
107  }
108  uint32_t outPeers() const override {
109  return out_peers_;
110  }
111  uint32_t inPeers() const override {
112  return in_peers_;
113  }
114  uint32_t inPeersLight() const override {
115  return in_peers_light_;
116  }
117  int32_t luckyPeers() const override {
118  return lucky_peers_;
119  }
120  const boost::asio::ip::tcp::endpoint &rpcHttpEndpoint() const override {
121  return rpc_http_endpoint_;
122  }
123  const boost::asio::ip::tcp::endpoint &rpcWsEndpoint() const override {
124  return rpc_ws_endpoint_;
125  }
126  const boost::asio::ip::tcp::endpoint &openmetricsHttpEndpoint()
127  const override {
128  return openmetrics_http_endpoint_;
129  }
130  uint32_t maxWsConnections() const override {
131  return max_ws_connections_;
132  }
133  std::chrono::seconds getRandomWalkInterval() const override {
134  return std::chrono::seconds(random_walk_interval_);
135  }
136  const std::vector<std::string> &log() const override {
137  return logger_tuning_config_;
138  }
139  uint32_t maxBlocksInResponse() const override {
140  return max_blocks_in_response_;
141  }
142  const network::PeeringConfig &peeringConfig() const override {
143  return peering_config_;
144  }
145  bool isRunInDevMode() const override {
146  return dev_mode_;
147  }
148  const std::string &nodeName() const override {
149  return node_name_;
150  }
151  const std::string &nodeVersion() const override {
152  return node_version_;
153  }
154  bool isTelemetryEnabled() const override {
155  return is_telemetry_enabled_;
156  }
157  const std::vector<telemetry::TelemetryEndpoint> &telemetryEndpoints()
158  const override {
159  return telemetry_endpoints_;
160  }
161  SyncMethod syncMethod() const override {
162  return sync_method_;
163  }
164  RuntimeExecutionMethod runtimeExecMethod() const override {
165  return runtime_exec_method_;
166  }
167  bool useWavmCache() const override {
168  return use_wavm_cache_;
169  }
170  bool purgeWavmCache() const override {
171  return purge_wavm_cache_;
172  }
174  return offchain_worker_mode_;
175  }
176  bool isOffchainIndexingEnabled() const override {
177  return enable_offchain_indexing_;
178  }
179  bool subcommandChainInfo() const override {
180  return subcommand_chain_info_;
181  }
182  std::optional<primitives::BlockId> recoverState() const override {
183  return recovery_state_;
184  }
185  StorageBackend storageBackend() const override {
186  return storage_backend_;
187  }
188  std::optional<std::string_view> devMnemonicPhrase() const override {
189  if (dev_mnemonic_phrase_) {
190  return *dev_mnemonic_phrase_;
191  }
192  return std::nullopt;
193  }
194 
195  private:
196  void parse_general_segment(const rapidjson::Value &val);
197  void parse_blockchain_segment(const rapidjson::Value &val);
198  void parse_storage_segment(const rapidjson::Value &val);
199  void parse_network_segment(const rapidjson::Value &val);
200  void parse_additional_segment(const rapidjson::Value &val);
201 
204  struct SegmentHandler {
205  using Handler = std::function<void(rapidjson::Value &)>;
206  char const *segment_name;
208  };
209 
210  // clang-format off
211  std::vector<SegmentHandler> handlers_ = {
212  SegmentHandler{"general", std::bind(&AppConfigurationImpl::parse_general_segment, this, std::placeholders::_1)},
213  SegmentHandler{"blockchain", std::bind(&AppConfigurationImpl::parse_blockchain_segment, this, std::placeholders::_1)},
214  SegmentHandler{"storage", std::bind(&AppConfigurationImpl::parse_storage_segment, this, std::placeholders::_1)},
215  SegmentHandler{"network", std::bind(&AppConfigurationImpl::parse_network_segment, this, std::placeholders::_1)},
216  SegmentHandler{"additional", std::bind(&AppConfigurationImpl::parse_additional_segment, this, std::placeholders::_1)},
217  };
218  // clang-format on
219 
220  bool validate_config();
221 
222  void read_config_from_file(const std::string &filepath);
223 
224  bool load_ms(const rapidjson::Value &val,
225  char const *name,
226  std::vector<std::string> &target);
227 
228  bool load_ma(const rapidjson::Value &val,
229  char const *name,
230  std::vector<libp2p::multi::Multiaddress> &target);
231  bool load_telemetry_uris(const rapidjson::Value &val,
232  char const *name,
233  std::vector<telemetry::TelemetryEndpoint> &target);
234  bool load_str(const rapidjson::Value &val,
235  char const *name,
236  std::string &target);
237  bool load_u16(const rapidjson::Value &val,
238  char const *name,
239  uint16_t &target);
240  bool load_u32(const rapidjson::Value &val,
241  char const *name,
242  uint32_t &target);
243  bool load_i32(const rapidjson::Value &val,
244  char const *name,
245  int32_t &target);
246  bool load_bool(const rapidjson::Value &val, char const *name, bool &target);
247 
254  boost::asio::ip::tcp::endpoint getEndpointFrom(const std::string &host,
255  uint16_t port) const;
256 
264  outcome::result<boost::asio::ip::tcp::endpoint> getEndpointFrom(
265  const libp2p::multi::Multiaddress &multiaddress) const;
266 
272  bool testListenAddresses() const;
273 
281  std::optional<telemetry::TelemetryEndpoint> parseTelemetryEndpoint(
282  const std::string &record) const;
283 
284  FilePtr open_file(const std::string &filepath);
285 
287 
289  std::optional<crypto::Ed25519PrivateKey> node_key_;
290  std::optional<std::string> node_key_file_;
292  std::vector<libp2p::multi::Multiaddress> listen_addresses_;
293  std::vector<libp2p::multi::Multiaddress> public_addresses_;
294  std::vector<libp2p::multi::Multiaddress> boot_nodes_;
295  std::vector<telemetry::TelemetryEndpoint> telemetry_endpoints_;
297  uint16_t p2p_port_;
298  boost::asio::ip::tcp::endpoint rpc_http_endpoint_;
299  boost::asio::ip::tcp::endpoint rpc_ws_endpoint_;
300  boost::asio::ip::tcp::endpoint openmetrics_http_endpoint_;
301  std::vector<std::string> logger_tuning_config_;
303  std::string rpc_http_host_;
304  std::string rpc_ws_host_;
306  boost::filesystem::path chain_spec_path_;
307  boost::filesystem::path base_path_;
308  std::optional<boost::filesystem::path> keystore_path_;
309  uint16_t rpc_http_port_;
310  uint16_t rpc_ws_port_;
312  uint32_t out_peers_;
313  uint32_t in_peers_;
314  uint32_t in_peers_light_;
315  int32_t lucky_peers_;
317  bool dev_mode_;
318  std::string node_name_;
319  std::string node_version_;
323  RuntimeExecutionMethod runtime_exec_method_;
329  std::optional<primitives::BlockId> recovery_state_;
330  StorageBackend storage_backend_ = StorageBackend::RocksDB;
331  std::optional<std::string> dev_mnemonic_phrase_;
332  };
333 
334 } // namespace kagome::application
335 
336 #undef DECLARE_PROPERTY
337 
338 #endif // KAGOME_APP_CONFIGURATION_IMPL_HPP
RuntimeExecutionMethod runtimeExecMethod() const override
const boost::asio::ip::tcp::endpoint & rpcWsEndpoint() const override
boost::asio::ip::tcp::endpoint rpc_http_endpoint_
std::optional< primitives::BlockId > recovery_state_
boost::asio::ip::tcp::endpoint openmetrics_http_endpoint_
const std::vector< std::string > & log() const override
std::optional< std::string_view > devMnemonicPhrase() const override
const std::vector< telemetry::TelemetryEndpoint > & telemetryEndpoints() const override
const std::vector< libp2p::multi::Multiaddress > & bootNodes() const override
std::vector< libp2p::multi::Multiaddress > boot_nodes_
OffchainWorkerMode offchainWorkerMode() const override
std::optional< crypto::Ed25519PrivateKey > node_key_
std::chrono::seconds getRandomWalkInterval() const override
const std::optional< crypto::Ed25519PrivateKey > & nodeKey() const override
std::optional< boost::filesystem::path > keystore_path_
std::vector< libp2p::multi::Multiaddress > listen_addresses_
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
const std::string & nodeName() const override
std::vector< libp2p::multi::Multiaddress > public_addresses_
const network::PeeringConfig & peeringConfig() const override
std::optional< primitives::BlockId > recoverState() const override
const std::string & nodeVersion() const override
std::unique_ptr< std::FILE, decltype(&std::fclose)> FilePtr
const boost::asio::ip::tcp::endpoint & openmetricsHttpEndpoint() const override
const std::vector< libp2p::multi::Multiaddress > & publicAddresses() const override
const std::vector< libp2p::multi::Multiaddress > & listenAddresses() const override
const std::optional< std::string > & nodeKeyFile() const override
const boost::asio::ip::tcp::endpoint & rpcHttpEndpoint() const override
::std::size_t SizeType
StorageBackend storageBackend() const override
std::vector< telemetry::TelemetryEndpoint > telemetry_endpoints_