Skip to content

Commit

Permalink
Merge pull request #121 from hyperledger/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
motxx committed Jan 13, 2017
2 parents cdae96d + 6a48916 commit 1489338
Show file tree
Hide file tree
Showing 24 changed files with 566 additions and 531 deletions.
2 changes: 1 addition & 1 deletion config/sumeragi.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"publicKey":"Sht5opDIxbyK+oNuEnXUs5rLbrvVgb2GjSPfqIYGFdU="
}
]
}
}
6 changes: 3 additions & 3 deletions core/consensus/consensus_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ namespace event{

template <>
void ConsensusEvent<Transaction<Update<object::Asset>>>::execution(){
logger::info("execution","update! Asset");
logger::info("execution") << "update! Asset";
}
template <>
void ConsensusEvent<Transaction<Add<object::Asset>>>::execution(){
logger::info("execution","add! Asset");
logger::info("execution") << "add! Asset";
}
template <>
void ConsensusEvent<Transaction<Add<object::Domain>>>::execution(){
logger::info("execution","add! Asset");
logger::info("execution") << "add! Asset";
}

};
291 changes: 147 additions & 144 deletions core/consensus/sumeragi.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions core/crypto/signature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ limitations under the License.

namespace signature {

class KeyPair {
public:
struct KeyPair {
std::vector<unsigned char> publicKey;
std::vector<unsigned char> privateKey;

KeyPair(
std::vector<unsigned char>&& pub,
std::vector<unsigned char>&& pri
Expand Down
62 changes: 31 additions & 31 deletions core/infra/connection/connection_with_aeron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace connection {
return [](AtomicBuffer& buffer, util::index_t offset, util::index_t length, Header& header) {
std::string raw_data = std::string((char *)buffer.buffer() + offset, (unsigned long)length);
// WIP parse json.
// logger::info("receive", raw_data);
// logger::info("receive") << raw_data;
for(auto& f : receivers) {
f(peer::getMyIp(), raw_data);
}
Expand All @@ -82,10 +82,10 @@ namespace connection {

int exec_subscription(std::string ip) {
try {
logger::info("connection", "subscript [" + ip + "]");
logger::info("connection") << "subscript [" << ip << "]";

std::int64_t sid = aeron->addSubscription("aeron:udp?endpoint=" + ip + ":40123", streamId);
auto subscription = aeron->findSubscription(sid);
auto sid = aeron->addSubscription("aeron:udp?endpoint=" + ip + ":40123", streamId);
auto subscription = aeron->findSubscription(sid);

while (!subscription) {
std::this_thread::yield();
Expand All @@ -97,68 +97,68 @@ namespace connection {
while (subscription_running){
const int fragmentsRead = subscription->poll(handler, FRAGMENTS_LIMIT);
}
logger::info("connection", "subscription halt");
logger::info("connection") << "subscription halt";
});
logger::info("connection", "subscription begin run");
logger::info("connection") << "subscription begin run";
}catch (SourcedException& e) {
logger::error("connection", "FAILED: "+ std::string(e.what()) +" : "+ std::string(e.where()));
logger::error("connection") << "FAILED: " << e.what() << " : " << e.where();
return -1;
}catch (std::exception& e) {
logger::error("connection", "FAILED: "+ std::string(e.what()));
logger::error("connection") << "FAILED: " << e.what();
return -1;
}
}

void addPublication(std::string ip) {
std::int64_t pid = aeron->addPublication("aeron:udp?endpoint=" + ip + ":40123", streamId);
auto publication = aeron->findPublication(pid);
auto pid = aeron->addPublication("aeron:udp?endpoint=" + ip + ":40123", streamId);
auto publication = aeron->findPublication(pid);
while (!publication) {
std::this_thread::yield();
publication = aeron->findPublication(pid);
}
logger::info("connection", "publication [" + ip + "]");
logger::info("connection") << "publication [" << ip << "]";
publications.emplace(ip, publication);
}

bool send(const std::string& to, const std::string& msg) {
logger::info("connection", "Start send");
logger::info("connection") << "Start send";
if(publications.find(to) == publications.end()){
logger::error("connection", to + " is not registerd");
logger::error("connection") << to << " is not registerd";
return false;
}
try{
auto message = const_cast<char*>(msg.c_str());
AERON_DECL_ALIGNED(std::uint8_t buffer[4096], 16);
AtomicBuffer srcBuffer(&buffer[0], 4096);
srcBuffer.putBytes(0, reinterpret_cast<std::uint8_t *>(message), strlen(message));
const std::int64_t result = publications[to]->offer(srcBuffer, 0, strlen(message));
if (result < 0){
if (NOT_CONNECTED == result){
logger::error("connection", " not connected yet.");
}else if (BACK_PRESSURED == result){
logger::error("connection", " back pressured.");
}else{
logger::error("connection", "unknown");
const auto result = publications[to]->offer(srcBuffer, 0, strlen(message));
if (result < 0) {
if (NOT_CONNECTED == result) {
logger::error("connection") << " not connected yet.";
} else if (BACK_PRESSURED == result) {
logger::error("connection") << " back pressured.";
} else {
logger::error("connection") << "unknown";
}
}else{
logger::debug("connection", "Ok");
} else {
logger::debug("connection") << "Ok";
}
if (!publications[to]->isConnected()){
logger::error("connection", "No active subscribers detected");
if (!publications[to]->isConnected()) {
logger::error("connection") << "No active subscribers detected";
}
return true;
}catch (SourcedException& e) {
logger::error("connection", "FAILED: "+ std::string(e.what()) +" : "+ std::string(e.where()));
} catch (SourcedException& e) {
logger::error("connection") << "FAILED: " << e.what() << " : " << e.where();
return false;
}catch (std::exception& e) {
logger::error("connection", "FAILED: "+ std::string(e.what()));
} catch (std::exception& e) {
logger::error("connection") << "FAILED: " << e.what();
return false;
}
}

bool sendAll(const std::string& msg) {
logger::info("connection", "send mesage"+ msg);
logger::info("connection", "send mesage publlications "+ std::to_string(publications.size()));
logger::info("connection") << "send mesage" << msg;
logger::info("connection") << "send mesage publlications " << publications.size();
for(auto& p : publications){
if(p.first != peer::getMyIp()){
send(p.first, msg);
Expand Down
34 changes: 17 additions & 17 deletions core/infra/connection/connection_with_grpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ namespace connection {

std::string Operation(const Event::ConsensusEvent& consensusEvent) {
Event::StatusResponse response;
logger::info("connection","Operation");
logger::info("connection","size: " + std::to_string(consensusEvent.eventsignatures_size()));
logger::info("connection","name: " + consensusEvent.transaction().asset().name());
logger::info("connection") << "Operation";
logger::info("connection") << "size: " << consensusEvent.eventsignatures_size();
logger::info("connection") << "name: " << consensusEvent.transaction().asset().name();

ClientContext context;

Status status = stub_->Operation(&context, consensusEvent, &response);
if (status.ok()) {
logger::info("connection", "response:" + response.value());
logger::info("connection") << "response: " << response.value();
return response.value();
} else {
std::cout << status.error_code() << ": "
<< status.error_message() << std::endl;
logger::error("connection") << status.error_code() << ": " << status.error_message();
//std::cout << status.error_code() << ": " << status.error_message();
return "RPC failed";
}
}
Expand All @@ -105,10 +105,10 @@ namespace connection {
event.CopyFrom(pevent->default_instance());
event.mutable_eventsignatures()->CopyFrom(pevent->eventsignatures());
event.mutable_transaction()->CopyFrom(pevent->transaction());
logger::info("connection","size: " + std::to_string(event.eventsignatures_size()));
logger::info("connection") << "size: " << event.eventsignatures_size();
auto dummy = "";
for(auto& f: receivers){
f( dummy, event);
for (auto& f: receivers){
f(dummy, event);
}
response->set_value("OK");
return Status::OK;
Expand All @@ -128,18 +128,18 @@ namespace connection {
const std::string& ip,
const Event::ConsensusEvent& event
) {
logger::info("connection", "start send");
if(find( receiver_ips.begin(), receiver_ips.end() , ip) != receiver_ips.end()){
logger::info("connection", "create client");
logger::info("connection") << "start send";
if (find(receiver_ips.begin(), receiver_ips.end(), ip) != receiver_ips.end()){
logger::info("connection") << "create client";
IrohaConnectionClient client(grpc::CreateChannel(
ip + ":50051", grpc::InsecureChannelCredentials())
);
logger::info("connection", "invoke client Operation");
logger::info("connection", "size " + std::to_string(event.eventsignatures_size()));
logger::info("connection") << "invoke client Operation";
logger::info("connection") << "size " << event.eventsignatures_size();
std::string reply = client.Operation(event);
return true;
}else{
logger::error("connection", "not found");
logger::error("connection") << "not found";
return false;
}
}
Expand All @@ -148,8 +148,8 @@ namespace connection {
const Event::ConsensusEvent& event
) {
// WIP
for(auto& ip : receiver_ips){
if( ip != peer::getMyIp()){
for (auto& ip : receiver_ips){
if (ip != peer::getMyIp()){
send( ip, event);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace repository {

bool loggerStatus(leveldb::Status const status) {
if (!status.ok()) {
logger::info("WorldStateRepositoryWithLeveldb", status.ToString());
logger::info("WorldStateRepositoryWithLeveldb") << status.ToString();
return false;
}
return true;
Expand Down
15 changes: 8 additions & 7 deletions core/infra/server/http_server_with_cappuccino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ namespace http {
}

void server() {
logger::info("server", "initialize server!");
logger::info("server") << "initialize server!";
Cappuccino::Cappuccino( 0, nullptr);

Cappuccino::route<Cappuccino::Method::POST>( "/account/register",[](std::shared_ptr<Request> request) -> Response{
Cappuccino::route<Cappuccino::Method::POST>("/account/register", [](std::shared_ptr<Request> request) -> Response {
auto res = Response(request);
auto data = request->json();
std::string uuid = "";
std::string uuid;

if(!data.empty()){
try{

Expand Down Expand Up @@ -136,11 +137,11 @@ namespace http {
std::string uuid = request->params("uuid");
auto res = Response(request);

logger::debug("Cappuccino", "param's uuid is " + uuid);
logger::debug("Cappuccino") << "param's uuid is " << uuid;
object::Account account = repository::account::findByUuid(uuid);

logger::debug("Cappuccino", "name: " + account.name);
logger::debug("Cappuccino", "publicKey: " + account.publicKey);
logger::debug("Cappuccino") << "name: " << account.name;
logger::debug("Cappuccino") << "publicKey: " << account.publicKey;

json assets = json::array();
for(auto&& as: account.assets){
Expand Down Expand Up @@ -262,7 +263,7 @@ namespace http {
return res;
});

logger::info("server", "start server!");
logger::info("server") << "start server!";
// runnning
Cappuccino::run();

Expand Down
64 changes: 35 additions & 29 deletions core/infra/service/peer_service_with_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,31 @@ namespace peer {
using json = nlohmann::json;
static optional<json> configData;

optional<json> openConfig(){
namespace detail {
std::string openJSONText(const std::string& PathToJSONFile) {
std::ifstream ifs(PathToJSONFile);
if (ifs.fail()) {
logger::error("peer with json") << "Not found: " << PathToJSONFile;
exit(EXIT_FAILURE);
}

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

void setConfigData(std::string&& jsonStr) {
try {
configData = json::parse(std::move(jsonStr));
} catch(...) {
logger::error("peer with json") << "Bad json!!";
exit(EXIT_FAILURE);
}
}
}

optional<json> openConfig() {

if(configData) { // already content loaded
if (configData) { // already content loaded
return configData;
}

Expand All @@ -28,60 +50,44 @@ namespace peer {
}();

if (PathToIROHA_HOME.empty()) {
logger::error("peer with json", "You must set IROHA_HOME!");
logger::error("peer with json") << "You must set IROHA_HOME!";
exit(EXIT_FAILURE);
// return nullopt;
}

const auto PathToSumeragiJson = PathToIROHA_HOME + "/config/sumeragi.json";
std::ifstream ifs(PathToSumeragiJson);
if(ifs.fail()) {
logger::error("peer with json", "Not found: " + PathToSumeragiJson);
exit(EXIT_FAILURE);
// return nullopt;
}

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

logger::info("peer with json", "load json is " + jsonStr);
auto jsonStr = detail::openJSONText(PathToIROHA_HOME + "/config/sumeragi.json");

logger::info("peer with json") << "load json is " << jsonStr;

try {
configData = json::parse(jsonStr);
return configData;
} catch(...) {
logger::error("peer with json", "Bad json!!");
exit(EXIT_FAILURE);
// return nullopt;
}
detail::setConfigData(std::move(jsonStr));

return configData;
}

std::string getMyPublicKey() {
if(auto config = openConfig()) {
if (auto config = openConfig()) {
return (*config)["me"]["publicKey"].get<std::string>();
}
return "";
}

std::string getPrivateKey() {
if(auto config = openConfig()) {
if (auto config = openConfig()) {
return (*config)["me"]["privateKey"].get<std::string>();
}
return "";
}

std::string getMyIp() {
if(auto config = openConfig()) {
if (auto config = openConfig()) {
return (*config)["me"]["ip"].get<std::string>();
}
return "";
}

std::vector<std::unique_ptr<peer::Node>> getPeerList() {
std::vector<std::unique_ptr<peer::Node>> nodes;
if(auto config = openConfig()) {
for(const auto& peer : (*config)["group"].get<std::vector<json>>()){
if (auto config = openConfig()) {
for (const auto& peer : (*config)["group"].get<std::vector<json>>()){
nodes.push_back(std::make_unique<peer::Node>(
peer["ip"].get<std::string>(),
peer["publicKey"].get<std::string>(),
Expand Down

0 comments on commit 1489338

Please sign in to comment.