JS: Fix account sequence caching.

This commit is contained in:
Arthur Britto
2012-11-03 14:28:31 -07:00
committed by Stefan Thomas
parent aac7f698ee
commit cbc244c1cb

View File

@@ -41,6 +41,7 @@ var Request = function (remote, command) {
'id' : undefined, 'id' : undefined,
}; };
this.remote = remote; this.remote = remote;
this.requested = false;
this.on('request', function () { this.on('request', function () {
self.request_default(); self.request_default();
@@ -68,7 +69,10 @@ Request.prototype.request = function (remote) {
}; };
Request.prototype.request_default = function () { Request.prototype.request_default = function () {
if (!this.requested) {
this.requested = true;
this.remote.request(this); this.remote.request(this);
}
}; };
Request.prototype.ledger_choose = function (current) { Request.prototype.ledger_choose = function (current) {
@@ -572,6 +576,7 @@ Remote.prototype.submit = function (transaction) {
else { else {
if (!transaction.transaction.Sequence) { if (!transaction.transaction.Sequence) {
transaction.transaction.Sequence = this.account_seq(transaction.transaction.Account, 'ADVANCE'); transaction.transaction.Sequence = this.account_seq(transaction.transaction.Account, 'ADVANCE');
// console.log("Sequence: %s", transaction.transaction.Sequence);
} }
if (!transaction.transaction.Sequence) { if (!transaction.transaction.Sequence) {
@@ -581,7 +586,7 @@ Remote.prototype.submit = function (transaction) {
// Try again. // Try again.
self.submit(transaction); self.submit(transaction);
}) })
.on('error', function (message) { .on('error_account_seq_cache', function (message) {
// XXX Maybe be smarter about this. Don't want to trust an untrusted server for this seq number. // XXX Maybe be smarter about this. Don't want to trust an untrusted server for this seq number.
// Look in the current ledger. // Look in the current ledger.
@@ -590,7 +595,7 @@ Remote.prototype.submit = function (transaction) {
// Try again. // Try again.
self.submit(transaction); self.submit(transaction);
}) })
.on('error', function (message) { .on('error_account_seq_cache', function (message) {
// Forward errors. // Forward errors.
transaction.emit('error', message); transaction.emit('error', message);
}) })
@@ -685,6 +690,11 @@ Remote.prototype.account_seq = function (account, advance) {
seq = account_info.seq; seq = account_info.seq;
if (advance) account_info.seq += 1; if (advance) account_info.seq += 1;
// console.log("cached: %s current=%d next=%d", account, seq, account_info.seq);
}
else {
// console.log("uncached: %s", account);
} }
return seq; return seq;
@@ -701,21 +711,40 @@ Remote.prototype.set_account_seq = function (account, seq) {
// Return a request to refresh accounts[account].seq. // Return a request to refresh accounts[account].seq.
Remote.prototype.account_seq_cache = function (account, current) { Remote.prototype.account_seq_cache = function (account, current) {
var self = this; var self = this;
var request = this.request_ledger_entry('account_root'); var request;
return request
.account_root(account)
.ledger_choose(current)
.on('success', function (message) {
var seq = message.node.Sequence;
if (!self.accounts[account]) self.accounts[account] = {}; if (!self.accounts[account]) self.accounts[account] = {};
self.accounts[account].seq = seq; var account_info = self.accounts[account];
request = account_info.caching_seq_request;
if (!request) {
// console.log("starting: %s", account);
request = self.request_ledger_entry('account_root')
.account_root(account)
.ledger_choose(current)
.on('success', function (message) {
delete account_info.caching_seq_request;
var seq = message.node.Sequence;
account_info.seq = seq;
// console.log("caching: %s %d", account, seq);
// If the caller also waits for 'success', they might run before this. // If the caller also waits for 'success', they might run before this.
request.emit('success_account_seq_cache'); request.emit('success_account_seq_cache', message);
})
.on('error', function (message) {
// console.log("error: %s", account);
delete account_info.caching_seq_request;
request.emit('error_account_seq_cache', message);
}); });
account_info.caching_seq_request = request;
}
return request
}; };
// Mark an account's root node as dirty. // Mark an account's root node as dirty.