hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
iroha_conf_loader.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_CONF_LOADER_HPP
7 #define IROHA_CONF_LOADER_HPP
8 
9 #include <fstream>
10 #include <string>
11 
12 #include <rapidjson/document.h>
13 #include <rapidjson/error/en.h>
14 #include <rapidjson/istreamwrapper.h>
15 #include <rapidjson/rapidjson.h>
16 
17 #include "main/assert_config.hpp"
18 
19 namespace config_members {
20  const char *BlockStorePath = "block_store_path";
21  const char *ToriiPort = "torii_port";
22  const char *InternalPort = "internal_port";
23  const char *KeyPairPath = "key_pair_path";
24  const char *PgOpt = "pg_opt";
25  const char *MaxProposalSize = "max_proposal_size";
26  const char *ProposalDelay = "proposal_delay";
27  const char *VoteDelay = "vote_delay";
28  const char *MstSupport = "mst_enable";
29  const char *MstExpirationTime = "mst_expiration_time";
30 } // namespace config_members
31 
32 static constexpr size_t kBadJsonPrintLength = 15;
33 static constexpr size_t kBadJsonPrintOffsset = 5;
35  "The place of error is out of the printed string boundaries!");
36 
37 std::string reportJsonParsingError(const rapidjson::Document &doc,
38  const std::string &conf_path,
39  std::istream &input) {
40  const size_t error_offset = doc.GetErrorOffset();
41  // This ensures the unsigned string beginning position does not cross zero:
42  const size_t print_offset =
43  std::max(error_offset, kBadJsonPrintOffsset) - kBadJsonPrintOffsset;
44  input.seekg(print_offset);
45  std::string json_error_buf(kBadJsonPrintLength, 0);
46  input.readsome(&json_error_buf[0], kBadJsonPrintLength);
47  return "JSON parse error [" + conf_path + "] " + "(near `" + json_error_buf
48  + "'): " + std::string(rapidjson::GetParseError_En(doc.GetParseError()));
49 }
50 
56 inline rapidjson::Document parse_iroha_config(const std::string &conf_path) {
57  namespace ac = assert_config;
58  namespace mbr = config_members;
59  rapidjson::Document doc;
60  std::ifstream ifs_iroha(conf_path);
61  rapidjson::IStreamWrapper isw(ifs_iroha);
62  const std::string kStrType = "string";
63  const std::string kUintType = "uint";
64  const std::string kBoolType = "bool";
65  doc.ParseStream(isw);
66  ac::assert_fatal(not doc.HasParseError(),
67  reportJsonParsingError(doc, conf_path, ifs_iroha));
68 
71  ac::assert_fatal(doc[mbr::BlockStorePath].IsString(),
73 
74  ac::assert_fatal(doc.HasMember(mbr::ToriiPort),
76  ac::assert_fatal(doc[mbr::ToriiPort].IsUint(),
77  ac::type_error(mbr::ToriiPort, kUintType));
78 
79  ac::assert_fatal(doc.HasMember(mbr::InternalPort),
81  ac::assert_fatal(doc[mbr::InternalPort].IsUint(),
82  ac::type_error(mbr::InternalPort, kUintType));
83 
85  ac::assert_fatal(doc[mbr::PgOpt].IsString(),
86  ac::type_error(mbr::PgOpt, kStrType));
87 
92 
93  ac::assert_fatal(doc.HasMember(mbr::ProposalDelay),
97 
98  ac::assert_fatal(doc.HasMember(mbr::VoteDelay),
100  ac::assert_fatal(doc[mbr::VoteDelay].IsUint(),
101  ac::type_error(mbr::VoteDelay, kUintType));
102 
103  ac::assert_fatal(doc.HasMember(mbr::MstSupport),
105  ac::assert_fatal(doc[mbr::MstSupport].IsBool(),
106  ac::type_error(mbr::MstSupport, kBoolType));
109  return doc;
110 }
111 
112 #endif // IROHA_CONF_LOADER_HPP
Definition: iroha_conf_loader.hpp:19
const char * ToriiPort
Definition: iroha_conf_loader.hpp:21
void assert_fatal(bool condition, std::string const &error)
Definition: assert_config.hpp:42
rapidjson::Document parse_iroha_config(const std::string &conf_path)
Definition: iroha_conf_loader.hpp:56
const char * VoteDelay
Definition: iroha_conf_loader.hpp:27
std::string input(const std::string &string)
Definition: logger.cpp:25
Definition: assert_config.hpp:12
std::string no_member_error(std::string const &member)
Definition: assert_config.hpp:16
std::string type_error(std::string const &value, std::string const &type)
Definition: assert_config.hpp:20
string(REPLACE".""/"CMAKE_SWIG_OUTDIR ${SWIG_JAVA_PKG}) else() set(CMAKE_SWIG_FLAGS"") endif() myswig_add_library(irohajava LANGUAGE java SOURCES bindings.i) unset(CMAKE_SWIG_FLAGS) swig_link_libraries(irohajava $
Definition: CMakeLists.txt:54
const char * MstExpirationTime
Definition: iroha_conf_loader.hpp:29
const char * ProposalDelay
Definition: iroha_conf_loader.hpp:26
static constexpr size_t kBadJsonPrintLength
Definition: iroha_conf_loader.hpp:32
const char * InternalPort
Definition: iroha_conf_loader.hpp:22
std::string reportJsonParsingError(const rapidjson::Document &doc, const std::string &conf_path, std::istream &input)
Definition: iroha_conf_loader.hpp:37
const char * MstSupport
Definition: iroha_conf_loader.hpp:28
static constexpr size_t kBadJsonPrintOffsset
Definition: iroha_conf_loader.hpp:33
const char * MaxProposalSize
Definition: iroha_conf_loader.hpp:25
const char * PgOpt
Definition: iroha_conf_loader.hpp:24
const char * BlockStorePath
Definition: iroha_conf_loader.hpp:20
const char * KeyPairPath
Definition: iroha_conf_loader.hpp:23