Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jedmccaleb/NewCoin into api
Browse files Browse the repository at this point in the history
  • Loading branch information
jed committed Nov 4, 2012
2 parents c2367c5 + 8045059 commit f180da5
Show file tree
Hide file tree
Showing 13 changed files with 405 additions and 186 deletions.
5 changes: 4 additions & 1 deletion js/nodeutils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
var async = require("async");
var fs = require("fs");
var path = require("path");

var utils = require("./utils.js");

// Empty a directory.
// done(err) : err = true if an error occured.
var emptyPath = function(dirPath, done) {
fs.readdir(dirPath, function(err, files) {
if (err) {
done(err);
}
else {
utils.mapOr(rmPath, files.map(function(f) { return path.join(dirPath, f); }), done);
async.some(files.map(function(f) { return path.join(dirPath, f); }), rmPath, done);
}
});
};
Expand Down Expand Up @@ -53,6 +55,7 @@ var resetPath = function(dirPath, mode, done) {
};

// Remove path recursively.
// done(err)
var rmPath = function(dirPath, done) {
// console.log("rmPath: %s", dirPath);

Expand Down
84 changes: 62 additions & 22 deletions js/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ var config = require('../test/config.js');
var Request = function (remote, command) {
var self = this;

this.message = {
this.message = {
'command' : command,
'id' : undefined,
};
this.remote = remote;
this.remote = remote;
this.requested = false;

this.on('request', function () {
self.request_default();
Expand Down Expand Up @@ -68,7 +69,10 @@ Request.prototype.request = function (remote) {
};

Request.prototype.request_default = function () {
this.remote.request(this);
if (!this.requested) {
this.requested = true;
this.remote.request(this);
}
};

Request.prototype.ledger_choose = function (current) {
Expand Down Expand Up @@ -572,6 +576,7 @@ Remote.prototype.submit = function (transaction) {
else {
if (!transaction.transaction.Sequence) {
transaction.transaction.Sequence = this.account_seq(transaction.transaction.Account, 'ADVANCE');
// console.log("Sequence: %s", transaction.transaction.Sequence);
}

if (!transaction.transaction.Sequence) {
Expand All @@ -581,7 +586,7 @@ Remote.prototype.submit = function (transaction) {
// Try again.
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.

// Look in the current ledger.
Expand All @@ -590,7 +595,7 @@ Remote.prototype.submit = function (transaction) {
// Try again.
self.submit(transaction);
})
.on('error', function (message) {
.on('error_account_seq_cache', function (message) {
// Forward errors.
transaction.emit('error', message);
})
Expand Down Expand Up @@ -685,6 +690,11 @@ Remote.prototype.account_seq = function (account, advance) {
seq = account_info.seq;

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;
Expand All @@ -701,21 +711,40 @@ Remote.prototype.set_account_seq = function (account, seq) {
// Return a request to refresh accounts[account].seq.
Remote.prototype.account_seq_cache = function (account, current) {
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];

// If the caller also waits for 'success', they might run before this.
request.emit('success_account_seq_cache');
});
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.
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.
Expand Down Expand Up @@ -1042,6 +1071,16 @@ Transaction.prototype.send_max = function (send_max) {
return this;
}

// --> rate: In billionths.
Transaction.prototype.transfer_rate = function (rate) {
this.transaction.TransferRate = Number(rate);

if (this.transaction.TransferRate < 1e9)
throw 'invalidTransferRate';

return this;
}

// Add flags to a transaction.
// --> flags: undefined, _flag_, or [ _flags_ ]
Transaction.prototype.set_flags = function (flags) {
Expand Down Expand Up @@ -1081,14 +1120,15 @@ Transaction.prototype._account_secret = function (account) {
return this.remote.config.accounts[account] ? this.remote.config.accounts[account].secret : undefined;
};

// .wallet_locator()
// .message_key()
// .domain()
// .transfer_rate()
// .publish()
// Options:
// .domain() NYI
// .message_key() NYI
// .transfer_rate()
// .wallet_locator() NYI
Transaction.prototype.account_set = function (src) {
this.secret = this._account_secret(src);
this.transaction.TransactionType = 'AccountSet';
this.transaction.Account = UInt160.json_rewrite(src);

return this;
};
Expand Down
19 changes: 0 additions & 19 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,6 @@ var throwErr = function(done) {
};
};

// apply function to elements of array. Return first true value to done or undefined.
var mapOr = function(func, array, done) {
if (array.length) {
func(array[array.length-1], function(v) {
if (v) {
done(v);
}
else {
array.length -= 1;
mapOr(func, array, done);
}
});
}
else {
done();
}
};

var trace = function(comment, func) {
return function() {
console.log("%s: %s", trace, arguments.toString);
Expand Down Expand Up @@ -88,7 +70,6 @@ var stringToArray = function (s) {
return a;
};

exports.mapOr = mapOr;
exports.trace = trace;
exports.arraySet = arraySet;
exports.hexToString = hexToString;
Expand Down
3 changes: 3 additions & 0 deletions src/FieldNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ SField sfIndex(STI_HASH256, 258, "index");

static int initFields()
{
sfTxnSignature.notSigningField(); sfTxnSignatures.notSigningField();
sfSignature.notSigningField();

sfHighQualityIn.setMeta(SFM_CHANGE); sfHighQualityOut.setMeta(SFM_CHANGE);
sfLowQualityIn.setMeta(SFM_CHANGE); sfLowQualityOut.setMeta(SFM_CHANGE);

Expand Down
11 changes: 9 additions & 2 deletions src/FieldNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ class SField
const int fieldValue; // Code number for protocol
std::string fieldName;
SF_Meta fieldMeta;
bool signingField;

SField(int fc, SerializedTypeID tid, int fv, const char* fn) :
fieldCode(fc), fieldType(tid), fieldValue(fv), fieldName(fn), fieldMeta(SFM_NEVER)
fieldCode(fc), fieldType(tid), fieldValue(fv), fieldName(fn), fieldMeta(SFM_NEVER), signingField(true)
{
boost::mutex::scoped_lock sl(mapMutex);
codeToField[fieldCode] = this;
}

SField(SerializedTypeID tid, int fv, const char *fn) :
fieldCode(FIELD_CODE(tid, fv)), fieldType(tid), fieldValue(fv), fieldName(fn), fieldMeta(SFM_NEVER)
fieldCode(FIELD_CODE(tid, fv)), fieldType(tid), fieldValue(fv), fieldName(fn),
fieldMeta(SFM_NEVER), signingField(true)
{
boost::mutex::scoped_lock sl(mapMutex);
codeToField[fieldCode] = this;
Expand Down Expand Up @@ -97,6 +99,11 @@ class SField
bool shouldMetaDel() const { return (fieldMeta == SFM_DELETE) || (fieldMeta == SFM_ALWAYS); }
bool shouldMetaMod() const { return (fieldMeta == SFM_CHANGE) || (fieldMeta == SFM_ALWAYS); }
void setMeta(SF_Meta m) { fieldMeta = m; }
bool isSigningField() const { return signingField; }
void notSigningField() { signingField = false; }

bool shouldInclude(bool withSigningField) const
{ return (fieldValue < 256) && (withSigningField || signingField); }

bool operator==(const SField& f) const { return fieldCode == f.fieldCode; }
bool operator!=(const SField& f) const { return fieldCode != f.fieldCode; }
Expand Down
Loading

0 comments on commit f180da5

Please sign in to comment.