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

Fix unconfirmed txs being weird #415

Merged
merged 9 commits into from
Oct 7, 2024
Merged
3 changes: 2 additions & 1 deletion scripts/composables/use_wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export const useWallet = defineStore('wallet', () => {
}
const res = await network.sendTransaction(tx.serialize());
if (res) {
await wallet.addTransaction(tx);
// Don't add unconfirmed txs to the database
await wallet.addTransaction(tx, true);
} else {
wallet.discardTransaction(tx);
}
Expand Down
26 changes: 22 additions & 4 deletions scripts/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import { Settings } from './settings.js';
import { cChainParams } from './chain_params.js';
import {
confirmPopup,

Check warning on line 6 in scripts/database.js

View workflow job for this annotation

GitHub Actions / Run linters

'confirmPopup' is defined but never used
sanitizeHTML,

Check warning on line 7 in scripts/database.js

View workflow job for this annotation

GitHub Actions / Run linters

'sanitizeHTML' is defined but never used
createAlert,
isSameType,
isEmpty,
} from './misc.js';
import { PromoWallet } from './promos.js';
import { ALERTS, translation } from './i18n.js';

Check warning on line 13 in scripts/database.js

View workflow job for this annotation

GitHub Actions / Run linters

'ALERTS' is defined but never used

Check warning on line 13 in scripts/database.js

View workflow job for this annotation

GitHub Actions / Run linters

'translation' is defined but never used
import { Account } from './accounts.js';
import { COutpoint, CTxIn, CTxOut, Transaction } from './transaction.js';

Expand All @@ -22,9 +22,10 @@
* Version 3 = TX Database (#235)
* Version 4 = Tx Refactor (#284)
* Version 5 = Tx shield data (#295)
* @type{Number}
* Version 6 = Filter unconfirmed txs (#415)
* @type {number}
*/
static version = 5;
static version = 6;

/**
* @type{import('idb').IDBPDatabase}
Expand Down Expand Up @@ -68,6 +69,7 @@
* @param {Transaction} tx
*/
async storeTx(tx) {
if (!tx) throw new Error('Cannot store undefined');
const store = this.#db
.transaction('txs', 'readwrite')
.objectStore('txs');
Expand Down Expand Up @@ -331,7 +333,8 @@
// We'll manually cursor iterate to merge the Index (TXID) with it's components
const cursor = await store.openCursor();
const txs = [];
while (cursor.key) {
while (cursor) {
if (!cursor.value) break;
// Append the TXID from the Index key
cursor.value.txid = cursor.key;
txs.push(cursor.value);
Expand Down Expand Up @@ -417,7 +420,7 @@
static async create(name) {
const database = new Database({ db: null });
const db = await openDB(`MPW-${name}`, Database.version, {
upgrade: (db, oldVersion) => {
upgrade: (db, oldVersion, _, transaction) => {
console.log(
'DB: Upgrading from ' +
oldVersion +
Expand All @@ -442,6 +445,21 @@
db.deleteObjectStore('txs');
db.createObjectStore('txs');
}

if (oldVersion < 6) {
// Delete all txs with -1 as blockHeight (unconfirmed)
(async () => {
const store = transaction.objectStore('txs');
let cursor = await store.openCursor();
while (cursor) {
if (!cursor.value) break;
if (cursor.value.blockHeight === -1) {
await cursor.delete();
}
cursor = await cursor.continue();
}
})();
}
},
blocking: () => {
// Another instance is waiting to upgrade, and we're preventing it
Expand Down
5 changes: 4 additions & 1 deletion scripts/network.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cChainParams } from './chain_params.js';
import { createAlert } from './misc.js';

Check warning on line 2 in scripts/network.js

View workflow job for this annotation

GitHub Actions / Run linters

'createAlert' is defined but never used
import {
debugLog,
debugTimerEnd,
Expand All @@ -10,7 +10,7 @@
import { getEventEmitter } from './event_bus.js';
import { setExplorer, fAutoSwitch } from './settings.js';
import { cNode } from './settings.js';
import { ALERTS, tr, translation } from './i18n.js';

Check warning on line 13 in scripts/network.js

View workflow job for this annotation

GitHub Actions / Run linters

'ALERTS' is defined but never used
import { Transaction } from './transaction.js';

/**
Expand Down Expand Up @@ -205,7 +205,10 @@
const parsed = Transaction.fromHex(tx.hex);
parsed.blockHeight = tx.blockHeight;
parsed.blockTime = tx.blockTime;
await wallet.addTransaction(parsed);
await wallet.addTransaction(
parsed,
parsed.blockHeight === -1
);
}
}
}
Expand Down
Loading