Ledger maintenance refactor. (#130)

* Added ledger namespace.
* Thread-safe lcl access and update.
* Refactored history sync and serving into a thread.
* Restructured ledger cache item.
This commit is contained in:
Ravin Perera
2020-10-08 22:25:47 +05:30
committed by GitHub
parent 31048f55b8
commit cb4d0c4f59
30 changed files with 945 additions and 754 deletions

View File

@@ -1,6 +1,5 @@
#include "../../pchheader.hpp"
#include "../../util.hpp"
#include "../../cons/cons.hpp"
#include "../../hplog.hpp"
#include "../usrmsg_common.hpp"
#include "usrmsg_bson.hpp"
@@ -17,16 +16,16 @@ namespace msg::usrmsg::bson
* "lcl_seqno": <integer>
* }
*/
void create_status_response(std::vector<uint8_t> &msg)
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl)
{
jsoncons::bson::bson_bytes_encoder encoder(msg);
encoder.begin_object();
encoder.key(msg::usrmsg::FLD_TYPE);
encoder.string_value(msg::usrmsg::MSGTYPE_STAT_RESPONSE);
encoder.key(msg::usrmsg::FLD_LCL);
encoder.string_value(cons::ctx.lcl);
encoder.string_value(lcl);
encoder.key(msg::usrmsg::FLD_LCL_SEQ);
encoder.int64_value(cons::ctx.led_seq_no);
encoder.int64_value(lcl_seq_no);
encoder.end_object();
encoder.flush();
}
@@ -95,16 +94,16 @@ namespace msg::usrmsg::bson
* }
* @param content The contract binary output content to be put in the message.
*/
void create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content)
void create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content, const uint64_t lcl_seq_no, std::string_view lcl)
{
jsoncons::bson::bson_bytes_encoder encoder(msg);
encoder.begin_object();
encoder.key(msg::usrmsg::FLD_TYPE);
encoder.string_value(msg::usrmsg::MSGTYPE_CONTRACT_OUTPUT);
encoder.key(msg::usrmsg::FLD_LCL);
encoder.string_value(cons::ctx.lcl);
encoder.string_value(lcl);
encoder.key(msg::usrmsg::FLD_LCL_SEQ);
encoder.int64_value(cons::ctx.led_seq_no);
encoder.int64_value(lcl_seq_no);
encoder.key(msg::usrmsg::FLD_CONTENT);
encoder.byte_string_value(content);
encoder.end_object();

View File

@@ -8,14 +8,14 @@ namespace msg::usrmsg::bson
void create_user_challenge(std::vector<uint8_t> &msg, std::string &challengehex);
void create_status_response(std::vector<uint8_t> &msg);
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl);
void create_contract_input_status(std::vector<uint8_t> &msg, std::string_view status, std::string_view reason,
std::string_view input_sig);
void create_contract_read_response_container(std::vector<uint8_t> &msg, std::string_view content);
void create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content);
void create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content, const uint64_t lcl_seq_no, std::string_view lcl);
int verify_user_handshake_response(std::string &extracted_pubkeyhex, std::string &extracted_protocol,
std::string_view response, std::string_view original_challenge);

View File

@@ -7,25 +7,30 @@
namespace msg::fbuf::ledger
{
/**
/**
* Create ledger from the given proposal struct.
* @param p The proposal struct to be placed in ledger.
*/
const std::string_view create_ledger_from_proposal(flatbuffers::FlatBufferBuilder &builder, const p2p::proposal &p, const uint64_t seq_no)
{
flatbuffers::Offset<ledger::Ledger> ledger =
ledger::CreateLedger(
builder,
seq_no,
p.time,
sv_to_flatbuff_bytes(builder, p.lcl),
sv_to_flatbuff_bytes(builder, p.state.to_string_view()),
stringlist_to_flatbuf_bytearrayvector(builder, p.users),
stringlist_to_flatbuf_bytearrayvector(builder, p.hash_inputs),
stringlist_to_flatbuf_bytearrayvector(builder, p.hash_outputs));
void create_ledger_from_proposal(flatbuffers::FlatBufferBuilder &builder, const p2p::proposal &p, const uint64_t seq_no)
{
flatbuffers::Offset<ledger::Ledger> ledger =
ledger::CreateLedger(
builder,
seq_no,
p.time,
sv_to_flatbuff_bytes(builder, p.lcl),
sv_to_flatbuff_bytes(builder, p.state.to_string_view()),
stringlist_to_flatbuf_bytearrayvector(builder, p.users),
stringlist_to_flatbuf_bytearrayvector(builder, p.hash_inputs),
stringlist_to_flatbuf_bytearrayvector(builder, p.hash_outputs));
builder.Finish(ledger); // Finished building message content to get serialised content.
builder.Finish(ledger); // Finished building message content to get serialised content.
}
bool verify_ledger_buffer(const uint8_t *ledger_buf_ptr, const size_t buf_len)
{
flatbuffers::Verifier ledger_verifier(ledger_buf_ptr, buf_len);
return VerifyLedgerBuffer(ledger_verifier);
}
return flatbuff_bytes_to_sv(builder.GetBufferPointer(), builder.GetSize());
}
} // namespace msg::fbuf::ledger

