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

fix: memory & lock issues in sortTransactions #588

Open
wants to merge 1 commit into
base: develop
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
3 changes: 0 additions & 3 deletions DashSync/shared/Models/Wallet/DSAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,6 @@ FOUNDATION_EXPORT NSString *_Nonnull const DSAccountNewAccountShouldBeAddedFromT
// returns the fee for the given transaction if all its inputs are from wallet transactions, UINT64_MAX otherwise
- (uint64_t)feeForTransaction:(DSTransaction *)transaction;

// historical wallet balance after the given transaction, or current balance if transaction is not registered in wallet
- (uint64_t)balanceAfterTransaction:(DSTransaction *)transaction;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used anywhere


- (void)chainUpdatedBlockHeight:(int32_t)height;

- (NSArray *)setBlockHeight:(int32_t)height andTimestamp:(NSTimeInterval)timestamp forTransactionHashes:(NSArray *)txHashes;
Expand Down
29 changes: 7 additions & 22 deletions DashSync/shared/Models/Wallet/DSAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,6 @@ - (void)updateBalance {
NSMutableOrderedSet *utxos = [NSMutableOrderedSet orderedSet];
NSMutableSet *spentOutputs = [NSMutableSet set], *invalidTx = [NSMutableSet set], *pendingTransactionHashes = [NSMutableSet set];
NSMutableDictionary *pendingCoinbaseLockedTransactionHashes = [NSMutableDictionary dictionary];
NSMutableArray *balanceHistory = [NSMutableArray array];
uint32_t now = [NSDate timeIntervalSince1970];

for (DSFundsDerivationPath *derivationPath in self.fundDerivationPaths) {
Expand Down Expand Up @@ -701,7 +700,6 @@ - (void)updateBalance {
if (tx.blockHeight == TX_UNCONFIRMED &&
([spent intersectsSet:spentOutputs] || [inputs intersectsSet:invalidTx])) {
[invalidTx addObject:uint256_obj(tx.txHash)];
[balanceHistory insertObject:@(balance) atIndex:0];
continue;
}
} else {
Expand Down Expand Up @@ -754,7 +752,6 @@ - (void)updateBalance {

if (pending || [inputs intersectsSet:pendingTransactionHashes]) {
[pendingTransactionHashes addObject:uint256_obj(tx.txHash)];
[balanceHistory insertObject:@(balance) atIndex:0];
continue;
}

Expand All @@ -765,7 +762,6 @@ - (void)updateBalance {
pendingCoinbaseLockedTransactionHashes[@(lockedBlockHeight)] = [NSMutableSet set];
}
[((NSMutableSet *)pendingCoinbaseLockedTransactionHashes[@(lockedBlockHeight)]) addObject:uint256_obj(tx.txHash)];
[balanceHistory insertObject:@(balance) atIndex:0];
continue;
}

Expand Down Expand Up @@ -811,7 +807,6 @@ - (void)updateBalance {

if (prevBalance < balance) totalReceived += balance - prevBalance;
if (balance < prevBalance) totalSent += prevBalance - balance;
[balanceHistory insertObject:@(balance) atIndex:0];
prevBalance = balance;
#if LOG_BALANCE_UPDATE
DSLog(@"===UTXOS===");
Expand All @@ -835,7 +830,6 @@ - (void)updateBalance {
self.pendingCoinbaseLockedTransactionHashes = pendingCoinbaseLockedTransactionHashes;
self.spentOutputs = spentOutputs;
self.utxos = utxos;
self.balanceHistory = balanceHistory;
_totalSent = totalSent;
_totalReceived = totalReceived;

Expand All @@ -849,16 +843,6 @@ - (void)updateBalance {
}
}

// historical wallet balance after the given transaction, or current balance if transaction is not registered in wallet
- (uint64_t)balanceAfterTransaction:(DSTransaction *)transaction {
NSParameterAssert(transaction);

NSUInteger i = [self.transactions indexOfObject:transaction];

return (i < self.balanceHistory.count) ? [self.balanceHistory[i] unsignedLongLongValue] : self.balance;
}


- (void)postBalanceDidChangeNotification {
[[NSNotificationCenter defaultCenter] postNotificationName:DSWalletBalanceDidChangeNotification object:nil];
}
Expand Down Expand Up @@ -895,21 +879,22 @@ __block __weak BOOL (^_isAscending)(id, id) = isAscending = ^BOOL(DSTransaction
}] != NSNotFound) return NO;
if ([self.invalidTransactionHashes containsObject:hash1] && ![self.invalidTransactionHashes containsObject:hash2]) return YES;
if ([self.pendingTransactionHashes containsObject:hash1] && ![self.pendingTransactionHashes containsObject:hash2]) return YES;
for (DSTransactionInput *input in tx1.inputs) {
if (_isAscending(self.allTx[uint256_obj(input.inputHash)], tx2)) return YES;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precise sorting causes long waits for the lock to be freed, which leads to stalled sync and UI freezes occasionally.
It doesn't look like we need precise sorting anywhere.


return NO;
};

NSArray *externalAddresses = self.externalAddresses;
NSArray *internalAddresses = self.internalAddresses;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.externalAddresses and self.internalAddresses was calculated from scratch on every tx comparison


[self.transactions sortWithOptions:NSSortStable
usingComparator:^NSComparisonResult(id tx1, id tx2) {
if (isAscending(tx1, tx2)) return NSOrderedAscending;
if (isAscending(tx2, tx1)) return NSOrderedDescending;

NSUInteger i = transactionAddressIndex(tx1, self.internalAddresses);
NSUInteger j = transactionAddressIndex(tx2, (i == NSNotFound) ? self.externalAddresses : self.internalAddresses);
NSUInteger i = transactionAddressIndex(tx1, internalAddresses);
NSUInteger j = transactionAddressIndex(tx2, (i == NSNotFound) ? externalAddresses : internalAddresses);

if (i == NSNotFound && j != NSNotFound) i = transactionAddressIndex(tx1, self.externalAddresses);
if (i == NSNotFound && j != NSNotFound) i = transactionAddressIndex(tx1, externalAddresses);
if (i == NSNotFound || j == NSNotFound || i == j) return NSOrderedSame;
return (i > j) ? NSOrderedAscending : NSOrderedDescending;
}];
Expand Down
Loading