Skip to content

Commit

Permalink
WIP fix syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
MizukiSonoko committed Sep 26, 2016
1 parent fb47ad2 commit 3c18903
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 68 deletions.
2 changes: 1 addition & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#add_subdirectory(consensus)
add_subdirectory(consensus)
add_subdirectory(model)
#add_subdirectory(repository)
add_subdirectory(infra)
Expand Down
3 changes: 2 additions & 1 deletion core/consensus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
set(CMAKE_BUILD_TYPE Debug)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

ADD_LIBRARY(sumeragi STATIC
sumeragi.cpp
)
target_link_libraries(sumeragi
crypto
txRepo
yaml_loader
)
2 changes: 1 addition & 1 deletion core/consensus/consensus_event.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "consensus_event.hpp"

#include "../domain/transactions/abstract_transaction.hpp"
#include "../model/transactions/abstract_transaction.hpp"

namespace consensus_event {
struct ConsensusEvent {
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/consensus_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string>
#include <functional>

#include "../domain/transactions/abstract_transaction.hpp"
#include "../model/transactions/abstract_transaction.hpp"

namespace consensus_event {

Expand Down
33 changes: 17 additions & 16 deletions core/consensus/sumeragi.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include "sumeragi.hpp"
#include <queue>
#include <map>

#include "../util/logger.hpp"
#include "../repository/merkle_transaction_repository.hpp"
#include "../domain/transactions/abstract_transaction.hpp"
#include "../connection/connection.hpp"
#include "../repository/consensus/merkle_transaction_repository.hpp"
#include "../model/transactions/abstract_transaction.hpp"
#include "../crypto/hash.hpp"
#include "../validation/transaction_validator.hpp"
#include "../service/peer_service.hpp"
#include "./connection/connection.hpp"

#include "../service/peer_service.hpp"

/**
* |ーーー| |ーーー| |ーーー| |ーーー|
* | ス |ー| メ |ー| ラ |ー| ギ |
Expand All @@ -22,48 +25,46 @@
*/
namespace sumeragi {

using ConsensusEvent = consensus_event::ConsensusEvent;

struct Context {
int maxFaulty; // f
int proxyTailNdx;
const unsigned char* myPublicKey;
int panicCount;
int numValidatingPeers;
std::vector<Node> validatingPeers;
std::unique_ptr<merkle_transaction_repository> txRepository;
std::vector<peer::Node> validatingPeers;
std::unique_ptr<TransactionCache> txCache;
std::unique_ptr<TransactionValidator> txValidator;
std::queue<ConsensusEvent> eventCache;

std::map<ConsensusEvent> processedCache;
connection conn;
std::map<ConsensusEvent> processedCache;
};

std::unique_ptr<Context> context;

void initializeSumeragi(std::string myPublicKey, std::vector<Node> peers) {
void initializeSumeragi(std::string myPublicKey, std::vector<peer::Node> peers) {
logger::info( __FILE__, "initializeSumeragi");
context->validatingPeers = peers;
context->numValidatingPeers = validatingPeers::size();
context->maxFaulty = context->numValidatingPeers / 3; // Default to approx. 1/3 of the network. TODO: make this configurable
context->proxyTailNdx = context->maxFault*2 + 1;
context->txRepository = std::make_unique<merkle_transaction_repository>();
context->proxyTailNdx = context->maxFaulty*2 + 1;
context->panicCount = 0;
context->conn = std::make_unique<connection>(); //TODO: is this syntax correct (connection is a namespace...)?

context->eventCache = std::make_uniquestd::queue<ConsensusEvent>>();
context->processedCache = std::make_unique<std::map<ConsensusEvent>>();

context->myPublicKey = myPublicKey;
}

void processTransaction(td::shared_ptr<ConsensusEvent> const event, std::vector<Node> const nodeOrder) {
void processTransaction(std::shared_ptr<ConsensusEvent> const event, std::vector<peer::Node> const nodeOrder) {
if (!txValidator::isValid(event)) {
return; //TODO-futurework: give bad trust rating to nodes that sent an invalid event
}

event::addSignature(sign(hash));
if (nodeOrder::get(context->proxyTailNdx)->publicKey == context->myPublicKey) {
context->conn::send(nodeOrder::get(proxyTail)::getIP(), event);
connection::send(nodeOrder::get(proxyTail)::getIP(), event);
} else {
context->conn::sendAll(event);
}
Expand Down Expand Up @@ -115,7 +116,7 @@ void setAwkTimer(int const sleepMillisecs, std::function<void(void)> const actio
}).detach();
}

std::vector<Node> determineConsensusOrder(std::shared_ptr<ConsensusEvent> const event/*, std::vector<double> trustVector*/) {
std::vector<peer::Node> determineConsensusOrder(std::shared_ptr<ConsensusEvent> const event/*, std::vector<double> trustVector*/) {
unsigned char* const txHash = event::getHash();
std::vector<std::tuple> distances = std::make_shared<std::vector<std::tuple>>();

Expand All @@ -126,7 +127,7 @@ std::vector<Node> determineConsensusOrder(std::shared_ptr<ConsensusEvent> const
distances[ndx] = std::make_tuple(node->publicKey, distance);
}

std::vector<Node> const nodeOrder = std::sort(distances.begin(), distances.end(), COMPARATOR(l::get<1> < r::get<1>));
std::vector<peer::Node> const nodeOrder = std::sort(distances.begin(), distances.end(), COMPARATOR(l::get<1> < r::get<1>));

return nodeOrder;
}
Expand All @@ -140,7 +141,7 @@ void loop() {
continue;
}
// Determine node order
std::vector<Node> const nodeOrder = determineConsensusOrder(event);
std::vector<peer::Node> const nodeOrder = determineConsensusOrder(event);

// Process transaction
processTransaction(event, nodeOrder);
Expand Down
11 changes: 7 additions & 4 deletions core/consensus/sumeragi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

#include "consensus_event.hpp"

#include "../service/peer_service.hpp"


namespace sumeragi {
void initializeSumeragi(std::string myPublicKey, std::vector<Node> peers);
void initializeSumeragi(std::string myPublicKey, std::vector<peer::Node> peers);
void loop();
void processTransaction(const std::shared_ptr<ConsensusEvent::ConsensusEvent> event, const std::vector<Node> nodeOrder);
void panic(const std::shared_ptr<ConsensusEvent::ConsensusEvent> event);
void processTransaction(const std::shared_ptr<consensus_event::ConsensusEvent> event, const std::vector<peer::Node> nodeOrder);
void panic(const std::shared_ptr<consensus_event::ConsensusEvent> event);
void setAwkTimer(const int sleepMillisecs, const std::function<void(void)> action);
std::vector<Node> determineConsensusOrder(const std::shared_ptr<ConsensusEvent::ConsensusEvent> event);
std::vector<peer::Node> determineConsensusOrder(const std::shared_ptr<consensus_event::ConsensusEvent> event);
}; // namespace sumeragi

#endif // CORE_CONSENSUS_SUMERAGI_HPP_
6 changes: 3 additions & 3 deletions core/repository/consensus/merkle_transaction_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

#include <string>
#include <memory>
#include "../domain/transactions/abstract_transaction.hpp"
#include "../../model/transactions/abstract_transaction.hpp"

#include "../consensus/consensus_event.hpp"
#include "../../consensus/consensus_event.hpp"

namespace merkle_transaction_repository {
bool commit(std::string const hash, ConsensusEvent::ConsensusEvent const tx);
bool commit(std::string const hash, consensus_event::ConsensusEvent const tx);
std::unique_ptr<abstract_transaction::AbstractTransaction> findLeaf(std::string const hash);
}; // namespace merkle_transaction_repository

Expand Down
42 changes: 31 additions & 11 deletions core/service/peer_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,47 @@

#include "../util/yaml_loader.hpp"

namespace service {

namespace peer {

class Node {
namespace peer{
class Node {
std::string ip;
std::string publicKey;
public:

Node(
std::string aip,
std::string apubkey
):
ip(aip),
publicKey(apubkey)
{}

/*
virtual ~Node() = default; // make dtor virtual
Node(Node&&) = default; // support moving
Node& operator = (Node&&) = default;
Node(const Node&) = default; // support copying
Node& operator = (const Node&) = default;

virtual std::string getIP() = 0;
virtual std::string getPublicKey() = 0;
};
*/

std::string getIP() {
return ip;
}

std::string getPublicKey() {
return publicKey;
}
};


std::vector<Node> getPeerList() {
std::unique_ptr<yaml::YamlLoader> yamlLoader(new yaml::YamlLoader(std::string(getenv("IROHA_HOME")) + "/config/config.yml"));
return std::move(yamlLoader->get<std::vector<std::string> >("peer", "ip"));
}
std::vector<Node> getPeerList() {
std::unique_ptr<yaml::YamlLoader> yamlLoader(new yaml::YamlLoader(std::string(getenv("IROHA_HOME")) + "/config/config.yml"));
return std::vector<Node>();
//return std::move(yamlLoader->get<std::vector<std::string> >("peer", "ip"));
}
}
};

#endif
6 changes: 6 additions & 0 deletions core/util/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SET(YAML_PATH "${PROJECT_SOURCE_DIR}/core/vendor/yaml-cpp")

# Datetime
ADD_LIBRARY(datetime STATIC datetime.cpp)
Expand All @@ -11,3 +12,8 @@ target_link_libraries(logger
# Datetime
ADD_LIBRARY(exception STATIC exception.cpp)

# Yaml_loader
include_directories(
${YAML_PATH}/include
)
ADD_LIBRARY(yaml_loader STATIC yaml_loader.cpp)
44 changes: 44 additions & 0 deletions core/util/yaml_loader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <yaml-cpp/yaml.h>
#include <string>

#include "logger.hpp"
#include "terminate.hpp"
#include "yaml_loader.hpp"
#include "../service/peer_service.hpp"

// Umm..., This is really util? not service?
// ToDo future work
// make yaml_service and separate yaml-cpp library from yaml service.
namespace yaml{


YamlLoader::YamlLoader(std::string afileName) :
fileName(fileName)
{}


template<typename T>
T YamlLoader::get(const std::string &root,const std::string &key) {
YAML::Node config = YAML::LoadFile(std::move(fileName));
try{
return config[root][key].as<T>();
}catch(YAML::Exception& e){
logger::fital("YamlLoader.get()", e.what());
terminate::finish();
}
}
/*
template <>
std::string YamlLoader::get<
std::string
>(std::string root, std::string key);
template std::vector<peer::Node> YamlLoader::get<
std::vector<peer::Node>
>(std::string root, std::string key);
template std::vector<std::string> YamlLoader::get<
std::vector<std::string>
>(std::string root, std::string key);
*/
};
18 changes: 3 additions & 15 deletions core/util/yaml_loader.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef __YAML_LOADER_HPP_
#define __YAML_LOADER_HPP_

#include <yaml-cpp/yaml.h>
#include <string>

#include "logger.hpp"
Expand All @@ -13,24 +12,13 @@
namespace yaml{

class YamlLoader{
std::string fileName;
public:

explicit YamlLoader(std::string fileName) :
config(YAML::LoadFile(std::move(fileName)))
{}
YamlLoader(std::string fileName);

template<typename T>
T get(const std::string &root,const std::string &key) {
try{
return config[root][key].as<T>();
}catch(YAML::Exception& e){
logger::fital("YamlLoader.get()", e.what());
terminate::finish();
}
}

private:
YAML::Node config;
T get(const std::string &root,const std::string &key);

YamlLoader(YamlLoader const&) = delete;
YamlLoader(YamlLoader&&) = delete;
Expand Down
8 changes: 4 additions & 4 deletions core/validation/transaction_validator.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "transaction_validator.hpp"

#include "../domain/abstract_transaction.hpp"
#include "../model/abstract_transaction.hpp"
#include "../crypto/signature.hpp"

namespace transaction_validator {
bool isValid(AbstractTransaction const tx) {
bool isValid(abstract_transaction::AbstractTransaction& tx) {
return signaturesAreValid(tx) && validForType(tx);
}

bool signaturesAreValid(AbstractTransaction const tx) {
bool signaturesAreValid(abstract_transaction::AbstractTransaction& tx) {
bool areAllValid = true;
for (Signature const sig in tx->signatures) {
if (!signature::verify(sig, tx::getRawData(), tx::getPublicKey) {
Expand All @@ -18,7 +18,7 @@ bool signaturesAreValid(AbstractTransaction const tx) {
return areAllValid; // TODO: change this!
}

bool validForType(AbstractTransaction const tx) {
bool validForType(abstract_transaction::AbstractTransaction& tx) {
if (abstract_transaction::TransactionType::transfer == tx::getType) {
TransferTransaction transferTx = static_cast<TransferTransaction>(tx);
getBalance();
Expand Down
8 changes: 5 additions & 3 deletions core/validation/transaction_validator.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef CORE_VALIDATION_TRANSACTIONVALIDATOR_HPP_
#define CORE_VALIDATION_TRANSACTIONVALIDATOR_HPP_

#include "../model/transactions/abstract_transaction.hpp"

namespace transaction_validator {
bool isValid(AbstractTransaction const tx);
bool signaturesAreValid(AbstractTransaction const tx);
bool validForType(AbstractTransaction const tx);
bool isValid(abstract_transaction::AbstractTransaction& tx);
bool signaturesAreValid(abstract_transaction::AbstractTransaction& tx);
bool validForType(abstract_transaction::AbstractTransaction& tx);
}; // namespace transaction_validator

#endif // CORE_VALIDATION_TRANSACTIONVALIDATOR_HPP_
Loading

0 comments on commit 3c18903

Please sign in to comment.