Skip to content

Commit

Permalink
Merge pull request #161 from hyperledger/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
takemiyamakoto committed Feb 16, 2017
2 parents 6ac8c37 + ddfce00 commit a2f9e30
Show file tree
Hide file tree
Showing 17 changed files with 441 additions and 60 deletions.
10 changes: 10 additions & 0 deletions core/infra/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ add_library(config_manager STATIC
peer_service_with_json.cpp
iroha_config_with_json.cpp
)

target_link_libraries(config_manager
config_format
exception
logger
)

add_library(config_format STATIC
config_format.cpp
)
4 changes: 2 additions & 2 deletions core/infra/config/abstract_config_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AbstractConfigManager {

logger::debug("config") << "load json is " << jsonStr;

setConfigData(std::move(jsonStr));
parseConfigDataFromString(std::move(jsonStr));

return _configData;
}
Expand All @@ -60,7 +60,7 @@ class AbstractConfigManager {
return std::string(it, std::istreambuf_iterator<char>());
}

void setConfigData(std::string&& jsonStr) {
virtual void parseConfigDataFromString(std::string&& jsonStr) {
try {
_configData = json::parse(std::move(jsonStr));
} catch (...) {
Expand Down
162 changes: 162 additions & 0 deletions core/infra/config/config_format.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <regex>
#include <json.hpp>
#include <util/exception.hpp>
#include <util/logger.hpp>
#include "config_format.hpp"

using ConfigFormat = config::ConfigFormat;
using nlohmann::json;

ConfigFormat::ConfigFormat() {}

ConfigFormat& ConfigFormat::getInstance() {
static ConfigFormat instance;
return instance;
}

bool ConfigFormat::ensureFormatSumeragi(const std::string& configStr) {
return ensureFormat(configStr, R"(
{
"me":{
"ip":"ip",
"name":"*",
"publicKey":"publicKey",
"privateKey":"privateKey"
},
"group":[
{
"ip":"ip",
"name":"*",
"publicKey":"publicKey"
}
]
}
)");
}

bool ConfigFormat::ensureFormat(const std::string& configStr, const std::string& formatConfigStr) {

json config, formatConfig;

try {
config = json::parse(configStr);
} catch(std::exception& e) {
return false;
}

try {
formatConfig = json::parse(formatConfigStr);
} catch(std::exception& e) {
throw std::domain_error("cannot parse config format. " + std::string(e.what()));
}

return ensureFormat(config, formatConfig, "(root)");
}

bool ConfigFormat::ensureFormat(json& actualConfig, json& formatConfig, const std::string& history) {

if (actualConfig.type() != formatConfig.type()) {
logger::warning("peer service with json")
<< "type must be " << formatConfig.type() << ", but is " << actualConfig.type();
return false;
} else {

if (actualConfig.is_object()) {

for (auto it = formatConfig.begin(); it != formatConfig.end(); it++) {
if (actualConfig.find(it.key()) == actualConfig.end()) {
logger::warning("peer service with json")
<< "Not found: \"" << it.key() << "\" in " << history;
return false;
}
}

std::vector<json::iterator> configIters;

bool res = true;

for (auto it = actualConfig.begin(); it != actualConfig.end(); it++) {
if (formatConfig.find(it.key()) == formatConfig.end()) {
logger::warning("peer service with json")
<< "Unused keys: \"" << it.key() << "\" in " << history;
return false;
} else {
res = res && ensureFormat(
it.value(),
formatConfig.find(it.key()).value(),
history + " : \"" + it.key() + "\""
);
}
}

return res;
}
else if (actualConfig.is_string()) {

const auto& format = formatConfig.get<std::string>();
const auto& value = actualConfig.get<std::string>();

if (format == "ip") {
// cannot use config file because nlohmann::json cannot parse string
// std::regex ipRegex(FormatRegExConfig::getInstance().getIPRegEx());
std::regex ipRegex("^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
if (not std::regex_match(value, ipRegex)) {
logger::warning("peer service with json")
<< "IP " << value << " looks like not a valid ip.";
return false;
}
return true;
}
else if (format == "publicKey") {
// currently do nothing
return true;
}
else if (format == "privateKey") {
// currently do nothing
return true;
}
else if (format == "*") {
// DO NOTHING. Any string is accepted.
return true;
}
else {
throw exception::NotImplementedException("ensureFormat", "peer service with json");
}
}
else if (actualConfig.is_array()) {
auto formats = formatConfig.get<std::vector<json>>();
auto values = actualConfig.get<std::vector<json>>();

if (formats.size() != 1) {
throw std::domain_error("array size should be 1 , but is " + std::to_string(formats.size()) + ")");
}

auto& format = formats.front();

bool res = true;
for (auto& value: values) {
res = res && ensureFormat(value, format, history);
}
return res;
}
else {
throw std::invalid_argument("not implemented error");
}
}
}
28 changes: 28 additions & 0 deletions core/infra/config/config_format.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace config {
class ConfigFormat {
public:
static ConfigFormat& getInstance();
bool ensureFormatSumeragi(const std::string& configStr);

private:
ConfigFormat();
bool ensureFormat(const std::string& configStr, const std::string& formatConfigStr);
bool ensureFormat(nlohmann::json& actualConfig, nlohmann::json& formatConfig, const std::string& history);
};
}
55 changes: 55 additions & 0 deletions core/infra/config/peer_service_with_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#include <deque>
#include <regex>
#include <crypto/base64.hpp>
#include <util/logger.hpp>
#include <util/exception.hpp>
#include <util/use_optional.hpp>
#include <json.hpp>
#include "peer_service_with_json.hpp"
#include "config_format.hpp"

using PeerServiceConfig = config::PeerServiceConfig;
using nlohmann::json;


PeerServiceConfig::PeerServiceConfig() {}

Expand Down Expand Up @@ -58,6 +68,51 @@ std::vector<std::unique_ptr<peer::Node>> PeerServiceConfig::getPeerList() {
return nodes;
}


void PeerServiceConfig::parseConfigDataFromString(std::string&& jsonStr) {
try {
if (not ConfigFormat::getInstance().ensureFormatSumeragi(jsonStr)) {
throw exception::ParseFromStringException("sumeragi");
}
_configData = json::parse(std::move(jsonStr));
} catch (exception::ParseFromStringException& e) {
logger::warning("peer service config") << e.what();
logger::warning("peer service config") << getConfigName() << " is set to be default.";

// default sumeragi.json
_configData = json::parse(R"({
"me":{
"ip":"172.17.0.6",
"name":"samari",
"publicKey":"Sht5opDIxbyK+oNuEnXUs5rLbrvVgb2GjSPfqIYGFdU=",
"privateKey":"aGIuSZRhnGfFyeoKNm/NbTylnAvRfMu3KumOEfyT2HPf36jSF22m2JXWrdCmKiDoshVqjFtZPX3WXaNuo9L8WA=="
},
"group":[
{
"ip":"172.17.0.3",
"name":"mizuki",
"publicKey":"jDQTiJ1dnTSdGH+yuOaPPZIepUj1Xt3hYOvLQTME3V0="
},
{
"ip":"172.17.0.4",
"name":"natori",
"publicKey":"Q5PaQEBPQLALfzYmZyz9P4LmCNfgM5MdN1fOuesw3HY="
},
{
"ip":"172.17.0.5",
"name":"kabohara",
"publicKey":"f5MWZUZK9Ga8XywDia68pH1HLY/Ts0TWBHsxiFDR0ig="
},
{
"ip":"172.17.0.6",
"name":"samari",
"publicKey":"Sht5opDIxbyK+oNuEnXUs5rLbrvVgb2GjSPfqIYGFdU="
}
]
})");
}
}

std::string PeerServiceConfig::getConfigName() {
return "config/sumeragi.json";
}
3 changes: 3 additions & 0 deletions core/infra/config/peer_service_with_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class PeerServiceConfig : config::AbstractConfigManager {
private:
PeerServiceConfig();

protected:
void parseConfigDataFromString(std::string&& jsonStr) override;

public:
static PeerServiceConfig &getInstance();

Expand Down
6 changes: 3 additions & 3 deletions core/infra/protobuf/convertor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ namespace convertor{
object::Account decodeObject(Event::Account aAccount){
auto publicKey = aAccount.publickey();
auto name = aAccount.name();
std::vector<std::tuple<std::string,long>> assets;
std::vector<std::tuple<std::string, std::int64_t>> assets;
for(const Event::Asset& as: aAccount.assets()){
assets.emplace_back(as.name(), static_cast<long>(as.value()));
assets.emplace_back(as.name(), static_cast<std::int64_t>(as.value()));
}
return object::Account(
std::move(publicKey),
Expand Down Expand Up @@ -114,7 +114,7 @@ namespace convertor{
ConsensusEvent<Transaction<Add<object::Account>>> decodeTransaction2ConsensusEvent(Event::Transaction tx){
auto name = tx.account().name();
auto publicKey = tx.account().publickey();
std::vector<std::tuple<std::string,long>> assets;
std::vector<std::tuple<std::string, std::int64_t>> assets;
for(auto&& as: tx.account().assets()){
assets.emplace_back(as.name(), as.value());
}
Expand Down
2 changes: 1 addition & 1 deletion core/repository/domain/account_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace repository{
bool update_quantity(
const std::string& uuid,
const std::string& assetName,
long newValue
std::int64_t newValue
);

object::Account findByUuid(const std::string& uuid);
Expand Down

0 comments on commit a2f9e30

Please sign in to comment.