mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
JS: Fix self and lots of other stuff for remote.js
This commit is contained in:
82
js/remote.js
82
js/remote.js
@@ -23,11 +23,11 @@ var Remote = function (trusted, websocket_ip, websocket_port, trace) {
|
|||||||
this.stand_alone = undefined;
|
this.stand_alone = undefined;
|
||||||
|
|
||||||
// Cache information for accounts.
|
// Cache information for accounts.
|
||||||
this.account = {
|
this.accounts = {
|
||||||
// Consider sequence numbers stable if you know you're not generating bad transactions.
|
// Consider sequence numbers stable if you know you're not generating bad transactions.
|
||||||
// Otherwise, clear it to have it automatically refreshed from the network.
|
// Otherwise, clear it to have it automatically refreshed from the network.
|
||||||
|
|
||||||
// acount : { seq : __ }
|
// account : { seq : __ }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ Remote.method('connect', function (done, timeout) {
|
|||||||
this.expire = false;
|
this.expire = false;
|
||||||
|
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
if (this.trace) console.log("remote: expire: timeout");
|
if (self.trace) console.log("remote: expire: timeout");
|
||||||
self.expire = true;
|
self.expire = true;
|
||||||
}, timeout);
|
}, timeout);
|
||||||
} else {
|
} else {
|
||||||
@@ -139,10 +139,11 @@ Remote.method('connect', function (done, timeout) {
|
|||||||
|
|
||||||
// Target stated is disconnected.
|
// Target stated is disconnected.
|
||||||
Remote.method('disconnect', function (done) {
|
Remote.method('disconnect', function (done) {
|
||||||
var ws = this.ws;
|
var self = this;
|
||||||
|
var ws = this.ws;
|
||||||
|
|
||||||
ws.onclose = function () {
|
ws.onclose = function () {
|
||||||
if (this.trace) console.log("remote: onclose: %s", ws.readyState);
|
if (self.trace) console.log("remote: onclose: %s", ws.readyState);
|
||||||
done(ws.readyState);
|
done(ws.readyState);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -152,14 +153,14 @@ Remote.method('disconnect', function (done) {
|
|||||||
// Send a request. The request should lack the id.
|
// Send a request. The request should lack the id.
|
||||||
// <-> request: what to send, consumed.
|
// <-> request: what to send, consumed.
|
||||||
Remote.method('request', function (request, onDone, onFailure) {
|
Remote.method('request', function (request, onDone, onFailure) {
|
||||||
this.id += 1; // Advance id.
|
var self = this;
|
||||||
|
|
||||||
var ws = this.ws;
|
this.id += 1; // Advance id.
|
||||||
|
|
||||||
request.id = this.id;
|
request.id = this.id;
|
||||||
|
|
||||||
ws.response[request.id] = function (response) {
|
this.ws.response[request.id] = function (response) {
|
||||||
if (this.trace) console.log("remote: response: %s", JSON.stringify(response));
|
if (self.trace) console.log("remote: response: %s", JSON.stringify(response));
|
||||||
|
|
||||||
if (onFailure && response.error)
|
if (onFailure && response.error)
|
||||||
{
|
{
|
||||||
@@ -173,7 +174,7 @@ Remote.method('request', function (request, onDone, onFailure) {
|
|||||||
|
|
||||||
if (this.trace) console.log("remote: request: %s", JSON.stringify(request));
|
if (this.trace) console.log("remote: request: %s", JSON.stringify(request));
|
||||||
|
|
||||||
ws.send(JSON.stringify(request));
|
this.ws.send(JSON.stringify(request));
|
||||||
});
|
});
|
||||||
|
|
||||||
Remote.method('request_ledger_closed', function (onDone, onFailure) {
|
Remote.method('request_ledger_closed', function (onDone, onFailure) {
|
||||||
@@ -192,6 +193,8 @@ Remote.method('request_ledger_current', function (onDone, onFailure) {
|
|||||||
// --> ledger_index : optional
|
// --> ledger_index : optional
|
||||||
// --> type
|
// --> type
|
||||||
Remote.method('request_ledger_entry', function (req, onDone, onFailure) {
|
Remote.method('request_ledger_entry', function (req, onDone, onFailure) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
assert(this.trusted); // If not trusted, need to check proof, maybe talk packet protocol.
|
assert(this.trusted); // If not trusted, need to check proof, maybe talk packet protocol.
|
||||||
|
|
||||||
req.command = 'ledger_entry';
|
req.command = 'ledger_entry';
|
||||||
@@ -201,7 +204,8 @@ Remote.method('request_ledger_entry', function (req, onDone, onFailure) {
|
|||||||
// XXX Initial implementation no caching.
|
// XXX Initial implementation no caching.
|
||||||
this.request(req, onDone, onFailure);
|
this.request(req, onDone, onFailure);
|
||||||
}
|
}
|
||||||
else if (req.ledger_index)
|
// else if (req.ledger_index)
|
||||||
|
else
|
||||||
{
|
{
|
||||||
// Current
|
// Current
|
||||||
// XXX Only allow with standalone mode. Must sync response with advance.
|
// XXX Only allow with standalone mode. Must sync response with advance.
|
||||||
@@ -216,7 +220,7 @@ Remote.method('request_ledger_entry', function (req, onDone, onFailure) {
|
|||||||
cache = this.ledgers.current.account_root = {};
|
cache = this.ledgers.current.account_root = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
var entry = this.ledgers.current.account_root[req.account];
|
entry = this.ledgers.current.account_root[req.account];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -233,17 +237,19 @@ Remote.method('request_ledger_entry', function (req, onDone, onFailure) {
|
|||||||
|
|
||||||
// Submit request
|
// Submit request
|
||||||
this.request(req, function (r) {
|
this.request(req, function (r) {
|
||||||
// Got result.
|
// Got result.
|
||||||
switch (req.type) {
|
switch (req.type) {
|
||||||
case 'account_root':
|
case 'account_root':
|
||||||
this.ledgers.current.account_root.account = r;
|
self.ledgers.current.account_root[r.node.Account] = r.node;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// This type not cached.
|
// This type not cached.
|
||||||
}
|
// nothing();
|
||||||
onDone(r);
|
break;
|
||||||
}, onFailure);
|
}
|
||||||
|
onDone(r.node);
|
||||||
|
}, onFailure);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -252,6 +258,8 @@ Remote.method('request_ledger_entry', function (req, onDone, onFailure) {
|
|||||||
// done(value)
|
// done(value)
|
||||||
// XXX <-> value: { 'status', status, 'result' : result, ... }
|
// XXX <-> value: { 'status', status, 'result' : result, ... }
|
||||||
Remote.method('submit', function (request, onDone, onFailure) {
|
Remote.method('submit', function (request, onDone, onFailure) {
|
||||||
|
if (this.trace) console.log("remote: submit: %s", request);
|
||||||
|
|
||||||
var req = {};
|
var req = {};
|
||||||
|
|
||||||
req.command = 'submit';
|
req.command = 'submit';
|
||||||
@@ -274,12 +282,14 @@ Remote.method('submit', function (request, onDone, onFailure) {
|
|||||||
// Subscribe to a server to get the current and closed ledger.
|
// Subscribe to a server to get the current and closed ledger.
|
||||||
// XXX Set up routine to update on notification.
|
// XXX Set up routine to update on notification.
|
||||||
Remote.method('server_subscribe', function (onDone, onFailure) {
|
Remote.method('server_subscribe', function (onDone, onFailure) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
this.request(
|
this.request(
|
||||||
{ 'command' : 'server_subscribe' },
|
{ 'command' : 'server_subscribe' },
|
||||||
function (r) {
|
function (r) {
|
||||||
this.ledger_current_index = r.ledger_current_index;
|
self.ledger_current_index = r.ledger_current_index;
|
||||||
this.ledger_closed = r.ledger_closed;
|
self.ledger_closed = r.ledger_closed;
|
||||||
this.stand_alone = r.stand_alone;
|
self.stand_alone = r.stand_alone;
|
||||||
onDone();
|
onDone();
|
||||||
},
|
},
|
||||||
onFailure
|
onFailure
|
||||||
@@ -289,6 +299,7 @@ Remote.method('server_subscribe', function (onDone, onFailure) {
|
|||||||
// Refresh accounts[account].seq
|
// Refresh accounts[account].seq
|
||||||
// done(result);
|
// done(result);
|
||||||
Remote.method('account_seq', function (account, advance, onDone, onFailure) {
|
Remote.method('account_seq', function (account, advance, onDone, onFailure) {
|
||||||
|
var self = this;
|
||||||
var account_root_entry = this.accounts[account];
|
var account_root_entry = this.accounts[account];
|
||||||
|
|
||||||
if (account_root_entry && account_root_entry.seq)
|
if (account_root_entry && account_root_entry.seq)
|
||||||
@@ -304,14 +315,17 @@ Remote.method('account_seq', function (account, advance, onDone, onFailure) {
|
|||||||
// Need to get the ledger entry.
|
// Need to get the ledger entry.
|
||||||
this.request_ledger_entry(
|
this.request_ledger_entry(
|
||||||
{
|
{
|
||||||
'ledger' : this.ledger_closed,
|
'ledger' : this.ledger_closed,
|
||||||
'account_root' : account
|
'type' : 'account_root',
|
||||||
|
'account_root' : account
|
||||||
},
|
},
|
||||||
function (r) {
|
function (node) {
|
||||||
// Extract the seqence number from the account root entry.
|
// Extract the seqence number from the account root entry.
|
||||||
var seq = r.seq;
|
var seq = node.Sequence;
|
||||||
|
|
||||||
this.accounts[account].seq = seq + 1;
|
if (!self.accounts[account]) self.accounts[account] = {};
|
||||||
|
|
||||||
|
self.accounts[account].seq = seq + 1;
|
||||||
|
|
||||||
onDone(seq);
|
onDone(seq);
|
||||||
},
|
},
|
||||||
@@ -322,11 +336,13 @@ Remote.method('account_seq', function (account, advance, onDone, onFailure) {
|
|||||||
|
|
||||||
// A submit that fills in the sequence number.
|
// A submit that fills in the sequence number.
|
||||||
Remote.method('submit_seq', function (transaction, onDirty, onDone, onFailure) {
|
Remote.method('submit_seq', function (transaction, onDirty, onDone, onFailure) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
// Get the next sequence number for the account.
|
// Get the next sequence number for the account.
|
||||||
this.account_seq(transaction.Signer, true,
|
this.account_seq(transaction.fields.Signer, true,
|
||||||
function (seq) {
|
function (seq) {
|
||||||
request.seq = seq;
|
transaction.seq = seq;
|
||||||
this.submit(onDone, onFailure);
|
self.submit(transaction, onDone, onFailure);
|
||||||
},
|
},
|
||||||
onFailure);
|
onFailure);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user