View File

@@ -8,7 +8,10 @@
namespace msg::fbuf::ledger
{
const std::string_view create_ledger_from_proposal(flatbuffers::FlatBufferBuilder &builder, const p2p::proposal &p, const uint64_t seq_no);
}
void create_ledger_from_proposal(flatbuffers::FlatBufferBuilder &builder, const p2p::proposal &p, const uint64_t seq_no);
bool verify_ledger_buffer(const uint8_t *ledger_buf_ptr, const size_t buf_len);
} // namespace msg::fbuf::ledger
#endif

View File

@@ -60,6 +60,7 @@ enum Ledger_Response_Error : ubyte
}
table History_Response_Message { //Ledger History request type message schema
requester_lcl:[ubyte];
hist_ledgers:[HistoryLedgerPair];
error: Ledger_Response_Error;
}
@@ -70,7 +71,6 @@ table HistoryLedgerPair { //A key, value pair of byte[].
}
table HistoryLedger {
state:[ubyte];
lcl:[ubyte];
raw_ledger:[ubyte];
}

View File

@@ -974,9 +974,16 @@ inline flatbuffers::Offset<History_Request_Message> CreateHistory_Request_Messag
struct History_Response_Message FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef History_Response_MessageBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_HIST_LEDGERS = 4,
VT_ERROR = 6
VT_REQUESTER_LCL = 4,
VT_HIST_LEDGERS = 6,
VT_ERROR = 8
};
const flatbuffers::Vector<uint8_t> *requester_lcl() const {
return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_REQUESTER_LCL);
}
flatbuffers::Vector<uint8_t> *mutable_requester_lcl() {
return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_REQUESTER_LCL);
}
const flatbuffers::Vector<flatbuffers::Offset<msg::fbuf::p2pmsg::HistoryLedgerPair>> *hist_ledgers() const {
return GetPointer<const flatbuffers::Vector<flatbuffers::Offset<msg::fbuf::p2pmsg::HistoryLedgerPair>> *>(VT_HIST_LEDGERS);
}
@@ -991,6 +998,8 @@ struct History_Response_Message FLATBUFFERS_FINAL_CLASS : private flatbuffers::T
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffset(verifier, VT_REQUESTER_LCL) &&
verifier.VerifyVector(requester_lcl()) &&
VerifyOffset(verifier, VT_HIST_LEDGERS) &&
verifier.VerifyVector(hist_ledgers()) &&
verifier.VerifyVectorOfTables(hist_ledgers()) &&
@@ -1003,6 +1012,9 @@ struct History_Response_MessageBuilder {
typedef History_Response_Message Table;
flatbuffers::FlatBufferBuilder &fbb_;
flatbuffers::uoffset_t start_;
void add_requester_lcl(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> requester_lcl) {
fbb_.AddOffset(History_Response_Message::VT_REQUESTER_LCL, requester_lcl);
}
void add_hist_ledgers(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<msg::fbuf::p2pmsg::HistoryLedgerPair>>> hist_ledgers) {
fbb_.AddOffset(History_Response_Message::VT_HIST_LEDGERS, hist_ledgers);
}
@@ -1022,21 +1034,26 @@ struct History_Response_MessageBuilder {
inline flatbuffers::Offset<History_Response_Message> CreateHistory_Response_Message(
flatbuffers::FlatBufferBuilder &_fbb,
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> requester_lcl = 0,
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<msg::fbuf::p2pmsg::HistoryLedgerPair>>> hist_ledgers = 0,
msg::fbuf::p2pmsg::Ledger_Response_Error error = msg::fbuf::p2pmsg::Ledger_Response_Error_None) {
History_Response_MessageBuilder builder_(_fbb);
builder_.add_hist_ledgers(hist_ledgers);
builder_.add_requester_lcl(requester_lcl);
builder_.add_error(error);
return builder_.Finish();
}
inline flatbuffers::Offset<History_Response_Message> CreateHistory_Response_MessageDirect(
flatbuffers::FlatBufferBuilder &_fbb,
const std::vector<uint8_t> *requester_lcl = nullptr,
const std::vector<flatbuffers::Offset<msg::fbuf::p2pmsg::HistoryLedgerPair>> *hist_ledgers = nullptr,
msg::fbuf::p2pmsg::Ledger_Response_Error error = msg::fbuf::p2pmsg::Ledger_Response_Error_None) {
auto requester_lcl__ = requester_lcl ? _fbb.CreateVector<uint8_t>(*requester_lcl) : 0;
auto hist_ledgers__ = hist_ledgers ? _fbb.CreateVector<flatbuffers::Offset<msg::fbuf::p2pmsg::HistoryLedgerPair>>(*hist_ledgers) : 0;
return msg::fbuf::p2pmsg::CreateHistory_Response_Message(
_fbb,
requester_lcl__,
hist_ledgers__,
error);
}
@@ -1102,16 +1119,9 @@ inline flatbuffers::Offset<HistoryLedgerPair> CreateHistoryLedgerPair(
struct HistoryLedger FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef HistoryLedgerBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_STATE = 4,
VT_LCL = 6,
VT_RAW_LEDGER = 8
VT_LCL = 4,
VT_RAW_LEDGER = 6
};
const flatbuffers::Vector<uint8_t> *state() const {
return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_STATE);
}
flatbuffers::Vector<uint8_t> *mutable_state() {
return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_STATE);
}
const flatbuffers::Vector<uint8_t> *lcl() const {
return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_LCL);
}
@@ -1126,8 +1136,6 @@ struct HistoryLedger FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffset(verifier, VT_STATE) &&
verifier.VerifyVector(state()) &&
VerifyOffset(verifier, VT_LCL) &&
verifier.VerifyVector(lcl()) &&
VerifyOffset(verifier, VT_RAW_LEDGER) &&
@@ -1140,9 +1148,6 @@ struct HistoryLedgerBuilder {
typedef HistoryLedger Table;
flatbuffers::FlatBufferBuilder &fbb_;
flatbuffers::uoffset_t start_;
void add_state(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> state) {
fbb_.AddOffset(HistoryLedger::VT_STATE, state);
}
void add_lcl(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> lcl) {
fbb_.AddOffset(HistoryLedger::VT_LCL, lcl);
}
@@ -1162,27 +1167,22 @@ struct HistoryLedgerBuilder {
inline flatbuffers::Offset<HistoryLedger> CreateHistoryLedger(
flatbuffers::FlatBufferBuilder &_fbb,
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> state = 0,
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> lcl = 0,
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> raw_ledger = 0) {
HistoryLedgerBuilder builder_(_fbb);
builder_.add_raw_ledger(raw_ledger);
builder_.add_lcl(lcl);
builder_.add_state(state);
return builder_.Finish();
}
inline flatbuffers::Offset<HistoryLedger> CreateHistoryLedgerDirect(
flatbuffers::FlatBufferBuilder &_fbb,
const std::vector<uint8_t> *state = nullptr,
const std::vector<uint8_t> *lcl = nullptr,
const std::vector<uint8_t> *raw_ledger = nullptr) {
auto state__ = state ? _fbb.CreateVector<uint8_t>(*state) : 0;
auto lcl__ = lcl ? _fbb.CreateVector<uint8_t>(*lcl) : 0;
auto raw_ledger__ = raw_ledger ? _fbb.CreateVector<uint8_t>(*raw_ledger) : 0;
return msg::fbuf::p2pmsg::CreateHistoryLedger(
_fbb,
state__,
lcl__,
raw_ledger__);
}

View File

@@ -201,6 +201,9 @@ namespace msg::fbuf::p2pmsg
{
p2p::history_response hr;
if (msg.requester_lcl())
hr.requester_lcl = flatbuff_bytes_to_sv(msg.requester_lcl());
if (msg.hist_ledgers())
hr.hist_ledgers = flatbuf_historyledgermap_to_historyledgermap(msg.hist_ledgers());
@@ -427,6 +430,7 @@ namespace msg::fbuf::p2pmsg
flatbuffers::Offset<History_Response_Message> hrmsg =
CreateHistory_Response_Message(
builder,
sv_to_flatbuff_bytes(builder, hr.requester_lcl),
historyledgermap_to_flatbuf_historyledgermap(builder, hr.hist_ledgers),
(Ledger_Response_Error)hr.error);
@@ -692,7 +696,6 @@ namespace msg::fbuf::p2pmsg
{
flatbuffers::Offset<HistoryLedger> history_ledger = CreateHistoryLedger(
builder,
sv_to_flatbuff_bytes(builder, ledger.state),
sv_to_flatbuff_bytes(builder, ledger.lcl),
builder.CreateVector(ledger.raw_ledger));

View File

@@ -1,7 +1,6 @@
#include "../../pchheader.hpp"
#include "../../util.hpp"
#include "../../crypto.hpp"
#include "../../cons/cons.hpp"
#include "../../hplog.hpp"
#include "../usrmsg_common.hpp"
#include "usrmsg_json.hpp"
@@ -73,7 +72,7 @@ namespace msg::usrmsg::json
* "lcl_seqno": <integer>
* }
*/
void create_status_response(std::vector<uint8_t> &msg)
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl)
{
msg.reserve(128);
msg += "{\"";
@@ -83,11 +82,11 @@ namespace msg::usrmsg::json
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_LCL;
msg += SEP_COLON;
msg += cons::ctx.lcl;
msg += lcl;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_LCL_SEQ;
msg += SEP_COLON_NOQUOTE;
msg += std::to_string(cons::ctx.led_seq_no);
msg += std::to_string(lcl_seq_no);
msg += "}";
}
@@ -172,7 +171,7 @@ namespace msg::usrmsg::json
* }
* @param content The contract binary output content to be put in the message.
*/
void create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content)
void create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content, const uint64_t lcl_seq_no, std::string_view lcl)
{
std::string contenthex;
util::bin2hex(
@@ -188,11 +187,11 @@ namespace msg::usrmsg::json
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_LCL;
msg += SEP_COLON;
msg += cons::ctx.lcl;
msg += lcl;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_LCL_SEQ;
msg += SEP_COLON_NOQUOTE;
msg += std::to_string(cons::ctx.led_seq_no);
msg += std::to_string(lcl_seq_no);
msg += SEP_COMMA_NOQUOTE;
msg += msg::usrmsg::FLD_CONTENT;
msg += SEP_COLON;

View File

@@ -8,14 +8,14 @@ namespace msg::usrmsg::json
void create_user_challenge(std::vector<uint8_t> &msg, std::string &challengehex);
void create_status_response(std::vector<uint8_t> &msg);
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl);
void create_contract_input_status(std::vector<uint8_t> &msg, std::string_view status, std::string_view reason,
std::string_view input_sig);
void create_contract_read_response_container(std::vector<uint8_t> &msg, std::string_view content);
void create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content);
void create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content, const uint64_t lcl_seq_no, std::string_view lcl);
int verify_user_handshake_response(std::string &extracted_pubkeyhex, std::string &extracted_protocol,
std::string_view response, std::string_view original_challenge);

