Skip to content

Commit

Permalink
WIP v3 FIX syntax error 🙇 🙇
Browse files Browse the repository at this point in the history
  • Loading branch information
MizukiSonoko committed Sep 26, 2016
1 parent 2cc03a9 commit 1bd28d1
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 51 deletions.
3 changes: 2 additions & 1 deletion core/consensus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ADD_LIBRARY(sumeragi STATIC
)
target_link_libraries(sumeragi
crypto
txRepo
yaml_loader
unconfirmed_transaction_repository
merkle_transaction_repository
)
2 changes: 1 addition & 1 deletion core/consensus/consensus_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct ConsensusEvent {
txSignatures::push_back(signature);
}

std::string getHash() {
std::string getHash() const{
return self->tx::getHash();
}
};
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 @@ -15,7 +15,7 @@ struct ConsensusEvent {
std::vector<std::string> merkleRootSignatures;

void addSignature(std::string const signature);
std::string getHash();
std::string getHash() const;
};
}; // namespace ConsensusEvent

Expand Down
37 changes: 20 additions & 17 deletions core/consensus/sumeragi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void processTransaction(std::shared_ptr<ConsensusEvent> const event, std::vector
if (
context->processedCache.find(event->getHash()) !=
context->processedCache.end()
) { panic(event->getHash()); } });
) { panic(event); } });

context->processedCache[event->getHash()] = event;
}
Expand Down Expand Up @@ -117,7 +117,7 @@ void panic(std::shared_ptr<ConsensusEvent> const event) {
}

