mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-07-27 09:00:34 +00:00
Merge pull request #10 from HotPocketDev/protobuf
Protobuf initial implementation.
This commit is contained in:
@@ -12,6 +12,7 @@ add_executable(hpcore
|
||||
src/shared.cpp
|
||||
src/usr/usr.cpp
|
||||
src/shared.cpp
|
||||
src/p2p/message.pb.cc
|
||||
src/sock/socket_client.cpp
|
||||
src/sock/socket_server.cpp
|
||||
src/sock/socket_session.cpp
|
||||
@@ -22,4 +23,5 @@ target_link_libraries(hpcore
|
||||
libboost_system.a
|
||||
libboost_filesystem.a
|
||||
pthread
|
||||
protobuf.a
|
||||
)
|
||||
14
README.md
14
README.md
@@ -35,6 +35,20 @@ Following Instructions are based on Boost [getting started](https://www.boost.or
|
||||
2. Navigate to the extracted directory.
|
||||
3. Run `sudo cp -r include/rapidjson /usr/local/include/`
|
||||
|
||||
#### Install Protocol buffers
|
||||
Instructions are based on [this](https://github.com/protocolbuffers/protobuf/tree/master/src).
|
||||
|
||||
1. Download and extract Protobuf 3.10.0 from [here](https://github.com/protocolbuffers/protobuf/releases/tag/v3.10.0).
|
||||
2. Navigate to the extracted Protobuf directory in a terminal.
|
||||
3. Run `./configure`
|
||||
4. Run `make && make check`
|
||||
5. Run `sudo make install`
|
||||
|
||||
#### Compile Protocol buffers
|
||||
1. Run `protoc -I=./src/p2p --cpp_out=./src/p2p ./src/p2p/message.proto`
|
||||
Ex - For message protobuf
|
||||
`protoc -I=./src/p2p --cpp_out=./src/p2p ./src/p2p/message.proto`
|
||||
|
||||
#### Run ldconfig
|
||||
1. Run `sudo ldconfig`
|
||||
|
||||
|
||||
4139
src/p2p/message.pb.cc
Normal file
4139
src/p2p/message.pb.cc
Normal file
File diff suppressed because it is too large
Load Diff
2885
src/p2p/message.pb.h
Normal file
2885
src/p2p/message.pb.h
Normal file
File diff suppressed because it is too large
Load Diff
69
src/p2p/message.proto
Normal file
69
src/p2p/message.proto
Normal file
@@ -0,0 +1,69 @@
|
||||
syntax = "proto2";
|
||||
|
||||
package p2p;
|
||||
|
||||
message Message {
|
||||
optional string version = 1;
|
||||
optional bytes publicKey = 2;
|
||||
optional int32 timestamp = 3;
|
||||
optional bytes signature = 4;
|
||||
|
||||
enum Messagetype {
|
||||
PROPOSAL = 0;
|
||||
NPL = 1;
|
||||
STATE_REQUEST = 2;
|
||||
STATE_RESPONSE = 3;
|
||||
HISTORY_REQUEST = 4;
|
||||
HISTORY_RESPONSE = 5;
|
||||
}
|
||||
|
||||
optional Messagetype type = 5;
|
||||
optional bytes content = 6;
|
||||
}
|
||||
|
||||
message StateDifference {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
message Proposal {
|
||||
repeated string connections = 1;
|
||||
repeated string inputs = 2;
|
||||
repeated string outputs = 3;
|
||||
optional int32 stage = 4;
|
||||
optional int32 time = 5;
|
||||
optional State state = 6;
|
||||
optional bytes lcl = 7;
|
||||
}
|
||||
|
||||
message NPL {
|
||||
optional bytes data = 1;
|
||||
optional bytes lcl = 2;
|
||||
}
|
||||
|
||||
message StateRequest {
|
||||
|
||||
}
|
||||
|
||||
message StateResponse {
|
||||
|
||||
}
|
||||
|
||||
message HistoryRequest {
|
||||
|
||||
}
|
||||
|
||||
message HistoryResponse {
|
||||
|
||||
}
|
||||
|
||||
|
||||
95
src/p2p/p2p.cpp
Normal file
95
src/p2p/p2p.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#include <string>
|
||||
#include "message.pb.h"
|
||||
#include "p2p.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace p2p
|
||||
{
|
||||
|
||||
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);
|
||||
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)
|
||||
{
|
||||
//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)
|
||||
{
|
||||
google::protobuf::RepeatedPtrField<std::string>* proposal_inputs = proposal.mutable_outputs();
|
||||
proposal_inputs-> Reserve(inputs.size());
|
||||
*proposal_inputs = {inputs.begin(), inputs.end()};
|
||||
}
|
||||
|
||||
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() = {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)
|
||||
{
|
||||
//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
|
||||
58
src/p2p/p2p.h
Normal file
58
src/p2p/p2p.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef _HP_P2P_H_
|
||||
#define _HP_P2P_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "message.pb.h"
|
||||
|
||||
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 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);
|
||||
|
||||
// Parsing the message from binary message 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<string> inputs);
|
||||
|
||||
//Set proposal outputs from given string vector.
|
||||
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);
|
||||
|
||||
//Set proposal state patches from given map of 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);
|
||||
|
||||
// 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);
|
||||
|
||||
} // namespace p2p
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user