Skip to content

Commit

Permalink
Speed up isMyVout + add another test-timer
Browse files Browse the repository at this point in the history
  • Loading branch information
panleone committed Sep 21, 2023
1 parent 6818643 commit 61cfddf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions scripts/mempool.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ export class Mempool {
* Get the total wallet balance
*/
async getBalanceNew(filter) {
const startTime = new Date();
console.log('Starting calculating total balance');
let totBalance = 0;
for (let [txid, tx] of this.txmap) {
for (let vout of tx.vout) {
Expand All @@ -298,6 +300,8 @@ export class Mempool {
totBalance += vout.value;
}
}
const endTime = new Date();
console.log('Finished calculating total balance',(endTime - startTime)/1000);
return totBalance;
}
/**
Expand Down
20 changes: 16 additions & 4 deletions scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Database } from './database.js';
import { guiRenderCurrentReceiveModal } from './contacts-book.js';
import { Account } from './accounts.js';
import { debug, fAdvancedMode } from './settings.js';
import { hexToBytes } from './utils.js';
import { bytesToHex, hexToBytes } from './utils.js';
import { strHardwareName, getHardwareWalletKeys } from './ledger.js';
import { isP2CS, isP2PKH, getAddressFromPKH } from './script.js';
export let fWalletLoaded = false;
Expand Down Expand Up @@ -64,6 +64,11 @@ class Wallet {
* @type {Map<String, String?>}
*/
#ownAddresses = new Map();
/**
* Map public key hash -> Address
* @type {Map<String,String>}
*/
#knownPKH = new Map();
constructor(nAccount) {
this.#nAccount = nAccount;
}
Expand Down Expand Up @@ -267,14 +272,14 @@ class Wallet {
let addresses = [];
const dataBytes = hexToBytes(script);
if (isP2PKH(dataBytes)) {
addresses.push(getAddressFromPKH(dataBytes.slice(3, 23)));
addresses.push(this.updatePkhMap(bytesToHex(dataBytes.slice(3, 23))))
const path = await this.isOwnAddress(addresses[0]);
if (path) {
return [UTXO_WALLET_STATE.SPENDABLE, path];
}
} else if (isP2CS(dataBytes)) {
addresses.push(getAddressFromPKH(dataBytes.slice(6, 26)));
addresses.push(getAddressFromPKH(dataBytes.slice(28, 48)));
addresses.push(this.updatePkhMap(bytesToHex((dataBytes.slice(6, 26)))));
addresses.push(this.updatePkhMap(bytesToHex((dataBytes.slice(28, 48)))))
const path1 = await this.isOwnAddress(addresses[0]);
const path2 = await this.isOwnAddress(addresses[1]);
if (path1) {
Expand All @@ -286,6 +291,13 @@ class Wallet {
}
return [UTXO_WALLET_STATE.NOT_MINE, null];
}
// Avoid calculating over and over the same getAddressFromPKH by saving the result in a map
updatePkhMap(pkh_hex){
if(!this.#knownPKH.has(pkh_hex)){
this.#knownPKH.set(pkh_hex, getAddressFromPKH(hexToBytes(pkh_hex)))
}
return this.#knownPKH.get(pkh_hex);
}
}

/**
Expand Down

0 comments on commit 61cfddf

Please sign in to comment.