update lastUsedOn
This commit is contained in:
parent
163418f504
commit
7c5ad7bdfa
|
@ -170,9 +170,11 @@ BlockchainMonitor.prototype._handleTxOuts = function(data) {
|
||||||
walletId: walletId,
|
walletId: walletId,
|
||||||
});
|
});
|
||||||
self.storage.softResetTxHistoryCache(walletId, function() {
|
self.storage.softResetTxHistoryCache(walletId, function() {
|
||||||
|
self.storage.updateLastUsedOn(out.address, function() {
|
||||||
self._storeAndBroadcastNotification(notification, next);
|
self._storeAndBroadcastNotification(notification, next);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
return;
|
return;
|
||||||
});
|
});
|
||||||
|
|
|
@ -11,10 +11,11 @@ function Address() {};
|
||||||
Address.create = function(opts) {
|
Address.create = function(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
|
||||||
|
var now = Math.floor(Date.now() / 1000);
|
||||||
var x = new Address();
|
var x = new Address();
|
||||||
|
|
||||||
x.version = '1.0.0';
|
x.version = '1.0.0';
|
||||||
x.createdOn = Math.floor(Date.now() / 1000);
|
x.createdOn = now;
|
||||||
x.address = opts.address;
|
x.address = opts.address;
|
||||||
x.walletId = opts.walletId;
|
x.walletId = opts.walletId;
|
||||||
x.isChange = opts.isChange;
|
x.isChange = opts.isChange;
|
||||||
|
@ -23,6 +24,7 @@ Address.create = function(opts) {
|
||||||
x.network = Bitcore.Address(x.address).toObject().network;
|
x.network = Bitcore.Address(x.address).toObject().network;
|
||||||
x.type = opts.type || Constants.SCRIPT_TYPES.P2SH;
|
x.type = opts.type || Constants.SCRIPT_TYPES.P2SH;
|
||||||
x.hasActivity = undefined;
|
x.hasActivity = undefined;
|
||||||
|
x.lastUsedOn = now;
|
||||||
return x;
|
return x;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -39,6 +41,7 @@ Address.fromObj = function(obj) {
|
||||||
x.publicKeys = obj.publicKeys;
|
x.publicKeys = obj.publicKeys;
|
||||||
x.type = obj.type || Constants.SCRIPT_TYPES.P2SH;
|
x.type = obj.type || Constants.SCRIPT_TYPES.P2SH;
|
||||||
x.hasActivity = obj.hasActivity;
|
x.hasActivity = obj.hasActivity;
|
||||||
|
x.lastUsedOn = obj.lastUsedOn;
|
||||||
return x;
|
return x;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,10 @@ Storage.prototype._createIndexes = function() {
|
||||||
walletId: 1,
|
walletId: 1,
|
||||||
createdOn: 1,
|
createdOn: 1,
|
||||||
});
|
});
|
||||||
|
this.db.collection(collections.ADDRESSES).createIndex({
|
||||||
|
walletId: 1,
|
||||||
|
lastUsedOn: 1,
|
||||||
|
});
|
||||||
this.db.collection(collections.ADDRESSES).createIndex({
|
this.db.collection(collections.ADDRESSES).createIndex({
|
||||||
address: 1,
|
address: 1,
|
||||||
});
|
});
|
||||||
|
@ -845,5 +849,43 @@ Storage.prototype._dump = function(cb, fn) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Storage.prototype.updateLastUsedOn = function(address, cb) {
|
||||||
|
this.db.collection(collections.ADDRESSES).update({
|
||||||
|
address: address,
|
||||||
|
}, {
|
||||||
|
$set: {
|
||||||
|
lastUsedOn: Math.floor(Date.now() / 1000),
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
w: 1,
|
||||||
|
}, cb);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Storage.prototype.fetchRecentAddresses = function(walletId, minUsedOn, cb) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
this.db.collection(collections.ADDRESSES).find({
|
||||||
|
walletId: walletId,
|
||||||
|
$or: [{
|
||||||
|
lastUsedOn: {
|
||||||
|
$gte: minUsedOn,
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
lastUsedOn: null,
|
||||||
|
}],
|
||||||
|
}).toArray(function(err, result) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
if (!result) return cb();
|
||||||
|
var addresses = _.map(result, function(address) {
|
||||||
|
return Model.Address.fromObj(address);
|
||||||
|
});
|
||||||
|
return cb(null, addresses);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Storage.collections = collections;
|
Storage.collections = collections;
|
||||||
module.exports = Storage;
|
module.exports = Storage;
|
||||||
|
|
|
@ -42,7 +42,6 @@ describe('Blockchain monitor', function() {
|
||||||
helpers.createAndJoinWallet(2, 3, function(s, w) {
|
helpers.createAndJoinWallet(2, 3, function(s, w) {
|
||||||
server = s;
|
server = s;
|
||||||
wallet = w;
|
wallet = w;
|
||||||
|
|
||||||
var bcmonitor = new BlockchainMonitor();
|
var bcmonitor = new BlockchainMonitor();
|
||||||
bcmonitor.start({
|
bcmonitor.start({
|
||||||
lockOpts: {},
|
lockOpts: {},
|
||||||
|
@ -61,16 +60,21 @@ describe('Blockchain monitor', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should notify copayers of incoming txs', function(done) {
|
it('should notify copayers of incoming txs', function(done) {
|
||||||
server.createAddress({}, function(err, address) {
|
|
||||||
should.not.exist(err);
|
|
||||||
|
|
||||||
var incoming = {
|
var incoming = {
|
||||||
txid: '123',
|
txid: '123',
|
||||||
vout: [{}],
|
vout: [{}],
|
||||||
};
|
};
|
||||||
incoming.vout[0][address.address] = 1500;
|
var address;
|
||||||
|
|
||||||
|
server.createAddress({}, function(err, a) {
|
||||||
|
should.not.exist(err);
|
||||||
|
address = a.address;
|
||||||
|
|
||||||
|
incoming.vout[0][address] = 1500;
|
||||||
socket.handlers['tx'](incoming);
|
socket.handlers['tx'](incoming);
|
||||||
|
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
server.getNotifications({}, function(err, notifications) {
|
server.getNotifications({}, function(err, notifications) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
|
@ -80,11 +84,51 @@ describe('Blockchain monitor', function() {
|
||||||
should.exist(notification);
|
should.exist(notification);
|
||||||
notification.walletId.should.equal(wallet.id);
|
notification.walletId.should.equal(wallet.id);
|
||||||
notification.data.txid.should.equal('123');
|
notification.data.txid.should.equal('123');
|
||||||
notification.data.address.should.equal(address.address);
|
notification.data.address.should.equal(address);
|
||||||
notification.data.amount.should.equal(1500);
|
notification.data.amount.should.equal(1500);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
}, 100);
|
}, 100);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should update address lastUsedOn for incoming txs', function(done) {
|
||||||
|
|
||||||
|
var incoming = {
|
||||||
|
txid: '123',
|
||||||
|
vout: [{}, {}],
|
||||||
|
};
|
||||||
|
var address, address2;
|
||||||
|
var aLongTimeAgo = Date.now() - (1000 * 10 * 86400);
|
||||||
|
|
||||||
|
var clock = sinon.useFakeTimers(aLongTimeAgo, 'Date');
|
||||||
|
|
||||||
|
server.createAddress({}, function(err, a) {
|
||||||
|
should.not.exist(err);
|
||||||
|
address = a.address;
|
||||||
|
|
||||||
|
server.createAddress({}, function(err, a2) {
|
||||||
|
should.not.exist(err);
|
||||||
|
address2 = a2.address;
|
||||||
|
|
||||||
|
clock.restore();
|
||||||
|
|
||||||
|
storage.fetchRecentAddresses(wallet.id, (Date.now() / 1000) - 100, function(err, addr) {
|
||||||
|
addr.length.should.equal(0);
|
||||||
|
|
||||||
|
incoming.vout[0][address] = 1500;
|
||||||
|
incoming.vout[1][address2] = 150;
|
||||||
|
|
||||||
|
socket.handlers['tx'](incoming);
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
storage.fetchRecentAddresses(wallet.id, (Date.now() / 1000) - 100, function(err, addr) {
|
||||||
|
addr.length.should.equal(2);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}, 50);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue