Skip to content

Commit

Permalink
move getLatestTx to wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
panleone committed May 10, 2024
1 parent 44fcdee commit 89e56e7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 64 deletions.
63 changes: 0 additions & 63 deletions scripts/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { createAlert } from './misc.js';
import {
debugError,
debugLog,
debugTimerEnd,
debugTimerStart,
DebugTopics,
debugWarn,
} from './debug.js';
Expand Down Expand Up @@ -286,67 +284,6 @@ export class ExplorerNetwork extends Network {
}
}

/**
* //TODO: do not take the wallet as parameter but instead something weaker like a public key or address?
* Must be called only for initial wallet sync
* @param {import('./wallet.js').Wallet} wallet - Wallet that we are getting the txs of
* @returns {Promise<void>}
*/
async getLatestTxs(wallet) {
if (wallet.isSynced) {
throw new Error('getLatestTxs must only be for initial sync');
}
let nStartHeight = Math.max(
...wallet.getTransactions().map((tx) => tx.blockHeight)
);
debugTimerStart(DebugTopics.NET, 'getLatestTxsTimer');
const probePage = await this.getAccountInfo(
wallet.getKeyToExport(),
1,
1,
nStartHeight
);
const txNumber = probePage.txs - wallet.getTransactions().length;
// Compute the total pages and iterate through them until we've synced everything
const totalPages = Math.ceil(txNumber / 1000);
for (let i = totalPages; i > 0; i--) {
getEventEmitter().emit(
'transparent-sync-status-update',
tr(translation.syncStatusHistoryProgress, [
{ current: totalPages - i + 1 },
{ total: totalPages },
]),
false
);

// Fetch this page of transactions
const iPage = await this.getAccountInfo(
wallet.getKeyToExport(),
i,
1000,
nStartHeight
);

// Update the internal mempool if there's new transactions
// Note: Extra check since Blockbook sucks and removes `.transactions` instead of an empty array if there's no transactions
if (iPage?.transactions?.length > 0) {
for (const tx of iPage.transactions.reverse()) {
const parsed = Transaction.fromHex(tx.hex);
parsed.blockHeight = tx.blockHeight;
parsed.blockTime = tx.blockTime;
await wallet.addTransaction(parsed);
}
}
}

debugLog(
DebugTopics.NET,
'Fetched latest txs: total number of pages was ',
totalPages
);
debugTimerEnd(DebugTopics.NET, 'getLatestTxsTimer');
}

/**
* @typedef {object} BlockbookUTXO
* @property {string} txid - The TX hash of the output
Expand Down
56 changes: 55 additions & 1 deletion scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ import {
import { PIVXShield } from 'pivx-shield';
import { guiToggleReceiveType } from './contacts-book.js';
import { TransactionBuilder } from './transaction_builder.js';
import {
debugLog,
debugTimerEnd,
debugTimerStart,
DebugTopics,
} from './debug.js';

/**
* Class Wallet, at the moment it is just a "realization" of Masterkey with a given nAccount
Expand Down Expand Up @@ -713,8 +719,56 @@ export class Wallet {

async #transparentSync() {
if (!this.isLoaded() || this.#isSynced) return;
let nStartHeight = Math.max(
...this.getTransactions().map((tx) => tx.blockHeight)
);
debugTimerStart(DebugTopics.WALLET, 'transparentSyncTimer');
const cNet = getNetwork();
await cNet.getLatestTxs(this);
const probePage = await cNet.getAccountInfo(
this.getKeyToExport(),
1,
1,
nStartHeight
);
const txNumber = probePage.txs - this.getTransactions().length;
const batchSize = 1000;
// Compute the total pages and iterate through them until we've synced everything
const totalPages = Math.ceil(txNumber / batchSize);
for (let i = totalPages; i > 0; i--) {
getEventEmitter().emit(
'transparent-sync-status-update',
tr(translation.syncStatusHistoryProgress, [
{ current: totalPages - i + 1 },
{ total: totalPages },
]),
false
);

// Fetch this page of transactions
const iPage = await cNet.getAccountInfo(
this.getKeyToExport(),
i,
batchSize,
nStartHeight
);

// Update the internal mempool if there's new transactions
// Note: Extra check since Blockbook sucks and removes `.transactions` instead of an empty array if there's no transactions
if (iPage?.transactions?.length > 0) {
for (const tx of iPage.transactions.reverse()) {
const parsed = Transaction.fromHex(tx.hex);
parsed.blockHeight = tx.blockHeight;
parsed.blockTime = tx.blockTime;
await this.addTransaction(parsed);
}
}
}
debugLog(
DebugTopics.WALLET,
'Transparent sync finished: total number of pages was ',
totalPages
);
debugTimerEnd(DebugTopics.WALLET, 'transparentSyncTimer');
getEventEmitter().emit('transparent-sync-status-update', '', true);
}

Expand Down

0 comments on commit 89e56e7

Please sign in to comment.