Added user message to get lcl. (#318)

This commit is contained in:
Ravin Perera
2021-06-04 16:16:22 +05:30
committed by GitHub
parent 33bd63ac64
commit a7b7f6c9de
9 changed files with 137 additions and 8 deletions

View File

@@ -356,6 +356,10 @@
return getMultiConnectionResult(con => con.getStatus());
}
this.getLcl = () => {
return getMultiConnectionResult(con => con.getLcl());
}
this.getLedgerBySeqNo = (seqNo, includeInputs, includeOutputs) => {
return getMultiConnectionResult(con => con.getLedgerBySeqNo(seqNo, includeInputs, includeOutputs));
}
@@ -378,6 +382,7 @@
let handshakeResolver = null;
let closeResolver = null;
let statResponseResolvers = [];
let lclResponseResolvers = [];
let contractInputResolvers = {}; // Contract input status-awaiting resolvers (keyed by input hash).
let ledgerQueryResolvers = {}; // Message resolvers that uses request/reply associations.
@@ -620,12 +625,21 @@
contractExecutionEnabled: m.contract_execution_enabled,
readRequestsEnabled: m.read_requests_enabled,
isFullHistoryNode: m.is_full_history_node,
currentUnl: m.current_unl,
currentUnl: m.current_unl.map(u => msgHelper.deserializeValue(u)),
peers: m.peers
});
})
statResponseResolvers = [];
}
else if (m.type == "lcl_response") {
lclResponseResolvers.forEach(resolver => {
resolver({
ledgerSeqNo: m.ledger_seq_no,
ledgerHash: msgHelper.deserializeValue(m.ledger_hash)
});
})
lclResponseResolvers = [];
}
else if (m.type == "unl_change") {
if (m.unl) {
// Convert unl pubkeys to hex string.
@@ -823,6 +837,24 @@
return p;
}
this.getLcl = () => {
if (connectionStatus != 2)
return Promise.resolve(null);
const p = new Promise(resolve => {
lclResponseResolvers.push(resolve);
});
// If this is the only awaiting lcl request, then send an actual lcl request.
// Otherwise simply wait for the previously sent request.
if (lclResponseResolvers.length == 1) {
const msg = msgHelper.createLclRequest();
wsSend(msgHelper.serializeObject(msg));
}
return p;
}
this.submitContractInput = async (input, nonce, maxLedger, isOffset) => {
if (connectionStatus != 2)
@@ -843,12 +875,12 @@
if (!maxLedger)
maxLedger = 10; // Default offset applied if not specified.
// Acquire the current ledger status and add the specified offset.
const stat = await this.getStatus();
if (!stat)
throw "Error retrieving ledger status."
// Acquire the last ledger information and add the specified offset.
const lcl = await this.getLcl();
if (!lcl)
throw "Error retrieving last closed ledger."
maxLedger += stat.ledgerSeqNo;
maxLedger += lcl.ledgerSeqNo;
}
const inp = msgHelper.createContractInputComponents(input, nonce, maxLedger);
@@ -1018,6 +1050,10 @@
return { type: "stat" };
}
this.createLclRequest = () => {
return { type: "lcl" };
}
this.createLedgerQuery = (filterBy, params, includeInputs, includeOutputs) => {
const includes = [];

View File

@@ -17,8 +17,15 @@ namespace msg::usrmsg::bson
* Message format:
* {
* "type": "stat_response",
* "hp_version": "<version>",
* "ledger_seq_no": <lcl sequence no>,
* "ledger_hash": <binary lcl hash>
* "ledger_hash": <binary lcl hash>,
* "roundtime": <roundtime milliseconds>,
* "contract_execution_enabled": true | false,
* "read_requests_enabled": true | false,
* "is_full_history_node": true | false,
* "current_unl": [ <ed prefixed pubkey>, ... ],
* "peers": [ "ip:port", ... ]
* }
*/
void create_status_response(std::vector<uint8_t> &msg)
@@ -70,6 +77,32 @@ namespace msg::usrmsg::bson
encoder.flush();
}
/**
* Constructs a lcl response message.
* @param msg Buffer to construct the generated bson message into.
* Message format:
* {
* "type": "lcl_response",
* "ledger_seq_no": <lcl sequence no>,
* "ledger_hash": <binary lcl hash>
* }
*/
void create_lcl_response(std::vector<uint8_t> &msg)
{
const util::sequence_hash lcl_id = status::get_lcl_id();
jsoncons::bson::bson_bytes_encoder encoder(msg);
encoder.begin_object();
encoder.key(msg::usrmsg::FLD_TYPE);
encoder.string_value(msg::usrmsg::MSGTYPE_LCL_RESPONSE);
encoder.key(msg::usrmsg::FLD_LEDGER_SEQ_NO);
encoder.int64_value(lcl_id.seq_no);
encoder.key(msg::usrmsg::FLD_LEDGER_HASH);
encoder.byte_string_value(lcl_id.hash.to_string_view());
encoder.end_object();
encoder.flush();
}
/**
* Constructs a contract input status message.
* @param msg Buffer to construct the generated bson message into.

View File

@@ -10,6 +10,8 @@ namespace msg::usrmsg::bson
void create_status_response(std::vector<uint8_t> &msg);
void create_lcl_response(std::vector<uint8_t> &msg);
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);

View File

@@ -136,8 +136,15 @@ namespace msg::usrmsg::json
* Message format:
* {
* "type": "stat_response",
* "hp_version": "<version>",
* "ledger_seq_no": <lcl sequence no>,
* "ledger_hash": "<lcl hash hex>"
* "ledger_hash": "<lcl hash hex>",
* "roundtime": <roundtime milliseconds>,
* "contract_execution_enabled": true | false,
* "read_requests_enabled": true | false,
* "is_full_history_node": true | false,
* "current_unl": [ "<ed prefixed pubkey hex>"", ... ],
* "peers": [ "ip:port", ... ]
* }
*/
void create_status_response(std::vector<uint8_t> &msg)
@@ -217,6 +224,36 @@ namespace msg::usrmsg::json
msg += "}";
}
/**
* Constructs a lcl response message.
* @param msg Buffer to construct the generated json message string into.
* Message format:
* {
* "type": "lcl_response",
* "ledger_seq_no": <lcl sequence no>,
* "ledger_hash": "<lcl hash hex>"
* }
*/
void create_lcl_response(std::vector<uint8_t> &msg)
{
const util::sequence_hash lcl_id = status::get_lcl_id();
msg.reserve(512);
msg += "{\"";
msg += msg::usrmsg::FLD_TYPE;
msg += SEP_COLON;
msg += msg::usrmsg::MSGTYPE_LCL_RESPONSE;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_LEDGER_SEQ_NO;
msg += SEP_COLON_NOQUOTE;
msg += std::to_string(lcl_id.seq_no);
msg += SEP_COMMA_NOQUOTE;
msg += msg::usrmsg::FLD_LEDGER_HASH;
msg += SEP_COLON;
msg += util::to_hex(lcl_id.hash.to_string_view());
msg += "\"}";
}
/**
* Constructs a contract input status message.
* @param msg Buffer to construct the generated json message string into.

View File

@@ -10,6 +10,8 @@ namespace msg::usrmsg::json
void create_user_challenge(std::vector<uint8_t> &msg, std::string &challenge);
void create_lcl_response(std::vector<uint8_t> &msg);
void create_server_challenge_response(std::vector<uint8_t> &msg, const std::string &original_challenge);
void create_status_response(std::vector<uint8_t> &msg);

View File

@@ -72,6 +72,8 @@ namespace msg::usrmsg
constexpr const char *MSGTYPE_CONTRACT_OUTPUT = "contract_output";
constexpr const char *MSGTYPE_STAT = "stat";
constexpr const char *MSGTYPE_STAT_RESPONSE = "stat_response";
constexpr const char *MSGTYPE_LCL = "lcl";
constexpr const char *MSGTYPE_LCL_RESPONSE = "lcl_response";
constexpr const char *MSGTYPE_UNL_CHANGE = "unl_change";
constexpr const char *MSGTYPE_LEDGER_QUERY = "ledger_query";
constexpr const char *MSGTYPE_LEDGER_QUERY_RESULT = "ledger_query_result";

View File

@@ -21,6 +21,14 @@ namespace msg::usrmsg
busrmsg::create_status_response(msg);
}
void usrmsg_parser::create_lcl_response(std::vector<uint8_t> &msg) const
{
if (protocol == util::PROTOCOL::JSON)
jusrmsg::create_lcl_response(msg);
else
busrmsg::create_lcl_response(msg);
}
void usrmsg_parser::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) const
{

View File

@@ -22,6 +22,8 @@ namespace msg::usrmsg
void create_status_response(std::vector<uint8_t> &msg) const;
void create_lcl_response(std::vector<uint8_t> &msg) const;
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) const;

View File

@@ -239,6 +239,13 @@ namespace usr
user.session.send(resp);
return 0;
}
else if (msg_type == msg::usrmsg::MSGTYPE_LCL)
{
std::vector<uint8_t> resp;
parser.create_lcl_response(resp);
user.session.send(resp);
return 0;
}
else if (msg_type == msg::usrmsg::MSGTYPE_LEDGER_QUERY)
{
ledger::query::query_request req;