walletdb and txpool.
This commit is contained in:
parent
ef55d1cebc
commit
4df5b305ea
@ -67,7 +67,7 @@ HTTPServer.prototype._init = function _init() {
|
||||
});
|
||||
|
||||
// UTXO by address
|
||||
this.get('/utxo/address/:address', function(req, res, next, send) {
|
||||
this.get('/coin/address/:address', function(req, res, next, send) {
|
||||
var addresses = req.params.address.split(',');
|
||||
self.node.getCoinByAddress(addresses, function(err, coins) {
|
||||
if (err)
|
||||
@ -81,7 +81,7 @@ HTTPServer.prototype._init = function _init() {
|
||||
});
|
||||
|
||||
// UTXO by id
|
||||
this.get('/utxo/:hash/:index', function(req, res, next, send) {
|
||||
this.get('/coin/:hash/:index', function(req, res, next, send) {
|
||||
req.params.hash = utils.revHex(req.params.hash);
|
||||
self.node.getCoin(req.params.hash, +req.params.index, function(err, coin) {
|
||||
if (err)
|
||||
@ -95,7 +95,7 @@ HTTPServer.prototype._init = function _init() {
|
||||
});
|
||||
|
||||
// Bulk read UTXOs
|
||||
this.post('/utxo/address', function(req, res, next, send) {
|
||||
this.post('/coin/address', function(req, res, next, send) {
|
||||
self.node.getCoinByAddress(req.body.addresses, function(err, coins) {
|
||||
if (err)
|
||||
return next(err);
|
||||
@ -228,8 +228,8 @@ HTTPServer.prototype._init = function _init() {
|
||||
});
|
||||
|
||||
// Wallet UTXOs
|
||||
this.get('/wallet/:id/utxo', function(req, res, next, send) {
|
||||
self.node.walletdb.getUnspent(req.params.id, function(err, coins) {
|
||||
this.get('/wallet/:id/coin', function(req, res, next, send) {
|
||||
self.node.walletdb.getCoins(req.params.id, function(err, coins) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
@ -241,7 +241,7 @@ HTTPServer.prototype._init = function _init() {
|
||||
});
|
||||
|
||||
// Wallet TXs
|
||||
this.get('/wallet/:id/tx', function(req, res, next, send) {
|
||||
this.get('/wallet/:id/all', function(req, res, next, send) {
|
||||
self.node.walletdb.getAll(req.params.id, function(err, txs) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
@ -109,7 +109,7 @@ Fullnode.prototype._init = function _init() {
|
||||
});
|
||||
|
||||
this.on('tx', function(tx) {
|
||||
self.walletdb.tx.addTX(tx, function(err) {
|
||||
self.walletdb.addTX(tx, function(err) {
|
||||
if (err)
|
||||
self.emit('error', err);
|
||||
});
|
||||
@ -134,19 +134,7 @@ Fullnode.prototype._init = function _init() {
|
||||
|
||||
this.chain.on('remove block', function(block) {
|
||||
self.mempool.removeBlock(block);
|
||||
});
|
||||
|
||||
// Handle forks by unconfirming txs
|
||||
// in our wallets' tx pools.
|
||||
this.chain.on('remove entry', function(entry) {
|
||||
self.walletdb.tx.getHeightHashes(entry.height, function(err, txs) {
|
||||
if (err)
|
||||
return self.emit('error', err);
|
||||
|
||||
txs.forEach(function(tx) {
|
||||
self.walletdb.tx.unconfirm(tx);
|
||||
});
|
||||
});
|
||||
self.walletdb.removeBlock(block);
|
||||
});
|
||||
|
||||
function load() {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -513,12 +513,12 @@ Wallet.prototype.fill = function fill(tx, options, callback) {
|
||||
|
||||
assert(this._initialized);
|
||||
|
||||
this.getUnspent(function(err, unspent) {
|
||||
this.getCoins(function(err, coins) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
try {
|
||||
tx.fill(unspent, {
|
||||
tx.fill(coins, {
|
||||
selection: options.selection || 'age',
|
||||
fee: options.fee,
|
||||
subtractFee: options.subtractFee,
|
||||
@ -814,11 +814,11 @@ Wallet.prototype.getAll = function getAll(callback) {
|
||||
return this.provider.getAll(this, callback);
|
||||
};
|
||||
|
||||
Wallet.prototype.getUnspent = function getUnspent(callback) {
|
||||
Wallet.prototype.getCoins = function getCoins(callback) {
|
||||
if (!this.provider)
|
||||
return callback(new Error('No wallet provider available.'));
|
||||
|
||||
return this.provider.getUnspent(this, callback);
|
||||
return this.provider.getCoins(this, callback);
|
||||
};
|
||||
|
||||
Wallet.prototype.getPending = function getPending(callback) {
|
||||
|
||||
@ -14,6 +14,7 @@ var network = bcoin.protocol.network;
|
||||
var utils = bcoin.utils;
|
||||
var assert = utils.assert;
|
||||
var fs = bcoin.fs;
|
||||
var DUMMY = new Buffer([]);
|
||||
|
||||
/**
|
||||
* WalletDB
|
||||
@ -117,44 +118,39 @@ WalletDB.prototype._init = function _init() {
|
||||
this.db = WalletDB._db[this.file];
|
||||
|
||||
this.tx = new bcoin.txdb('w', this.db, {
|
||||
addressFilter: this.hasAddress.bind(this)
|
||||
ids: true
|
||||
});
|
||||
|
||||
this.tx.on('error', function(err) {
|
||||
self.emit('error', err);
|
||||
});
|
||||
|
||||
this.tx.on('updated', function(tx) {
|
||||
self.getIDs(tx.getOutputAddresses(), function(err, ids) {
|
||||
if (err)
|
||||
return self.emit('error', err);
|
||||
this.tx.on('updated', function(tx, map) {
|
||||
self.emit('wallet tx', tx, map);
|
||||
|
||||
self.emit('wallet tx', tx, ids);
|
||||
// Only sync for confirmed txs.
|
||||
if (tx.ts === 0)
|
||||
return;
|
||||
|
||||
// Only sync for confirmed txs.
|
||||
if (tx.ts === 0)
|
||||
return;
|
||||
|
||||
utils.forEachSerial(ids, function(id, next) {
|
||||
self.getJSON(id, function(err, json) {
|
||||
if (err) {
|
||||
self.emit('error', err);
|
||||
return next();
|
||||
}
|
||||
|
||||
// Allocate new addresses if necessary.
|
||||
json = bcoin.wallet.sync(json, { txs: [tx] });
|
||||
|
||||
self.saveJSON(id, json, function(err) {
|
||||
if (err)
|
||||
return next(err);
|
||||
next();
|
||||
});
|
||||
});
|
||||
}, function(err) {
|
||||
if (err)
|
||||
utils.forEachSerial(map.output, function(id, next) {
|
||||
self.getJSON(id, function(err, json) {
|
||||
if (err) {
|
||||
self.emit('error', err);
|
||||
return next();
|
||||
}
|
||||
|
||||
// Allocate new addresses if necessary.
|
||||
json = bcoin.wallet.sync(json, { txs: [tx] });
|
||||
|
||||
self.saveJSON(id, json, function(err) {
|
||||
if (err)
|
||||
return next(err);
|
||||
next();
|
||||
});
|
||||
});
|
||||
}, function(err) {
|
||||
if (err)
|
||||
self.emit('error', err);
|
||||
});
|
||||
});
|
||||
};
|
||||
@ -210,7 +206,7 @@ WalletDB.prototype.saveJSON = function saveJSON(id, json, callback) {
|
||||
if (json) {
|
||||
batch = self.db.batch();
|
||||
Object.keys(json.addressMap).forEach(function(address) {
|
||||
batch.put('w/a/' + address + '/' + json.id, new Buffer([]));
|
||||
batch.put('w/a/' + address + '/' + json.id, DUMMY);
|
||||
});
|
||||
return batch.write(function(err) {
|
||||
if (err)
|
||||
@ -439,18 +435,18 @@ WalletDB.prototype.update = function update(wallet, address) {
|
||||
|
||||
batch.put(
|
||||
'w/a/' + address.getKeyAddress() + '/' + wallet.id,
|
||||
new Buffer([]));
|
||||
DUMMY);
|
||||
|
||||
if (address.type === 'multisig') {
|
||||
batch.put(
|
||||
'w/a/' + address.getScriptAddress() + '/' + wallet.id,
|
||||
new Buffer([]));
|
||||
DUMMY);
|
||||
}
|
||||
|
||||
if (address.witness) {
|
||||
batch.put(
|
||||
'w/a/' + address.getProgramAddress() + '/' + wallet.id,
|
||||
new Buffer([]));
|
||||
DUMMY);
|
||||
}
|
||||
|
||||
batch.write(function(err) {
|
||||
@ -468,184 +464,75 @@ WalletDB.prototype.addTX = function addTX(tx, callback) {
|
||||
return this.tx.add(tx, callback);
|
||||
};
|
||||
|
||||
WalletDB.prototype.getAll = function getAll(id, callback) {
|
||||
var self = this;
|
||||
return this.getAddresses(id, function(err, addresses) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
return self.tx.getAllByAddress(addresses, callback);
|
||||
});
|
||||
WalletDB.prototype.getTX = function getTX(hash, callback) {
|
||||
return this.tx.getTX(hash, callback);
|
||||
};
|
||||
|
||||
WalletDB.prototype.getUnspent = function getUnspent(id, callback) {
|
||||
var self = this;
|
||||
return this.getAddresses(id, function(err, addresses) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
WalletDB.prototype.getCoin = function getCoin(hash, index, callback) {
|
||||
return this.tx.getCoin(hash, index, callback);
|
||||
};
|
||||
|
||||
return self.tx.getUnspentByAddress(addresses, callback);
|
||||
});
|
||||
WalletDB.prototype.getAll = function getAll(id, callback) {
|
||||
var self = this;
|
||||
id = id.id || id;
|
||||
return this.tx.getAllByAddress(id, callback);
|
||||
};
|
||||
|
||||
WalletDB.prototype.getCoins = function getCoins(id, callback) {
|
||||
var self = this;
|
||||
id = id.id || id;
|
||||
return this.tx.getCoinsByAddress(id, callback);
|
||||
};
|
||||
|
||||
WalletDB.prototype.getPending = function getPending(id, callback) {
|
||||
var self = this;
|
||||
return this.getAddresses(id, function(err, addresses) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
return self.tx.getPendingByAddress(addresses, callback);
|
||||
});
|
||||
id = id.id || id;
|
||||
return this.tx.getPendingByAddress(id, callback);
|
||||
};
|
||||
|
||||
WalletDB.prototype.getBalance = function getBalance(id, callback) {
|
||||
var self = this;
|
||||
return this.getAddresses(id, function(err, addresses) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
return self.tx.getBalanceByAddress(addresses, callback);
|
||||
});
|
||||
id = id.id || id;
|
||||
return this.tx.getBalanceByAddress(id, callback);
|
||||
};
|
||||
|
||||
WalletDB.prototype.getLast = function getLast(id, callback) {
|
||||
var self = this;
|
||||
return this.getAddresses(id, function(err, addresses) {
|
||||
id = id.id || id;
|
||||
return this.tx.getLast(id, callback);
|
||||
};
|
||||
|
||||
WalletDB.prototype.fillTX = function fillTX(tx, callback) {
|
||||
return this.tx.fillTX(tx, callback);
|
||||
};
|
||||
|
||||
WalletDB.prototype.fillCoin = function fillCoin(tx, callback) {
|
||||
return this.tx.fillCoin(tx, callback);
|
||||
};
|
||||
|
||||
WalletDB.prototype.removeBlockSPV = function removeBlockSPV(block, callback) {
|
||||
var self = this;
|
||||
callback = utils.ensure(callback);
|
||||
this.tx.getHeightHashes(block.height, function(err, txs) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
return self.tx.getLast(addresses, callback);
|
||||
txs.forEach(function(tx) {
|
||||
self.tx.unconfirm(tx);
|
||||
});
|
||||
|
||||
callback();
|
||||
});
|
||||
};
|
||||
|
||||
WalletDB.prototype.getAddresses = function getAddresses(id, callback) {
|
||||
// Try to avoid a database lookup if we can...
|
||||
if (typeof id === 'string' && bcoin.address.validate(id))
|
||||
return callback(null, [id]);
|
||||
|
||||
if (Array.isArray(id))
|
||||
return callback(null, id);
|
||||
|
||||
if (id.addressMap)
|
||||
return callback(null, Object.keys(id.addressMap));
|
||||
|
||||
if (typeof id === 'object')
|
||||
return callback(null, Object.keys(id));
|
||||
|
||||
return this.db.get('w/w/' + id, function(err, buf) {
|
||||
var json;
|
||||
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
try {
|
||||
json = JSON.parse(buf.toString('utf8'));
|
||||
} catch (e) {
|
||||
return callback(e);
|
||||
}
|
||||
|
||||
return callback(null, Object.keys(json.addressMap));
|
||||
});
|
||||
};
|
||||
|
||||
WalletDB.prototype.hasAddress = function hasAddress(address, callback) {
|
||||
WalletDB.prototype.removeBlock = function removeBlock(block, callback) {
|
||||
var self = this;
|
||||
|
||||
callback = utils.ensure(callback);
|
||||
|
||||
if (!address)
|
||||
return callback(null, false);
|
||||
|
||||
var iter = this.db.db.iterator({
|
||||
gte: 'w/a/' + address,
|
||||
lte: 'w/a/' + address + '~',
|
||||
keys: true,
|
||||
values: false,
|
||||
fillCache: false,
|
||||
keyAsBuffer: false
|
||||
});
|
||||
|
||||
(function next() {
|
||||
iter.next(function(err, key, value) {
|
||||
if (err) {
|
||||
return iter.end(function() {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
if (key === undefined) {
|
||||
return iter.end(function(err) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
return callback(null, false);
|
||||
});
|
||||
}
|
||||
|
||||
return iter.end(function(err) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
callback(null, true);
|
||||
});
|
||||
});
|
||||
})();
|
||||
};
|
||||
|
||||
WalletDB.prototype.getIDs = function getIDs(address, callback) {
|
||||
var self = this;
|
||||
var ids = [];
|
||||
|
||||
if (Array.isArray(address)) {
|
||||
return utils.forEachSerial(address, function(address, next) {
|
||||
self.getIDs(address, function(err, id) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
ids = ids.concat(id);
|
||||
|
||||
next();
|
||||
});
|
||||
}, function(err) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
ids = utils.uniqs(ids);
|
||||
|
||||
return callback(null, ids);
|
||||
});
|
||||
}
|
||||
|
||||
var iter = this.db.db.iterator({
|
||||
gte: 'w/a/' + address,
|
||||
lte: 'w/a/' + address + '~',
|
||||
keys: true,
|
||||
values: false,
|
||||
fillCache: false,
|
||||
keyAsBuffer: false
|
||||
});
|
||||
|
||||
callback = utils.ensure(callback);
|
||||
|
||||
(function next() {
|
||||
iter.next(function(err, key, value) {
|
||||
if (err) {
|
||||
return iter.end(function() {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
if (key === undefined) {
|
||||
return iter.end(function(err) {
|
||||
if (err)
|
||||
return callback(err);
|
||||
return callback(null, ids);
|
||||
});
|
||||
}
|
||||
|
||||
ids.push(key.split('/')[3]);
|
||||
|
||||
next();
|
||||
});
|
||||
})();
|
||||
utils.forEach(block.txs, function(tx, next) {
|
||||
self.tx.unconfirm(tx.hash('hex'));
|
||||
}, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -179,30 +179,30 @@ describe('Wallet', function() {
|
||||
fake.hint = 'fake';
|
||||
|
||||
// Fake TX should temporarly change output
|
||||
// w.addTX(fake);
|
||||
// wdb.addTX(fake);
|
||||
|
||||
w.addTX(fake, function(err) {
|
||||
wdb.addTX(fake, function(err) {
|
||||
assert(!err);
|
||||
w.addTX(t4, function(err) {
|
||||
wdb.addTX(t4, function(err) {
|
||||
assert(!err);
|
||||
w.getBalance(function(err, balance) {
|
||||
assert(!err);
|
||||
assert.equal(balance.toString(10), '22500');
|
||||
w.addTX(t1, function(err) {
|
||||
wdb.addTX(t1, function(err) {
|
||||
w.getBalance(function(err, balance) {
|
||||
assert(!err);
|
||||
assert.equal(balance.toString(10), '73000');
|
||||
w.addTX(t2, function(err) {
|
||||
wdb.addTX(t2, function(err) {
|
||||
assert(!err);
|
||||
w.getBalance(function(err, balance) {
|
||||
assert(!err);
|
||||
assert.equal(balance.toString(10), '47000');
|
||||
w.addTX(t3, function(err) {
|
||||
wdb.addTX(t3, function(err) {
|
||||
assert(!err);
|
||||
w.getBalance(function(err, balance) {
|
||||
assert(!err);
|
||||
assert.equal(balance.toString(10), '22000');
|
||||
w.addTX(f1, function(err) {
|
||||
wdb.addTX(f1, function(err) {
|
||||
assert(!err);
|
||||
w.getBalance(function(err, balance) {
|
||||
assert(!err);
|
||||
@ -250,7 +250,7 @@ describe('Wallet', function() {
|
||||
t1.addInput(dummyInput);
|
||||
|
||||
// Fake TX should temporarly change output
|
||||
w1.addTX(t1, function(err) {
|
||||
wdb.addTX(t1, function(err) {
|
||||
assert(!err);
|
||||
|
||||
// Create new transaction
|
||||
@ -307,9 +307,9 @@ describe('Wallet', function() {
|
||||
t2.addInput(dummyInput);
|
||||
// Fake TX should temporarly change output
|
||||
|
||||
w1.addTX(t1, function(err) {
|
||||
wdb.addTX(t1, function(err) {
|
||||
assert(!err);
|
||||
w2.addTX(t2, function(err) {
|
||||
wdb.addTX(t2, function(err) {
|
||||
assert(!err);
|
||||
|
||||
// Create our tx with an output
|
||||
@ -319,18 +319,18 @@ describe('Wallet', function() {
|
||||
var cost = tx.getOutputValue();
|
||||
var total = cost.add(new bn(constants.tx.minFee));
|
||||
|
||||
w1.getUnspent(function(err, unspent1) {
|
||||
w1.getCoins(function(err, coins1) {
|
||||
assert(!err);
|
||||
w2.getUnspent(function(err, unspent2) {
|
||||
w2.getCoins(function(err, coins2) {
|
||||
assert(!err);
|
||||
|
||||
// Add dummy output (for `left`) to calculate maximum TX size
|
||||
tx.addOutput(w1, new bn(0));
|
||||
|
||||
// Add our unspent inputs to sign
|
||||
tx.addInput(unspent1[0]);
|
||||
tx.addInput(unspent1[1]);
|
||||
tx.addInput(unspent2[0]);
|
||||
tx.addInput(coins1[0]);
|
||||
tx.addInput(coins1[1]);
|
||||
tx.addInput(coins2[0]);
|
||||
|
||||
var left = tx.getInputValue().sub(total);
|
||||
if (left.cmpn(constants.tx.dustThreshold) < 0) {
|
||||
@ -351,9 +351,9 @@ describe('Wallet', function() {
|
||||
|
||||
// Sign transaction using `inputs` and `off` params.
|
||||
tx.inputs.length = 0;
|
||||
tx.addInput(unspent1[1]);
|
||||
tx.addInput(unspent1[2]);
|
||||
tx.addInput(unspent2[1]);
|
||||
tx.addInput(coins1[1]);
|
||||
tx.addInput(coins1[2]);
|
||||
tx.addInput(coins2[1]);
|
||||
assert.equal(w1.sign(tx, 'all'), 2);
|
||||
assert.equal(w2.sign(tx, 'all'), 1);
|
||||
|
||||
@ -438,11 +438,11 @@ describe('Wallet', function() {
|
||||
|
||||
assert.equal(w1.receiveDepth, 1);
|
||||
|
||||
w1.addTX(utx, function(err) {
|
||||
wdb.addTX(utx, function(err) {
|
||||
assert(!err);
|
||||
w2.addTX(utx, function(err) {
|
||||
wdb.addTX(utx, function(err) {
|
||||
assert(!err);
|
||||
w3.addTX(utx, function(err) {
|
||||
wdb.addTX(utx, function(err) {
|
||||
assert(!err);
|
||||
|
||||
assert.equal(w1.receiveDepth, 2);
|
||||
@ -479,11 +479,11 @@ describe('Wallet', function() {
|
||||
send.ts = 1;
|
||||
send.height = 1;
|
||||
|
||||
w1.addTX(send, function(err) {
|
||||
wdb.addTX(send, function(err) {
|
||||
assert(!err);
|
||||
w2.addTX(send, function(err) {
|
||||
wdb.addTX(send, function(err) {
|
||||
assert(!err);
|
||||
w3.addTX(send, function(err) {
|
||||
wdb.addTX(send, function(err) {
|
||||
assert(!err);
|
||||
|
||||
assert.equal(w1.receiveDepth, 2);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user