Separated lcl string usage to sequence no and hash. (#251)

This commit is contained in:
Ravin Perera
2021-02-18 18:28:38 +05:30
committed by GitHub
parent 8eac87fb85
commit 6b8d60a404
21 changed files with 116 additions and 111 deletions

View File

@@ -12,20 +12,20 @@ namespace msg::usrmsg::bson
* Message format:
* {
* "type": "stat_response",
* "lcl": "<lcl id>",
* "lcl_seqno": <integer>
* "lcl_seq_no": <lcl sequence no>,
* "lcl_hash": <binary lcl hash>
* }
*/
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl)
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl_hash)
{
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(lcl);
encoder.key(msg::usrmsg::FLD_LCL_SEQ);
encoder.int64_value(lcl_seq_no);
encoder.key(msg::usrmsg::FLD_LCL_HASH);
encoder.byte_string_value(lcl_hash);
encoder.end_object();
encoder.flush();
}
@@ -88,8 +88,8 @@ namespace msg::usrmsg::bson
* Message format:
* {
* "type": "contract_output",
* "lcl": "<lcl id>"
* "lcl_seqno": <integer>,
* "lcl_seq_no": <integer>,
* "lcl_hash": <binary lcl hash>
* "outputs": [<binary output 1>, <binary output 2>, ...], // The output order is the hash order.
* "hashes": [<binary merkle hash tree>], // Always includes user's output hash [output hash = hash(pubkey+all outputs for the user)]
* "unl_sig": [["<pubkey>", "<sig>"], ...] // Binary UNL pubkeys and signatures of root hash.
@@ -98,16 +98,16 @@ namespace msg::usrmsg::bson
*/
void create_contract_output_container(std::vector<uint8_t> &msg, const ::std::vector<std::string_view> &outputs,
const util::merkle_hash_node &hash_root, const std::vector<std::pair<std::string, std::string>> &unl_sig,
const uint64_t lcl_seq_no, std::string_view lcl)
const uint64_t lcl_seq_no, std::string_view lcl_hash)
{
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(lcl);
encoder.key(msg::usrmsg::FLD_LCL_SEQ);
encoder.int64_value(lcl_seq_no);
encoder.key(msg::usrmsg::FLD_LCL_HASH);
encoder.byte_string_value(lcl_hash);
encoder.key(msg::usrmsg::FLD_OUTPUTS);
encoder.begin_array();
@@ -261,16 +261,16 @@ namespace msg::usrmsg::bson
* Extract the individual components of a given input container bson.
* @param input The extracted input.
* @param nonce The extracted nonce.
* @param max_lcl_seqno The extracted max ledger sequence no.
* @param max_lcl_seq_no The extracted max ledger sequence no.
* @param contentjson The bson input container message.
* {
* "input": <binary buffer>,
* "nonce": "<random string with optional sorted order>",
* "max_lcl_seqno": <integer>
* "max_lcl_seq_no": <integer>
* }
* @return 0 on succesful extraction. -1 on failure.
*/
int extract_input_container(std::string &input, std::string &nonce, uint64_t &max_lcl_seqno, std::string_view contentbson)
int extract_input_container(std::string &input, std::string &nonce, uint64_t &max_lcl_seq_no, std::string_view contentbson)
{
jsoncons::ojson d;
try
@@ -299,7 +299,7 @@ namespace msg::usrmsg::bson
input = std::string_view(reinterpret_cast<const char *>(bsv.data()), bsv.size());
nonce = d[msg::usrmsg::FLD_NONCE].as<std::string>();
max_lcl_seqno = d[msg::usrmsg::FLD_MAX_LCL_SEQ].as<uint64_t>();
max_lcl_seq_no = d[msg::usrmsg::FLD_MAX_LCL_SEQ].as<uint64_t>();
return 0;
}

View File

@@ -7,7 +7,7 @@
namespace msg::usrmsg::bson
{
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl);
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl_hash);
void create_contract_input_status(std::vector<uint8_t> &msg, std::string_view status, std::string_view reason,
std::string_view input_sig);
@@ -16,7 +16,7 @@ namespace msg::usrmsg::bson
void create_contract_output_container(std::vector<uint8_t> &msg, const ::std::vector<std::string_view> &outputs,
const util::merkle_hash_node &hash_root, const std::vector<std::pair<std::string, std::string>> &unl_sig,
const uint64_t lcl_seq_no, std::string_view lcl);
const uint64_t lcl_seq_no, std::string_view lcl_hash);
void create_unl_list_container(std::vector<uint8_t> &msg, const ::std::set<std::string> &unl_list);
@@ -33,7 +33,7 @@ namespace msg::usrmsg::bson
const jsoncons::ojson &d);
int extract_input_container(std::string &input, std::string &nonce,
uint64_t &max_lcl_seqno, std::string_view contentbson);
uint64_t &max_lcl_seq_no, std::string_view contentbson);
void populate_output_hash_array(jsoncons::bson::bson_bytes_encoder &encoder, const util::merkle_hash_node &node);

View File

