txdb: refactor.
This commit is contained in:
parent
37586e5ad8
commit
6357795fd9
@ -1130,8 +1130,12 @@ Chain.prototype._add = co(function* add(block) {
|
||||
if (this.options.useCheckpoints) {
|
||||
checkpoint = this.network.checkpoints[height];
|
||||
if (checkpoint) {
|
||||
// Someone is very likely trying to fool us.
|
||||
// Someone is either trying to fool us, or
|
||||
// the consensus protocol is broken and
|
||||
// there was a 20k+ block reorg.
|
||||
if (hash !== checkpoint) {
|
||||
this.logger.warning('Checkpoint mismatch!');
|
||||
|
||||
this.purgeOrphans();
|
||||
|
||||
this.emit('fork', block, height, checkpoint);
|
||||
|
||||
@ -1365,10 +1365,10 @@ Pool.prototype._handleTX = co(function* _handleTX(tx, peer) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (missing) {
|
||||
for (i = 0; i < missing.length; i++)
|
||||
yield this.getData(peer, this.txType, missing[i]);
|
||||
}
|
||||
// if (missing) {
|
||||
// for (i = 0; i < missing.length; i++)
|
||||
// yield this.getData(peer, this.txType, missing[i]);
|
||||
// }
|
||||
|
||||
this.emit('tx', tx, peer);
|
||||
});
|
||||
|
||||
@ -252,16 +252,18 @@ Fullnode.prototype._open = co(function* open() {
|
||||
*/
|
||||
|
||||
Fullnode.prototype._close = co(function* close() {
|
||||
this.wallet = null;
|
||||
|
||||
if (this.http)
|
||||
yield this.http.close();
|
||||
|
||||
this.walletdb.close();
|
||||
this.pool.close();
|
||||
this.miner.close();
|
||||
this.mempool.close();
|
||||
this.chain.close();
|
||||
yield this.wallet.destroy();
|
||||
|
||||
this.wallet = null;
|
||||
|
||||
yield this.walletdb.close();
|
||||
yield this.pool.close();
|
||||
yield this.miner.close();
|
||||
yield this.mempool.close();
|
||||
yield this.chain.close();
|
||||
|
||||
this.logger.info('Node is closed.');
|
||||
});
|
||||
|
||||
@ -178,9 +178,13 @@ SPVNode.prototype._open = co(function* open(callback) {
|
||||
*/
|
||||
|
||||
SPVNode.prototype._close = co(function* close() {
|
||||
this.wallet = null;
|
||||
if (this.http)
|
||||
yield this.http.close();
|
||||
|
||||
yield this.wallet.destroy();
|
||||
|
||||
this.wallet = null;
|
||||
|
||||
yield this.walletdb.close();
|
||||
yield this.pool.close();
|
||||
yield this.chain.close();
|
||||
|
||||
@ -1181,6 +1181,7 @@ TXDB.prototype.__unconfirm = co(function* unconfirm(tx, info) {
|
||||
}
|
||||
|
||||
this.balance.unconfirm(coin.value);
|
||||
|
||||
coin.height = tx.height;
|
||||
coin = coin.toRaw();
|
||||
|
||||
@ -1366,10 +1367,10 @@ TXDB.prototype.getCoinHashes = function getCoinHashes(account) {
|
||||
parse: function(key) {
|
||||
if (account != null) {
|
||||
key = layout.Cc(key);
|
||||
return [key[1], key[2]];
|
||||
return new bcoin.outpoint(key[1], key[2]);
|
||||
}
|
||||
key = layout.cc(key);
|
||||
return key;
|
||||
return new bcoin.outpoint(key[0], key[1]);
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -1631,14 +1632,13 @@ TXDB.prototype.getCoins = function getCoins(account) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.getAccountCoins = co(function* getCoins(account) {
|
||||
var prevout = yield this.getCoinHashes(account);
|
||||
var coins = [];
|
||||
var i, hashes, key, coin;
|
||||
var i, op, coin;
|
||||
|
||||
hashes = yield this.getCoinHashes(account);
|
||||
|
||||
for (i = 0; i < hashes.length; i++) {
|
||||
key = hashes[i];
|
||||
coin = yield this.getCoin(key[0], key[1]);
|
||||
for (i = 0; i < prevout.length; i++) {
|
||||
op = prevout[i];
|
||||
coin = yield this.getCoin(op.hash, op.index);
|
||||
|
||||
if (!coin)
|
||||
continue;
|
||||
@ -1920,29 +1920,29 @@ TXDB.prototype.getBalance = co(function* getBalance(account) {
|
||||
*/
|
||||
|
||||
TXDB.prototype.getAccountBalance = co(function* getBalance(account) {
|
||||
var prevout = yield this.getCoinHashes(account);
|
||||
var balance = new Balance(this.wallet);
|
||||
var i, key, coin, hashes, hash, data;
|
||||
var i, ckey, key, coin, op, data;
|
||||
|
||||
hashes = yield this.getCoinHashes(account);
|
||||
|
||||
for (i = 0; i < hashes.length; i++) {
|
||||
hash = hashes[i];
|
||||
key = hash[0] + hash[1];
|
||||
coin = this.coinCache.get(key);
|
||||
for (i = 0; i < prevout.length; i++) {
|
||||
op = prevout[i];
|
||||
ckey = op.hash + op.index;
|
||||
coin = this.coinCache.get(ckey);
|
||||
|
||||
if (coin) {
|
||||
balance.addRaw(coin);
|
||||
continue;
|
||||
}
|
||||
|
||||
data = yield this.get(layout.c(hash[0], hash[1]));
|
||||
key = layout.c(op.hash, op.index);
|
||||
data = yield this.get(key);
|
||||
|
||||
if (!data)
|
||||
continue;
|
||||
|
||||
balance.addRaw(data);
|
||||
|
||||
this.coinCache.set(key, data);
|
||||
this.coinCache.set(ckey, data);
|
||||
}
|
||||
|
||||
return balance;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user