Skip to content

Commit

Permalink
Refactoring (#148) (#151)
Browse files Browse the repository at this point in the history
* Refactoring
- changed include style from `"../../something/header.hpp"` to relative to `core`: `#incude <model/object/transaction.hpp`
- fixed few bugs related to config.json
- removed empty files
- improved circle.yml: now after docker run hyperledger/iroha-docker it shows its output

	modified:   circle.yml
	new file:   config/config.json
	modified:   core/CMakeLists.txt
	modified:   core/consensus/CMakeLists.txt
	new file:   core/consensus/connection/CMakeLists.txt
	modified:   core/consensus/connection/connection.hpp
	modified:   core/consensus/consensus_event.hpp
	modified:   core/consensus/sumeragi.cpp
	modified:   core/consensus/sumeragi.hpp
	modified:   core/infra/config/CMakeLists.txt
	new file:   core/infra/config/abstract_config_manager.hpp
	deleted:    core/infra/config/iroha_config.hpp
	modified:   core/infra/config/iroha_config_with_json.cpp
	modified:   core/infra/config/iroha_config_with_json.hpp
	modified:   core/infra/config/peer_service_with_json.cpp
	modified:   core/infra/config/peer_service_with_json.hpp
	modified:   core/infra/connection/CMakeLists.txt
	modified:   core/infra/connection/connection_with_grpc.cpp
	modified:   core/infra/protobuf/CMakeLists.txt
	modified:   core/infra/repository/CMakeLists.txt
	modified:   core/infra/repository/world_state_repository_with_level_db.cpp
	modified:   core/infra/server/CMakeLists.txt
	modified:   core/infra/server/http_server_with_cappuccino.cpp
	modified:   core/infra/smart_contract/smart_contract_with_java.cpp
	modified:   core/model/CMakeLists.txt
	modified:   core/model/commands/CMakeLists.txt
	modified:   core/model/commands/add.cpp
	modified:   core/model/commands/add.hpp
	modified:   core/model/commands/command.hpp
	modified:   core/model/commands/transfer.cpp
	modified:   core/model/commands/transfer.hpp
	modified:   core/model/commands/update.hpp
	modified:   core/model/objects/CMakeLists.txt
	modified:   core/model/objects/object.hpp
	deleted:    core/model/transaction.cpp
	modified:   core/model/transaction.hpp
	modified:   core/repository/consensus/CMakeLists.txt
	modified:   core/repository/consensus/transaction_repository.cpp
	modified:   core/repository/consensus/transaction_repository.hpp
	modified:   core/repository/domain/CMakeLists.txt
	modified:   core/repository/domain/account_repository.hpp
	modified:   core/repository/domain/domain_sample_asset_repository.hpp
	modified:   core/repository/domain/instance/CMakeLists.txt
	modified:   core/repository/domain/instance/account_repository.cpp
	modified:   core/repository/domain/instance/asset_repository.cpp
	modified:   core/repository/domain/instance/asset_repository.hpp
	modified:   core/validation/CMakeLists.txt
	modified:   core/validation/consensus_event_validator.cpp
	modified:   core/validation/consensus_event_validator.hpp
	modified:   core/validation/transaction_validator.hpp
	modified:   docker/build/config/config.json
	modified:   peer/CMakeLists.txt
	modified:   peer/main.cpp
	modified:   test/connection/CMakeLists.txt
	modified:   test/connection/connection_with_grpc_test.cpp
	modified:   test/consensus/CMakeLists.txt
	modified:   test/consensus/sumeragi_test.cpp
	modified:   test/consensus/transaction_test.cpp

* Fixed pro->priv

* Fix typo in log message on JSON config parsing

* Create typed accessors for "config.json" parameters

* Fix initializer_list typos, replace rvalue-ref to const lvalue-ref

* Refactoring

* Fixed dependency hell
Probably you've got an error after make -j4? This commit fixes this.

* Fixed bug in build.sh
Now build.sh stops after errors at some step.
  • Loading branch information
Warchant committed Feb 3, 2017
1 parent a9d9425 commit 9bc0500
Show file tree
Hide file tree
Showing 73 changed files with 508 additions and 474 deletions.
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ SET(CMAKE_SHARED_LINKER_FLAGS "-lpthread -lssl")

add_subdirectory(core)
add_subdirectory(peer)

add_subdirectory(tools)

# at some point we want to include pool as following: #include <worker.hpp>
# which resides in that folder
include_directories("${PROJECT_SOURCE_DIR}/core/vendor/thread_pool_cpp/thread_pool")

if(NOT without-test)
enable_testing()
include(test/gtest.cmake)
Expand Down
5 changes: 3 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ test:
# TESTS:
# first, run ctest inside a container
- docker run -p 1204:1204 hyperledger/iroha-docker /test.sh
# then, run iroha with dummy config (1 node) and test curl request
- docker run -d -p 1204:1204 hyperledger/iroha-docker /run.sh && sleep 5
# then, run iroha with dummy config (1 node) and print its output to stdout
- docker logs $(docker run -it -d -p 1204:1204 hyperledger/iroha-docker /run.sh)
# put new tests below
- curl -X POST http://127.0.0.1:1204/account/register -d '{"publicKey":"WdvM/DPabapmtA7ISbTYPywbHxk8gWu2221LzmcmAgw=","alias":"yonezu","timestamp":1482053586}'


Expand Down
6 changes: 6 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"database_path":"/tmp/iroha_ledger",
"concurrency": 0,
"max_faulty_peers" : 1,
"pool_worker_queue_size": 1024
}
2 changes: 1 addition & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include_directories(".")