@@ -130,11 +130,11 @@ namespace msg::usrmsg::json
* Message format:
* {
* "type": "stat_response",
* "lcl": "<lcl id>",
* "lcl_seqno": <integer>
* "lcl_seq_no": <lcl sequence no>,
* "lcl_hash": "<lcl hash hex>"
* }
*/
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl)
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl_hash)
{
msg.reserve(256);
msg += "{\"";
@@ -142,14 +142,14 @@ namespace msg::usrmsg::json
msg += SEP_COLON;
msg += msg::usrmsg::MSGTYPE_STAT_RESPONSE;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_LCL;
msg += SEP_COLON;
msg += lcl;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_LCL_SEQ;
msg += SEP_COLON_NOQUOTE;
msg += std::to_string(lcl_seq_no);
msg += "}";
msg += SEP_COMMA_NOQUOTE;
msg += msg::usrmsg::FLD_LCL_HASH;
msg += SEP_COLON;
msg += util::to_hex(lcl_hash);
msg += "\"}";
}
/**
@@ -235,8 +235,8 @@ namespace msg::usrmsg::json
* Message format:
* {
* "type": "contract_output",
* "lcl": "<lcl id>"
* "lcl_seqno": <integer>,
* "lcl_seq_no": <integer>,
* "lcl_hash": "<lcl hash hex>",
* "outputs": ["<output string 1>", "<output string 2>", ...], // The output order is the hash order.
* "hashes": [<hex merkle hash tree>], // Always includes user's output hash [output hash = hash(pubkey+all outputs for the user)]
* "unl_sig": [["<pubkey hex>", "<sig hex>"], ...] // UNL pubkeys and signatures of root hash.
@@ -245,7 +245,7 @@ namespace msg::usrmsg::json
*/
void create_contract_output_container(std::vector<uint8_t> &msg, const ::std::vector<std::string_view> &outputs,
const util::merkle_hash_node &hash_root, const std::vector<std::pair<std::string, std::string>> &unl_sig,
const uint64_t lcl_seq_no, std::string_view lcl)
const uint64_t lcl_seq_no, std::string_view lcl_hash)
{
msg.reserve(1024);
msg += "{\"";
@@ -253,14 +253,14 @@ namespace msg::usrmsg::json
msg += SEP_COLON;
msg += msg::usrmsg::MSGTYPE_CONTRACT_OUTPUT;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_LCL;
msg += SEP_COLON;
msg += lcl;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_LCL_SEQ;
msg += SEP_COLON_NOQUOTE;
msg += std::to_string(lcl_seq_no);
msg += SEP_COMMA_NOQUOTE;
msg += msg::usrmsg::FLD_LCL_HASH;
msg += SEP_COLON;
msg += util::to_hex(lcl_hash);
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_OUTPUTS;
msg += "\":[";
@@ -568,16 +568,16 @@ namespace msg::usrmsg::json
* Extract the individual components of a given input container json.
* @param input The extracted input.
* @param nonce The extracted nonce.
* @param max_lcl_seqno The extracted max ledger sequence no.
* @param max_lcl_seq_no The extracted max ledger sequence no.
* @param contentjson The json string containing the input container message.
* {
* "input": "<any string>",
* "nonce": "<random string with optional sorted order>",
* "max_lcl_seqno": <integer>
* "max_lcl_seq_no": <integer>
* }
* @return 0 on succesful extraction. -1 on failure.
*/
int extract_input_container(std::string &input, std::string &nonce, uint64_t &max_lcl_seqno, std::string_view contentjson)
int extract_input_container(std::string &input, std::string &nonce, uint64_t &max_lcl_seq_no, std::string_view contentjson)
{
jsoncons::json d;
try
@@ -604,7 +604,7 @@ namespace msg::usrmsg::json
input = d[msg::usrmsg::FLD_INPUT].as<std::string>();
nonce = d[msg::usrmsg::FLD_NONCE].as<std::string>();
max_lcl_seqno = d[msg::usrmsg::FLD_MAX_LCL_SEQ].as<uint64_t>();
max_lcl_seq_no = d[msg::usrmsg::FLD_MAX_LCL_SEQ].as<uint64_t>();
return 0;
}

View File

@@ -11,7 +11,7 @@ namespace msg::usrmsg::json
void create_server_challenge_response(std::vector<uint8_t> &msg, const std::string &original_challenge);
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl);
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl_hash);
void create_contract_input_status(std::vector<uint8_t> &msg, std::string_view status, std::string_view reason,
std::string_view input_sig);
@@ -20,7 +20,7 @@ namespace msg::usrmsg::json
void create_contract_output_container(std::vector<uint8_t> &msg, const ::std::vector<std::string_view> &outputs,
const util::merkle_hash_node &hash_root, const std::vector<std::pair<std::string, std::string>> &unl_sig,
const uint64_t lcl_seq_no, std::string_view lcl);
const uint64_t lcl_seq_no, std::string_view lcl_hash);
void create_unl_list_container(std::vector<uint8_t> &msg, const ::std::set<std::string> &unl_list);
@@ -37,7 +37,7 @@ namespace msg::usrmsg::json
const jsoncons::json &d);
int extract_input_container(std::string &input, std::string &nonce,
uint64_t &max_lcl_seqno, std::string_view contentjson);
uint64_t &max_lcl_seq_no, std::string_view contentjson);
bool is_json_string(std::string_view content);

