Formatting changes and code optimations.

This commit is contained in:
asanka-indrajith
2019-10-09 08:57:55 -04:00
parent 7e4690bbe6
commit 79e70a9891
3 changed files with 77 additions and 87 deletions

View File

@@ -22,16 +22,16 @@ message Message {
}
message StateDifference {
map<string, string> created = 1;
map<string, string> updated = 2;
map<string, string> deleted = 3;
map<string, string> created = 1;
map<string, string> updated = 2;
map<string, string> deleted = 3;
}
message State {
optional bytes previous = 1;
optional bytes current = 2;
optional StateDifference difference = 3;
map<string, string> patch = 4;
optional bytes previous = 1;
optional bytes current = 2;
optional StateDifference difference = 3;
map<string, string> patch = 4;
}

View File

@@ -4,9 +4,10 @@
using namespace std;
namespace p2p {
namespace p2p
{
void set_message(Message &message, int timestamp, string version, string publicKey, string signature, p2p::Message::Messagetype type, string content)
void set_message(Message &message, int timestamp, const string &version, const string &publicKey, const string &signature, p2p::Message::Messagetype type, const string &content)
{
message.set_version(version);
message.set_timestamp(timestamp);
@@ -14,92 +15,81 @@ void set_message(Message &message, int timestamp, string version, string publicK
message.set_signature(signature);
message.set_type(type);
message.set_content(content);
}
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()
&& 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)
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() && 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<string> inputs)
void set_proposal_inputs(Proposal &proposal, vector<string> inputs)
{
proposal.mutable_inputs() -> Reserve(inputs.size());
*proposal.mutable_inputs() = {inputs.begin(), inputs.end()};
}
void set_proposal_outputs(Proposal& proposal, vector<string> outputs)
{
proposal.mutable_inputs() -> Reserve(outputs.size());
*proposal.mutable_inputs() = {outputs.begin(), outputs.end()};
}
void set_proposal_connections(Proposal& proposal, vector<string> connections)
{
proposal.mutable_inputs() -> Reserve(connections.size());
*proposal.mutable_inputs() = {connections.begin(), connections.end()};
google::protobuf::RepeatedPtrField<std::string>* proposal_inputs = proposal.mutable_outputs();
proposal_inputs-> Reserve(inputs.size());
*proposal_inputs = {inputs.begin(), inputs.end()};
}
void set_state_patch(State& state, map<string, string> patches)
void set_proposal_outputs(Proposal &proposal, vector<string> outputs)
{
google::protobuf::RepeatedPtrField<std::string>* proposal_outputs = proposal.mutable_outputs();
proposal_outputs-> Reserve(outputs.size());
*proposal_outputs = {outputs.begin(), outputs.end()};
}
void set_proposal_connections(Proposal &proposal, vector<string> connections)
{
google::protobuf::RepeatedPtrField<std::string>* proposal_connections = proposal.mutable_inputs();
proposal_connections -> Reserve(connections.size());
(*proposal_connections) = {connections.begin(), connections.end()};
}
void set_state_patch(State &state, map<string, string> 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()
&& 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)
bool proposal_serialize_to_string(Proposal &proposal, 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())
return npl.SerializeToString(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 npl_parse_from_string(NPL& npl, const string& dataString)
bool proposal_parse_from_string(Proposal &proposal, const string &dataString)
{
return proposal.ParseFromString(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())
return npl.SerializeToString(output);
else
return false;
}
bool npl_parse_from_string(NPL &npl, const string &dataString)
{
return npl.ParseFromString(dataString);
}
}
} // namespace p2p

View File

@@ -17,42 +17,42 @@ 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);
//set all fields of given message.
void set_message(Message &message, int timestamp, const string &version, const string &publicKey, const string &signature, p2p::Message::Messagetype type, const string &content);
// Serialize the message and store it in the given string. All message
// fields must be set. Consensus rounds need all fileds.
bool message_serialize_to_string(Message& message, string* output);
bool message_serialize_to_string(Message &message, string *output);
// Parsing the message from binary message string to given message.
bool message_parse_from_string(Message& message, const string& dataString);
bool message_parse_from_string(Message &message, const string &dataString);
//Set proposal inputs from given string vector.
void set_proposal_inputs(Proposal& proposal, vector<string> inputs);
void set_proposal_inputs(Proposal &proposal, vector<string> inputs);
//Set proposal outputs from given string vector.
void set_proposal_outputs(Proposal& proposal, vector<string> outputs);
void set_proposal_outputs(Proposal &proposal, vector<string> outputs);
//Set proposal connections from given string vector.
void set_proposal_connections(Proposal& proposal, vector<string> connections);
void set_proposal_connections(Proposal &proposal, vector<string> connections);
//Set proposal state patches from given map of patches.
void set_state_patch(State& state, map<string, string> patches);
void set_state_patch(State &state, map<string, string> patches);
// Serialize the proposal message and store it in the given string. All propsal message
// fields must be set. Consensus rounds need all fileds.
bool proposal_serialize_to_string(Proposal& proposal, string* output);
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);
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);
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);
bool npl_parse_from_string(NPL &npl, const string &dataString);
}
} // namespace p2p
#endif