Fixed integer serialization issue with bson. (#287)

This commit is contained in:
Ravin Perera
2021-04-12 07:15:57 +05:30
committed by GitHub
parent 370e03fe84
commit 83bb158dd3
6 changed files with 34 additions and 13 deletions

View File

@@ -806,12 +806,12 @@
throw "Max ledger seq no. or offset cannot be 0.";
if (!isOffset && !maxLedger)
throw "Max ledger seq. no not specified.";
if (nonce && (!Number.isInteger(nonce) || nonce < 0))
if (nonce && (!Number.isInteger(nonce) || nonce <= 0))
throw "Input nonce must be a positive integer.";
// Use time-based incrementing nonce if not specified.
// Use epoch-based auto incrementing nonce if nonce is not specified.
if (!nonce)
nonce = (new Date()).getTime();
nonce = new Date().getTime();
// If max ledger is specified as offset, we need to get current ledger status and add the offset to it.
if (isOffset) {
@@ -859,7 +859,8 @@
if (connectionStatus != 2)
return Promise.resolve(null);
const msg = msgHelper.createLedgerQuery("seq_no", { "seq_no": seqNo }, includeInputs, includeOutputs);
const queryParams = { "seq_no": msgHelper.serializeNumber(seqNo) };
const msg = msgHelper.createLedgerQuery("seq_no", queryParams, includeInputs, includeOutputs);
const p = new Promise(resolve => {
ledgerQueryResolvers[msg.id] = {
type: "seq_no",
@@ -906,6 +907,12 @@
return protocol == protocols.json ? val : val.buffer;
}
this.serializeNumber = (num) => {
// For standard javascript numbers, Bson library does not support serializing large numbers correctly.
// Hence we have to encode as special bson long type when using bson protocol.
return (protocol == protocols.json) ? num : bson.Long.fromNumber(num);
}
// Used for generating strings to hold values as js object keys.
this.stringifyValue = (val) => {
if (isString(val))
@@ -945,8 +952,8 @@
const inpContainer = {
input: this.serializeInput(input),
nonce: nonce,
max_ledger_seq_no: maxLedgerSeqNo
nonce: this.serializeNumber(nonce),
max_ledger_seq_no: this.serializeNumber(maxLedgerSeqNo)
}
const serlializedInpContainer = this.serializeObject(inpContainer);

View File

@@ -42,7 +42,7 @@ namespace ledger::query
ledgers.push_back(std::move(ledger));
// Fill raw data if required.
if (seq_q.seq_no > 0 && (seq_q.inputs || seq_q.outputs))
if (seq_q.inputs || seq_q.outputs)
{
for (ledger_record &ledger : ledgers)
{
@@ -51,7 +51,8 @@ namespace ledger::query
if (seq_q.outputs)
ledger.outputs = std::vector<ledger::ledger_user_output>();
if (get_ledger_raw_data(ledger, user_pubkey, fs_sess_name) != -1)
// No need to actually query raw data for genesis ledger.
if (seq_q.seq_no == 0 || get_ledger_raw_data(ledger, user_pubkey, fs_sess_name) != -1)
res = ledgers;
}
}

View File

@@ -397,11 +397,18 @@ namespace msg::usrmsg::bson
return -1;
}
nonce = d[msg::usrmsg::FLD_NONCE].as<uint64_t>();
if (nonce == 0)
{
LOG_DEBUG << "Input nonce must be a positive integer.";
return -1;
}
max_ledger_seq_no = d[msg::usrmsg::FLD_MAX_LEDGER_SEQ_NO].as<uint64_t>();
const jsoncons::byte_string_view &bsv = d[msg::usrmsg::FLD_INPUT].as_byte_string_view();
input = std::string_view(reinterpret_cast<const char *>(bsv.data()), bsv.size());
nonce = d[msg::usrmsg::FLD_NONCE].as<uint64_t>();
max_ledger_seq_no = d[msg::usrmsg::FLD_MAX_LEDGER_SEQ_NO].as<uint64_t>();
return 0;
}

View File

@@ -745,8 +745,14 @@ namespace msg::usrmsg::json
return -1;
}
input = d[msg::usrmsg::FLD_INPUT].as<std::string>();
nonce = d[msg::usrmsg::FLD_NONCE].as<uint64_t>();
if (nonce == 0)
{
LOG_DEBUG << "Input nonce must be a positive integer.";
return -1;
}
input = d[msg::usrmsg::FLD_INPUT].as<std::string>();
max_ledger_seq_no = d[msg::usrmsg::FLD_MAX_LEDGER_SEQ_NO].as<uint64_t>();
return 0;

View File

@@ -20,5 +20,5 @@ COPY ./bin/hpws .
ENTRYPOINT ["/hp/hpcore"]
# Run with vagrind
# Run with valgrind
# ENTRYPOINT ["valgrind", "/hp/hpcore"]

View File

@@ -109,7 +109,7 @@ function singleUserInputOutput(payloadKB, requestCount) {
timer.start();
for (let i = 0; i < requestCount; i++) {
const input = await hpc.submitContractInput(payload, i, 10);
const input = await hpc.submitContractInput(payload);
input.submissionStatus.then(s => {
if (s.status != "accepted")
console.log(s.reason);