Implementation for message handling

This commit is contained in:
chalith
2023-10-31 13:35:37 +05:30
parent 23ee18e454
commit f84a02ce09
18 changed files with 265 additions and 169 deletions

View File

@@ -34,7 +34,7 @@ namespace msg::usrmsg::json
* Constructs user challenge message json and the challenge string required for
* initial user challenge handshake. This gets called when a user establishes
* a web socket connection to HP.
*
*
* @param msg Buffer to construct the generated json message string into.
* Message format:
* {
@@ -84,7 +84,7 @@ namespace msg::usrmsg::json
/**
* Constructs server challenge response message json. This gets sent when we receive
* a challenge from the user.
*
*
* @param msg Buffer to construct the generated json message string into.
* Message format:
* {
@@ -325,6 +325,52 @@ namespace msg::usrmsg::json
msg += "\"}";
}
/**
* Constructs a hpsh response message.
* @param msg Buffer to construct the generated json message string into.
* Message format:
* {
* "type": "hpsh_response",
* "reply_for": "<corresponding request id>",
* "content": "<response string>"
* }
* @param content The contract binary output content to be put in the message.
*/
void create_hpsh_response_container(std::vector<uint8_t> &msg, std::string_view reply_for, std::string_view content)
{
msg.reserve(content.size() + 256);
msg += "{\"";
msg += msg::usrmsg::FLD_TYPE;
msg += SEP_COLON;
msg += msg::usrmsg::MSGTYPE_HPSH_RESPONSE;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_REPLY_FOR;
msg += SEP_COLON;
msg += reply_for;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_CONTENT;
msg += SEP_COLON_NOQUOTE;
if (is_json_string(content))
{
// Process the final string using jsoncons.
jsoncons::json jstring = content;
jsoncons::json_options options;
options.escape_all_non_ascii(true);
std::string escaped_content;
jstring.dump(escaped_content);
msg += escaped_content;
}
else
{
msg += content;
}
msg += "}";
}
/**
* Constructs a contract read response message.
* @param msg Buffer to construct the generated json message string into.
@@ -381,7 +427,7 @@ namespace msg::usrmsg::json
* "ledger_hash": "<lcl hash hex>",
* "outputs": ["<output string 1>", "<output string 2>", ...], // The output order is the hash generation order.
* "output_hash": "<hex hash of user's outputs>", [output hash = hash(pubkey+all outputs for the user)]
* "hash_tree": [<hex merkle hash tree>], // Collapsed merkle tree with user's hash element marked as null.
* "hash_tree": [<hex merkle hash tree>], // Collapsed merkle tree with user's hash element marked as null.
* "unl_sig": [["<pubkey hex>", "<sig hex>"], ...] // UNL pubkeys and signatures of root hash.
* }
* @param hash This user's combined output hash. [output hash = hash(pubkey+all outputs for the user)]
@@ -566,12 +612,12 @@ namespace msg::usrmsg::json
* {
* "type": "health_event",
* "event": "proposal" | "connectivity",
*
*
* // proposal
* "comm_latency": {min:0, max:0, avg:0},
* "read_latency": {min:0, max:0, avg:0}
* "batch_size": 0
*
*
* // connectivity
* "peer_count": 0,
* "weakly_connected": true | false
@@ -697,7 +743,7 @@ namespace msg::usrmsg::json
/**
* Verifies the user handshake response with the original challenge issued to the user
* and the user public key contained in the response.
*
*
* @param extracted_pubkeyhex The hex public key extracted from the response.
* @param extracted_protocol The protocol code extracted from the response.
* @param extracted_server_challenge Any server challenge issued by user.
@@ -842,7 +888,7 @@ namespace msg::usrmsg::json
/**
* Extracts a contract read request message sent by user.
*
*
* @param extracted_content The content to be passed to the contract, extracted from the message.
* @param d The json document holding the read request message.
* Accepted signed input container format:
@@ -872,30 +918,30 @@ namespace msg::usrmsg::json
return 0;
}
/**
* Extracts a contract shell input message sent by user.
*
* @param extracted_content The content to be passed to the contract, extracted from the message.
* @param d The json document holding the shell input message.
/**
* Extracts a hpsh input message sent by user.
*
* @param extracted_content The content to be passed to the, extracted from the message.
* @param d The json document holding the hpsh input message.
* Accepted signed input container format:
* {
* "type": "contract_shell_input",
* "type": "hpsh_request",
* "id": "<any string>",
* "content": "<any string>"
* }
* @return 0 on successful extraction. -1 for failure.
*/
int extract_shell_input(std::string &extracted_id, std::string &extracted_content, const jsoncons::json &d)
int extract_hpsh_request(std::string &extracted_id, std::string &extracted_content, const jsoncons::json &d)
{
if (!d.contains(msg::usrmsg::FLD_ID) || !d[msg::usrmsg::FLD_ID].is<std::string>())
{
LOG_DEBUG << "Shell input 'id' field missing or invalid.";
LOG_DEBUG << "Hpsh input 'id' field missing or invalid.";
return -1;
}
if (!d.contains(msg::usrmsg::FLD_CONTENT) || !d[msg::usrmsg::FLD_CONTENT].is<std::string>())
{
LOG_DEBUG << "Shell input 'content' field missing or invalid.";
LOG_DEBUG << "Hpsh input 'content' field missing or invalid.";
return -1;
}
@@ -906,9 +952,9 @@ namespace msg::usrmsg::json
/**
* Extracts a signed input container message sent by user.
*
*
* @param extracted_input_container The input container extracted from the message.
* @param extracted_sig The binary signature extracted from the message.
* @param extracted_sig The binary signature extracted from the message.
* @param d The json document holding the input container.
* Accepted signed input container format:
* {