View File

@@ -23,14 +23,14 @@ namespace msg::usrmsg
constexpr const char *FLD_INPUT = "input";
constexpr const char *FLD_INPUT_CONTAINER = "input_container";
constexpr const char *FLD_INPUT_SIG = "input_sig";
constexpr const char *FLD_MAX_LCL_SEQ = "max_lcl_seqno";
constexpr const char *FLD_MAX_LCL_SEQ = "max_lcl_seq_no";
constexpr const char *FLD_CONTENT = "content";
constexpr const char *FLD_OUTPUTS = "outputs";
constexpr const char *FLD_HASHES = "hashes";
constexpr const char *FLD_UNL_SIG = "unl_sig";
constexpr const char *FLD_NONCE = "nonce";
constexpr const char *FLD_LCL = "lcl";
constexpr const char *FLD_LCL_SEQ = "lcl_seqno";
constexpr const char *FLD_LCL_HASH = "lcl_hash";
constexpr const char *FLD_LCL_SEQ = "lcl_seq_no";
constexpr const char *FLD_STATUS = "status";
constexpr const char *FLD_REASON = "reason";

View File

@@ -13,12 +13,12 @@ namespace msg::usrmsg
{
}
void usrmsg_parser::create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl) const
void usrmsg_parser::create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl_hash) const
{
if (protocol == util::PROTOCOL::JSON)
jusrmsg::create_status_response(msg, lcl_seq_no, lcl);
jusrmsg::create_status_response(msg, lcl_seq_no, lcl_hash);
else
busrmsg::create_status_response(msg, lcl_seq_no, lcl);
busrmsg::create_status_response(msg, lcl_seq_no, lcl_hash);
}
void usrmsg_parser::create_contract_input_status(std::vector<uint8_t> &msg, std::string_view status,
@@ -40,12 +40,12 @@ namespace msg::usrmsg
void usrmsg_parser::create_contract_output_container(std::vector<uint8_t> &msg, const ::std::vector<std::string_view> &outputs,
const util::merkle_hash_node &hash_root, const std::vector<std::pair<std::string, std::string>> &unl_sig,
const uint64_t lcl_seq_no, std::string_view lcl) const
const uint64_t lcl_seq_no, std::string_view lcl_hash) const
{
if (protocol == util::PROTOCOL::JSON)
jusrmsg::create_contract_output_container(msg, outputs, hash_root, unl_sig, lcl_seq_no, lcl);
jusrmsg::create_contract_output_container(msg, outputs, hash_root, unl_sig, lcl_seq_no, lcl_hash);
else
busrmsg::create_contract_output_container(msg, outputs, hash_root, unl_sig, lcl_seq_no, lcl);
busrmsg::create_contract_output_container(msg, outputs, hash_root, unl_sig, lcl_seq_no, lcl_hash);
}
void usrmsg_parser::create_unl_list_container(std::vector<uint8_t> &msg, const ::std::set<std::string> &unl_list) const
@@ -89,12 +89,12 @@ namespace msg::usrmsg
}
int usrmsg_parser::extract_input_container(std::string &input, std::string &nonce,
uint64_t &max_lcl_seqno, std::string_view encoded_content) const
uint64_t &max_lcl_seq_no, std::string_view encoded_content) const
{
if (protocol == util::PROTOCOL::JSON)
return jusrmsg::extract_input_container(input, nonce, max_lcl_seqno, encoded_content);
return jusrmsg::extract_input_container(input, nonce, max_lcl_seq_no, encoded_content);
else
return busrmsg::extract_input_container(input, nonce, max_lcl_seqno, encoded_content);
return busrmsg::extract_input_container(input, nonce, max_lcl_seq_no, encoded_content);
}
} // namespace msg::usrmsg

View File

@@ -19,7 +19,7 @@ namespace msg::usrmsg
public:
usrmsg_parser(const util::PROTOCOL protocol);
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl) const;
void create_status_response(std::vector<uint8_t> &msg, const uint64_t lcl_seq_no, std::string_view lcl_hash) 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;
@@ -28,7 +28,7 @@ namespace msg::usrmsg
void create_contract_output_container(std::vector<uint8_t> &msg, const ::std::vector<std::string_view> &outputs,
const util::merkle_hash_node &hash_root, const std::vector<std::pair<std::string, std::string>> &unl_sig,
const uint64_t lcl_seq_no, std::string_view lcl) const;
const uint64_t lcl_seq_no, std::string_view lcl_hash) const;
void create_unl_list_container(std::vector<uint8_t> &msg, const ::std::set<std::string> &unl_list) const;
@@ -41,7 +41,7 @@ namespace msg::usrmsg
int extract_signed_input_container(std::string &extracted_input_container, std::string &extracted_sig) const;
int extract_input_container(std::string &input, std::string &nonce,
uint64_t &max_lcl_seqno, std::string_view encoded_content) const;
uint64_t &max_lcl_seq_no, std::string_view encoded_content) const;
};
} // namespace msg::usrmsg