Skip to content

Commit

Permalink
wdb: Pass mtime to the TXRecord.
Browse files Browse the repository at this point in the history
test: Add tests for the wallet.zap.
  • Loading branch information
nodech committed Mar 27, 2024
1 parent d569559 commit 333b4ed
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 33 deletions.
24 changes: 13 additions & 11 deletions lib/wallet/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,30 +245,31 @@ class TXRecord extends bio.Struct {
/**
* Create tx record.
* @constructor
* @param {TX} tx
* @param {BlockMeta?} block
* @param {Number} mtime
*/

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

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

assert(typeof mtime === 'number');

this.tx = null;
this.hash = null;
this.mtime = util.now();
this.mtime = mtime;
this.height = -1;
this.block = null;
this.index = -1;
this.time = 0;

if (tx)
this.fromTX(tx, block);
}

/**
* Inject properties from tx and block.
* @private
* @param {TX} tx
* @param {Block?} block
* @param {Block} [block]
* @returns {TXRecord}
*/

Expand All @@ -284,13 +285,14 @@ class TXRecord extends bio.Struct {

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

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

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,8 @@ class TXDB {
return this.confirm(existing, block, extra);
}

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

if (!block) {
// Potentially remove double-spenders.
Expand Down
13 changes: 10 additions & 3 deletions test/wallet-pagination-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ALT_ACCOUNT = 'alt';

const UNCONFIRMED_HEIGHT = 0xffffffff;
const MAX_TIME = 0xffffffff;
const INITIAL_TIME = 500000000;
const INITIAL_TIME = 1580745078;

describe('WalletDB Pagination', function() {
/** @type {WalletDB} */
Expand Down Expand Up @@ -125,12 +125,16 @@ describe('WalletDB Pagination', function() {
const totalTXs = totalConfirmed + totalUnconfirmed;
const entries = [];
const mtxs = [];
const times = [];

// 5 directly confirmed
for (let i = 0; i < totalConfirmed; i++) {
const mtx = await dummyTX(wallet);
const entry = nextEntry(wdb);
await wdb.addBlock(entry, [mtx.toTX()]);
// timeCounter is incremented twice, once for wdb.add/txrecord
// creation and another on time index creation.
times.push(timeCounter - 1);
entries.push(entry);
mtxs.push(mtx);
}
Expand All @@ -141,6 +145,9 @@ describe('WalletDB Pagination', function() {
await wdb.addTX(mtx.toTX());
const entry = nextEntry(wdb);
await wdb.addBlock(entry, [mtx.toTX()]);
// timeCounter is incremented twice, once for wdb.add/txrecord
// creation and another on time index creation.
times.push(timeCounter - 1);
entries.push(entry);
mtxs.push(mtx);
}
Expand All @@ -161,13 +168,13 @@ describe('WalletDB Pagination', function() {

const txByTime = await wallet.listUnconfirmedByTime(-1, {
limit: 1,
time: INITIAL_TIME + index,
time: times[index],
reverse: false
});

const txByTimeRev = await wallet.listUnconfirmedByTime(-1, {
limit: 1,
time: INITIAL_TIME + index,
time: times[index],
reverse: true
});

Expand Down
45 changes: 27 additions & 18 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 @@ -268,16 +272,21 @@ describe('Wallet Records', function() {
});
});

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

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

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, true);
const wtx = new TXRecord(mtime).fromTX(tx, block);

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

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

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

Expand All @@ -306,9 +315,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).fromTX(tx, block);
const encoded = wtx.encode();
const decoded = TXRecord.decode(encoded);

Expand All @@ -317,20 +326,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(mtime, tx);
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(mtime, tx, block);
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(mtime, tx);

assert.strictEqual(wtx.getBlock(), null);
assert.strictEqual(wtx.getDepth(random.randomInt()), 0);
Expand Down
Loading

0 comments on commit 333b4ed

Please sign in to comment.