Returned input hash and ledger info for input submissions. (#283)

- Introduced input hash which can later be used to query the ledger.
- Returned input hash and ledger info at input submission.
- Updated js client lib to support input hash.
- Updated consensus proposal candidate expiration rules.
This commit is contained in:
Ravin Perera
2021-04-07 21:22:29 +05:30
committed by GitHub
parent 137d7633e0
commit d4646179c2
33 changed files with 668 additions and 393 deletions

View File

@@ -75,13 +75,17 @@ namespace msg::usrmsg::bson
* "type": "contract_input_status",
* "status": "<accepted|rejected>",
* "reason": "<reson>",
* "input_sig": <signature of original input message>
* "input_hash": <hash of original input signature>,
* "ledger_seq_no": <sequence no of the ledger that the input got included in>,
* "ledger_hash": "<hash no of the ledger that the input got included in>"
* }
* @param is_accepted Whether the original message was accepted or not.
* @param reason Rejected reason. Empty if accepted.
* @param input_sig Binary signature of the original input message which generated this result.
* @param input_hash Binary Hash of the original input signature. This is used by user
* to tie the response with the input submission.
*/
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_input_status(std::vector<uint8_t> &msg, std::string_view status, std::string_view reason,
std::string_view input_hash, const uint64_t ledger_seq_no, const util::h32 &ledger_hash)
{
jsoncons::bson::bson_bytes_encoder encoder(msg);
encoder.begin_object();
@@ -89,10 +93,26 @@ namespace msg::usrmsg::bson
encoder.string_value(msg::usrmsg::MSGTYPE_CONTRACT_INPUT_STATUS);
encoder.key(msg::usrmsg::FLD_STATUS);
encoder.string_value(status);
encoder.key(msg::usrmsg::FLD_REASON);
encoder.string_value(reason);
encoder.key(msg::usrmsg::FLD_INPUT_SIG);
encoder.byte_string_value(input_sig);
// Reject reason is only included for rejected inputs.
if (!reason.empty())
{
encoder.key(msg::usrmsg::FLD_REASON);
encoder.string_value(reason);
}
encoder.key(msg::usrmsg::FLD_INPUT_HASH);
encoder.byte_string_value(input_hash);
// Ledger information is only included in 'accepted' input statuses.
if (ledger_seq_no > 0)
{
encoder.key(msg::usrmsg::FLD_LEDGER_SEQ_NO);
encoder.uint64_value(ledger_seq_no);
encoder.key(msg::usrmsg::FLD_LEDGER_HASH);
encoder.byte_string_value(ledger_hash.to_string_view());
}
encoder.end_object();
encoder.flush();
}
@@ -334,16 +354,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_seq_no The extracted max ledger sequence no.
* @param max_ledger_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_seq_no": <integer>
* "max_ledger_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_seq_no, std::string_view contentbson)
int extract_input_container(std::string &input, std::string &nonce, uint64_t &max_ledger_seq_no, std::string_view contentbson)
{
jsoncons::ojson d;
try
@@ -356,13 +376,13 @@ namespace msg::usrmsg::bson
return -1;
}
if (!d.contains(msg::usrmsg::FLD_INPUT) || !d.contains(msg::usrmsg::FLD_NONCE) || !d.contains(msg::usrmsg::FLD_MAX_LCL_SEQ))
if (!d.contains(msg::usrmsg::FLD_INPUT) || !d.contains(msg::usrmsg::FLD_NONCE) || !d.contains(msg::usrmsg::FLD_MAX_LEDGER_SEQ_NO))
{
LOG_DEBUG << "User input container required fields missing or invalid.";
return -1;
}
if (!d[msg::usrmsg::FLD_INPUT].is_byte_string_view() || !d[msg::usrmsg::FLD_NONCE].is_string() || !d[msg::usrmsg::FLD_MAX_LCL_SEQ].is_uint64())
if (!d[msg::usrmsg::FLD_INPUT].is_byte_string_view() || !d[msg::usrmsg::FLD_NONCE].is_string() || !d[msg::usrmsg::FLD_MAX_LEDGER_SEQ_NO].is_uint64())
{
LOG_DEBUG << "User input container invalid field values.";
return -1;
@@ -372,7 +392,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_seq_no = d[msg::usrmsg::FLD_MAX_LCL_SEQ].as<uint64_t>();
max_ledger_seq_no = d[msg::usrmsg::FLD_MAX_LEDGER_SEQ_NO].as<uint64_t>();
return 0;
}