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

feat: add method to handle when there is a late voided tx #582

Open
wants to merge 1 commit into
base: old-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions src/new/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,10 @@ class HathorWallet extends EventEmitter {
// Process history to update metadatas
await this.storage.processHistory();

if (isNewTx && newTx.is_voided) {
await this.storage.handleVoidedTx(newTx);
}

if (isNewTx) {
this.emit('new-tx', newTx);
} else {
Expand Down
32 changes: 32 additions & 0 deletions src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,4 +872,36 @@ export class Storage implements IStorage {
const accessData = await this._getValidAccessData();
return (accessData.walletFlags & WALLET_FLAGS.HARDWARE) > 0;
}

/**
* Clean spent_by from outputs that were spent by transactions that became voided
* @returns {Promise<void>}
*/
async handleVoidedTx(tx: IHistoryTx): Promise<void> {
if (!tx.is_voided) {
// We are just handling voided tx here
return;
}

for (const input of tx.inputs) {
const inputTx = await this.getTx(input.tx_id);
if (!inputTx) {
// This input is not spending an output from this wallet
continue;
}

if (input.index > inputTx.outputs.length - 1) {
// Invalid output index
throw new Error('Try to get output in an index that doesn\'t exist.');
}

const output = inputTx.outputs[input.index];
// If the spent by of this output is the transaction that has just become voided
// then we must clean it and update the storage
if (output.spent_by === tx.tx_id) {
output.spent_by = null;
await this.addTx(inputTx);
}
}
}
}