From 8eed6422772864fb14ab7fdcdada8b70b7200e86 Mon Sep 17 00:00:00 2001 From: asanka-indrajith Date: Wed, 9 Oct 2019 03:09:55 -0400 Subject: [PATCH] Add comments to protobuf helper methods and add protobuf to cmakelist. --- CMakeLists.txt | 3 +++ src/p2p/p2p.cpp | 6 ++++++ src/p2p/p2p.h | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3903752c..cd783fa4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,10 +12,13 @@ add_executable(hpcore src/shared.cpp src/usr/usr.cpp src/shared.cpp + src/p2p/message.pb.cc ) target_link_libraries(hpcore libsodium.a libboost_system.a libboost_filesystem.a + pthread + protobuf.a ) \ No newline at end of file diff --git a/src/p2p/p2p.cpp b/src/p2p/p2p.cpp index 7ba0de4a..2265fa38 100644 --- a/src/p2p/p2p.cpp +++ b/src/p2p/p2p.cpp @@ -19,6 +19,7 @@ void set_message(Message &message, int timestamp, string version, string publicK bool message_serialize_to_string(Message& message, string* output) { + //check all fields are set in message if(message.has_publickey() && message.has_signature() && message.has_timestamp() @@ -58,11 +59,13 @@ void set_proposal_connections(Proposal& proposal, vector connections) void set_state_patch(State& state, map patches) { //state.mutable_patch().Reserve(patches.size()); + //reserve memory need to optimise copy *state.mutable_patch() = {patches.begin(), patches.end()}; } bool proposal_serialize_to_string(Proposal& proposal, string* output) { + //check all fields are set in the proposal if(proposal.has_stage() && proposal.has_lcl() && proposal.has_state() @@ -82,6 +85,9 @@ bool proposal_parse_from_string(Proposal& proposal, const string& dataString) bool npl_serialize_to_string(NPL& npl, string* output) { + //check all fields are set in the proposal + //not sure npl messages need both data or lcl have to be set. + //may be only one needed. need to deal with this when processing npl messages if(npl.has_data() && npl.has_lcl()) diff --git a/src/p2p/p2p.h b/src/p2p/p2p.h index 603a2f47..e3719a03 100644 --- a/src/p2p/p2p.h +++ b/src/p2p/p2p.h @@ -10,26 +10,47 @@ using namespace std; namespace p2p { +/* +Protobuf helpers ------------------------------------------------- +Purpose of these helper methods is to wrap up protobuf functionality and provide additional functionality +such as message validation. +Need to improve and add additional functionality once started to use. +*/ + +//set message fields of passed of given message. void set_message(Message &message, int timestamp, string version, string publicKey, string signature, p2p::Message::Messagetype type, string content); +// Serialize the message and store it in the given string. All message +// fields must be set. bool message_serialize_to_string(Message& message, string* output); +// Parsing the message from binary string to given message. bool message_parse_from_string(Message& message, const string& dataString); +//Set proposal inputs from given string vector. void set_proposal_inputs(Proposal& proposal, vector inputs); +//Set proposal outputs from given string vector. void set_proposal_outputs(Proposal& proposal, vector outputs); +//Set proposal connections from given string vector. void set_proposal_connections(Proposal& proposal, vector connections); +//Set proposal state patches from given map. Both keys and value of map should be strings. void set_state_patch(State& state, map patches); +// Serialize the proposal message and store it in the given string. All propsal message +// fields must be set. bool proposal_serialize_to_string(Proposal& proposal, string* output); +// Parsing the proposal message from binary string to given message. bool proposal_parse_from_string(Proposal& proposal, const string& dataString); +// Serialize the npl message and store it in the given string. All npl message +// fields must be set. bool npl_serialize_to_string(NPL& npl, string* output); +// Parsing the npl message from binary string to given message. bool npl_parse_from_string(NPL& npl, const string& dataString); }