diff --git a/lib/blockchain/migrations.js b/lib/blockchain/migrations.js index 3edfef0f8..aab81dd95 100644 --- a/lib/blockchain/migrations.js +++ b/lib/blockchain/migrations.js @@ -351,6 +351,8 @@ class MigrateBlockStore extends AbstractMigration { this.ldb = options.ldb; this.blocks = options.db.blocks; this.layout = MigrateBlockStore.layout(); + + this.batchWriteSize = 10000; } /** @@ -407,7 +409,7 @@ class MigrateBlockStore extends AbstractMigration { await this.blocks.writeBlock(hash, value); parent.del(key); - if (++total % 10000 === 0) { + if (++total % this.batchWriteSize === 0) { await parent.write(); this.logger.debug('Migrated up %d blocks.', total); parent = this.ldb.batch(); @@ -441,7 +443,7 @@ class MigrateBlockStore extends AbstractMigration { await this.blocks.writeUndo(hash, value); parent.del(key); - if (++total % 10000 === 0) { + if (++total % this.batchWriteSize === 0) { await parent.write(); this.logger.debug('Migrated up %d undo blocks.', total); parent = this.ldb.batch(); diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index cd8043220..57b860251 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -1178,13 +1178,15 @@ class TXDB { // we need to add that information. // TODO: This can be skipped if TX is coinbase, but even now // it will be properly cleaned up on erase. - await this.addTimeAndCountIndexUnconfirmedUndo(b, hash); + await this.addTimeAndCountIndexUnconfirmedUndo(b, hash, wtx.mtime); await this.addBlockMap(b, height); await this.addBlock(b, tx.hash(), block); } else { // Add indexing for unconfirmed transactions. - await this.addCountAndTimeIndexUnconfirmed(b, state.accounts, hash); + await this.addCountAndTimeIndexUnconfirmed(b, state.accounts, hash, + wtx.mtime); + await this.addTXMap(b, hash); } @@ -1920,11 +1922,11 @@ class TXDB { * @private * @param {Batch} b * @param {Hash} hash - Transaction hash. + * @param {Number} time - Transaction time. * @returns {Promise} */ - async addTimeAndCountIndexUnconfirmedUndo(b, hash) { - const time = this.nowFn(); + async addTimeAndCountIndexUnconfirmedUndo(b, hash, time) { const count = await this.getLatestUnconfirmedTXCount(); b.put(layout.e.encode(hash), fromU32(time)); @@ -1972,16 +1974,16 @@ class TXDB { * @param {Batch} b * @param {Map} accounts * @param {Hash} hash - Transaction hash. + * @param {Number} time - Transaction time. * @returns {Promise} */ - async addCountAndTimeIndexUnconfirmed(b, accounts, hash) { + async addCountAndTimeIndexUnconfirmed(b, accounts, hash, time) { const count = await this.getLatestUnconfirmedTXCount(); b.put(layout.z.encode(count.height, count.index), hash); b.put(layout.y.encode(hash), count.encode()); - const time = this.nowFn(); b.put(layout.e.encode(hash), fromU32(time)); b.put(layout.w.encode(time, count.index, hash)); @@ -2121,11 +2123,11 @@ class TXDB { b.put(layout.y.encode(hash), count.encode()); const time = blockextra.medianTime; - b.put(layout.g.encode(time, height, index, options.hash)); + b.put(layout.g.encode(time, height, index, hash)); for (const [acct] of accounts) { b.put(layout.Z.encode(acct, height, index), hash); - b.put(layout.G.encode(acct, time, height, index, options.hash)); + b.put(layout.G.encode(acct, time, height, index, hash)); } } @@ -4628,7 +4630,7 @@ class BlockRecord extends bio.Struct { /** * Instantiate wallet block from serialized tip data. * @private - * @param {Buffer} data + * @param {bio.BufferReader} br */ read(br) { diff --git a/test/wallet-test.js b/test/wallet-test.js index c1ffff563..48b77623d 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -3975,15 +3975,20 @@ describe('Wallet', function() { for (let i = 0; i < 2; i++) { for (const account of [DEFAULT, ALT]) { const mtx = await dummyTX(wallet, account); - // this increments/calls nowFn twice. One for - // wtx creation and another for unconfirmed index. + // this increments/calls nowFn once. await wdb.addTX(mtx.toTX()); hashes.push(mtx.hash()); } } - // zap will also call nowFn once. (0 - 3 time is incremented by first two) - const zapped = await wallet.zap(-1, time - 3); + // time will be 4 (4 txs), if we want to zap oldest 2 txs, + // zap will call nowFn once more, so time will be 5. + // First 2 txs have time 0 and 1. Zap accepts second argument + // age, which is time - age. So, we need to pass time - 1. + // e.g. time - 1 = 4. Internal timer will be 5 (nowFn increment). + // Age becomes: 5 - 4 = 1. So, zap will zap all txs with age 1 + // - so first 2 txs. + const zapped = await wallet.zap(-1, time - 1); assert.strictEqual(zapped.length, 2); const txsAfterZap = await wallet.listUnconfirmed(-1, { @@ -4008,8 +4013,8 @@ describe('Wallet', function() { } } - // two transactions from default - const zapped = await wallet.zap(DEFAULT, time - 5); + // two transactions from default (calculation above.) + const zapped = await wallet.zap(DEFAULT, time - 3); assert.strictEqual(zapped.length, 2); const txsAfterZap = await wallet.listUnconfirmed(DEFAULT, {