add_subdirectory(consensus)
add_subdirectory(model)
Expand All @@ -6,5 +7,4 @@ add_subdirectory(infra)
add_subdirectory(util)
add_subdirectory(vendor)
add_subdirectory(crypto)

add_subdirectory(validation)
17 changes: 9 additions & 8 deletions core/consensus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

ADD_LIBRARY(sumeragi STATIC
sumeragi.cpp
)

include_directories(
"${PROJECT_SOURCE_DIR}/core"
"${PROJECT_SOURCE_DIR}/core/vendor/thread_pool_cpp/thread_pool"
)

target_link_libraries(sumeragi
peer_service_with_json
iroha_config_with_json
ADD_LIBRARY(sumeragi STATIC
sumeragi.cpp
)

target_link_libraries(
sumeragi
config_manager
connection_with_grpc
signature
event_repository
Expand All @@ -23,9 +24,9 @@ ADD_LIBRARY(consensus_event STATIC
)

target_link_libraries(consensus_event
config_manager
commands
objects
transaction
validator
hash
)
3 changes: 3 additions & 0 deletions core/consensus/connection/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(
"${PROJECT_SOURCE_DIR}/core"
)
2 changes: 1 addition & 1 deletion core/consensus/connection/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ limitations under the License.
#include <memory>
#include <functional>

#include "../../infra/protobuf/event.grpc.pb.h"
#include <infra/protobuf/event.grpc.pb.h>
#include "../consensus_event.hpp"

namespace connection {
Expand Down
19 changes: 10 additions & 9 deletions core/consensus/consensus_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ limitations under the License.
#include <unordered_map>
#include <algorithm>

#include "../crypto/signature.hpp"
#include "../util/logger.hpp"
#include "../model/transaction.hpp"
#include <crypto/signature.hpp>
#include <util/logger.hpp>

#include "../model/commands/add.hpp"
#include "../model/commands/transfer.hpp"
#include "../model/commands/update.hpp"
#include <model/transaction.hpp>

#include "../model/objects/account.hpp"
#include "../model/objects/asset.hpp"
#include "../model/objects/domain.hpp"
#include <model/commands/add.hpp>
#include <model/commands/transfer.hpp>
#include <model/commands/update.hpp>

#include <model/objects/account.hpp>
#include <model/objects/asset.hpp>
#include <model/objects/domain.hpp>


namespace event {
Expand Down
63 changes: 28 additions & 35 deletions core/consensus/sumeragi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ limitations under the License.

#include <thread_pool.hpp>

#include "../util/logger.hpp"
#include "../repository/consensus/merkle_transaction_repository.hpp"
#include "../crypto/hash.hpp"
#include "../crypto/signature.hpp"
#include <util/logger.hpp>
#include <repository/consensus/merkle_transaction_repository.hpp>
#include <crypto/hash.hpp>
#include <crypto/signature.hpp>

#include "../infra/protobuf/convertor.hpp"
#include <infra/protobuf/convertor.hpp>

#include "../validation/transaction_validator.hpp"
#include "../service/peer_service.hpp"
#include "./connection/connection.hpp"
#include "../model/objects/asset.hpp"
#include "../model/objects/domain.hpp"
#include "../model/commands/transfer.hpp"
#include <validation/transaction_validator.hpp>
#include <service/peer_service.hpp>
#include "connection/connection.hpp"
#include <model/objects/asset.hpp>
#include <model/objects/domain.hpp>
#include <model/commands/transfer.hpp>

#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"
#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 @@ -60,22 +60,16 @@ namespace sumeragi {
using namespace object;


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{
.threads_count = concurrency,
.worker_queue_size = 1024
.threads_count = config::IrohaConfigManager::getInstance()
.getConcurrency(0),
.worker_queue_size = config::IrohaConfigManager::getInstance()
.getPoolWorkerQueueSize(1024),
}
);


namespace detail {

std::uint32_t getNumValidSignatures(const Event::ConsensusEvent& event) {
Expand Down Expand Up @@ -204,10 +198,9 @@ namespace sumeragi {
logger::info("sumeragi") << "set number of validatingPeer";

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

// maxFaulty = Default to approx. 1/3 of the network.
context->maxFaulty = config::IrohaConfigManager::getInstance()
.getMaxFaultyPeers(context->numValidatingPeers / 3);
context->proxyTailNdx = context->maxFaulty * 2 + 1;

if (context->validatingPeers.empty()) {
Expand Down Expand Up @@ -266,12 +259,12 @@ namespace sumeragi {
logger::info("sumeragi") << "Add my signature...";

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

//detail::printIsSumeragi(context->isSumeragi);
Expand All @@ -280,7 +273,7 @@ namespace sumeragi {
config::PeerServiceConfig::getInstance().getMyPublicKey(),
signature::sign(event.transaction().hash(),
config::PeerServiceConfig::getInstance().getMyPublicKey(),
config::PeerServiceConfig::getInstance().getPrivateKey())
config::PeerServiceConfig::getInstance().getMyPrivateKey())
);

if (detail::eventSignatureIsEmpty(event) && context->isSumeragi) {
Expand Down Expand Up @@ -367,7 +360,7 @@ namespace sumeragi {
config::PeerServiceConfig::getInstance().getMyPublicKey(),
signature::sign(event.transaction().hash(),
config::PeerServiceConfig::getInstance().getMyPublicKey(),
config::PeerServiceConfig::getInstance().getPrivateKey()).c_str());
config::PeerServiceConfig::getInstance().getMyPrivateKey()).c_str());

logger::info("sumeragi") << "tail public key is " << context->validatingPeers.at(context->proxyTailNdx)->getPublicKey();
logger::info("sumeragi") << "tail is " << context->proxyTailNdx;
Expand Down
16 changes: 8 additions & 8 deletions core/consensus/sumeragi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ limitations under the License.

#include "consensus_event.hpp"

#include "../service/peer_service.hpp"
#include "../infra/protobuf/event.grpc.pb.h"
#include <service/peer_service.hpp>
#include <infra/protobuf/event.grpc.pb.h>

#include "../model/commands/add.hpp"
#include "../model/commands/transfer.hpp"
#include "../model/commands/update.hpp"
#include <model/commands/add.hpp>
#include <model/commands/transfer.hpp>
#include <model/commands/update.hpp>

#include "../model/objects/account.hpp"
#include "../model/objects/asset.hpp"
#include "../model/objects/domain.hpp"
#include <model/objects/account.hpp>
#include <model/objects/asset.hpp>
#include <model/objects/domain.hpp>

namespace sumeragi {

Expand Down
20 changes: 5 additions & 15 deletions core/infra/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
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
${PROJECT_SOURCE_DIR}/core/vendor/json/src
${PROJECT_SOURCE_DIR}/core
)

target_link_libraries(
iroha_config_with_json
peer_service_with_json
logger
add_library(config_manager STATIC
peer_service_with_json.cpp
iroha_config_with_json.cpp
)
80 changes: 80 additions & 0 deletions core/infra/config/abstract_config_manager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
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 <fstream> // ifstream, ofstream
#include <util/logger.hpp>
#include <util/use_optional.hpp>
#include <vendor/json/src/json.hpp>

namespace config {

using json = nlohmann::json;

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

auto iroha_home = getenv("IROHA_HOME");
if (iroha_home == nullptr) {
logger::error("config") << "Set environment variable IROHA_HOME";
exit(EXIT_FAILURE);
}

auto configFolderPath = std::string(iroha_home) + "/";
auto jsonStr = readConfigData(configFolderPath + configName);

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

setConfigData(std::move(jsonStr));

return _configData;
}

std::string readConfigData(const std::string& pathToJSONFile) {
std::ifstream ifs(pathToJSONFile);
if (ifs.fail()) {
logger::error("config") << "Not found: " << pathToJSONFile;
return nullptr;
}

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

void setConfigData(std::string&& jsonStr) {
try {
_configData = json::parse(std::move(jsonStr));
} catch (...) {
logger::error("config") << "Can't parse json: " << getConfigName();
}
}

public:
virtual std::string getConfigName() = 0;
optional<json> getConfigData() { return this->_configData; }

protected:
optional<json> _configData;
};
}

#endif // IROHA_CONFIG_H

0 comments on commit 9bc0500

Please sign in to comment.