Kagome
Polkadot Runtime Engine in C++17
chain_spec_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CHAIN_SPEC_IMPL_HPP
7 #define KAGOME_CHAIN_SPEC_IMPL_HPP
8 
10 
11 #include <boost/property_tree/ptree.hpp>
12 
13 #include "log/logger.hpp"
15 
16 namespace kagome::application {
17 
18  class ChainSpecImpl : public ChainSpec {
19  public:
20  enum class Error {
21  MISSING_ENTRY = 1,
25  };
26 
27  static outcome::result<std::shared_ptr<ChainSpecImpl>> loadFrom(
28  const std::string &config_path);
29 
30  ~ChainSpecImpl() override = default;
31 
32  const std::string &name() const override {
33  return name_;
34  }
35 
36  const std::string &id() const override {
37  return id_;
38  }
39 
40  const std::string &chainType() const override {
41  return chain_type_;
42  }
43 
44  const std::vector<libp2p::multi::Multiaddress> &bootNodes() const override {
45  return boot_nodes_;
46  }
47 
48  const std::vector<std::pair<std::string, size_t>> &telemetryEndpoints()
49  const override {
50  return telemetry_endpoints_;
51  }
52 
53  const std::string &protocolId() const override {
54  return protocol_id_;
55  }
56 
57  const std::map<std::string, std::string> &properties() const override {
58  return properties_;
59  }
60 
61  std::optional<std::reference_wrapper<const std::string>> getProperty(
62  const std::string &property) const override {
63  auto it = properties_.find(property);
64  if (it != properties_.end()) {
65  return {{it->second}};
66  }
67  return std::nullopt;
68  }
69 
70  const std::set<primitives::BlockHash> &forkBlocks() const override {
71  return fork_blocks_;
72  }
73 
74  const std::set<primitives::BlockHash> &badBlocks() const override {
75  return bad_blocks_;
76  }
77 
78  std::optional<std::string> consensusEngine() const override {
79  return consensus_engine_;
80  }
81 
82  std::shared_ptr<const primitives::CodeSubstituteBlockIds> codeSubstitutes()
83  const override {
85  }
86 
87  const GenesisRawData &getGenesisTopSection() const override {
88  return genesis_;
89  }
90 
92  const override {
93  return children_default_;
94  }
95 
96  outcome::result<common::Buffer> fetchCodeSubstituteByBlockInfo(
97  const primitives::BlockInfo &block_info) const override;
98 
99  private:
100  outcome::result<void> loadFromJson(const std::string &file_path);
101  outcome::result<void> loadFields(const boost::property_tree::ptree &tree);
102  outcome::result<void> loadGenesis(const boost::property_tree::ptree &tree);
103  outcome::result<void> loadBootNodes(
104  const boost::property_tree::ptree &tree);
105 
106  template <typename T>
107  outcome::result<std::decay_t<T>> ensure(std::string_view entry_name,
108  boost::optional<T> opt_entry) {
109  if (not opt_entry) {
110  log_->error("Required '{}' entry not found in the chain spec",
111  entry_name);
112  return Error::MISSING_ENTRY;
113  }
114  return opt_entry.value();
115  }
116 
117  outcome::result<primitives::BlockId> parseBlockId(
118  const std::string_view block_id_str) const;
119 
120  ChainSpecImpl() = default;
121 
122  std::string name_;
123  std::string id_;
124  std::string chain_type_;
125  std::string config_path_;
126  std::vector<libp2p::multi::Multiaddress> boot_nodes_;
127  std::vector<std::pair<std::string, size_t>> telemetry_endpoints_;
128  std::string protocol_id_{"sup"};
129  std::map<std::string, std::string> properties_;
130  std::set<primitives::BlockHash> fork_blocks_;
131  std::set<primitives::BlockHash> bad_blocks_;
132  std::optional<std::string> consensus_engine_;
133  std::shared_ptr<primitives::CodeSubstituteBlockIds> known_code_substitutes_;
136  log::Logger log_ = log::createLogger("chain_spec", "kagome");
137  };
138 
139 } // namespace kagome::application
140 
142 
143 #endif // KAGOME_CHAIN_SPEC_IMPL_HPP
std::shared_ptr< primitives::CodeSubstituteBlockIds > known_code_substitutes_
std::vector< libp2p::multi::Multiaddress > boot_nodes_
const std::set< primitives::BlockHash > & badBlocks() const override
std::map< std::string, std::string > properties_
outcome::result< void > loadFromJson(const std::string &file_path)
outcome::result< primitives::BlockId > parseBlockId(const std::string_view block_id_str) const
const std::string & id() const override
outcome::result< common::Buffer > fetchCodeSubstituteByBlockInfo(const primitives::BlockInfo &block_info) const override
std::vector< std::pair< std::string, size_t > > telemetry_endpoints_
ChildrenDefaultRawData children_default_
std::optional< std::reference_wrapper< const std::string > > getProperty(const std::string &property) const override
static outcome::result< std::shared_ptr< ChainSpecImpl > > loadFrom(const std::string &config_path)
outcome::result< void > loadFields(const boost::property_tree::ptree &tree)
const std::string & protocolId() const override
OUTCOME_HPP_DECLARE_ERROR(kagome::api, JRpcServerImpl::Error)
const std::string & chainType() const override
std::set< primitives::BlockHash > fork_blocks_
std::vector< std::pair< common::Buffer, common::Buffer >> GenesisRawData
Definition: chain_spec.hpp:18
std::optional< std::string > consensusEngine() const override
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
std::optional< std::string > consensus_engine_
std::set< primitives::BlockHash > bad_blocks_
std::shared_ptr< const primitives::CodeSubstituteBlockIds > codeSubstitutes() const override
const ChildrenDefaultRawData & getGenesisChildrenDefaultSection() const override
const std::vector< libp2p::multi::Multiaddress > & bootNodes() const override
Return ids of peer nodes of the current node.
const std::vector< std::pair< std::string, size_t > > & telemetryEndpoints() const override
std::map< common::Buffer, GenesisRawData > ChildrenDefaultRawData
Definition: chain_spec.hpp:19
const std::set< primitives::BlockHash > & forkBlocks() const override
outcome::result< void > loadGenesis(const boost::property_tree::ptree &tree)
const std::map< std::string, std::string > & properties() const override
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
outcome::result< void > loadBootNodes(const boost::property_tree::ptree &tree)
const GenesisRawData & getGenesisTopSection() const override
const std::string & name() const override
outcome::result< std::decay_t< T > > ensure(std::string_view entry_name, boost::optional< T > opt_entry)