From 8aa95a2eb7943b078658f28ceb9b16c8478acbaf Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Wed, 11 Sep 2024 18:44:25 -0700 Subject: [PATCH] more performance fixes --- cw_bitcoin/lib/electrum_wallet_addresses.dart | 4 ++++ cw_bitcoin/lib/litecoin_wallet_addresses.dart | 12 ++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cw_bitcoin/lib/electrum_wallet_addresses.dart b/cw_bitcoin/lib/electrum_wallet_addresses.dart index dd4f931ad..2de86d780 100644 --- a/cw_bitcoin/lib/electrum_wallet_addresses.dart +++ b/cw_bitcoin/lib/electrum_wallet_addresses.dart @@ -627,6 +627,10 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store { void _validateAddresses() { _addresses.forEach((element) async { + if (element.type == SegwitAddresType.mweb) { + // this would add a ton of startup lag for mweb addresses since we have 1000 of them + return; + } if (!element.isHidden && element.address != await getAddressAsync(index: element.index, hd: mainHd, addressType: element.type)) { diff --git a/cw_bitcoin/lib/litecoin_wallet_addresses.dart b/cw_bitcoin/lib/litecoin_wallet_addresses.dart index 8f5aefcc8..0bdfafdcd 100644 --- a/cw_bitcoin/lib/litecoin_wallet_addresses.dart +++ b/cw_bitcoin/lib/litecoin_wallet_addresses.dart @@ -59,18 +59,24 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with Future ensureMwebAddressUpToIndexExists(int index) async { Uint8List scan = Uint8List.fromList(scanSecret); Uint8List spend = Uint8List.fromList(spendPubkey); + int count = 0; while (mwebAddrs.length <= (index + 1)) { final address = await CwMweb.address(scan, spend, mwebAddrs.length); mwebAddrs.add(address!); + count++; + // sleep for a bit to avoid making the main thread unresponsive: + if (count > 50) { + count = 0; + await Future.delayed(Duration(milliseconds: 100)); + } } } Future initMwebAddresses() async { - print("Initializing MWEB addresses!"); - if (mwebAddrs.length < 1000) { print("Generating MWEB addresses..."); await ensureMwebAddressUpToIndexExists(1020); + print("done generating MWEB addresses"); List addressRecords = mwebAddrs .asMap() .entries @@ -81,6 +87,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with network: network, )) .toList(); + print("converted to list"); addMwebAddresses(addressRecords); print("added ${addressRecords.length} mweb addresses"); return; @@ -105,6 +112,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with required Bip32Slip10Secp256k1 hd, BitcoinAddressType? addressType, }) async { + print("getting address for index $index"); if (addressType == SegwitAddresType.mweb) { await ensureMwebAddressUpToIndexExists(index); }