From ffc17b48c2a2aa5af03af992cdf576872208db5b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 24 Oct 2016 10:52:28 -0700 Subject: [PATCH] walletdb: more refactoring. --- lib/chain/chaindb.js | 7 ++++--- lib/protocol/networks.js | 18 ++++++++++++------ lib/wallet/walletdb.js | 21 ++++++++++----------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/chain/chaindb.js b/lib/chain/chaindb.js index 11038d39..d9ee3a9d 100644 --- a/lib/chain/chaindb.js +++ b/lib/chain/chaindb.js @@ -1119,19 +1119,20 @@ ChainDB.prototype.scan = co(function* scan(start, filter, iter) { while (entry) { block = yield this.getBlock(entry.hash); + txs = []; total++; if (!block) { if (!this.options.spv && !this.options.prune) throw new Error('Block not found.'); + yield iter(entry, txs); + entry = yield entry.getNext(); continue; } this.logger.info( 'Scanning block %s (%d).', - entry.rhash, block.height); - - txs = []; + entry.rhash, entry.height); for (i = 0; i < block.txs.length; i++) { tx = block.txs[i]; diff --git a/lib/protocol/networks.js b/lib/protocol/networks.js index 502d4deb..1796d338 100644 --- a/lib/protocol/networks.js +++ b/lib/protocol/networks.js @@ -136,7 +136,8 @@ main.genesis = { merkleRoot: '3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a', ts: 1231006505, bits: 486604799, - nonce: 2083236893 + nonce: 2083236893, + height: 0 }; /** @@ -501,7 +502,8 @@ testnet.genesis = { merkleRoot: '3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a', ts: 1296688602, bits: 486604799, - nonce: 414098458 + nonce: 414098458, + height: 0 }; testnet.genesisBlock = @@ -646,7 +648,8 @@ regtest.genesis = { merkleRoot: '3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a', ts: 1296688602, bits: 545259519, - nonce: 2 + nonce: 2, + height: 0 }; regtest.genesisBlock = @@ -788,7 +791,8 @@ segnet3.genesis = { merkleRoot: '3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a', ts: 1452831101, bits: 486604799, - nonce: 0 + nonce: 0, + height: 0 }; segnet3.genesisBlock = @@ -907,7 +911,8 @@ segnet4.genesis = { merkleRoot: '3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a', ts: 1452831101, bits: 503447551, - nonce: 0 + nonce: 0, + height: 0 }; segnet4.genesisBlock = @@ -1047,7 +1052,8 @@ simnet.genesis = { merkleRoot: '3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a', ts: 1401292357, bits: 545259519, - nonce: 2 + nonce: 2, + height: 0 }; simnet.genesisBlock = diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 6bfbaaea..9185cede 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -1213,6 +1213,7 @@ WalletDB.prototype._rescan = co(function* rescan(chaindb, height) { } if (height == null) { + assert(this.network.block.keepBlocks > 36); height = this.height - 36; if (height < 0) height = 0; @@ -1462,10 +1463,6 @@ WalletDB.prototype.writeGenesis = co(function* writeGenesis() { WalletDB.prototype.forceTip = co(function* forceTip(entry) { var tip = HeaderRecord.fromEntry(entry); - - if (entry === this.network.genesis) - tip.height = 0; - yield this.setTip(tip); }); @@ -1476,8 +1473,10 @@ WalletDB.prototype.forceTip = co(function* forceTip(entry) { WalletDB.prototype.getTip = co(function* getTip() { var height = yield this.getHeight(); + if (height === -1) return; + return yield this.getHeader(height); }); @@ -1655,7 +1654,7 @@ WalletDB.prototype._addBlock = co(function* addBlock(entry, txs) { var i, tip, tx; if (entry.height <= this.height) { - this.logger.warning('Connecting low blocks.'); + this.logger.warning('Wallet is connecting low blocks.'); return total; } @@ -1673,7 +1672,7 @@ WalletDB.prototype._addBlock = co(function* addBlock(entry, txs) { for (i = 0; i < txs.length; i++) { tx = txs[i]; - if (yield this._addTX(tx, tip)) + if (yield this._insert(tx, tip)) total++; } @@ -1712,7 +1711,7 @@ WalletDB.prototype._removeBlock = co(function* removeBlock(entry) { var i, tx, tip, prev, block; if (entry.height > this.height) { - this.logger.warning('Disconnecting high blocks.'); + this.logger.warning('Wallet is disconnecting high blocks.'); return 0; } @@ -1738,7 +1737,7 @@ WalletDB.prototype._removeBlock = co(function* removeBlock(entry) { for (i = block.txs.length - 1; i >= 0; i--) { tx = block.txs[i]; - yield this._unconfirmTX(tx, tip); + yield this._unconfirm(tx, tip); } yield this.setTip(prev); @@ -1774,7 +1773,7 @@ WalletDB.prototype.addTX = co(function* addTX(tx) { this.logger.warning('Retroactively inserting confirmed transaction.'); } - return yield this._addTX(tx); + return yield this._insert(tx); } finally { unlock(); } @@ -1788,7 +1787,7 @@ WalletDB.prototype.addTX = co(function* addTX(tx) { * @returns {Promise} */ -WalletDB.prototype._addTX = co(function* addTX(tx, block) { +WalletDB.prototype._insert = co(function* insert(tx, block) { var result = false; var i, wids, wid, wallet; @@ -1832,7 +1831,7 @@ WalletDB.prototype._addTX = co(function* addTX(tx, block) { * @returns {Promise} */ -WalletDB.prototype._unconfirmTX = co(function* unconfirmTX(tx, block) { +WalletDB.prototype._unconfirm = co(function* unconfirm(tx, block) { var i, wid, wallet; for (i = 0; i < tx.wids.length; i++) {