Skip to content

Commit

Permalink
Merge pull request #212 from hyperledger/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
takemiyamakoto committed Mar 11, 2017
2 parents 5b17833 + f9fbfbb commit 77d2f09
Show file tree
Hide file tree
Showing 44 changed files with 1,525 additions and 557 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ build/*
*.gch
*.a
*.o
*.pb.cc
*.pb.h
.gradle/*
.vscode/*
.idea/*
Expand Down
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ add_subdirectory(repository)
add_subdirectory(infra)
add_subdirectory(util)
add_subdirectory(crypto)
add_subdirectory(service)
add_subdirectory(validation)
add_subdirectory(transaction_builder)
1 change: 1 addition & 0 deletions core/consensus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ target_link_libraries(sumeragi
thread_pool
executor
merkle_transaction_repository
transaction_repository
validator
)
43 changes: 42 additions & 1 deletion core/consensus/connection/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace connection {
using Api::ConsensusEvent;
using Api::Transaction;
using Api::Query;
using Api::TransactionResponse;


struct Config {
Expand Down Expand Up @@ -67,17 +68,39 @@ namespace connection {
> &callback);

};
// This only reply pong.
namespace Kagami{}
};

namespace PeerService {

namespace Torii {
namespace Sumeragi {

bool send(
const std::string &ip,
const Transaction &transaction
);

bool ping(
const std::string &ip
);

}

namespace Izanami {
bool send(
const std::string& ip,
const TransactionResponse &txResponse
);
}
}

namespace Izanami {
namespace Izanagi {
bool receive(const std::function<void(
const std::string &,
TransactionResponse&)
> &callback);
}
}

Expand All @@ -92,6 +115,24 @@ namespace connection {

};

namespace fetch {

bool receive(const std::function<void(
const std::string &,
const Query &)
> &callback);

};

namespace fetchStream {

bool receive(const std::function<void(
const std::string &,
const Query &)
> &callback);

};

}

namespace AssetRepository {
Expand Down
15 changes: 6 additions & 9 deletions core/consensus/sumeragi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ limitations under the License.

#include <validation/transaction_validator.hpp>
#include <service/peer_service.hpp>
#include <repository/transaction_repository.hpp>
#include <infra/config/peer_service_with_json.hpp>
#include "connection/connection.hpp"
#include <consensus/connection/connection.hpp>

#include <service/executor.hpp>
#include <infra/config/peer_service_with_json.hpp>
#include <infra/config/iroha_config_with_json.hpp>

#include "connection/connection.hpp"

/**
* |ーーー| |ーーー| |ーーー| |ーーー|
* | ス |ー| メ |ー| ラ |ー| ギ |
Expand Down Expand Up @@ -182,8 +181,7 @@ namespace sumeragi {

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

if (this->validatingPeers.empty()) {
Expand Down Expand Up @@ -224,7 +222,7 @@ namespace sumeragi {
context = std::make_unique<Context>();

connection::iroha::Sumeragi::Torii::receive([](const std::string& from, Transaction& transaction) {
logger::info("sumeragi") << "receive!";
logger::info("sumeragi") << "receive! Torii";
ConsensusEvent event;
event.set_status("uncommit");
event.mutable_transaction()->CopyFrom(transaction);
Expand All @@ -239,12 +237,12 @@ namespace sumeragi {
connection::iroha::Sumeragi::Verify::receive([](const std::string& from, ConsensusEvent& event) {
logger::info("sumeragi") << "receive!";
logger::info("sumeragi") << "received message! sig:[" << event.eventsignatures_size() << "]";

logger::info("sumeragi") << "received message! status:[" << event.status() << "]";
if(event.status() == "commited") {
if(txCache.find(detail::hash(event.transaction())) == txCache.end()) {
executor::execute(event.transaction());
txCache[detail::hash(event.transaction())] = "commited";
repository::transaction::add(detail::hash(event.transaction()), event.transaction());
executor::execute(event.transaction());
}
}else{
// send processTransaction(event) as a task to processing pool
Expand Down Expand Up @@ -275,7 +273,6 @@ namespace sumeragi {
//return merkle_transaction_repository::getLastLeafOrder() + 1;
}


void processTransaction(ConsensusEvent& event) {

logger::info("sumeragi") << "processTransaction";
Expand Down
57 changes: 34 additions & 23 deletions core/infra/config/abstract_config_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,61 +19,72 @@ limitations under the License.

#include <fstream> // ifstream, ofstream
#include <util/logger.hpp>
#include <util/use_optional.hpp>
#include <util/exception.hpp>
#include <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;
private:
std::string readConfigData(const std::string& pathToJSONFile, const std::string& defaultValue) {
std::ifstream ifs(pathToJSONFile);
if (ifs.fail()) {
return defaultValue;
}

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

json openConfigData() {

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;
auto jsonStr = readConfigData(configFolderPath + this->getConfigName(), "");

parseConfigDataFromString(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;
if (jsonStr.empty()) {
logger::warning("config") << "there is no config '" << getConfigName() << "', we will use default values.";
} else {
logger::debug("config") << "load json is " << jsonStr;
parseConfigDataFromString(std::move(jsonStr));
}

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

protected:
virtual void parseConfigDataFromString(std::string&& jsonStr) {
try {
_configData = json::parse(std::move(jsonStr));
} catch (...) {
logger::error("config") << "Can't parse json: " << getConfigName();
throw exception::config::ConfigException("Can't parse json: " + getConfigName());
}
}

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

json getConfigData() {
if (_loaded) {
// If defaultValue is used, _configData is empty, but _loaded = true. It's cofusing. Any good solution?
return this->_configData;
} else {
_loaded = true;
return openConfigData();
}
}

protected:
optional<json> _configData;
bool _loaded = false;
json _configData;

};
}

Expand Down
9 changes: 2 additions & 7 deletions core/infra/config/iroha_config_with_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,12 @@ IrohaConfigManager& IrohaConfigManager::getInstance() {
template <typename T>
T IrohaConfigManager::getParam(const std::string& param,
const T& defaultValue) {
if (auto config = openConfig(getConfigName())) {
return config->value(param, defaultValue);
}
return defaultValue;
return getConfigData().value(param, defaultValue);
}

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

std::string IrohaConfigManager::getDatabasePath(
const std::string& defaultValue
) {
std::string IrohaConfigManager::getDatabasePath(const std::string& defaultValue) {
return this->getParam<std::string>("database_path", defaultValue);
}

Expand Down

0 comments on commit 77d2f09

Please sign in to comment.