Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Feat/2 step opt #718

Merged
merged 9 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/blockchainexplorers/insight.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ Insight.prototype._doRequest = function(args, cb) {
'User-Agent': this.userAgent,
}
};

if (log.level == 'verbose') {
var s = JSON.stringify(args);

if ( s.length > 100 )
s= s.substr(0,100) + '...';

log.debug('', 'Insight Q: %s', s);
}

requestList(_.defaults(args, opts), cb);
};

Expand All @@ -59,9 +69,10 @@ Insight.prototype.getUtxos = function(addresses, cb) {
json: {
addrs: _.uniq([].concat(addresses)).join(',')
},
timeout: 120000,
};

log.info('','Querying utxos: %s addrs', addresses.length);

this._doRequest(args, function(err, res, unspent) {
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));
return cb(null, unspent);
Expand Down Expand Up @@ -122,6 +133,9 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
timeout: 120000,
};


log.info('','Querying addresses: %s addrs', addresses.length);

this._doRequest(args, function(err, res, txs) {
if (err || res.statusCode !== 200) return cb(_parseErr(err, res));

Expand Down
12 changes: 12 additions & 0 deletions lib/blockchainexplorers/request-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,23 @@ var requestList = function(args, cb) {
async.whilst(
function() {
nextUrl = urls.shift();
if (!nextUrl && success === 'false')
log.warn('no more servers to test for the request');
return nextUrl && !success;
},
function(a_cb) {
args.uri = nextUrl;

var time = 0;
var interval = setInterval(function() {
time += 10;
log.debug('', 'Delayed insight query: %s, time: %d s', args.uri, time);
}, 10000);

request(args, function(err, res, body) {
clearInterval(interval);
sucess = false;

if (err) {
log.warn('REQUEST FAIL: ' + nextUrl + ' ERROR: ' + err);
}
Expand Down
24 changes: 19 additions & 5 deletions lib/blockchainmonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ BlockchainMonitor.prototype._handleThirdPartyBroadcasts = function(data, process

BlockchainMonitor.prototype._handleIncomingPayments = function(coin, network, data) {
var self = this;

if (!data || !data.vout) return;

var outs = _.compact(_.map(data.vout, function(v) {
Expand Down Expand Up @@ -197,7 +196,7 @@ BlockchainMonitor.prototype._handleIncomingPayments = function(coin, network, da
walletId: walletId,
});
self.storage.softResetTxHistoryCache(walletId, function() {
self._updateActiveAddresses(address, function() {
self._updateAddressesWithBalance(address, function() {
self._storeAndBroadcastNotification(notification, next);
});
});
Expand All @@ -208,14 +207,29 @@ BlockchainMonitor.prototype._handleIncomingPayments = function(coin, network, da
});
};

BlockchainMonitor.prototype._updateActiveAddresses = function(address, cb) {
BlockchainMonitor.prototype._updateAddressesWithBalance = function(address, cb) {

var self = this;

self.storage.storeActiveAddresses(address.walletId, address.address, function(err) {
self.storage.fetchAddressesWithBalance(address.walletId, function(err, result) {
if (err) {
log.warn('Could not update wallet cache', err);
return cb(err);
}
return cb(err);
var addresses = _.map(result,'address');

if (_.indexOf(addresses, address.address) >= 0) {
return cb();
}

addresses.push(address.address);
log.info('Activating address '+address);
self.storage.storeAddressesWithBalance(address.walletId, addresses, function(err) {
if (err) {
log.warn('Could not update wallet cache', err);
}
return cb(err);
});
});
};

Expand Down
6 changes: 6 additions & 0 deletions lib/common/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ Defaults.FEE_LEVELS_FALLBACK = 2;
// Minimum nb of addresses a wallet must have to start using 2-step balance optimization
Defaults.TWO_STEP_BALANCE_THRESHOLD = 100;

// Age Limit for addresses to be considered 'active' always
Defaults.TWO_STEP_CREATION_HOURS = 24;

// Time to prevent re-quering inactive addresses (MIN)
Defaults.TWO_STEP_INACTIVE_CLEAN_DURATION_MIN = 60;

Defaults.FIAT_RATE_PROVIDER = 'BitPay';
Defaults.FIAT_RATE_FETCH_INTERVAL = 10; // In minutes
Defaults.FIAT_RATE_MAX_LOOK_BACK_TIME = 120; // In minutes
Expand Down
2 changes: 1 addition & 1 deletion lib/errors/errordefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var errors = {
UPGRADE_NEEDED: 'Client app needs to be upgraded',
WALLET_ALREADY_EXISTS: 'Wallet already exists',
WALLET_FULL: 'Wallet full',
WALLET_LOCKED: 'Wallet is locked',
WALLET_BUSY: 'Wallet is busy, try later',
WALLET_NOT_COMPLETE: 'Wallet is not complete',
WALLET_NOT_FOUND: 'Wallet not found',
};
Expand Down
13 changes: 6 additions & 7 deletions lib/expressapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Stats = require('./stats');

log.disableColor();
log.debug = log.verbose;
log.level = 'info';
log.level = 'verbose';

var ExpressApp = function() {
this.app = express();
Expand Down Expand Up @@ -75,14 +75,10 @@ ExpressApp.prototype.start = function(opts, cb) {
} else {
var morgan = require('morgan');
morgan.token('walletId', function getId(req) {
return req.walletId
return req.walletId ? '<' + req.walletId + '>' : '<>';
});

morgan.token('copayerId', function getId(req) {
return req.copayerId
});

var logFormat = ':remote-addr :date[iso] ":method :url" :status :res[content-length] :response-time ":user-agent" :walletId :copayerId';
var logFormat = ':walletId :remote-addr :date[iso] ":method :url" :status :res[content-length] :response-time ":user-agent" ';
var logOpts = {
skip: function(req, res) {
if (res.statusCode != 200) return false;
Expand Down Expand Up @@ -141,6 +137,7 @@ ExpressApp.prototype.start = function(opts, cb) {
};

function getServer(req, res) {
log.heading = '<>';
var opts = {
clientVersion: req.header('x-client-version'),
};
Expand Down Expand Up @@ -183,6 +180,8 @@ ExpressApp.prototype.start = function(opts, cb) {
req.walletId = server.walletId;
req.copayerId = server.copayerId;

log.heading = '<' + req.walletId + '>';

return cb(server);
});
};
Expand Down
2 changes: 1 addition & 1 deletion lib/lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Lock.prototype.runLocked = function(token, cb, task) {
$.shouldBeDefined(token);

this.lock.locked(token, 5 * 1000, 5 * 60 * 1000, function(err, release) {
if (err) return cb(Errors.WALLET_LOCKED);
if (err) return cb(Errors.WALLET_BUSY);
var _cb = function() {
cb.apply(null, arguments);
release();
Expand Down
2 changes: 2 additions & 0 deletions lib/model/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var _ = require('lodash');
var util = require('util');
var log = require('npmlog');
var $ = require('preconditions').singleton();
var Uuid = require('uuid');

Expand Down Expand Up @@ -156,6 +157,7 @@ Wallet.prototype.createAddress = function(isChange) {
var self = this;

var path = this.addressManager.getNewAddressPath(isChange);
log.verbose('Deriving addr:' + path);
var address = Address.derive(self.id, this.addressType, this.publicKeyRing, path, this.m, this.coin, this.network, isChange);
return address;
};
Expand Down