Skip to content

Commit

Permalink
Feature/sumeragi (#36) (#37)
Browse files Browse the repository at this point in the history
* change node order to trust-based

*Update sumeragi
  • Loading branch information
MizukiSonoko committed Oct 23, 2016
1 parent dd9b71e commit 6e8b84a
Show file tree
Hide file tree
Showing 27 changed files with 274 additions and 198 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# いろは(iroha)
![build status](https://circleci.com/gh/soramitsu/iroha.svg?style=shield&circle-token=80f2601e3bfb42d001e87728326659a0c96e0398)
![build status](https://circleci.com/gh/soramitsu/iroha.svg?style=shield&circle-token=80f2601e3bfb42d001e87728326659a0c96e0398) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/fa0f4ce83e584fc4a32b646536dd40eb)](https://www.codacy.com?utm_source=github.com&utm_medium=referral&utm_content=soramitsu/iroha&utm_campaign=Badge_Grade) [![Documentation Status](https://readthedocs.org/projects/iroha/badge/?version=feature-sumeragi)](http://iroha.readthedocs.io/en/feature-sumeragi/?badge=feature-sumeragi)

いろは(iroha) is ...
![alt tag](https://raw.githubusercontent.com/soramitsu/iroha/master/LGTM.gif)
Expand Down
6 changes: 3 additions & 3 deletions core/consensus/consensus_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace consensus_event {

void ConsensusEvent::addSignature(const std::string& signature){
txSignatures.push_back(signature);
void ConsensusEvent::addSignature(const std::string& signature) {
signatures.push_back(signature); //TODO: use std::move here?
}

std::string ConsensusEvent::getHash() const{
std::string ConsensusEvent::getHash() const {
return tx->getHash();
}
} // namespace consensus_event
5 changes: 3 additions & 2 deletions core/consensus/consensus_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ namespace consensus_event {

struct ConsensusEvent {
std::unique_ptr<abstract_transaction::AbstractTransaction> tx;
std::vector<std::string> txSignatures;
std::vector<std::string> signatures;
std::string merkleRoot;
unsigned long long order = 0;
std::vector<std::string> merkleRootSignatures;

ConsensusEvent(std::unique_ptr<abstract_transaction::AbstractTransaction> tx){
ConsensusEvent(std::unique_ptr<abstract_transaction::AbstractTransaction> tx) {

}

Expand Down
116 changes: 52 additions & 64 deletions core/consensus/sumeragi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ namespace sumeragi {
using ConsensusEvent = consensus_event::ConsensusEvent;

struct Context {
unsigned int maxFaulty; // f
int proxyTailNdx;
bool isSumeragi; // am I the leader or not?
unsigned long maxFaulty; // f
unsigned long proxyTailNdx;
int panicCount;
unsigned int numValidatingPeers;
unsigned long numValidatingPeers;
std::vector<
std::unique_ptr<peer::Node>
> validatingPeers;
Expand All @@ -56,10 +57,6 @@ struct Context {
{}
//std::unique_ptr<TransactionCache> txCache;
//std::unique_ptr<TransactionValidator> txValidator;

std::queue<
std::unique_ptr<ConsensusEvent>
> eventCache;

std::map<std::string, std::unique_ptr<ConsensusEvent> > processedCache;
};
Expand All @@ -80,28 +77,25 @@ void initializeSumeragi(
context->maxFaulty = context->numValidatingPeers / 3; // Default to approx. 1/3 of the network. TODO: make this configurable
context->proxyTailNdx = context->maxFaulty*2 + 1;
context->panicCount = 0;
logger::info( "sumeragi", "initialize..... complate!");
logger::info( "sumeragi", "initialize..... complete!");
}

void processTransaction(
std::unique_ptr<ConsensusEvent> event
) {
if (!transaction_validator::isValid(*event->tx)) {
void processTransaction(std::unique_ptr<ConsensusEvent> event) {
if (!transaction_validator::isValid<abstract_transaction::AbstractTransaction>(*event->tx)) {
return; //TODO-futurework: give bad trust rating to nodes that sent an invalid event
}

event->addSignature(signature::sign( event->getHash(), peer::getMyPublicKey(), peer::getPrivateKey()));
event->addSignature(signature::sign(event->getHash(), peer::getMyPublicKey(), peer::getPrivateKey()));
if (context->validatingPeers[context->proxyTailNdx]->getPublicKey() == peer::getMyPublicKey()) {
connection::send(context->validatingPeers[context->proxyTailNdx]->getIP(), event->getHash()); // Think In Process
} else {
connection::sendAll(event->getHash());// Think In Process
connection::sendAll(event->getHash()); // Think In Process
}

setAwkTimer(5000, [&](){
if (
context->processedCache.find(event->getHash()) !=
context->processedCache.end()
) { panic(event); }
if (context->processedCache.find(event->getHash()) != context->processedCache.end()) {
panic(event);
}
});

context->processedCache[event->getHash()] = std::move(event);
Expand All @@ -122,13 +116,13 @@ void processTransaction(
* ________________________ __________
* / A \ / B \
* |---| |---| |---| |---| |---| |---|
* | 0 |--| 1 |--| 4 |--| 5 |--| 2 |--| 3 |
* | 0 |--| 1 |--| 2 |--| 3 |--| 4 |--| 5 |
* |---| |---| |---| |---| |---| |---|.
*/
void panic(const std::unique_ptr<ConsensusEvent>& event) {
context->panicCount++; // TODO: reset this later
unsigned int broadcastStart = 2 * context->maxFaulty + 1 + context->maxFaulty*context->panicCount;
unsigned int broadcastEnd = broadcastStart + context->maxFaulty;
unsigned int broadcastStart = (unsigned int) (2 * context->maxFaulty + 1 + context->maxFaulty * context->panicCount);
unsigned int broadcastEnd = (unsigned int) (broadcastStart + context->maxFaulty);

// Do some bounds checking
if (broadcastStart > context->numValidatingPeers - 1) {
Expand All @@ -149,60 +143,54 @@ void setAwkTimer(int const sleepMillisecs, std::function<void(void)> const actio
}).join();
}

// WIP
long long int getDistance(const std::string& publicKey, const std::string& txHash){
// I want (node->publicKey && 0xffffff) - (txHash && 0xffffff)
return 0;
}

void determineConsensusOrder(const std::unique_ptr<ConsensusEvent>& event/*, std::vector<double> trustVector*/) {
std::string txHash = event->getHash();
/**
* The consensus order is based primarily on the trust scores. If two trust scores
* are the same, then the order (ascending) of the public keys for the servers are used.
*/
void determineConsensusOrder() {
std::sort(context->validatingPeers.begin(), context->validatingPeers.end(),
[txHash](const std::unique_ptr<peer::Node> &lhs,
[](const std::unique_ptr<peer::Node> &lhs,
const std::unique_ptr<peer::Node> &rhs) {
return getDistance(
lhs->getPublicKey(),
txHash
) < getDistance(
lhs->getPublicKey(),
txHash
);
return lhs->getTrustScore() > rhs->getTrustScore()
|| (lhs->getTrustScore() == rhs->getTrustScore()
&& lhs->getPublicKey() < rhs->getPublicKey());
}
);
}

void loop() {
logger::info("sumeragi","start main loop");
while (true) { // TODO(M->M): replace with callback linking aeron

if(!repository::event::empty()){
std::vector<
std::unique_ptr<abstract_transaction::AbstractTransaction>
> txs = repository::event::findAll();
for(auto& tx : txs){
// How to convert AbstractTransaction to Consensus Event?
// context->eventCache.push( consensus_event );
logger::info("sumeragi", "start main loop");
while (true) { // TODO: replace with callback linking the event repository?

if(!repository::event::empty()) {
std::vector<std::unique_ptr<ConsensusEvent>> events = repository::event::findAll();
// Sort the events to determine priority to process
std::sort(events.begin(), events.end(),
[](const std::unique_ptr<ConsensusEvent> &lhs,
const std::unique_ptr<ConsensusEvent> &rhs) {
return lhs->signatures.size() > rhs->signatures.size()
|| (context->isSumeragi && lhs->order == 0)
|| lhs->order < rhs->order;
}
);

for (auto&& event : events) {
if (!transaction_validator::isValid(*event->tx)) {
continue;
}
// Determine node order
determineConsensusOrder();

// Process transaction
processTransaction(std::move(event));
}
}

if (!context->eventCache.empty()) { //TODO: mutex here?
std::unique_ptr<ConsensusEvent> event = std::move(context->eventCache.front());
context->eventCache.pop();
if (!transaction_validator::isValid(*event->tx)) {
continue;
}
// Determine node order
determineConsensusOrder(event);

// Process transaction
processTransaction(std::move(event));
}

for (auto&& kv : context->processedCache) {
auto event = std::move(kv.second);

for (auto&& tuple : context->processedCache) {
auto event = std::move(tuple.second);

// Check if we have at least 2f+1 signatures
if (event->txSignatures.size() > context->maxFaulty*2 + 1) {
if (event->signatures.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
Expand Down
1 change: 0 additions & 1 deletion core/consensus/sumeragi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

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


namespace sumeragi {
void initializeSumeragi(
const std::string& myPublicKey,
Expand Down
1 change: 0 additions & 1 deletion core/infra/connection/connection_with_aeron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "../../util/logger.hpp"


#include <cstdint>
#include <cstdio>
#include <signal.h>
Expand Down
1 change: 0 additions & 1 deletion core/infra/smart_contract/jvm/java_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <iostream>
#include <unordered_map>


struct JavaContext {
JNIEnv* env;
JavaVM* jvm;
Expand Down
27 changes: 27 additions & 0 deletions core/infra/util/convert_string_with_message_pack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

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

namespace convert {

using Abs_tx = abstract_transaction::AbstractTransaction;

template<typename T>
std::string to_string(std::unique_ptr<T> object){
std::stringstream buffer;
msgpack::pack(buffer, *object);
buffer.seekg(0);
return std::string(buffer.str());
}

template<typename T>
T to_object(std::string msg){
msgpack::object_handle oh =
msgpack::unpack(msg.data(), msg.size());

msgpack::object deserialized = oh.get();
T dst;
deserialized.convert(dst);
return dst;
}
};
1 change: 0 additions & 1 deletion core/model/transactions/abstract_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ enum class TransactionType {

class AbstractTransaction {
public:

std::string publicKey;
std::string signature;

Expand Down
4 changes: 2 additions & 2 deletions core/publisher/asset_publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <memory>

// What's publisher?
// publisher is like factory in Domain Driven Development.
// What's a publisher?
// A publisher is like factory in Domain-Driven Development.
namespace publisher{

template<typename T, typename... Args>
Expand Down
51 changes: 25 additions & 26 deletions core/repository/consensus/event_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,41 @@

#include "../../util/convert_string.hpp"
#include "../../model/transactions/abstract_transaction.hpp"
#include "../../consensus/consensus_event.hpp"

namespace repository{
namespace event {

std::vector<std::string> cache;
bool add(const std::string &hash, std::unique_ptr<consensus_event::ConsensusEvent> tx) {
return false;
}

namespace event {
bool add(const std::string& hash, std::unique_ptr<abstract_transaction::AbstractTransaction> tx){
if(tx->getType() == abstract_transaction::TransactionType::message){
/*
world_state_repository::add(
hash,
convert::to_string<
abstract_transaction::AbstractTransaction
>(std::move(tx))
);
*/
bool update(const std::string &hash, const consensus_event::ConsensusEvent &consensusEvent) {
return false;
}

bool remove(const std::string &hash) {
return false;
}
}

bool update(const std::string& hash, const abstract_transaction::AbstractTransaction& tx){
bool empty() {
return false;
}

}
bool remove(const std::string& hash){
std::vector<
std::unique_ptr<consensus_event::ConsensusEvent>
> findAll() {

}
bool empty(){
}

}
std::vector<
std::unique_ptr<abstract_transaction::AbstractTransaction>
> findAll(){
std::vector<
std::unique_ptr<consensus_event::ConsensusEvent>
> findNext() {

}
}

std::unique_ptr<abstract_transaction::AbstractTransaction>& find(std::string hash){
std::unique_ptr<consensus_event::ConsensusEvent> &find(std::string hash) {

}
}
};
};
};
13 changes: 8 additions & 5 deletions core/repository/consensus/event_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
#include <string>
#include <memory>
#include "../../model/transactions/abstract_transaction.hpp"
#include "../../consensus/consensus_event.hpp"

namespace repository{
namespace repository {
namespace event {
bool add(const std::string& hash, std::unique_ptr<abstract_transaction::AbstractTransaction> tx);
bool update(std::string hash, const abstract_transaction::AbstractTransaction& tx);
bool add(const std::string& hash, std::unique_ptr<consensus_event::ConsensusEvent> consensusEvent);
bool update(std::string hash, const consensus_event::ConsensusEvent& consensusEvent);
bool remove(std::string hash);
bool empty();

std::vector<
std::unique_ptr<abstract_transaction::AbstractTransaction>
std::unique_ptr<consensus_event::ConsensusEvent>
> findAll();
std::unique_ptr<abstract_transaction::AbstractTransaction>& find(std::string hash);

std::unique_ptr<consensus_event::ConsensusEvent>& find(std::string hash);
}; // namespace unconfirmed_transaction_repository
};
#endif // CORE_REPOSITORY_UNCONFIRMEDTRANSACTIONREPOSITORY_HPP_
8 changes: 5 additions & 3 deletions core/repository/consensus/merkle_transaction_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ using abs_tx = abstract_transaction::AbstractTransaction;
// WIP
//std::unique_ptr<merkle::MerkleRoot> merkle_root;

bool commit(std::unique_ptr<consensus_event::ConsensusEvent> event) {
}
//bool commit(std::unique_ptr<consensus_event::ConsensusEvent> const event) {
// return false;
//}

bool commit(std::string hash, std::unique_ptr<consensus_event::ConsensusEvent> event) {
bool commit(std::string hash, const std::unique_ptr<consensus_event::ConsensusEvent> &event) {
return false;
}

std::unique_ptr<abs_tx> findLeaf(std::string const hash) {
Expand Down

0 comments on commit 6e8b84a

Please sign in to comment.