void setAwkTimer(int const sleepMillisecs, std::function<void(void)> const action) {
std::thread([action, sleepMillisecs, tx]() {
std::thread([action, sleepMillisecs]() {
std::this_thread::sleep_for(std::chrono::milliseconds(sleepMillisecs));
action();
}).detach();
Expand All @@ -129,10 +129,10 @@ long long int getDistance(std::string publicKey, std::string txHash){
return 0;
}

std::vector<peer::Node> determineConsensusOrder(std::shared_ptr<ConsensusEvent> const event/*, std::vector<double> trustVector*/) {
std::string txHash = event->getHash();
std::vector<peer::Node> determineConsensusOrder(ConsensusEvent const event/*, std::vector<double> trustVector*/) {
std::string txHash = event.getHash();
std::vector<std::tuple<
std::string, long long int
peer::Node, long long int
> > distances;

for (int ndx = 0; ndx < context->numValidatingPeers; ++ndx) {
Expand All @@ -141,31 +141,34 @@ std::vector<peer::Node> determineConsensusOrder(std::shared_ptr<ConsensusEvent>
long long int distance = getDistance(node.getPublicKey(), txHash);/* + trustVector[ndx]*/;

distances.push_back(std::tuple<
std::string, long long int
>(node.getPublicKey(), distance));
peer::Node, long long int
>(node, distance));
}

std::sort(distances.begin(), distances.end(),
[](
std::tuple<
std::string, long long int
peer::Node, long long int
> const &lhs,
std::tuple<
std::string, long long int
peer::Node, long long int
> const &rhs) {
return std::get<1>(lhs) < std::get<1>(lhs);
}
);

return distances;
std::vector<peer::Node> res;
for(const auto t : distances){
res.push_back(std::get<0>(t));
}
return res;
}

void loop() {
logger::info("sumeragi","start loop");
while (true) { // TODO(M->M): replace with callback linking aeron
if (context->eventCache.empty()) { //TODO: mutex here?
ConsensusEvent const event = context->eventCache.front();
if (!transaction_validator::isValid(*event.tx)) {
const std::shared_ptr<ConsensusEvent> event = std::shared_ptr<ConsensusEvent>(&context->eventCache.front());
if (!transaction_validator::isValid(*event->tx)) {
continue;
}
// Determine node order
Expand All @@ -175,18 +178,18 @@ void loop() {
processTransaction(event, nodeOrder);
}

for (auto const &key : context->processedCache) {
auto event = context->processedCache[&key];
for (auto const &kv : context->processedCache) {
auto event = kv.second;

// Check if we have at least 2f+1 signatures
if (event->signatures.size() > context->maxFaulty*2 + 1) {
if (event->txSignatures.size() > context->maxFaulty*2 + 1) {
// Check Merkle roots to see if match for new state
//TODO: std::vector<std::string>>const merkleSignatures = event.merkleRootSignatures;
//TODO: try applying transaction locally and compute the merkle root
//TODO: see if the merkle root matches or not

// Commit locally
merkle_transaction_repository::commit(event->gethash(), event); //TODO: add error handling in case not saved
merkle_transaction_repository::commit(event->getHash(), event); //TODO: add error handling in case not saved
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/infra/repository/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(domain)
add_subdirectory(consensus)
31 changes: 31 additions & 0 deletions core/infra/repository/consensus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)


SET(MESSAGE_PACK_PATH ${PROJECT_SOURCE_DIR}/core/vendor/msgpack-c)
SET(LEVELDB_PATH ${PROJECT_SOURCE_DIR}/core/vendor/leveldb)

include_directories(
${LEVELDB_PATH}/include
${MESSAGE_PACK_PATH}/include
)

link_directories(
${MESSAGE_PACK_PATH}
${LEVELDB_PATH}/out-static
)

ADD_LIBRARY(merkle_transaction_repository STATIC merkle_transaction_repository.cpp)
target_link_libraries(merkle_transaction_repository
msgpackc
snappy
leveldb
exception
)

ADD_LIBRARY(unconfirmed_transaction_repository STATIC unconfirmed_transaction_repository.cpp)
target_link_libraries(unconfirmed_transaction_repository
msgpackc
snappy
leveldb
exception
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "merkle_transaction_repository.hpp"
#include "../../../repository/consensus/merkle_transaction_repository.hpp"

#include <leveldb/db.h>
#include <leveldb/write_batch.h>
Expand All @@ -8,8 +8,8 @@
#include <iostream>

#include <msgpack.hpp>
#include "../util/logger.hpp"
#include "../crypto/merkle_node.hpp"
#include "../../../util/logger.hpp"
#include "../../../crypto/merkle_node.hpp"

namespace merkle_transaction_repository {

Expand Down Expand Up @@ -48,7 +48,7 @@ void loadDb() {
db.reset(tmpDb);
}

bool commit(std::unique_ptr<ConsensusEvent::ConsensusEvent> event) {
bool commit(std::shared_ptr<ConsensusEvent::ConsensusEvent> event) {
if (nullptr == db) {
loadDb();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "../../consensus/consensus_event.hpp"

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

Expand Down
31 changes: 31 additions & 0 deletions core/service/peer_service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <vector>
#include <string>

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

namespace peer{

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

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


std::string getMyPublicKey() {
return "Base64";// WIP
}

std::string getPrivateKey() {
return "Base64";// WIP
}

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"));
}
};
28 changes: 6 additions & 22 deletions core/service/peer_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ namespace peer{
Node(
std::string aip,
std::string apubkey
):
ip(aip),
publicKey(apubkey)
{}
);

/*
virtual ~Node() = default; // make dtor virtual
Expand All @@ -32,29 +29,16 @@ namespace peer{
virtual std::string getPublicKey() = 0;
*/

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

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


std::string getMyPublicKey() {
return "Base64";// WIP
}
std::string getMyPublicKey();

std::string getPrivateKey() {
return "Base64";// WIP
}
std::string getPrivateKey();

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"));
}
std::vector<Node> getPeerList();
};

#endif
3 changes: 3 additions & 0 deletions core/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ target_link_libraries(logger
# Datetime
ADD_LIBRARY(exception STATIC exception.cpp)

# Datetime
ADD_LIBRARY(terminate STATIC terminate.cpp)

# Yaml_loader
include_directories(
${YAML_PATH}/include
Expand Down
10 changes: 10 additions & 0 deletions core/util/terminate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#include "terminate.hpp"

namespace terminate{

void finish(){
exit(1);
}

}
6 changes: 2 additions & 4 deletions core/util/terminate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

namespace terminate{

void finish(){
exit(1);
}

void finish();

}

#endif
3 changes: 3 additions & 0 deletions peer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ add_executable(iroha-main
main.cpp
)
target_link_libraries(iroha-main
sumeragi
logger
yaml_loader
http_server_with_crow
connection_with_aeron
pthread
Expand Down

0 comments on commit 1bd28d1

Please sign in to comment.