-
Notifications
You must be signed in to change notification settings - Fork 16
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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 { | ||
|
@@ -754,7 +752,6 @@ - (void)updateBalance { | |
|
||
if (pending || [inputs intersectsSet:pendingTransactionHashes]) { | ||
[pendingTransactionHashes addObject:uint256_obj(tx.txHash)]; | ||
[balanceHistory insertObject:@(balance) atIndex:0]; | ||
continue; | ||
} | ||
|
||
|
@@ -765,7 +762,6 @@ - (void)updateBalance { | |
pendingCoinbaseLockedTransactionHashes[@(lockedBlockHeight)] = [NSMutableSet set]; | ||
} | ||
[((NSMutableSet *)pendingCoinbaseLockedTransactionHashes[@(lockedBlockHeight)]) addObject:uint256_obj(tx.txHash)]; | ||
[balanceHistory insertObject:@(balance) atIndex:0]; | ||
continue; | ||
} | ||
|
||
|
@@ -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==="); | ||
|
@@ -835,7 +830,6 @@ - (void)updateBalance { | |
self.pendingCoinbaseLockedTransactionHashes = pendingCoinbaseLockedTransactionHashes; | ||
self.spentOutputs = spentOutputs; | ||
self.utxos = utxos; | ||
self.balanceHistory = balanceHistory; | ||
_totalSent = totalSent; | ||
_totalReceived = totalReceived; | ||
|
||
|
@@ -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]; | ||
} | ||
|
@@ -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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
return NO; | ||
}; | ||
|
||
NSArray *externalAddresses = self.externalAddresses; | ||
NSArray *internalAddresses = self.internalAddresses; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
[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; | ||
}]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not used anywhere