Skip to content

Commit

Permalink
txdb: add and pass nowFn from wdb.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Jun 26, 2024
1 parent 680a9da commit 5c63ffa
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 28 deletions.
23 changes: 14 additions & 9 deletions lib/wallet/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,20 @@ class TXRecord extends bio.Struct {
/**
* Create tx record.
* @constructor
* @param {TX} tx
* @param {BlockMeta?} block
* @param {Number} mtime
* @param {TX} [tx]
* @param {BlockMeta} [block]
*/

constructor(tx, block) {
constructor(mtime, tx, block) {
super();

if (mtime == null)
mtime = util.now();

this.tx = null;
this.hash = null;
this.mtime = util.now();
this.mtime = mtime;
this.height = -1;
this.block = null;
this.index = -1;
Expand All @@ -240,7 +244,7 @@ class TXRecord extends bio.Struct {
* Inject properties from tx and block.
* @private
* @param {TX} tx
* @param {Block?} block
* @param {Block} [block]
* @returns {TXRecord}
*/

Expand All @@ -256,13 +260,14 @@ class TXRecord extends bio.Struct {

/**
* Instantiate tx record from tx and block.
* @param {TX} tx
* @param {Block?} block
* @param {TX} [tx]
* @param {Block} [block]
* @param {Number} [mtime]
* @returns {TXRecord}
*/

static fromTX(tx, block) {
return new this().fromTX(tx, block);
static fromTX(tx, block, mtime) {
return new this(mtime).fromTX(tx, block);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TXDB {
this.wdb = wdb;
this.db = wdb.db;
this.logger = wdb.logger;
this.nowFn = wdb.options.nowFn || util.now;

this.wid = wid || 0;
this.bucket = null;
Expand Down Expand Up @@ -809,7 +810,8 @@ class TXDB {
return this.confirm(existing, block);
}

const wtx = TXRecord.fromTX(tx, block);
const now = this.nowFn();
const wtx = TXRecord.fromTX(tx, block, now);

if (!block) {
// Potentially remove double-spenders.
Expand Down Expand Up @@ -3252,7 +3254,7 @@ class TXDB {
async zap(acct, age) {
assert((age >>> 0) === age);

const now = util.now();
const now = this.nowFn();

const txs = await this.getRange(acct, {
start: 0,
Expand Down
8 changes: 8 additions & 0 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const WalletMigrator = require('./migrations');
const layout = layouts.wdb;
const tlayout = layouts.txdb;
const {states} = require('../covenants/namestate');
const util = require('../utils/util');

const {
ChainState,
Expand Down Expand Up @@ -2582,6 +2583,8 @@ class WalletOptions {
this.migrateNoRescan = false;
this.preloadAll = false;

this.nowFn = util.now;

if (options)
this.fromOptions(options);
}
Expand Down Expand Up @@ -2678,6 +2681,11 @@ class WalletOptions {
this.preloadAll = options.preloadAll;
}

if (options.timeFn != null) {
assert(typeof options.timeFn === 'function');
this.nowFn = options.timeFn;
}

return this;
}

Expand Down
48 changes: 32 additions & 16 deletions test/wallet-records-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ function getRandomTXRecordData(genTX = false, genBlock = false) {
if (genTX)
tx = getRandomTX();

const mtime = random.randomInt();

const data = {
height: block ? block.height : -1,
time: block ? block.time : 0,
block: block ? block.hash : null,
tx: tx,
hash: tx ? tx.hash() : null
hash: tx ? tx.hash() : null,
mtime: mtime
};

return {data, tx, block};
return {data, tx, block, mtime};
}

/*
Expand Down Expand Up @@ -114,6 +117,7 @@ function compareTXRecord(actual, expected) {

assert(typeof actual.mtime === 'number');
assert(actual.mtime > 0);
assert.strictEqual(actual.mtime, expected.mtime);
assert.strictEqual(actual.index, -1);
}

Expand Down Expand Up @@ -267,16 +271,28 @@ describe('Wallet Records', function() {
});
});

it('should initialize w/ time', () => {
const {mtime} = getRandomTXRecordData(false, false, true);
const wtx = new TXRecord(mtime);

compareTXRecord(wtx, {
index: -1,
mtime: mtime,
...emptyTX,
...emptyBlock
});
});

it('should initialize w/ tx', () => {
const {data, tx} = getRandomTXRecordData(true);
const wtx = new TXRecord(tx);
const {data, tx, mtime} = getRandomTXRecordData(true);
const wtx = new TXRecord(mtime, tx);

compareTXRecord(wtx, data);
});

it('should initialize w/ tx and block', () => {
const {data, block, tx} = getRandomTXRecordData(true, true);
const wtx = new TXRecord(tx, block);
const {data, block, tx, mtime} = getRandomTXRecordData(true, true);
const wtx = new TXRecord(mtime, tx, block);

compareTXRecord(wtx, data);
});
Expand All @@ -294,9 +310,9 @@ describe('Wallet Records', function() {
});

it('should encode/decode w/ tx', () => {
const {data, tx} = getRandomTXRecordData(true);
const {data, tx, mtime} = getRandomTXRecordData(true);

const wtx = new TXRecord(tx);
const wtx = new TXRecord(mtime, tx);
const encoded = wtx.encode();
const decoded = TXRecord.decode(encoded);

Expand All @@ -305,9 +321,9 @@ describe('Wallet Records', function() {
});

it('should encode/decode w/ tx and block', () => {
const {data, tx, block} = getRandomTXRecordData(true, true);
const {data, tx, block, mtime} = getRandomTXRecordData(true, true);

const wtx = new TXRecord(tx, block);
const wtx = new TXRecord(mtime, tx, block);
const encoded = wtx.encode();
const decoded = TXRecord.decode(encoded);

Expand All @@ -316,20 +332,20 @@ describe('Wallet Records', function() {
});

it('should initialize from TX', () => {
const {data, tx} = getRandomTXRecordData(true);
const wtx = TXRecord.fromTX(tx);
const {data, tx, mtime} = getRandomTXRecordData(true);
const wtx = TXRecord.fromTX(tx, null, mtime);
compareTXRecord(wtx, data);
});

it('should initialize from TX and Block', () => {
const {data, tx, block} = getRandomTXRecordData(true, true);
const wtx = TXRecord.fromTX(tx, block);
const {data, tx, block, mtime} = getRandomTXRecordData(true, true);
const wtx = TXRecord.fromTX(tx, block, mtime);
compareTXRecord(wtx, data);
});

it('should set and unset block', () => {
const {data, tx, block} = getRandomTXRecordData(true, true);
const wtx = TXRecord.fromTX(tx);
const {data, tx, block, mtime} = getRandomTXRecordData(true, true);
const wtx = TXRecord.fromTX(tx, null, mtime);

assert.strictEqual(wtx.getBlock(), null);
assert.strictEqual(wtx.getDepth(random.randomInt()), 0);
Expand Down
13 changes: 12 additions & 1 deletion test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1998,7 +1998,7 @@ describe('Wallet', function() {
});

it('should throw error with missing outputs', async () => {
const wallet = new Wallet({});
const wallet = new Wallet({ options: {} });

let err = null;

Expand All @@ -2012,6 +2012,17 @@ describe('Wallet', function() {
assert.equal(err.message, 'At least one output is required.');
});

it('should pass nowFn to the txdb', async () => {
const nowFn = () => 1;
const wallet = new Wallet({
options: {
nowFn
}
});

assert.strictEqual(wallet.txdb.nowFn(), nowFn());
});

it('should cleanup', async () => {
network.coinbaseMaturity = 2;
await wdb.close();
Expand Down

0 comments on commit 5c63ffa

Please sign in to comment.