Merge pull request #360 from isocolsky/uniq_addresses
Check for duplicate addresses
This commit is contained in:
commit
b7c1f96c94
|
@ -385,14 +385,33 @@ Storage.prototype.fetchAddresses = function(walletId, cb) {
|
|||
|
||||
Storage.prototype.storeAddressAndWallet = function(wallet, addresses, cb) {
|
||||
var self = this;
|
||||
|
||||
var addresses = [].concat(addresses);
|
||||
if (addresses.length == 0) return cb();
|
||||
this.db.collection(collections.ADDRESSES).insert(addresses, {
|
||||
|
||||
async.filter(addresses, function(address, next) {
|
||||
self.db.collection(collections.ADDRESSES).findOne({
|
||||
address: address.address,
|
||||
}, {
|
||||
walletId: true,
|
||||
}, function(err, result) {
|
||||
if (err || !result) return next(true);
|
||||
if (result.walletId != wallet.id) {
|
||||
log.warn('Address ' + address.address + ' exists in more than one wallet.');
|
||||
return next(true);
|
||||
}
|
||||
// Ignore if address was already in wallet
|
||||
return next(false);
|
||||
});
|
||||
}, function(newAddresses) {
|
||||
if (newAddresses.length == 0) return cb();
|
||||
self.db.collection(collections.ADDRESSES).insert(newAddresses, {
|
||||
w: 1
|
||||
}, function(err) {
|
||||
if (err) return cb(err);
|
||||
self.storeWallet(wallet, cb);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Storage.prototype.fetchAddress = function(address, cb) {
|
||||
|
|
|
@ -1476,6 +1476,22 @@ describe('Wallet service', function() {
|
|||
});
|
||||
});
|
||||
|
||||
it('should protect against storing same address multiple times', function(done) {
|
||||
server.createAddress({}, function(err, address) {
|
||||
should.not.exist(err);
|
||||
should.exist(address);
|
||||
delete address._id;
|
||||
server.storage.storeAddressAndWallet(wallet, address, function(err) {
|
||||
should.not.exist(err);
|
||||
server.getMainAddresses({}, function(err, addresses) {
|
||||
should.not.exist(err);
|
||||
addresses.length.should.equal(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should create many addresses on simultaneous requests', function(done) {
|
||||
var N = 5;
|
||||
async.map(_.range(N), function(i, cb) {
|
||||
|
|
Loading…
Reference in New Issue