Skip to content

Commit

Permalink
WIP replace grpc with Object
Browse files Browse the repository at this point in the history
  • Loading branch information
MizukiSonoko committed Dec 15, 2016
1 parent 87ad0a4 commit 73dbae6
Show file tree
Hide file tree
Showing 23 changed files with 862 additions and 841 deletions.
24 changes: 8 additions & 16 deletions core/consensus/connection/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,8 @@ limitations under the License.
#include <memory>
#include <functional>

#include "../../model/commands/add.hpp"
#include "../../model/commands/transfer.hpp"

#include "../../model/objects/asset.hpp"
#include "../../model/objects/domain.hpp"
#include "../../model/objects/message.hpp"

#include "../../model/transaction.hpp"

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

namespace connection {

Expand All @@ -47,24 +38,25 @@ namespace connection {
bool send(
const std::string& ip,
const std::unique_ptr<
::event::Event
Event::ConsensusEvent
>& msg);

bool sendAll(
const std::unique_ptr<
::event::Event
Event::ConsensusEvent
>& msg);

bool send(
const std::string& to,
const std::unique_ptr<
::event::Event
Event::ConsensusEvent
>& message);

bool receive(const std::function<void(
const std::string& from,
std::unique_ptr<::event::Event>&& message)
>& callback);
const std::string&,
std::unique_ptr<Event::ConsensusEvent>)>& callback
);


void addSubscriber(std::string ip);

Expand Down
4 changes: 1 addition & 3 deletions core/consensus/consensus_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ limitations under the License.
#include <unordered_map>
#include <algorithm>

#include "event.hpp"

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

namespace event {

template <typename T>
class ConsensusEvent: public T, public Event {
class ConsensusEvent: public T {

struct eventSignature{
std::string publicKey;
Expand Down
38 changes: 0 additions & 38 deletions core/consensus/event.hpp

This file was deleted.

61 changes: 39 additions & 22 deletions core/consensus/sumeragi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ namespace sumeragi {

namespace detail{

unsigned int getNumValidSignatures(const std::unique_ptr<Event::ConsensusEvent>& event){
return 1;
}

void addSignature(
const std::unique_ptr<Event::ConsensusEvent>& event,
const std::string& publicKey,
const std::string& signature
){

}

bool eventSignatureIsEmpty( const std::unique_ptr<Event::ConsensusEvent>& event) {
return true;
}

void printIsSumeragi(bool isSumeragi){
if(isSumeragi){
logger::explore("sumeragi", "===+==========+===");
Expand Down Expand Up @@ -184,12 +200,12 @@ namespace sumeragi {
connection::receive([&](
const std::string& from,
std::unique_ptr<
event::Event
Event::ConsensusEvent
>&& event
){
logger::info("sumeragi", "receive!");
auto hash = event->getHash();
logger::info("sumeragi", "received message! sig:[" + std::to_string(event->getNumValidSignatures()) +"]");
auto hash = event->transaction().hash();
//logger::info("sumeragi", "received message! sig:[" + std::to_string(event->getNumValidSignatures()) +"]");
// WIP currently, unuse hash in event repository,
repository::event::add( hash, std::move(event));
});
Expand All @@ -213,7 +229,7 @@ namespace sumeragi {
}


void processTransaction(const std::unique_ptr<event::Event>& event) {
void processTransaction(const std::unique_ptr<Event::ConsensusEvent>& event) {

logger::info("sumeragi", "processTransaction");
//if (!transaction_validator::isValid(event->getTx())) {
Expand All @@ -222,35 +238,35 @@ namespace sumeragi {
logger::info("sumeragi", "valied");
logger::info("sumeragi", "Add my signature...");

logger::info("sumeragi", "hash:" + event->getHash());
logger::info("sumeragi", "hash:" + event->transaction().hash());
logger::info("sumeragi", "pub:" + peer::getMyPublicKey());
logger::info("sumeragi", "pro:"+ peer::getPrivateKey());
logger::info("sumeragi", "sog:"+ std::string(signature::sign(event->getHash(), peer::getMyPublicKey(), peer::getPrivateKey()).c_str()));
logger::info("sumeragi", "sog:"+ std::string(signature::sign(event->transaction().hash(), peer::getMyPublicKey(), peer::getPrivateKey()).c_str()));

//detail::printIsSumeragi(context->isSumeragi);
// Really need? blow "if statement" will be false anytime.
event->addSignature( peer::getMyPublicKey(), signature::sign(event->getHash(), peer::getMyPublicKey(), peer::getPrivateKey()).c_str());
detail::addSignature(event, peer::getMyPublicKey(), signature::sign(event->transaction().hash(), peer::getMyPublicKey(), peer::getPrivateKey()).c_str());

if (event->eventSignatureIsEmpty() && context->isSumeragi) {
if (detail::eventSignatureIsEmpty(event) && context->isSumeragi) {
logger::info("sumeragi", "signatures.empty() isSumragi");
// Determine the order for processing this event
event->order = getNextOrder();
logger::info("sumeragi", "new order:" + std::to_string(event->order));
event->set_order( getNextOrder());
logger::info("sumeragi", "new order:" + std::to_string(event->order()));

} else if (!event->eventSignatureIsEmpty()) {
} else if (!detail::eventSignatureIsEmpty(event)) {
// Check if we have at least 2f+1 signatures needed for Byzantine fault tolerance
if (event->getNumValidSignatures() >= context->maxFaulty*2 + 1) {
if ( detail::getNumValidSignatures(event) >= context->maxFaulty*2 + 1) {

logger::info("sumeragi", "Signature exists");
logger::explore("sumeragi", "0--------------------------0");
logger::explore("sumeragi", "+~~~~~~~~~~~~~~~~~~~~~~~~~~+");
logger::explore("sumeragi", "|Would you agree with this?|");
logger::explore("sumeragi", "+~~~~~~~~~~~~~~~~~~~~~~~~~~+");
logger::explore("sumeragi", "\033[93m0================================================================0\033[0m");
logger::explore("sumeragi", "\033[93m0\033[1m"+ event->getHash() +"0\033[0m");
logger::explore("sumeragi", "\033[93m0\033[1m"+ event->transaction().hash() +"0\033[0m");
logger::explore("sumeragi", "\033[93m0================================================================0\033[0m");

detail::printJudge( event->getNumValidSignatures(), context->numValidatingPeers, context->maxFaulty*2 + 1);
detail::printJudge( detail::getNumValidSignatures(event), context->numValidatingPeers, context->maxFaulty*2 + 1);

detail::printAgree();
// Check Merkle roots to see if match for new state
Expand All @@ -271,10 +287,10 @@ namespace sumeragi {
merkle_transaction_repository::commit(event); //TODO: add error handling in case not saved

// Write exec code smart contract
event->execution();
// event->execution();
} else {
// This is a new event, so we should verify, sign, and broadcast it
event->addSignature( peer::getMyPublicKey(), signature::sign(event->getHash(), peer::getMyPublicKey(), peer::getPrivateKey()).c_str());
detail::addSignature( event, peer::getMyPublicKey(), signature::sign( event->transaction().hash(), peer::getMyPublicKey(), peer::getPrivateKey()).c_str());

logger::info("sumeragi", "tail pubkey is "+context->validatingPeers.at(context->proxyTailNdx)->getPublicKey());
logger::info("sumeragi", "tail is "+std::to_string(context->proxyTailNdx));
Expand All @@ -284,13 +300,13 @@ namespace sumeragi {
logger::info("sumeragi", "I will send event to "+context->validatingPeers.at(context->proxyTailNdx)->getIP());
connection::send(context->validatingPeers.at(context->proxyTailNdx)->getIP(), std::move(event)); // Think In Process
} else {
logger::info("sumeragi", "Send All! sig:[" + std::to_string(event->getNumValidSignatures()) +"]");
logger::info("sumeragi", "Send All! sig:[" + std::to_string( detail::getNumValidSignatures(event)) +"]");
connection::sendAll(std::move(event)); // TODO: Think In Process
}

setAwkTimer(3, [&](){
//setAwkTimer(3000, [&](){
if (!merkle_transaction_repository::leafExists(event->getHash())) {
if (!merkle_transaction_repository::leafExists( event->transaction().hash())) {
panic(event);
}
});
Expand All @@ -316,7 +332,7 @@ namespace sumeragi {
* | 0 |--| 1 |--| 2 |--| 3 |--| 4 |--| 5 |
* |---| |---| |---| |---| |---| |---|.
*/
void panic(const std::unique_ptr<event::Event>& event) {
void panic(const std::unique_ptr<Event::ConsensusEvent>& event) {
context->panicCount++; // TODO: reset this later
unsigned long broadcastStart = 2 * context->maxFaulty + 1 + context->maxFaulty * context->panicCount;
unsigned long broadcastEnd = broadcastStart + context->maxFaulty;
Expand All @@ -332,7 +348,7 @@ namespace sumeragi {
logger::info( "sumeragi", "broadcastEnd:"+ std::to_string(broadcastEnd));
logger::info( "sumeragi", "broadcastStart:"+ std::to_string(broadcastStart));
// WIP issue hash event
//connection::sendAll(event->getHash()); //TODO: change this to only broadcast to peer range between broadcastStart and broadcastEnd
//connection::sendAll(event->transaction().hash()); //TODO: change this to only broadcast to peer range between broadcastStart and broadcastEnd
}

void setAwkTimer(int const sleepMillisecs, std::function<void(void)> const action) {
Expand Down Expand Up @@ -391,6 +407,7 @@ namespace sumeragi {
logger::info("sumeragi", "event queue not empty");

auto events = repository::event::findAll();
/*
logger::info("sumeragi", "event's size " + std::to_string(events.size()));
// Sort the events to determine priority to process
Expand All @@ -401,11 +418,11 @@ namespace sumeragi {
|| lhs->order < rhs->order;
}
);

*/
logger::info("sumeragi", "sorted " + std::to_string(events.size()));
for (auto& event : events) {

logger::info("sumeragi", "evens order:" + std::to_string(event->order));
logger::info("sumeragi", "evens order:" + std::to_string(event->order()));
/*
if (!transaction_validator::isValid(event)) {
continue;
Expand Down
8 changes: 4 additions & 4 deletions core/consensus/sumeragi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ limitations under the License.
#include <memory>

#include "consensus_event.hpp"
#include "event.hpp"

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

namespace sumeragi {

Expand All @@ -37,12 +37,12 @@ namespace sumeragi {
void loop();

void getNextOrder(
const std::unique_ptr<event::Event> event
const std::unique_ptr<Event::ConsensusEvent> event
);

void processTransaction(const std::unique_ptr<event::Event>& event);
void processTransaction(const std::unique_ptr<Event::ConsensusEvent>& event);

void panic(const std::unique_ptr<event::Event>& event);
void panic(const std::unique_ptr<Event::ConsensusEvent>& event);
void setAwkTimer(const int sleepMillisecs, const std::function<void(void)> action);
void determineConsensusOrder(/*std::vector<double> trustVector*/);

Expand Down
1 change: 1 addition & 0 deletions core/infra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ add_subdirectory(repository)
add_subdirectory(server)
add_subdirectory(connection)
add_subdirectory(service)
add_subdirectory(protobuf)
31 changes: 1 addition & 30 deletions core/infra/connection/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,6 @@ SET(CMAKE_CXX_FLAGS "-std=c++14 -L/usr/local/lib `pkg-config --libs grpc++ grpc`
SET(CRYPTO_PATH "${PROJECT_SOURCE_DIR}/core/crypto")


#
#SET(AERON_PATH "${PROJECT_SOURCE_DIR}/core/vendor/Aeron")
#SET(CRYPTO_PATH "${PROJECT_SOURCE_DIR}/core/crypto")
#
#include_directories(
# ${AERON_PATH}/aeron-client/src/main/cpp
#)
#link_directories(
# ${AERON_PATH}/cppbuild/Release/lib
# /usr/local/lib
#)
#
#ADD_LIBRARY(connection_with_aeron STATIC connection_with_aeron.cpp)
#target_link_libraries(connection_with_aeron
# aeron_client
# logger
# peer_service_with_json
#)
#]]
#


link_directories(
${CRYPTO_PATH}
/usr/local/lib
Expand All @@ -33,15 +11,8 @@ link_directories(

ADD_LIBRARY(connection_with_grpc STATIC
connection_with_grpc.cpp
connection.grpc.pb.cc
connection.pb.cc
)

target_link_libraries(connection_with_grpc
grpc++
grpc++_reflection
protobuf
pthread
dl
logger
event_with_grpc
)
Loading

0 comments on commit 73dbae6

Please sign in to comment.