From 91319abc2d73a6f4645f6f9c56c546a4fdbd6150 Mon Sep 17 00:00:00 2001 From: asanka-indrajith Date: Mon, 7 Oct 2019 23:18:36 -0400 Subject: [PATCH] Protobuf helper methods and fixed makefile issue. --- makefile | 2 +- src/p2p/p2p.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ src/p2p/p2p.h | 37 ++++++++++++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 src/p2p/p2p.cpp create mode 100644 src/p2p/p2p.h diff --git a/makefile b/makefile index 17b74e6d..a04bd4ae 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ all: mkdir -p build - g++ src/*.cpp src/*.cc -lsodium -lboost_system -lboost_filesystem -pthread -lprotobuf -std=c++17 -o build/hpcore + g++ src/*.cpp src/p2p/*.cc -lsodium -lboost_system -lboost_filesystem -pthread -lprotobuf -std=c++17 -o build/hpcore echo 'build successful, binary in '`pwd`'/build/hpcore' diff --git a/src/p2p/p2p.cpp b/src/p2p/p2p.cpp new file mode 100644 index 00000000..7ba0de4a --- /dev/null +++ b/src/p2p/p2p.cpp @@ -0,0 +1,99 @@ +#include +#include "message.pb.h" +#include "p2p.h" + +using namespace std; + +namespace p2p { + +void set_message(Message &message, int timestamp, string version, string publicKey, string signature, p2p::Message::Messagetype type, string content) +{ + message.set_version(version); + message.set_timestamp(timestamp); + message.set_publickey(publicKey); + message.set_signature(signature); + message.set_type(type); + message.set_content(content); +} + + +bool message_serialize_to_string(Message& message, string* output) +{ + if(message.has_publickey() + && message.has_signature() + && message.has_timestamp() + && message.has_type() + && message.has_version() + && message.has_content()) + + return message.SerializeToString(output); + + else + return false; +} + +bool message_parse_from_string(Message& message, const string& dataString) +{ + return message.ParseFromString(dataString); +} + +void set_proposal_inputs(Proposal& proposal, vector inputs) +{ + proposal.mutable_inputs() -> Reserve(inputs.size()); + *proposal.mutable_inputs() = {inputs.begin(), inputs.end()}; +} + +void set_proposal_outputs(Proposal& proposal, vector outputs) +{ + proposal.mutable_inputs() -> Reserve(outputs.size()); + *proposal.mutable_inputs() = {outputs.begin(), outputs.end()}; +} + +void set_proposal_connections(Proposal& proposal, vector connections) +{ + proposal.mutable_inputs() -> Reserve(connections.size()); + *proposal.mutable_inputs() = {connections.begin(), connections.end()}; +} + +void set_state_patch(State& state, map patches) +{ + //state.mutable_patch().Reserve(patches.size()); + *state.mutable_patch() = {patches.begin(), patches.end()}; +} + +bool proposal_serialize_to_string(Proposal& proposal, string* output) +{ + if(proposal.has_stage() + && proposal.has_lcl() + && proposal.has_state() + && proposal.has_time() + && (proposal.inputs_size() == 0) + && (proposal.outputs_size() == 0)) + return proposal.SerializeToString(output); + + else + return false; +} + +bool proposal_parse_from_string(Proposal& proposal, const string& dataString) +{ + return proposal.ParseFromString(dataString); +} + +bool npl_serialize_to_string(NPL& npl, string* output) +{ + if(npl.has_data() + && npl.has_lcl()) + + return npl.SerializeToString(output); + + else + return false; +} + +bool npl_parse_from_string(NPL& npl, const string& dataString) +{ + return npl.ParseFromString(dataString); +} + +} \ No newline at end of file diff --git a/src/p2p/p2p.h b/src/p2p/p2p.h new file mode 100644 index 00000000..603a2f47 --- /dev/null +++ b/src/p2p/p2p.h @@ -0,0 +1,37 @@ +#ifndef _HP_P2P_H_ +#define _HP_P2P_H_ + +#include + +#include "message.pb.h" + +using namespace std; + +namespace p2p +{ + +void set_message(Message &message, int timestamp, string version, string publicKey, string signature, p2p::Message::Messagetype type, string content); + +bool message_serialize_to_string(Message& message, string* output); + +bool message_parse_from_string(Message& message, const string& dataString); + +void set_proposal_inputs(Proposal& proposal, vector inputs); + +void set_proposal_outputs(Proposal& proposal, vector outputs); + +void set_proposal_connections(Proposal& proposal, vector connections); + +void set_state_patch(State& state, map patches); + +bool proposal_serialize_to_string(Proposal& proposal, string* output); + +bool proposal_parse_from_string(Proposal& proposal, const string& dataString); + +bool npl_serialize_to_string(NPL& npl, string* output); + +bool npl_parse_from_string(NPL& npl, const string& dataString); + +} + +#endif \ No newline at end of file