Skip to content

Commit

Permalink
Merge pull request #139 from hyperledger/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
takemiyamakoto committed Jan 23, 2017
2 parents 9301a15 + f67d8aa commit 944618f
Show file tree
Hide file tree
Showing 26 changed files with 417 additions and 168 deletions.
3 changes: 2 additions & 1 deletion core/consensus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ include_directories(

target_link_libraries(sumeragi
peer_service_with_json
iroha_config_with_json
connection_with_grpc
signature
event_repository
Expand All @@ -27,4 +28,4 @@ target_link_libraries(consensus_event
transaction
validator
hash
)
)
38 changes: 25 additions & 13 deletions core/consensus/sumeragi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ limitations under the License.

#include "../repository/consensus/transaction_repository.hpp"
#include "../repository/domain/account_repository.hpp"
#include "../infra/config/peer_service_with_json.hpp"
#include "../infra/config/iroha_config_with_json.hpp"

/**
* |ーーー| |ーーー| |ーーー| |ーーー|
Expand All @@ -58,11 +60,13 @@ namespace sumeragi {
using namespace object;


static size_t concurrency =
std::thread::hardware_concurrency() <= 0
static size_t concurrency =
config::IrohaConfigManager::getInstance().getParam("concurrency", 0) <= 0
? 1
: std::thread::hardware_concurrency();



//thread pool and a storage of events
static ThreadPool pool(
ThreadPoolOptions{
Expand Down Expand Up @@ -195,12 +199,14 @@ namespace sumeragi {
context = std::make_unique<Context>(std::move(peers));
peers.clear();

logger::info("sumeragi") << "My key is " << peer::getMyIp();
logger::info("sumeragi") << "My key is " << config::PeerServiceConfig::getInstance().getMyIp();
logger::info("sumeragi") << "Sumeragi setted";
logger::info("sumeragi") << "set number of validatingPeer";

context->numValidatingPeers = context->validatingPeers.size();
context->maxFaulty = context->numValidatingPeers / 3; // Default to approx. 1/3 of the network. TODO: make this configurable
context->maxFaulty = context->numValidatingPeers / (
config::IrohaConfigManager::getInstance().getParam("numValidatingPeers", 3)
); // Default to approx. 1/3 of the network.

context->proxyTailNdx = context->maxFaulty * 2 + 1;

Expand Down Expand Up @@ -260,19 +266,21 @@ namespace sumeragi {
logger::info("sumeragi") << "Add my signature...";

logger::info("sumeragi") << "hash:" << event.transaction().hash();
logger::info("sumeragi") << "pub:" << peer::getMyPublicKey();
logger::info("sumeragi") << "pro:" << peer::getPrivateKey();
logger::info("sumeragi") << "pub:" << config::PeerServiceConfig::getInstance().getMyPublicKey();
logger::info("sumeragi") << "pro:" << config::PeerServiceConfig::getInstance().getPrivateKey();
logger::info("sumeragi") << "sog:" << signature::sign(
event.transaction().hash(),
peer::getMyPublicKey(),
peer::getPrivateKey()
config::PeerServiceConfig::getInstance().getMyPublicKey(),
config::PeerServiceConfig::getInstance().getPrivateKey()
);

//detail::printIsSumeragi(context->isSumeragi);
// Really need? blow "if statement" will be false anytime.
detail::addSignature(event,
peer::getMyPublicKey(),
signature::sign(event.transaction().hash(), peer::getMyPublicKey(), peer::getPrivateKey())
config::PeerServiceConfig::getInstance().getMyPublicKey(),
signature::sign(event.transaction().hash(),
config::PeerServiceConfig::getInstance().getMyPublicKey(),
config::PeerServiceConfig::getInstance().getPrivateKey())
);

if (detail::eventSignatureIsEmpty(event) && context->isSumeragi) {
Expand Down Expand Up @@ -355,13 +363,17 @@ namespace sumeragi {
// event->execution();
} else {
// This is a new event, so we should verify, sign, and broadcast it
detail::addSignature(event, peer::getMyPublicKey(), signature::sign(event.transaction().hash(), peer::getMyPublicKey(), peer::getPrivateKey()).c_str());
detail::addSignature(event,
config::PeerServiceConfig::getInstance().getMyPublicKey(),
signature::sign(event.transaction().hash(),
config::PeerServiceConfig::getInstance().getMyPublicKey(),
config::PeerServiceConfig::getInstance().getPrivateKey()).c_str());

logger::info("sumeragi") << "tail public key is " << context->validatingPeers.at(context->proxyTailNdx)->getPublicKey();
logger::info("sumeragi") << "tail is " << context->proxyTailNdx;
logger::info("sumeragi") << "my public key is " << peer::getMyPublicKey();
logger::info("sumeragi") << "my public key is " << config::PeerServiceConfig::getInstance().getMyPublicKey();

if (context->validatingPeers.at(context->proxyTailNdx)->getPublicKey() == peer::getMyPublicKey()) {
if (context->validatingPeers.at(context->proxyTailNdx)->getPublicKey() == config::PeerServiceConfig::getInstance().getMyPublicKey()) {
logger::info("sumeragi") << "I will send event to " << context->validatingPeers.at(context->proxyTailNdx)->getIP();
connection::send(context->validatingPeers.at(context->proxyTailNdx)->getIP(), std::move(event)); // Think In Process
} else {
Expand Down
5 changes: 3 additions & 2 deletions core/infra/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
add_subdirectory(smart_contract)
#add_subdirectory(smart_contract)
add_subdirectory(crypto)
add_subdirectory(repository)
add_subdirectory(server)
add_subdirectory(connection)
add_subdirectory(service)
#add_subdirectory(service)
add_subdirectory(protobuf)
add_subdirectory(config)
19 changes: 19 additions & 0 deletions core/infra/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set(JSON_PATH "${PROJECT_SOURCE_DIR}/core/vendor/json")

include_directories(
${JSON_PATH}/src
)

ADD_LIBRARY(iroha_config_with_json STATIC
iroha_config_with_json.cpp
)

ADD_LIBRARY(peer_service_with_json STATIC
peer_service_with_json.cpp
)

target_link_libraries(
iroha_config_with_json
peer_service_with_json
logger
)
83 changes: 83 additions & 0 deletions core/infra/config/iroha_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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.
*/

#ifndef IROHA_CONFIG_H
#define IROHA_CONFIG_H

#include "../../util/use_optional.hpp"
#include "../../util/logger.hpp"
#include "../../../core/vendor/json/src/json.hpp"
#include <fstream> // ifstream, ofstream

using json = nlohmann::json;

namespace config {

class IConfig {
protected:
virtual optional<json> openConfig(const std::string &configName) {
if (_configData) { // already content loaded
return _configData;
}

const auto PathToIROHA_HOME = [](){
const auto p = getenv("IROHA_HOME");
return p == nullptr ? "" : std::string(p);
}();

if (PathToIROHA_HOME.empty()) {
logger::error("peer with json") << "You must set IROHA_HOME!";
exit(EXIT_FAILURE);
}

auto jsonStr = openJSONText(PathToIROHA_HOME + "/" + configName);

logger::info("peer with json") << "load json is " << jsonStr;

setConfigData(std::move(jsonStr));

return _configData;
}

virtual std::string openJSONText(const std::string& PathToJSONFile) {
std::ifstream ifs(PathToJSONFile);
if (ifs.fail()) {
logger::error("peer with json") << "Not found: " << PathToJSONFile;
exit(EXIT_FAILURE);
}

std::istreambuf_iterator<char> it(ifs);
return std::string(it, std::istreambuf_iterator<char>());
}

virtual void setConfigData(std::string&& jsonStr) {
try {
_configData = json::parse(std::move(jsonStr));
} catch(...) {
logger::error("peer with json") << "Bad json!!";
exit(EXIT_FAILURE);
}
}

public:
virtual std::string getConfigName() = 0;

protected:
optional<json> _configData;
};
}

#endif // IROHA_CONFIG_H
50 changes: 50 additions & 0 deletions core/infra/config/iroha_config_with_json.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
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 "iroha_config_with_json.hpp"

namespace config {
IrohaConfigManager::IrohaConfigManager() { }

std::string IrohaConfigManager::openJSONText(const std::string& PathToJSONFile) {
std::ifstream ifs(PathToJSONFile);
if (ifs.fail()) {
logger::warning("json config") << "Not found: " << PathToJSONFile;
logger::warning("json config") << "All configurable parameters will be set to default";
return "{}";
}

std::istreambuf_iterator<char> it(ifs);
return std::string(it, std::istreambuf_iterator<char>());
}

void IrohaConfigManager::setConfigData(std::string&& jsonStr) {
try {
_configData = json::parse(std::move(jsonStr));
} catch(...) {
logger::warning("json config") << "Bad json!!" << jsonStr;
}
}

IrohaConfigManager& IrohaConfigManager::getInstance() {
static IrohaConfigManager manager;
return manager;
}

std::string IrohaConfigManager::getConfigName() {
return "docker/build/config/config.json";
}
}
48 changes: 48 additions & 0 deletions core/infra/config/iroha_config_with_json.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
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.
*/

#ifndef IROHA_CONFIG_WITH_JSON_HPP
#define IROHA_CONFIG_WITH_JSON_HPP

#include "iroha_config.hpp"

namespace config {
class IrohaConfigManager: IConfig {
protected:
std::string openJSONText(const std::string& PathToJSONFile);
void setConfigData(std::string&& jsonStr);

private:
IrohaConfigManager();
IrohaConfigManager(const IrohaConfigManager&);
IrohaConfigManager& operator=(const IrohaConfigManager&);

public:
static IrohaConfigManager &getInstance();

template <typename T>
T getParam(const std::string &param, const T &defaultValue) {
if (auto config = openConfig(getConfigName())) {
return config->value(param, defaultValue);
}
return defaultValue;
}

virtual std::string getConfigName();
};
}

#endif // IROHA_CONFIG_WITH_JSON_HPP
65 changes: 65 additions & 0 deletions core/infra/config/peer_service_with_json.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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 "peer_service_with_json.hpp"

namespace config {
PeerServiceConfig::PeerServiceConfig() {}

PeerServiceConfig& PeerServiceConfig::getInstance() {
static PeerServiceConfig serviceConfig;
return serviceConfig;
}

std::string PeerServiceConfig::getMyPublicKey() {
if (auto config = openConfig(getConfigName())) {
return (*config)["me"]["publicKey"].get<std::string>();
}
return "";
}

std::string PeerServiceConfig::getPrivateKey() {
if (auto config = openConfig(getConfigName())) {
return (*config)["me"]["privateKey"].get<std::string>();
}
return "";
}

std::string PeerServiceConfig::getMyIp() {
if (auto config = openConfig(getConfigName())) {
return (*config)["me"]["ip"].get<std::string>();
}
return "";
}

std::vector<std::unique_ptr<peer::Node>> PeerServiceConfig::getPeerList() {
std::vector<std::unique_ptr<peer::Node>> nodes;
if (auto config = openConfig(getConfigName())) {
for (const auto& peer : (*config)["group"].get<std::vector<json>>()){
nodes.push_back(std::make_unique<peer::Node>(
peer["ip"].get<std::string>(),
peer["publicKey"].get<std::string>(),
1
));
}
}
return nodes;
}

std::string PeerServiceConfig::getConfigName() {
return "config/sumeragi.json";
}
};

0 comments on commit 944618f

Please sign in to comment.