Skip to content

Commit

Permalink
txdb: call nowFn once in insert.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Sep 21, 2024
1 parent 46f099f commit ac20476
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
6 changes: 4 additions & 2 deletions lib/blockchain/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ class MigrateBlockStore extends AbstractMigration {
this.ldb = options.ldb;
this.blocks = options.db.blocks;
this.layout = MigrateBlockStore.layout();

this.batchWriteSize = 10000;
}

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 11 additions & 9 deletions lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -1972,16 +1974,16 @@ class TXDB {
* @param {Batch} b
* @param {Map<Number, Balance>} 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));

Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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, {
Expand Down

0 comments on commit ac20476

Please sign in to comment.