View File

@@ -13,12 +13,12 @@ namespace msg::usrmsg
{
}
void usrmsg_parser::create_status_response(std::vector<uint8_t> &msg) const
void usrmsg_parser::create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl) const
{
if (protocol == util::PROTOCOL::JSON)
jusrmsg::create_status_response(msg);
jusrmsg::create_status_response(msg, lcl_seq_no, lcl);
else
busrmsg::create_status_response(msg);
busrmsg::create_status_response(msg, lcl_seq_no, lcl);
}
void usrmsg_parser::create_contract_input_status(std::vector<uint8_t> &msg, std::string_view status,
@@ -38,12 +38,12 @@ namespace msg::usrmsg
busrmsg::create_contract_read_response_container(msg, content);
}
void usrmsg_parser::create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content) const
void usrmsg_parser::create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content, const uint64_t lcl_seq_no, std::string_view lcl) const
{
if (protocol == util::PROTOCOL::JSON)
jusrmsg::create_contract_output_container(msg, content);
jusrmsg::create_contract_output_container(msg, content, lcl_seq_no, lcl);
else
busrmsg::create_contract_output_container(msg, content);
busrmsg::create_contract_output_container(msg, content, lcl_seq_no, lcl);
}
int usrmsg_parser::parse(std::string_view message)

View File

@@ -18,14 +18,14 @@ namespace msg::usrmsg
public:
usrmsg_parser(const util::PROTOCOL protocol);
void create_status_response(std::vector<uint8_t> &msg) const;
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl) const;
void create_contract_input_status(std::vector<uint8_t> &msg, std::string_view status,
std::string_view reason, std::string_view input_sig) const;
void create_contract_read_response_container(std::vector<uint8_t> &msg, std::string_view content) const;
void create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content) const;
void create_contract_output_container(std::vector<uint8_t> &msg, std::string_view content, const uint64_t lcl_seq_no, std::string_view lcl) const;
int parse(std::string_view message);