more performance fixes

This commit is contained in:
Matthew Fosse 2024-09-11 18:44:25 -07:00
parent f8d76fb2e1
commit 8aa95a2eb7
2 changed files with 14 additions and 2 deletions

View file

@ -627,6 +627,10 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
void _validateAddresses() { void _validateAddresses() {
_addresses.forEach((element) async { _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 && if (!element.isHidden &&
element.address != element.address !=
await getAddressAsync(index: element.index, hd: mainHd, addressType: element.type)) { await getAddressAsync(index: element.index, hd: mainHd, addressType: element.type)) {

View file

@ -59,18 +59,24 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
Future<void> ensureMwebAddressUpToIndexExists(int index) async { Future<void> ensureMwebAddressUpToIndexExists(int index) async {
Uint8List scan = Uint8List.fromList(scanSecret); Uint8List scan = Uint8List.fromList(scanSecret);
Uint8List spend = Uint8List.fromList(spendPubkey); Uint8List spend = Uint8List.fromList(spendPubkey);
int count = 0;
while (mwebAddrs.length <= (index + 1)) { while (mwebAddrs.length <= (index + 1)) {
final address = await CwMweb.address(scan, spend, mwebAddrs.length); final address = await CwMweb.address(scan, spend, mwebAddrs.length);
mwebAddrs.add(address!); 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<void> initMwebAddresses() async { Future<void> initMwebAddresses() async {
print("Initializing MWEB addresses!");
if (mwebAddrs.length < 1000) { if (mwebAddrs.length < 1000) {
print("Generating MWEB addresses..."); print("Generating MWEB addresses...");
await ensureMwebAddressUpToIndexExists(1020); await ensureMwebAddressUpToIndexExists(1020);
print("done generating MWEB addresses");
List<BitcoinAddressRecord> addressRecords = mwebAddrs List<BitcoinAddressRecord> addressRecords = mwebAddrs
.asMap() .asMap()
.entries .entries
@ -81,6 +87,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
network: network, network: network,
)) ))
.toList(); .toList();
print("converted to list");
addMwebAddresses(addressRecords); addMwebAddresses(addressRecords);
print("added ${addressRecords.length} mweb addresses"); print("added ${addressRecords.length} mweb addresses");
return; return;
@ -105,6 +112,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
required Bip32Slip10Secp256k1 hd, required Bip32Slip10Secp256k1 hd,
BitcoinAddressType? addressType, BitcoinAddressType? addressType,
}) async { }) async {
print("getting address for index $index");
if (addressType == SegwitAddresType.mweb) { if (addressType == SegwitAddresType.mweb) {
await ensureMwebAddressUpToIndexExists(index); await ensureMwebAddressUpToIndexExists(index);
} }