refactor: fix cli. finish rpc.

This commit is contained in:
Christopher Jeffrey 2016-09-21 10:46:07 -07:00
parent b616d75128
commit df23810b0c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 1255 additions and 1401 deletions

835
bin/cli
View File

@ -4,6 +4,7 @@
var config = require('../lib/node/config'); var config = require('../lib/node/config');
var utils = require('../lib/utils/utils'); var utils = require('../lib/utils/utils');
var spawn = require('../lib/utils/spawn');
var Client = require('../lib/http/client'); var Client = require('../lib/http/client');
var Wallet = require('../lib/http/wallet'); var Wallet = require('../lib/http/wallet');
var assert = utils.assert; var assert = utils.assert;
@ -26,195 +27,161 @@ CLI.prototype.log = function log(json) {
console.log(JSON.stringify(json, null, 2)); console.log(JSON.stringify(json, null, 2));
}; };
CLI.prototype.createWallet = function createWallet(callback) { CLI.prototype.createWallet = function createWallet() {
var self = this; return spawn(function *() {
var options = { id: this.argv[0] }; var options = { id: this.argv[0] };
var wallet;
if (this.config.type) if (this.config.type)
options.type = this.config.type; options.type = this.config.type;
if (this.config.master) if (this.config.master)
options.master = this.config.master; options.master = this.config.master;
if (this.config.key) if (this.config.key)
options.key = this.config.key; options.key = this.config.key;
if (this.config.m) if (this.config.m)
options.m = this.config.m >>> 0; options.m = this.config.m >>> 0;
if (this.config.n) if (this.config.n)
options.n = this.config.n >>> 0; options.n = this.config.n >>> 0;
if (this.config.witness != null) if (this.config.witness != null)
options.witness = !!this.config.witness; options.witness = !!this.config.witness;
if (this.config.passphrase) if (this.config.passphrase)
options.passphrase = this.config.passphrase; options.passphrase = this.config.passphrase;
this.client.createWallet(options, function(err, wallet) { wallet = yield this.client.createWallet(options);
if (err) this.log(wallet);
return callback(err); }, this);
self.log(wallet);
callback();
});
}; };
CLI.prototype.addKey = function addKey(callback) { CLI.prototype.addKey = function addKey() {
var self = this; return spawn(function *() {
var key = this.argv[0]; var key = this.argv[0];
this.wallet.addKey(this.config.account, key, function(err, wallet) { yield this.wallet.addKey(this.config.account, key);
if (err) this.log('added');
return callback(err); }, this);
self.log('added');
callback();
});
}; };
CLI.prototype.removeKey = function removeKey(callback) { CLI.prototype.removeKey = function removeKey() {
var self = this; return spawn(function *() {
var key = this.argv[0]; var key = this.argv[0];
this.wallet.removeKey(this.config.account, key, function(err) { yield this.wallet.removeKey(this.config.account, key);
if (err) this.log('removed');
return callback(err); }, this);
self.log('removed');
callback();
});
}; };
CLI.prototype.getAccount = function getAccount(callback) { CLI.prototype.getAccount = function getAccount() {
var self = this; return spawn(function *() {
var account = this.argv[0] || this.config.account; var account = this.argv[0] || this.config.account;
this.wallet.getAccount(account, function(err, account) { yield this.wallet.getAccount(account);
if (err) this.log(account);
return callback(err); }, this);
self.log(account);
callback();
});
}; };
CLI.prototype.createAccount = function createAccount(callback) { CLI.prototype.createAccount = function createAccount() {
var self = this; return spawn(function *() {
var account = this.argv[0]; var name = this.argv[0];
this.wallet.createAccount(account, function(err, account) { var account = yield this.wallet.createAccount(name);
if (err) this.log(account);
return callback(err); }, this);
self.log(account);
callback();
});
}; };
CLI.prototype.createAddress = function createAddress(callback) { CLI.prototype.createAddress = function createAddress() {
var self = this; return spawn(function *() {
var account = this.argv[0]; var account = this.argv[0];
this.wallet.createAddress(account, function(err, account) { var addr = yield this.wallet.createAddress(account);
if (err) this.log(addr);
return callback(err); }, this);
self.log(account);
callback();
});
}; };
CLI.prototype.getAccounts = function getAccounts(callback) { CLI.prototype.getAccounts = function getAccounts() {
var self = this; return spawn(function *() {
this.wallet.getAccounts(function(err, accounts) { var accounts = yield this.wallet.getAccounts();
if (err) this.log(accounts);
return callback(err); }, this);
self.log(accounts);
callback();
});
}; };
CLI.prototype.getWallet = function getWallet(callback) { CLI.prototype.getWallet = function getWallet() {
var self = this; return spawn(function *() {
this.wallet.getInfo(function(err, wallet) { var info = yield this.wallet.getInfo();
if (err) this.log(wallet);
return callback(err); }, this);
self.log(wallet);
callback();
});
}; };
CLI.prototype.getTX = function getTX(callback) { CLI.prototype.getTX = function getTX() {
var self = this; return spawn(function *() {
var hash = this.argv[0]; var hash = this.argv[0];
if (utils.isBase58(hash)) { var txs, tx;
return this.client.getTXByAddress(hash, function(err, txs) {
if (err) if (utils.isBase58(hash)) {
return callback(err); txs = yield this.client.getTXByAddress(hash);
self.log(txs); this.log(txs);
callback(); return;
}); }
}
this.client.getTX(hash, function(err, tx) { tx = yield this.client.getTX(hash);
if (err)
return callback(err);
if (!tx) { if (!tx) {
self.log('TX not found.'); this.log('TX not found.');
return callback(); return;
} }
self.log(tx); this.log(tx);
callback(); }, this);
});
}; };
CLI.prototype.getBlock = function getBlock(callback) { CLI.prototype.getBlock = function getBlock() {
var self = this; return spawn(function *() {
var hash = this.argv[0]; var hash = this.argv[0];
if (hash.length !== 64) if (hash.length !== 64)
hash = +hash; hash = +hash;
this.client.getBlock(hash, function(err, block) {
if (err) block = yield this.client.getBlock(hash);
return callback(err);
if (!block) { if (!block) {
self.log('Block not found.'); this.log('Block not found.');
return callback(); return
} }
self.log(block); this.log(block);
callback(); }, this);
});
}; };
CLI.prototype.getCoin = function getCoin(callback) { CLI.prototype.getCoin = function getCoin() {
var self = this; return spawn(function *() {
var hash = this.argv[0]; var hash = this.argv[0];
var index = this.argv[1]; var index = this.argv[1];
if (utils.isBase58(hash)) { var coins, coin;
return this.client.getCoinsByAddress(hash, function(err, coins) {
if (err) if (utils.isBase58(hash)) {
return callback(err); coins = yield this.client.getCoinsByAddress(hash);
self.log(coins); this.log(coins);
callback(); return;
}); }
}
this.client.getCoin(hash, index, function(err, coin) { coin = yield this.client.getCoin(hash, index);
if (err)
return callback(err);
if (!coin) { if (!coin) {
self.log('Coin not found.'); this.log('Coin not found.');
return callback(); return;
} }
self.log(coin); this.log(coin);
callback(); }, this);
});
}; };
CLI.prototype.getWalletHistory = function getWalletHistory(callback) { CLI.prototype.getWalletHistory = function getWalletHistory() {
var self = this; return spawn(function *() {
this.wallet.getHistory(this.config.account, function(err, txs) { var txs = yield this.wallet.getHistory(this.config.account);
if (err) this.log(txs);
return callback(err); }, this);
self.log(txs);
callback();
});
}; };
CLI.prototype.listenWallet = function listenWallet(callback) { CLI.prototype.listenWallet = function listenWallet() {
var self = this; var self = this;
this.wallet.on('tx', function(details) { this.wallet.on('tx', function(details) {
self.log('TX:'); self.log('TX:');
@ -240,338 +207,304 @@ CLI.prototype.listenWallet = function listenWallet(callback) {
self.log('Balance:'); self.log('Balance:');
self.log(balance); self.log(balance);
}); });
return new Promise(function() {});
}; };
CLI.prototype.getBalance = function getBalance(callback) { CLI.prototype.getBalance = function getBalance() {
var self = this; return spawn(function *() {
this.wallet.getBalance(this.config.account, function(err, balance) { var balance = yield this.wallet.getBalance(this.config.account);
if (err) this.log(balance);
return callback(err); }, this);
self.log(balance);
callback();
});
}; };
CLI.prototype.getMempool = function getMempool(callback) { CLI.prototype.getMempool = function getMempool() {
var self = this; return spawn(function *() {
this.client.getMempool(function(err, txs) { var txs = yield this.client.getMempool();
if (err) this.log(txs);
return callback(err); }, this);
self.log(txs);
callback();
});
}; };
CLI.prototype.sendTX = function sendTX(callback) { CLI.prototype.sendTX = function sendTX() {
var self = this; return spawn(function *() {
var output = {}; var output = {};
var options; var options, tx;
if (this.config.script) { if (this.config.script) {
output.script = this.config.script; output.script = this.config.script;
output.value = utils.satoshi(this.config.value || this.argv[0]); output.value = utils.satoshi(this.config.value || this.argv[0]);
} else { } else {
output.address = this.config.address || this.argv[0]; output.address = this.config.address || this.argv[0];
output.value = utils.satoshi(this.config.value || this.argv[1]); output.value = utils.satoshi(this.config.value || this.argv[1]);
}
options = {
account: this.config.account,
passphrase: this.config.passphrase,
outputs: [output]
};
this.wallet.send(options, function(err, tx) {
if (err)
return callback(err);
self.log(tx);
callback();
});
};
CLI.prototype.createTX = function createTX(callback) {
var self = this;
var output = {};
var options;
if (this.config.script) {
output.script = this.config.script;
output.value = utils.satoshi(this.config.value || this.argv[0]);
} else {
output.address = this.config.address || this.argv[0];
output.value = utils.satoshi(this.config.value || this.argv[1]);
}
options = {
account: this.config.account,
passphrase: this.config.passphrase,
outputs: [output]
};
this.wallet.createTX(options, function(err, tx) {
if (err)
return callback(err);
self.log(tx);
callback();
});
};
CLI.prototype.signTX = function signTX(callback) {
var self = this;
var options = { passphrase: this.config.passphrase };
var tx = options.tx || this.argv[0];
this.wallet.sign(tx, options, function(err, tx) {
if (err)
return callback(err);
self.log(tx);
callback();
});
};
CLI.prototype.zap = function zap(callback) {
var self = this;
var age = (this.config.age >>> 0) || 72 * 60 * 60;
this.wallet.zap(this.config.account, age, function(err) {
if (err)
return callback(err);
self.log('Zapped!');
callback();
});
};
CLI.prototype.broadcast = function broadcast(callback) {
var self = this;
var tx = this.argv[0] || this.config.tx;
this.client.broadcast(tx, function(err, tx) {
if (err)
return callback(err);
self.log('Broadcasted:');
self.log(tx);
callback();
});
};
CLI.prototype.viewTX = function viewTX(callback) {
var self = this;
var tx = this.argv[0] || this.config.tx;
this.wallet.fill(tx, function(err, tx) {
if (err)
return callback(err);
self.log(tx);
callback();
});
};
CLI.prototype.getDetails = function getDetails(callback) {
var self = this;
var hash = this.argv[0];
this.wallet.getTX(hash, function(err, tx) {
if (err)
return callback(err);
self.log(tx);
callback();
});
};
CLI.prototype.retoken = function retoken(callback) {
var self = this;
this.wallet.retoken(function(err, result) {
if (err)
return callback(err);
self.log(result);
callback();
});
};
CLI.prototype.rpc = function rpc(callback) {
var self = this;
var method = this.argv.shift();
var params = [];
var i, arg, param;
for (i = 0; i < this.argv.length; i++) {
arg = this.argv[i];
try {
param = JSON.parse(arg);
} catch (e) {
param = arg;
} }
params.push(param);
}
this.client.rpc.call(method, params, function(err, result) { options = {
if (err) account: this.config.account,
return callback(err); passphrase: this.config.passphrase,
self.log(result); outputs: [output]
callback(); };
});
tx = yield this.wallet.send(options);
this.log(tx);
}, this);
}; };
CLI.prototype.handleWallet = function handleWallet(callback) { CLI.prototype.createTX = function createTX() {
var self = this; return spawn(function *() {
var output = {};
var options, tx;
var options = { if (this.config.script) {
id: this.config.id || 'primary', output.script = this.config.script;
token: this.config.token output.value = utils.satoshi(this.config.value || this.argv[0]);
}; } else {
output.address = this.config.address || this.argv[0];
this.wallet = new Wallet({ output.value = utils.satoshi(this.config.value || this.argv[1]);
uri: this.config.url || this.config.uri,
apiKey: this.config.apikey,
network: this.config.network
});
this.wallet.open(options, function(err) {
if (err)
return callback(err);
switch (self.argv.shift()) {
case 'listen':
return self.listenWallet(callback);
case 'get':
return self.getWallet(callback);
case 'addkey':
return self.addKey(callback);
case 'rmkey':
return self.removeKey(callback);
case 'balance':
return self.getBalance(callback);
case 'history':
return self.getWalletHistory(callback);
case 'account':
if (self.argv[0] === 'list') {
self.argv.shift();
return self.getAccounts(callback);
}
if (self.argv[0] === 'create') {
self.argv.shift();
return self.createAccount(callback);
}
if (self.argv[0] === 'get')
self.argv.shift();
return self.getAccount(callback);
case 'address':
return self.createAddress(callback);
case 'retoken':
return self.retoken(callback);
case 'sign':
return self.signTX(callback);
case 'mktx':
return self.createTX(callback);
case 'send':
return self.sendTX(callback);
case 'zap':
return self.zap(callback);
case 'tx':
return self.getDetails(callback);
case 'view':
return self.viewTX(callback);
default:
self.log('Unrecognized command.');
self.log('Commands:');
self.log(' $ listen: Listen for events.');
self.log(' $ get: View wallet.');
self.log(' $ addkey [xpubkey]: Add key to wallet.');
self.log(' $ rmkey [xpubkey]: Remove key from wallet.');
self.log(' $ balance: Get wallet balance.');
self.log(' $ history: View wallet TX history.');
self.log(' $ account list: List account names.');
self.log(' $ account create [account-name]: Create account.');
self.log(' $ account get [account-name]: Get account details.');
self.log(' $ address: Derive new address.');
self.log(' $ retoken: Create new api key.');
self.log(' $ send [address] [value]: Send transaction.');
self.log(' $ mktx [address] [value]: Create transaction.');
self.log(' $ sign [tx-hex]: Sign transaction.');
self.log(' $ zap --age [age]: Zap pending wallet TXs.');
self.log(' $ tx [hash]: View transaction details.');
self.log(' $ view [tx-hex]: Parse and view transaction.');
self.log('Other Options:');
self.log(' --passphrase [passphrase]: For signing and account creation.');
self.log(' --account [account-name]: Account name.');
return callback();
} }
});
options = {
account: this.config.account,
passphrase: this.config.passphrase,
outputs: [output]
};
tx = yield this.wallet.createTX(options);
this.log(tx);
}, this);
}; };
CLI.prototype.handleNode = function handleNode(callback) { CLI.prototype.signTX = function signTX() {
var self = this; return spawn(function *() {
var options = { passphrase: this.config.passphrase };
this.client = new Client({ var raw = options.tx || this.argv[0];
uri: this.config.url || this.config.uri, var tx = yield this.wallet.sign(raw, options);
apiKey: this.config.apikey, this.log(tx);
network: this.config.network }, this);
});
this.client.getInfo(function(err, info) {
if (err)
return callback(err);
switch (self.argv.shift()) {
case 'mkwallet':
return self.createWallet(callback);
case 'broadcast':
return self.broadcast(callback);
case 'mempool':
return self.getMempool(callback);
case 'tx':
return self.getTX(callback);
case 'coin':
return self.getCoin(callback);
case 'block':
return self.getBlock(callback);
case 'rpc':
return self.rpc(callback);
default:
self.log('Unrecognized command.');
self.log('Commands:');
self.log(' $ wallet create [id]: Create wallet.');
self.log(' $ broadcast [tx-hex]: Broadcast transaction.');
self.log(' $ mempool: Get mempool snapshot.');
self.log(' $ tx [hash/address]: View transactions.');
self.log(' $ coin [hash+index/address]: View coins.');
self.log(' $ block [hash/height]: View block.');
return callback();
}
});
}; };
CLI.prototype.open = function open(callback) { CLI.prototype.zap = function zap() {
switch (this.argv[0]) { return spawn(function *() {
case 'w': var age = (this.config.age >>> 0) || 72 * 60 * 60;
case 'wallet': yield this.wallet.zap(this.config.account, age);
this.argv.shift(); this.log('Zapped!');
if (this.argv[0] === 'create') { }, this);
this.argv[0] = 'mkwallet'; };
return this.handleNode(callback);
CLI.prototype.broadcast = function broadcast() {
return spawn(function *() {
var self = this;
var raw = this.argv[0] || this.config.tx;
var tx = yield this.client.broadcast(raw);
this.log('Broadcasted:');
this.log(tx);
}, this);
};
CLI.prototype.viewTX = function viewTX() {
return spawn(function *() {
var raw = this.argv[0] || this.config.tx;
var tx = yield this.wallet.fill(raw);
this.log(tx);
}, this);
};
CLI.prototype.getDetails = function getDetails() {
return spawn(function *() {
var hash = this.argv[0];
var details = yield this.wallet.getTX(hash);
this.log(details);
}, this);
};
CLI.prototype.retoken = function retoken() {
return spawn(function *() {
var result = yield this.wallet.retoken();
this.log(result);
}, this);
};
CLI.prototype.rpc = function rpc() {
return spawn(function *() {
var method = this.argv.shift();
var params = [];
var i, arg, param, result;
for (i = 0; i < this.argv.length; i++) {
arg = this.argv[i];
try {
param = JSON.parse(arg);
} catch (e) {
param = arg;
} }
return this.handleWallet(callback); params.push(param);
default: }
return this.handleNode(callback);
} result = yield this.client.rpc.call(method, params);
this.log(result);
}, this);
}; };
CLI.prototype.destroy = function destroy(callback) { CLI.prototype.handleWallet = function handleWallet() {
return spawn(function *() {
var options = {
id: this.config.id || 'primary',
token: this.config.token
};
this.wallet = new Wallet({
uri: this.config.url || this.config.uri,
apiKey: this.config.apikey,
network: this.config.network
});
yield this.wallet.open(options);
switch (this.argv.shift()) {
case 'listen':
return yield this.listenWallet();
case 'get':
return yield this.getWallet();
case 'addkey':
return yield this.addKey();
case 'rmkey':
return yield this.removeKey();
case 'balance':
return yield this.getBalance();
case 'history':
return yield this.getWalletHistory();
case 'account':
if (this.argv[0] === 'list') {
this.argv.shift();
return yield this.getAccounts();
}
if (this.argv[0] === 'create') {
this.argv.shift();
return yield this.createAccount();
}
if (this.argv[0] === 'get')
this.argv.shift();
return yield this.getAccount();
case 'address':
return yield this.createAddress();
case 'retoken':
return yield this.retoken();
case 'sign':
return yield this.signTX();
case 'mktx':
return yield this.createTX();
case 'send':
return yield this.sendTX();
case 'zap':
return yield this.zap();
case 'tx':
return yield this.getDetails();
case 'view':
return yield this.viewTX();
default:
this.log('Unrecognized command.');
this.log('Commands:');
this.log(' $ listen: Listen for events.');
this.log(' $ get: View wallet.');
this.log(' $ addkey [xpubkey]: Add key to wallet.');
this.log(' $ rmkey [xpubkey]: Remove key from wallet.');
this.log(' $ balance: Get wallet balance.');
this.log(' $ history: View wallet TX history.');
this.log(' $ account list: List account names.');
this.log(' $ account create [account-name]: Create account.');
this.log(' $ account get [account-name]: Get account details.');
this.log(' $ address: Derive new address.');
this.log(' $ retoken: Create new api key.');
this.log(' $ send [address] [value]: Send transaction.');
this.log(' $ mktx [address] [value]: Create transaction.');
this.log(' $ sign [tx-hex]: Sign transaction.');
this.log(' $ zap --age [age]: Zap pending wallet TXs.');
this.log(' $ tx [hash]: View transaction details.');
this.log(' $ view [tx-hex]: Parse and view transaction.');
this.log('Other Options:');
this.log(' --passphrase [passphrase]: For signing and account creation.');
this.log(' --account [account-name]: Account name.');
return;
}
}, this);
};
CLI.prototype.handleNode = function handleNode() {
return spawn(function *() {
var info;
this.client = new Client({
uri: this.config.url || this.config.uri,
apiKey: this.config.apikey,
network: this.config.network
});
info = yield this.client.getInfo();
switch (this.argv.shift()) {
case 'mkwallet':
return yield this.createWallet();
case 'broadcast':
return yield this.broadcast();
case 'mempool':
return yield this.getMempool();
case 'tx':
return yield this.getTX();
case 'coin':
return yield this.getCoin();
case 'block':
return yield this.getBlock();
case 'rpc':
return yield this.rpc();
default:
this.log('Unrecognized command.');
this.log('Commands:');
this.log(' $ wallet create [id]: Create wallet.');
this.log(' $ broadcast [tx-hex]: Broadcast transaction.');
this.log(' $ mempool: Get mempool snapshot.');
this.log(' $ tx [hash/address]: View transactions.');
this.log(' $ coin [hash+index/address]: View coins.');
this.log(' $ block [hash/height]: View block.');
return;
}
}, this);
};
CLI.prototype.open = function open() {
return spawn(function *() {
switch (this.argv[0]) {
case 'w':
case 'wallet':
this.argv.shift();
if (this.argv[0] === 'create') {
this.argv[0] = 'mkwallet';
return yield this.handleNode();
}
return yield this.handleWallet();
default:
return yield this.handleNode();
}
}, this);
};
CLI.prototype.destroy = function destroy() {
if (this.wallet && !this.wallet.client.loading) if (this.wallet && !this.wallet.client.loading)
this.wallet.client.destroy(); this.wallet.client.destroy();
if (this.client && !this.client.loading) if (this.client && !this.client.loading)
this.client.destroy(); this.client.destroy();
callback(); return Promise.resolve(null);
}; };
function main(callback) { function main() {
var cli = new CLI(); return spawn(function *() {
cli.open(function(err) { var cli = new CLI();
if (err) yield cli.open();
return callback(err); yield cli.destroy();
cli.destroy(callback); }, this);
});
} }
main(function(err) { main().then(process.exit).catch(function(err) {
if (err) { console.error(err.stack + '');
console.error(err.stack + ''); return process.exit(1);
return process.exit(1);
}
return process.exit(0);
}); });

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,7 @@
var Network = require('../protocol/network'); var Network = require('../protocol/network');
var request = require('./request'); var request = require('./request');
var spawn = require('../utils/spawn');
/** /**
* BCoin RPC client. * BCoin RPC client.
@ -43,38 +44,37 @@ function RPCClient(options) {
* @param {Function} callback - Returns [Error, Object?]. * @param {Function} callback - Returns [Error, Object?].
*/ */
RPCClient.prototype.call = function call(method, params, callback) { RPCClient.prototype.call = function call(method, params) {
request({ return spawn(function *() {
method: 'POST', var res = yield request.promise({
uri: this.uri, method: 'POST',
json: { uri: this.uri,
method: method, json: {
params: params, method: method,
id: this.id++ params: params,
}, id: this.id++
auth: { },
username: 'bitcoinrpc', auth: {
password: this.apiKey || '' username: 'bitcoinrpc',
}, password: this.apiKey || ''
expect: 'json' },
}, function(err, res, body) { expect: 'json'
if (err) });
return callback(err);
if (!body) if (!res.body)
return callback(); return;
if (res.statusCode === 400) if (res.statusCode === 400)
return callback(null, body.result); return res.body.result;
if (res.statusCode !== 200) { if (res.statusCode !== 200) {
if (body.error) if (res.body.error)
return callback(new Error(body.error.message)); throw new Error(res.body.error.message);
return callback(new Error('Status code: ' + res.statusCode)); throw new Error('Status code: ' + res.statusCode);
} }
return callback(null, body.result); return res.body.result;
}); }, this);
}; };
/* /*

View File

@ -377,8 +377,10 @@ HTTPServer.prototype._init = function _init() {
spawn(function *() { spawn(function *() {
var wallet; var wallet;
if (req.path.length < 2 || req.path[0] !== 'wallet') if (req.path.length < 2 || req.path[0] !== 'wallet') {
return next(); next();
return;
}
if (!self.options.walletAuth) { if (!self.options.walletAuth) {
wallet = yield self.walletdb.get(req.options.id); wallet = yield self.walletdb.get(req.options.id);

View File

@ -194,7 +194,6 @@ Fullnode.prototype._init = function _init() {
}); });
this.chain.on('connect', function(entry, block) { this.chain.on('connect', function(entry, block) {
return;
self.walletdb.addBlock(entry, block.txs).catch(onError); self.walletdb.addBlock(entry, block.txs).catch(onError);
if (self.chain.synced) if (self.chain.synced)

View File

@ -32,6 +32,8 @@ function Locker(parent, add) {
this.pending = []; this.pending = [];
this.pendingMap = {}; this.pendingMap = {};
this.add = add; this.add = add;
this._unlock = this.unlock.bind(this);
this._unlocker = this.unlocker.bind(this);
} }
utils.inherits(Locker, EventEmitter); utils.inherits(Locker, EventEmitter);
@ -83,9 +85,7 @@ Locker.prototype.lock = function lock(arg1, arg2) {
if (force) { if (force) {
assert(this.busy); assert(this.busy);
return new Promise(function(resolve, reject) { return new Promise(this._force);
resolve(function unlock() {});
});
} }
if (this.busy) { if (this.busy) {
@ -100,31 +100,39 @@ Locker.prototype.lock = function lock(arg1, arg2) {
this.busy = true; this.busy = true;
return new Promise(function(resolve, reject) { return new Promise(this._unlock);
resolve(function unlock() { };
var item, res, obj;
self.busy = false; Locker.prototype._force = function force(resolve, reject) {
resolve(utils.nop);
};
if (self.pending.length === 0) Locker.prototype.unlock = function unlock(resolve, reject) {
self.emit('drain'); resolve(this._unlocker);
};
if (self.jobs.length === 0) Locker.prototype.unlocker = function unlocker() {
return; var item, resolve, obj;
item = self.jobs.shift(); this.busy = false;
res = item[0];
obj = item[1];
if (obj) { if (this.pending.length === 0)
assert(obj === self.pending.shift()); this.emit('drain');
delete self.pendingMap[obj.hash('hex')];
}
self.busy = true; if (this.jobs.length === 0)
res(unlock); return;
});
}); item = this.jobs.shift();
resolve = item[0];
obj = item[1];
if (obj) {
assert(obj === this.pending.shift());
delete this.pendingMap[obj.hash('hex')];
}
this.busy = true;
resolve(this._unlocker);
}; };
/** /**

View File

@ -1297,6 +1297,7 @@ WalletDB.prototype.addBlock = function addBlock(entry, txs, force) {
unlock(); unlock();
throw e; throw e;
} }
unlock();
return; return;
} }
} }