Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache transaction ids #302

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/database.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { openDB, IDBPDatabase } from 'idb';

Check warning on line 1 in scripts/database.js

View workflow job for this annotation

GitHub Actions / Run linters

'IDBPDatabase' is defined but never used
import Masternode from './masternode.js';
import { Settings } from './settings.js';
import { cChainParams } from './chain_params.js';
Expand Down Expand Up @@ -70,7 +70,7 @@
const store = this.#db
.transaction('txs', 'readwrite')
.objectStore('txs');
await store.put(tx, tx.txid);
await store.put(tx.__original, tx.txid);
}

/**
Expand Down Expand Up @@ -248,7 +248,7 @@
* @param {Object} o
* @param {String} o.publicKey - Public key associated to the account.
*/
async removeAccount({ publicKey }) {

Check warning on line 251 in scripts/database.js

View workflow job for this annotation

GitHub Actions / Run linters

'publicKey' is defined but never used. Allowed unused args must match /^_/u
const store = this.#db
.transaction('accounts', 'readwrite')
.objectStore('accounts');
Expand Down
19 changes: 17 additions & 2 deletions scripts/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export class Transaction {

/** @type{number[]} */
shieldData = [];
/** Cached txid */
#txid = '';

constructor({
version = 1,
Expand All @@ -98,10 +100,23 @@ export class Transaction {
this.blockTime = blockTime;
this.lockTime = lockTime;
this.shieldData = shieldData;
/** Handle to the unproxied tx for when we need to clone it */
this.__original = this;
return new Proxy(this, {
set(obj) {
obj.#txid = '';
return Reflect.set(...arguments);
},
});
}

get txid() {
return bytesToHex(dSHA256(hexToBytes(this.serialize())).reverse());
if (!this.__original.#txid) {
this.__original.#txid = bytesToHex(
dSHA256(hexToBytes(this.serialize())).reverse()
);
}
return this.__original.#txid;
}

isConfirmed() {
Expand Down Expand Up @@ -236,7 +251,7 @@ export class Transaction {
* Using the sighash type SIGHASH_ALL
*/
transactionHash(index) {
const copy = structuredClone(this);
const copy = structuredClone(this.__original);
// Black out all inputs
for (let i = 0; i < copy.vin.length; i++) {
if (i != index) copy.vin[i].scriptSig = '';
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/transaction.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, test, vi, beforeAll } from 'vitest';
import { describe, it, test, vi, beforeAll, expect } from 'vitest';
import {
Transaction,
CTxIn,
Expand Down Expand Up @@ -78,6 +78,18 @@ describe('transaction tests', () => {
expect(ourTx).toStrictEqual(tx);
});

it.sequential('updates txid when a property changes', () => {
const [tx, txid] = testVector[0];
const originalVersion = tx.version;
expect(tx.txid).toBe(txid);
tx.version = 10;
expect(tx.txid).not.toBe(txid);
expect(tx.txid).toBe(
'416368b2101fab865db162d49d0540560f802e89801cad8eb1d9cf3a4e6ad5be'
);
tx.version = originalVersion;
});

it.each(testVector.filter((t) => t[3]))(
'signs correctly ($txid)',
async (tx, _, hex, inputs) => {
Expand Down
Loading