mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Added contract id verification. (#180)
* Added contract id config. * Verify contract id with user/peer challenge. * Updated cluster scripts. * Updated node client lib for contract id verification.
This commit is contained in:
@@ -15,7 +15,7 @@ async function main() {
|
||||
let server = 'wss://localhost:8080'
|
||||
if (process.argv.length == 3) server = 'wss://localhost:' + process.argv[2]
|
||||
if (process.argv.length == 4) server = 'wss://' + process.argv[2] + ':' + process.argv[3]
|
||||
const hpc = new HotPocket.Client(server, keys, HotPocket.protocols.bson);
|
||||
const hpc = new HotPocket.Client(null, server, keys, HotPocket.protocols.bson);
|
||||
|
||||
// Establish HotPocket connection.
|
||||
if (!await hpc.connect()) {
|
||||
|
||||
@@ -40,7 +40,7 @@ const HotPocketKeyGenerator = {
|
||||
},
|
||||
}
|
||||
|
||||
function HotPocketClient(server, keys, protocol = protocols.json) {
|
||||
function HotPocketClient(contractId, server, keys, protocol = protocols.json) {
|
||||
|
||||
let ws = null;
|
||||
const msgHelper = new MessageHelper(keys, protocol);
|
||||
@@ -102,7 +102,14 @@ function HotPocketClient(server, keys, protocol = protocols.json) {
|
||||
}
|
||||
|
||||
if (m.type == 'handshake_challenge') {
|
||||
// sign the challenge and send back the response
|
||||
// Check whether contract id is matching if specified.
|
||||
if (contractId && m.contract_id != contractId)
|
||||
{
|
||||
console.error("Contract id mismatch.")
|
||||
ws.close();
|
||||
}
|
||||
|
||||
// Sign the challenge and send back the response
|
||||
const response = msgHelper.createHandshakeResponse(m.challenge);
|
||||
ws.send(JSON.stringify(response));
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ function RoboClient(server, port, clientId) {
|
||||
|
||||
this.connect = async () => {
|
||||
this.keys = await HotPocketKeyGenerator.generate();
|
||||
this.hpclient = new HotPocketClient(server + port, this.keys);
|
||||
this.hpclient = new HotPocketClient(null, server + port, this.keys);
|
||||
|
||||
|
||||
if (!await this.hpclient.connect()) {
|
||||
|
||||
@@ -11,7 +11,7 @@ async function main() {
|
||||
let server = 'wss://localhost:8080'
|
||||
if (process.argv.length == 3) server = 'wss://localhost:' + process.argv[2]
|
||||
if (process.argv.length == 4) server = 'wss://' + process.argv[2] + ':' + process.argv[3]
|
||||
const hpc = new HotPocket.Client(server, keys, HotPocket.protocols.json);
|
||||
const hpc = new HotPocket.Client(null, server, keys, HotPocket.protocols.json);
|
||||
|
||||
// Establish HotPocket connection.
|
||||
if (!await hpc.connect()) {
|
||||
|
||||
29
src/conf.cpp
29
src/conf.cpp
@@ -91,6 +91,14 @@ namespace conf
|
||||
crypto::generate_signing_keys(cfg.pubkey, cfg.seckey);
|
||||
binpair_to_hex(cfg);
|
||||
|
||||
// Generate contract id hex.
|
||||
std::string rand_string;
|
||||
crypto::random_bytes(rand_string, 16);
|
||||
util::bin2hex(
|
||||
cfg.contractid,
|
||||
reinterpret_cast<const unsigned char *>(rand_string.data()),
|
||||
rand_string.length());
|
||||
|
||||
//Add self pubkey to the unl.
|
||||
cfg.unl.emplace(cfg.pubkey);
|
||||
|
||||
@@ -230,6 +238,8 @@ namespace conf
|
||||
|
||||
// Load up the values into the struct.
|
||||
|
||||
cfg.contractid = d["contractid"].as<std::string>();
|
||||
|
||||
if (d["mode"] == MODE_OBSERVER)
|
||||
cfg.operating_mode = OPERATING_MODE::OBSERVER;
|
||||
else if (d["mode"] == MODE_PROPOSER)
|
||||
@@ -320,7 +330,7 @@ namespace conf
|
||||
|
||||
cfg.msgforwarding = d["msgforwarding"].as<bool>();
|
||||
cfg.dynamicpeerdiscovery = d["dynamicpeerdiscovery"].as<bool>();
|
||||
cfg.fullhistory = d["fullhistory"].as<bool>();
|
||||
// cfg.fullhistory = d["fullhistory"].as<bool>();
|
||||
|
||||
cfg.loglevel = d["loglevel"].as<std::string>();
|
||||
cfg.loglevel_type = get_loglevel_type(cfg.loglevel);
|
||||
@@ -341,14 +351,15 @@ namespace conf
|
||||
// ojson is used instead of json to preserve insertion order.
|
||||
jsoncons::ojson d;
|
||||
d.insert_or_assign("version", util::HP_VERSION);
|
||||
d.insert_or_assign("contractid", cfg.contractid);
|
||||
d.insert_or_assign("mode", cfg.operating_mode == OPERATING_MODE::OBSERVER ? MODE_OBSERVER : MODE_PROPOSER);
|
||||
|
||||
d.insert_or_assign("pubkeyhex", cfg.pubkeyhex.data());
|
||||
d.insert_or_assign("seckeyhex", cfg.seckeyhex.data());
|
||||
d.insert_or_assign("binary", cfg.binary.data());
|
||||
d.insert_or_assign("binargs", cfg.binargs.data());
|
||||
d.insert_or_assign("appbill", cfg.appbill.data());
|
||||
d.insert_or_assign("appbillargs", cfg.appbillargs.data());
|
||||
d.insert_or_assign("pubkeyhex", cfg.pubkeyhex);
|
||||
d.insert_or_assign("seckeyhex", cfg.seckeyhex);
|
||||
d.insert_or_assign("binary", cfg.binary);
|
||||
d.insert_or_assign("binargs", cfg.binargs);
|
||||
d.insert_or_assign("appbill", cfg.appbill);
|
||||
d.insert_or_assign("appbillargs", cfg.appbillargs);
|
||||
|
||||
jsoncons::ojson peers(jsoncons::json_array_arg);
|
||||
for (const auto &peer : cfg.peers)
|
||||
@@ -394,14 +405,14 @@ namespace conf
|
||||
|
||||
d.insert_or_assign("msgforwarding", cfg.msgforwarding);
|
||||
d.insert_or_assign("dynamicpeerdiscovery", cfg.dynamicpeerdiscovery);
|
||||
d.insert_or_assign("fullhistory", cfg.fullhistory);
|
||||
// d.insert_or_assign("fullhistory", cfg.fullhistory);
|
||||
|
||||
d.insert_or_assign("loglevel", cfg.loglevel);
|
||||
|
||||
jsoncons::ojson loggers(jsoncons::json_array_arg);
|
||||
for (std::string_view logger : cfg.loggers)
|
||||
{
|
||||
loggers.push_back(logger.data());
|
||||
loggers.push_back(logger);
|
||||
}
|
||||
d.insert_or_assign("loggers", loggers);
|
||||
|
||||
|
||||
@@ -83,6 +83,7 @@ namespace conf
|
||||
std::vector<std::string> runtime_appbill_args; // Appbill execution args used during runtime.
|
||||
|
||||
// Config elements which are loaded from the config file.
|
||||
std::string contractid; // Contract guid.
|
||||
OPERATING_MODE operating_mode = OPERATING_MODE::OBSERVER; // Configured startup operating mode of the contract (Observer/Proposer).
|
||||
std::string pubkeyhex; // Contract hex public key
|
||||
std::string seckeyhex; // Contract hex secret key
|
||||
@@ -115,7 +116,7 @@ namespace conf
|
||||
|
||||
bool msgforwarding = false; // Whether peer message forwarding is on/off.
|
||||
bool dynamicpeerdiscovery = false; // Whether dynamic peer discovery is on/off.
|
||||
bool fullhistory = false; // Whether full history mode is on/off.
|
||||
bool fullhistory = false; // Whether full history mode is on/off.
|
||||
|
||||
std::string loglevel; // Log severity level (debug, info, warn, error)
|
||||
LOG_SEVERITY loglevel_type; // Log severity level enum (debug, info, warn, error)
|
||||
|
||||
@@ -5,11 +5,12 @@ include "common_schema.fbs";
|
||||
namespace msg.fbuf.p2pmsg;
|
||||
|
||||
table Peer_Challenge_Message {
|
||||
challenge:[ubyte];
|
||||
contract_id:string;
|
||||
challenge:string;
|
||||
}
|
||||
|
||||
table Peer_Challenge_Response_Message {
|
||||
challenge:[ubyte];
|
||||
challenge:string;
|
||||
sig:[ubyte];
|
||||
}
|
||||
|
||||
|
||||
@@ -297,18 +297,27 @@ bool VerifyState_ResponseVector(flatbuffers::Verifier &verifier, const flatbuffe
|
||||
struct Peer_Challenge_Message FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
typedef Peer_Challenge_MessageBuilder Builder;
|
||||
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
|
||||
VT_CHALLENGE = 4
|
||||
VT_CONTRACT_ID = 4,
|
||||
VT_CHALLENGE = 6
|
||||
};
|
||||
const flatbuffers::Vector<uint8_t> *challenge() const {
|
||||
return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_CHALLENGE);
|
||||
const flatbuffers::String *contract_id() const {
|
||||
return GetPointer<const flatbuffers::String *>(VT_CONTRACT_ID);
|
||||
}
|
||||
flatbuffers::Vector<uint8_t> *mutable_challenge() {
|
||||
return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_CHALLENGE);
|
||||
flatbuffers::String *mutable_contract_id() {
|
||||
return GetPointer<flatbuffers::String *>(VT_CONTRACT_ID);
|
||||
}
|
||||
const flatbuffers::String *challenge() const {
|
||||
return GetPointer<const flatbuffers::String *>(VT_CHALLENGE);
|
||||
}
|
||||
flatbuffers::String *mutable_challenge() {
|
||||
return GetPointer<flatbuffers::String *>(VT_CHALLENGE);
|
||||
}
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyOffset(verifier, VT_CONTRACT_ID) &&
|
||||
verifier.VerifyString(contract_id()) &&
|
||||
VerifyOffset(verifier, VT_CHALLENGE) &&
|
||||
verifier.VerifyVector(challenge()) &&
|
||||
verifier.VerifyString(challenge()) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
};
|
||||
@@ -317,13 +326,17 @@ struct Peer_Challenge_MessageBuilder {
|
||||
typedef Peer_Challenge_Message Table;
|
||||
flatbuffers::FlatBufferBuilder &fbb_;
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_challenge(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> challenge) {
|
||||
void add_contract_id(flatbuffers::Offset<flatbuffers::String> contract_id) {
|
||||
fbb_.AddOffset(Peer_Challenge_Message::VT_CONTRACT_ID, contract_id);
|
||||
}
|
||||
void add_challenge(flatbuffers::Offset<flatbuffers::String> challenge) {
|
||||
fbb_.AddOffset(Peer_Challenge_Message::VT_CHALLENGE, challenge);
|
||||
}
|
||||
explicit Peer_Challenge_MessageBuilder(flatbuffers::FlatBufferBuilder &_fbb)
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Peer_Challenge_MessageBuilder &operator=(const Peer_Challenge_MessageBuilder &);
|
||||
flatbuffers::Offset<Peer_Challenge_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Peer_Challenge_Message>(end);
|
||||
@@ -333,18 +346,23 @@ struct Peer_Challenge_MessageBuilder {
|
||||
|
||||
inline flatbuffers::Offset<Peer_Challenge_Message> CreatePeer_Challenge_Message(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> challenge = 0) {
|
||||
flatbuffers::Offset<flatbuffers::String> contract_id = 0,
|
||||
flatbuffers::Offset<flatbuffers::String> challenge = 0) {
|
||||
Peer_Challenge_MessageBuilder builder_(_fbb);
|
||||
builder_.add_challenge(challenge);
|
||||
builder_.add_contract_id(contract_id);
|
||||
return builder_.Finish();
|
||||
}
|
||||
|
||||
inline flatbuffers::Offset<Peer_Challenge_Message> CreatePeer_Challenge_MessageDirect(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
const std::vector<uint8_t> *challenge = nullptr) {
|
||||
auto challenge__ = challenge ? _fbb.CreateVector<uint8_t>(*challenge) : 0;
|
||||
const char *contract_id = nullptr,
|
||||
const char *challenge = nullptr) {
|
||||
auto contract_id__ = contract_id ? _fbb.CreateString(contract_id) : 0;
|
||||
auto challenge__ = challenge ? _fbb.CreateString(challenge) : 0;
|
||||
return msg::fbuf::p2pmsg::CreatePeer_Challenge_Message(
|
||||
_fbb,
|
||||
contract_id__,
|
||||
challenge__);
|
||||
}
|
||||
|
||||
@@ -354,11 +372,11 @@ struct Peer_Challenge_Response_Message FLATBUFFERS_FINAL_CLASS : private flatbuf
|
||||
VT_CHALLENGE = 4,
|
||||
VT_SIG = 6
|
||||
};
|
||||
const flatbuffers::Vector<uint8_t> *challenge() const {
|
||||
return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_CHALLENGE);
|
||||
const flatbuffers::String *challenge() const {
|
||||
return GetPointer<const flatbuffers::String *>(VT_CHALLENGE);
|
||||
}
|
||||
flatbuffers::Vector<uint8_t> *mutable_challenge() {
|
||||
return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_CHALLENGE);
|
||||
flatbuffers::String *mutable_challenge() {
|
||||
return GetPointer<flatbuffers::String *>(VT_CHALLENGE);
|
||||
}
|
||||
const flatbuffers::Vector<uint8_t> *sig() const {
|
||||
return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_SIG);
|
||||
@@ -369,7 +387,7 @@ struct Peer_Challenge_Response_Message FLATBUFFERS_FINAL_CLASS : private flatbuf
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyOffset(verifier, VT_CHALLENGE) &&
|
||||
verifier.VerifyVector(challenge()) &&
|
||||
verifier.VerifyString(challenge()) &&
|
||||
VerifyOffset(verifier, VT_SIG) &&
|
||||
verifier.VerifyVector(sig()) &&
|
||||
verifier.EndTable();
|
||||
@@ -380,7 +398,7 @@ struct Peer_Challenge_Response_MessageBuilder {
|
||||
typedef Peer_Challenge_Response_Message Table;
|
||||
flatbuffers::FlatBufferBuilder &fbb_;
|
||||
flatbuffers::uoffset_t start_;
|
||||
void add_challenge(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> challenge) {
|
||||
void add_challenge(flatbuffers::Offset<flatbuffers::String> challenge) {
|
||||
fbb_.AddOffset(Peer_Challenge_Response_Message::VT_CHALLENGE, challenge);
|
||||
}
|
||||
void add_sig(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> sig) {
|
||||
@@ -390,6 +408,7 @@ struct Peer_Challenge_Response_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Peer_Challenge_Response_MessageBuilder &operator=(const Peer_Challenge_Response_MessageBuilder &);
|
||||
flatbuffers::Offset<Peer_Challenge_Response_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Peer_Challenge_Response_Message>(end);
|
||||
@@ -399,7 +418,7 @@ struct Peer_Challenge_Response_MessageBuilder {
|
||||
|
||||
inline flatbuffers::Offset<Peer_Challenge_Response_Message> CreatePeer_Challenge_Response_Message(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> challenge = 0,
|
||||
flatbuffers::Offset<flatbuffers::String> challenge = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> sig = 0) {
|
||||
Peer_Challenge_Response_MessageBuilder builder_(_fbb);
|
||||
builder_.add_sig(sig);
|
||||
@@ -409,9 +428,9 @@ inline flatbuffers::Offset<Peer_Challenge_Response_Message> CreatePeer_Challenge
|
||||
|
||||
inline flatbuffers::Offset<Peer_Challenge_Response_Message> CreatePeer_Challenge_Response_MessageDirect(
|
||||
flatbuffers::FlatBufferBuilder &_fbb,
|
||||
const std::vector<uint8_t> *challenge = nullptr,
|
||||
const char *challenge = nullptr,
|
||||
const std::vector<uint8_t> *sig = nullptr) {
|
||||
auto challenge__ = challenge ? _fbb.CreateVector<uint8_t>(*challenge) : 0;
|
||||
auto challenge__ = challenge ? _fbb.CreateString(challenge) : 0;
|
||||
auto sig__ = sig ? _fbb.CreateVector<uint8_t>(*sig) : 0;
|
||||
return msg::fbuf::p2pmsg::CreatePeer_Challenge_Response_Message(
|
||||
_fbb,
|
||||
@@ -472,6 +491,7 @@ struct UserInputBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
UserInputBuilder &operator=(const UserInputBuilder &);
|
||||
flatbuffers::Offset<UserInput> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<UserInput>(end);
|
||||
@@ -548,6 +568,7 @@ struct UserInputGroupBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
UserInputGroupBuilder &operator=(const UserInputGroupBuilder &);
|
||||
flatbuffers::Offset<UserInputGroup> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<UserInputGroup>(end);
|
||||
@@ -707,6 +728,7 @@ struct ContentBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
ContentBuilder &operator=(const ContentBuilder &);
|
||||
flatbuffers::Offset<Content> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Content>(end);
|
||||
@@ -755,6 +777,7 @@ struct NonUnl_Proposal_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
NonUnl_Proposal_MessageBuilder &operator=(const NonUnl_Proposal_MessageBuilder &);
|
||||
flatbuffers::Offset<NonUnl_Proposal_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<NonUnl_Proposal_Message>(end);
|
||||
@@ -882,6 +905,7 @@ struct Proposal_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Proposal_MessageBuilder &operator=(const Proposal_MessageBuilder &);
|
||||
flatbuffers::Offset<Proposal_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Proposal_Message>(end);
|
||||
@@ -964,6 +988,7 @@ struct Npl_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Npl_MessageBuilder &operator=(const Npl_MessageBuilder &);
|
||||
flatbuffers::Offset<Npl_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Npl_Message>(end);
|
||||
@@ -1018,6 +1043,7 @@ struct History_Request_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
History_Request_MessageBuilder &operator=(const History_Request_MessageBuilder &);
|
||||
flatbuffers::Offset<History_Request_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<History_Request_Message>(end);
|
||||
@@ -1096,6 +1122,7 @@ struct History_Response_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
History_Response_MessageBuilder &operator=(const History_Response_MessageBuilder &);
|
||||
flatbuffers::Offset<History_Response_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<History_Response_Message>(end);
|
||||
@@ -1170,6 +1197,7 @@ struct HistoryLedgerBlockPairBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
HistoryLedgerBlockPairBuilder &operator=(const HistoryLedgerBlockPairBuilder &);
|
||||
flatbuffers::Offset<HistoryLedgerBlockPair> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<HistoryLedgerBlockPair>(end);
|
||||
@@ -1229,6 +1257,7 @@ struct HistoryLedgerBlockBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
HistoryLedgerBlockBuilder &operator=(const HistoryLedgerBlockBuilder &);
|
||||
flatbuffers::Offset<HistoryLedgerBlock> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<HistoryLedgerBlock>(end);
|
||||
@@ -1322,6 +1351,7 @@ struct State_Request_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
State_Request_MessageBuilder &operator=(const State_Request_MessageBuilder &);
|
||||
flatbuffers::Offset<State_Request_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<State_Request_Message>(end);
|
||||
@@ -1443,6 +1473,7 @@ struct State_Response_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
State_Response_MessageBuilder &operator=(const State_Response_MessageBuilder &);
|
||||
flatbuffers::Offset<State_Response_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<State_Response_Message>(end);
|
||||
@@ -1511,6 +1542,7 @@ struct Fs_Entry_ResponseBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Fs_Entry_ResponseBuilder &operator=(const Fs_Entry_ResponseBuilder &);
|
||||
flatbuffers::Offset<Fs_Entry_Response> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Fs_Entry_Response>(end);
|
||||
@@ -1576,6 +1608,7 @@ struct File_HashMap_ResponseBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
File_HashMap_ResponseBuilder &operator=(const File_HashMap_ResponseBuilder &);
|
||||
flatbuffers::Offset<File_HashMap_Response> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<File_HashMap_Response>(end);
|
||||
@@ -1645,6 +1678,7 @@ struct Block_ResponseBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Block_ResponseBuilder &operator=(const Block_ResponseBuilder &);
|
||||
flatbuffers::Offset<Block_Response> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Block_Response>(end);
|
||||
@@ -1726,6 +1760,7 @@ struct State_FS_Hash_EntryBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
State_FS_Hash_EntryBuilder &operator=(const State_FS_Hash_EntryBuilder &);
|
||||
flatbuffers::Offset<State_FS_Hash_Entry> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<State_FS_Hash_Entry>(end);
|
||||
@@ -1788,6 +1823,7 @@ struct Peer_Requirement_Announcement_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Peer_Requirement_Announcement_MessageBuilder &operator=(const Peer_Requirement_Announcement_MessageBuilder &);
|
||||
flatbuffers::Offset<Peer_Requirement_Announcement_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Peer_Requirement_Announcement_Message>(end);
|
||||
@@ -1843,6 +1879,7 @@ struct Available_Capacity_Announcement_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Available_Capacity_Announcement_MessageBuilder &operator=(const Available_Capacity_Announcement_MessageBuilder &);
|
||||
flatbuffers::Offset<Available_Capacity_Announcement_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Available_Capacity_Announcement_Message>(end);
|
||||
@@ -1876,6 +1913,7 @@ struct Peer_List_Request_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Peer_List_Request_MessageBuilder &operator=(const Peer_List_Request_MessageBuilder &);
|
||||
flatbuffers::Offset<Peer_List_Request_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Peer_List_Request_Message>(end);
|
||||
@@ -1920,6 +1958,7 @@ struct Peer_List_Response_MessageBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Peer_List_Response_MessageBuilder &operator=(const Peer_List_Response_MessageBuilder &);
|
||||
flatbuffers::Offset<Peer_List_Response_Message> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Peer_List_Response_Message>(end);
|
||||
@@ -2007,6 +2046,7 @@ struct Peer_PropertiesBuilder {
|
||||
: fbb_(_fbb) {
|
||||
start_ = fbb_.StartTable();
|
||||
}
|
||||
Peer_PropertiesBuilder &operator=(const Peer_PropertiesBuilder &);
|
||||
flatbuffers::Offset<Peer_Properties> Finish() {
|
||||
const auto end = fbb_.EndTable(start_);
|
||||
auto o = flatbuffers::Offset<Peer_Properties>(end);
|
||||
|
||||
@@ -154,11 +154,13 @@ namespace msg::fbuf::p2pmsg
|
||||
/**
|
||||
* Returns challenge from the peer challenge message.
|
||||
* @param The Flatbuffer peer challenge message received from the peer.
|
||||
* @return binary challenge.
|
||||
* @return Peer challenge struct.
|
||||
*/
|
||||
const std::string_view get_peer_challenge_from_msg(const Peer_Challenge_Message &msg)
|
||||
const p2p::peer_challenge get_peer_challenge_from_msg(const Peer_Challenge_Message &msg)
|
||||
{
|
||||
return flatbuff_bytes_to_sv(msg.challenge());
|
||||
return {
|
||||
std::string(flatbuff_str_to_sv(msg.contract_id())),
|
||||
std::string(flatbuff_str_to_sv(msg.challenge()))};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,7 +172,7 @@ namespace msg::fbuf::p2pmsg
|
||||
{
|
||||
p2p::peer_challenge_response pchalresp;
|
||||
|
||||
pchalresp.challenge = flatbuff_bytes_to_sv(msg.challenge());
|
||||
pchalresp.challenge = flatbuff_str_to_sv(msg.challenge());
|
||||
pchalresp.signature = flatbuff_bytes_to_sv(msg.sig());
|
||||
pchalresp.pubkey = flatbuff_bytes_to_sv(pubkey);
|
||||
|
||||
@@ -306,7 +308,8 @@ namespace msg::fbuf::p2pmsg
|
||||
const flatbuffers::Offset<Peer_Challenge_Message> peer_challenge_msg =
|
||||
CreatePeer_Challenge_Message(
|
||||
builder,
|
||||
sv_to_flatbuff_bytes(builder, challenge));
|
||||
sv_to_flatbuff_str(builder, conf::cfg.contractid),
|
||||
sv_to_flatbuff_str(builder, challenge));
|
||||
|
||||
const flatbuffers::Offset<Content> message = CreateContent(builder, Message_Peer_Challenge_Message, peer_challenge_msg.Union());
|
||||
builder.Finish(message); // Finished building message content to get serialised content.
|
||||
@@ -327,7 +330,7 @@ namespace msg::fbuf::p2pmsg
|
||||
const flatbuffers::Offset<Peer_Challenge_Response_Message> challenge_resp_msg =
|
||||
CreatePeer_Challenge_Response_Message(
|
||||
builder,
|
||||
sv_to_flatbuff_bytes(builder, challenge),
|
||||
sv_to_flatbuff_str(builder, challenge),
|
||||
sv_to_flatbuff_bytes(builder, crypto::sign(challenge, conf::cfg.seckey)));
|
||||
|
||||
const flatbuffers::Offset<Content> message = CreateContent(builder, Message_Peer_Challenge_Response_Message, challenge_resp_msg.Union());
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace msg::fbuf::p2pmsg
|
||||
|
||||
//---Message reading helpers---/
|
||||
|
||||
const std::string_view get_peer_challenge_from_msg(const Peer_Challenge_Message &msg);
|
||||
const p2p::peer_challenge get_peer_challenge_from_msg(const Peer_Challenge_Message &msg);
|
||||
|
||||
const p2p::peer_challenge_response create_peer_challenge_response_from_msg(const Peer_Challenge_Response_Message &msg, const flatbuffers::Vector<uint8_t> *pubkey);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "../../util/util.hpp"
|
||||
#include "../../crypto.hpp"
|
||||
#include "../../hplog.hpp"
|
||||
#include "../../conf.hpp"
|
||||
#include "../usrmsg_common.hpp"
|
||||
#include "usrmsg_json.hpp"
|
||||
|
||||
@@ -29,6 +30,7 @@ namespace msg::usrmsg::json
|
||||
* Message format:
|
||||
* {
|
||||
* "type": "handshake_challenge",
|
||||
* "contract_id": "<contract id>",
|
||||
* "challenge": "<hex challenge string>"
|
||||
* }
|
||||
* @param challengehex String reference to copy the generated hex challenge string into.
|
||||
@@ -48,14 +50,17 @@ namespace msg::usrmsg::json
|
||||
// We do not use jasoncons library here in favour of performance because this is a simple json message.
|
||||
|
||||
// Since we know the rough size of the challenge message we reserve adequate amount for the holder.
|
||||
// Only Hot Pocket version number is variable length. Therefore message size is roughly 90 bytes
|
||||
// so allocating 128bytes for heap padding.
|
||||
msg.reserve(128);
|
||||
// Only Hot Pocket version number is variable length.
|
||||
msg.reserve(256);
|
||||
msg += "{\"";
|
||||
msg += msg::usrmsg::FLD_TYPE;
|
||||
msg += SEP_COLON;
|
||||
msg += msg::usrmsg::MSGTYPE_HANDSHAKE_CHALLENGE;
|
||||
msg += SEP_COMMA;
|
||||
msg += msg::usrmsg::FLD_CONTRACT_ID;
|
||||
msg += SEP_COLON;
|
||||
msg += conf::cfg.contractid;
|
||||
msg += SEP_COMMA;
|
||||
msg += msg::usrmsg::FLD_CHALLENGE;
|
||||
msg += SEP_COLON;
|
||||
msg += challengehex;
|
||||
@@ -74,7 +79,7 @@ namespace msg::usrmsg::json
|
||||
*/
|
||||
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl)
|
||||
{
|
||||
msg.reserve(128);
|
||||
msg.reserve(256);
|
||||
msg += "{\"";
|
||||
msg += msg::usrmsg::FLD_TYPE;
|
||||
msg += SEP_COLON;
|
||||
@@ -109,7 +114,7 @@ namespace msg::usrmsg::json
|
||||
std::string sighex;
|
||||
util::bin2hex(sighex, reinterpret_cast<const unsigned char *>(input_sig.data()), input_sig.length());
|
||||
|
||||
msg.reserve(128);
|
||||
msg.reserve(256);
|
||||
msg += "{\"";
|
||||
msg += msg::usrmsg::FLD_TYPE;
|
||||
msg += SEP_COLON;
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace msg::usrmsg
|
||||
|
||||
// Message field names
|
||||
constexpr const char *FLD_TYPE = "type";
|
||||
constexpr const char *FLD_CONTRACT_ID = "contract_id";
|
||||
constexpr const char *FLD_CHALLENGE = "challenge";
|
||||
constexpr const char *FLD_SIG = "sig";
|
||||
constexpr const char *FLD_PUBKEY = "pubkey";
|
||||
|
||||
@@ -50,6 +50,12 @@ namespace p2p
|
||||
std::vector<uint8_t> block_buffer;
|
||||
};
|
||||
|
||||
struct peer_challenge
|
||||
{
|
||||
std::string contract_id;
|
||||
std::string challenge;
|
||||
};
|
||||
|
||||
struct peer_challenge_response
|
||||
{
|
||||
std::string challenge;
|
||||
|
||||
@@ -45,8 +45,10 @@ namespace p2p
|
||||
return 0;
|
||||
}
|
||||
|
||||
// peer session on message callback method.
|
||||
// validate and handle each type of peer messages.
|
||||
/**
|
||||
* Peer session on message callback method. Validate and handle each type of peer messages.
|
||||
* @return 0 on normal execution. -1 when session needs to be closed as a result of message handling.
|
||||
*/
|
||||
int handle_peer_message(p2p::peer_comm_session &session, std::string_view message)
|
||||
{
|
||||
// Adding message size to peer message characters(bytes) per minute counter.
|
||||
@@ -95,10 +97,15 @@ namespace p2p
|
||||
|
||||
if (content_message_type == p2pmsg::Message_Peer_Challenge_Message) // message is a peer challenge announcement
|
||||
{
|
||||
// Sending the challenge response to the respected peer.
|
||||
const std::string challenge = std::string(p2pmsg::get_peer_challenge_from_msg(*content->message_as_Peer_Challenge_Message()));
|
||||
const p2p::peer_challenge chall = p2pmsg::get_peer_challenge_from_msg(*content->message_as_Peer_Challenge_Message());
|
||||
|
||||
// Check whether contract ids match.
|
||||
if (chall.contract_id != conf::cfg.contractid)
|
||||
return -1;
|
||||
|
||||
// Sending the challenge response to the sender.
|
||||
flatbuffers::FlatBufferBuilder fbuf(1024);
|
||||
p2pmsg::create_peer_challenge_response_from_challenge(fbuf, challenge);
|
||||
p2pmsg::create_peer_challenge_response_from_challenge(fbuf, chall.challenge);
|
||||
std::string_view msg = std::string_view(
|
||||
reinterpret_cast<const char *>(fbuf.GetBufferPointer()), fbuf.GetSize());
|
||||
return session.send(msg);
|
||||
@@ -216,7 +223,7 @@ namespace p2p
|
||||
{
|
||||
// Check the cap and insert request with lock.
|
||||
std::scoped_lock<std::mutex> lock(ledger::sync_ctx.list_mutex);
|
||||
|
||||
|
||||
// If max number of history requests reached skip the rest.
|
||||
if (ledger::sync_ctx.collected_history_requests.size() < ledger::HISTORY_REQ_LIST_CAP)
|
||||
{
|
||||
@@ -303,7 +310,7 @@ namespace p2p
|
||||
{
|
||||
// Check the cap and insert proposal with lock.
|
||||
std::scoped_lock<std::mutex> lock(ctx.collected_msgs.proposals_mutex);
|
||||
|
||||
|
||||
// If max number of proposals reached skip the rest.
|
||||
if (ctx.collected_msgs.proposals.size() == p2p::PROPOSAL_LIST_CAP)
|
||||
return -1;
|
||||
@@ -331,7 +338,7 @@ namespace p2p
|
||||
|
||||
ctx.collected_msgs.nonunl_proposals.push_back(
|
||||
p2pmsg::create_nonunl_proposal_from_msg(*content->message_as_NonUnl_Proposal_Message(), container->timestamp()));
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ do
|
||||
|
||||
# Update contract config.
|
||||
node -p "JSON.stringify({...require('./tmp.json'), \
|
||||
contractid: 'dummy', \
|
||||
binary: '$binary', \
|
||||
binargs: '$binargs', \
|
||||
appbill: '', \
|
||||
|
||||
@@ -346,6 +346,7 @@ do
|
||||
|
||||
# Merge json contents to produce final contract config.
|
||||
echo "$(cat ./cfg/node$n.cfg)" \
|
||||
'{"contractid":"dummy"}' \
|
||||
'{"binary":"/usr/bin/node"}' \
|
||||
'{"binargs":"'$basedir'/hpfiles/nodejs_contract/echo_contract.js"}' \
|
||||
'{"peers":'${mypeers}'}' \
|
||||
|
||||
Reference in New Issue
Block a user