Skip to content

Commit

Permalink
[SWIFT-158] Clean up logging (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylefleming authored May 11, 2021
1 parent 3886040 commit f2d864a
Show file tree
Hide file tree
Showing 52 changed files with 658 additions and 494 deletions.
35 changes: 20 additions & 15 deletions Sources/Account/Account+BalanceUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,39 @@ extension Account {
logger.info("Updating balance...", logFunction: false)
checkForNewTxOuts {
guard $0.successOr(completion: completion) != nil else {
logger.warning("Failed to update balance.", logFunction: false)
logger.warning(
"Failed to update balance: checkForNewTxOuts error: \($0)",
logFunction: false)
return
}

self.viewKeyScanUnscannedMissedBlocks {
self.checkForSpentTxOuts {
guard $0.successOr(completion: completion) != nil else {
logger.warning("Failed to update balance.", logFunction: false)
logger.warning(
"Failed to update balance: checkForSpentTxOuts error: \($0)",
logFunction: false)
return
}

self.checkForSpentTxOuts {
guard $0.successOr(completion: completion) != nil else {
logger.warning("Failed to update balance.", logFunction: false)
return
}

let balance = self.account.readSync { $0.cachedBalance }
let balance = self.account.readSync { $0.cachedBalance }

logger.info(
"Update balance successful. balance: \(redacting: balance)",
logFunction: false)
completion(.success(balance))
}
logger.info(
"Balance update successful. balance: \(redacting: balance)",
logFunction: false)
completion(.success(balance))
}
}
}

func checkForNewTxOuts(completion: @escaping (Result<(), ConnectionError>) -> Void) {
checkForNewFogViewTxOuts {
guard $0.successOr(completion: completion) != nil else { return }

self.viewKeyScanUnscannedMissedBlocks(completion: completion)
}
}

func checkForNewFogViewTxOuts(completion: @escaping (Result<(), ConnectionError>) -> Void) {
txOutFetcher.fetchTxOuts(partialResultsWithWriteLock: { newTxOuts in
logger.info(
"Found \(redacting: newTxOuts.count) new TxOuts using Fog View",
Expand Down
101 changes: 66 additions & 35 deletions Sources/Account/Account+TransactionEstimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright (c) 2020-2021 MobileCoin. All rights reserved.
//

// swiftlint:disable closure_body_length

import Foundation

extension Account {
Expand Down Expand Up @@ -34,8 +36,8 @@ extension Account {
.flatMap { feeStrategy in
let txOuts = self.account.readSync { $0.unspentTxOuts }
logger.info(
"Calculating amountTransferable. unspentTxOutValues: " +
"\(redacting: txOuts.map { $0.value })",
"Calculating amountTransferable. feeLevel: \(feeLevel), " +
"unspentTxOutValues: \(redacting: txOuts.map { $0.value })",
logFunction: false)
return self.txOutSelector
.amountTransferable(feeStrategy: feeStrategy, txOuts: txOuts)
Expand All @@ -62,10 +64,9 @@ extension Account {
feeLevel: FeeLevel,
completion: @escaping (Result<UInt64, TransactionEstimationFetcherError>) -> Void
) {
logger.info("toSendAmount: \(redacting: amount), feeLevel: \(feeLevel)")
guard amount > 0 else {
let errorMessage = "Cannot spend 0 MOB"
logger.error(errorMessage)
let errorMessage = "estimateTotalFee failure: Cannot spend 0 MOB"
logger.error(errorMessage, logFunction: false)
serialQueue.async {
completion(.failure(.invalidInput(errorMessage)))
}
Expand All @@ -76,17 +77,26 @@ extension Account {
completion($0.mapError { .connectionError($0) }
.flatMap { feeStrategy in
let txOuts = self.account.readSync { $0.unspentTxOuts }
let totalFee = self.txOutSelector
logger.info(
"Estimating total fee: amount: \(redacting: amount), feeLevel: " +
"\(feeLevel), unspentTxOutValues: " +
"\(redacting: txOuts.map { $0.value })",
logFunction: false)
return self.txOutSelector
.estimateTotalFee(
toSendAmount: amount,
feeStrategy: feeStrategy,
txOuts: txOuts)
.mapError { _ in
TransactionEstimationFetcherError.insufficientBalance()
}
.map { $0.totalFee }
logger.info("totalFee result: \(redacting: totalFee)")
return totalFee
.map {
logger.info(
"estimateTotalFee: \(redacting: $0.totalFee), " +
"requiresDefrag: \($0.requiresDefrag)",
logFunction: false)
return $0.totalFee
}
})
}
}
Expand All @@ -96,10 +106,9 @@ extension Account {
feeLevel: FeeLevel,
completion: @escaping (Result<Bool, TransactionEstimationFetcherError>) -> Void
) {
logger.info("toSendAmount: \(redacting: amount), feeLevel: \(feeLevel)")
guard amount > 0 else {
let errorMessage = "Cannot spend 0 MOB"
logger.error(errorMessage)
let errorMessage = "requiresDefragmentation failure: Cannot spend 0 MOB"
logger.error(errorMessage, logFunction: false)
serialQueue.async {
completion(.failure(.invalidInput(errorMessage)))
}
Expand All @@ -110,18 +119,26 @@ extension Account {
completion($0.mapError { .connectionError($0) }
.flatMap { feeStrategy in
let txOuts = self.account.readSync { $0.unspentTxOuts }
let requiresDefragmentation = self.txOutSelector
logger.info(
"Calculation defragmentation required: amount: \(redacting: amount), " +
"feeLevel: \(feeLevel), unspentTxOutValues: " +
"\(redacting: txOuts.map { $0.value })",
logFunction: false)
return self.txOutSelector
.estimateTotalFee(
toSendAmount: amount,
feeStrategy: feeStrategy,
txOuts: txOuts)
.mapError { _ in
TransactionEstimationFetcherError.insufficientBalance()
}
.map { $0.requiresDefrag }
logger.info(
"requiresDefragmentation result: \(redacting: requiresDefragmentation)")
return requiresDefragmentation
.map {
logger.info(
"requiresDefragmentation: \($0.requiresDefrag), totalFee: " +
"\(redacting: $0.totalFee)",
logFunction: false)
return $0.requiresDefrag
}
})
}
}
Expand All @@ -133,12 +150,12 @@ extension Account.TransactionEstimator {
func amountTransferable(feeLevel: FeeLevel)
-> Result<UInt64, BalanceTransferEstimationError>
{
let feeStrategy = feeLevel.defaultFeeStrategy
let txOuts = account.readSync { $0.unspentTxOuts }
logger.info(
"Calculating amountTransferable. unspentTxOutValues: " +
"Calculating amountTransferable. feeLevel: \(feeLevel), unspentTxOutValues: " +
"\(redacting: txOuts.map { $0.value })",
logFunction: false)
let feeStrategy = feeLevel.defaultFeeStrategy
return txOutSelector.amountTransferable(feeStrategy: feeStrategy, txOuts: txOuts)
.mapError {
switch $0 {
Expand All @@ -159,42 +176,56 @@ extension Account.TransactionEstimator {
func estimateTotalFee(toSendAmount amount: UInt64, feeLevel: FeeLevel)
-> Result<UInt64, TransactionEstimationError>
{
logger.info("toSendAmount: \(redacting: amount), feeLevel: \(feeLevel)")
guard amount > 0 else {
let errorMessage = "Cannot spend 0 MOB"
logger.error(errorMessage)
let errorMessage = "estimateTotalFee failure: Cannot spend 0 MOB"
logger.error(errorMessage, logFunction: false)
return .failure(.invalidInput(errorMessage))
}

let txOuts = account.readSync { $0.unspentTxOuts }
let feeStrategy = feeLevel.defaultFeeStrategy
let totalFee = txOutSelector
let txOuts = account.readSync { $0.unspentTxOuts }
logger.info(
"Estimating total fee: amount: \(redacting: amount), feeLevel: \(feeLevel), " +
"unspentTxOutValues: \(redacting: txOuts.map { $0.value })",
logFunction: false)
return txOutSelector
.estimateTotalFee(toSendAmount: amount, feeStrategy: feeStrategy, txOuts: txOuts)
.mapError { _ -> TransactionEstimationError in .insufficientBalance() }
.map { $0.totalFee }
logger.info("totalFee result: \(redacting: totalFee)")
return totalFee
.map {
logger.info(
"estimateTotalFee: \(redacting: $0.totalFee), requiresDefrag: " +
"\($0.requiresDefrag)",
logFunction: false)
return $0.totalFee
}
}

@available(*, deprecated, message:
"Use requiresDefragmentation(toSendAmount:feeLevel:completion:) instead")
func requiresDefragmentation(toSendAmount amount: UInt64, feeLevel: FeeLevel)
-> Result<Bool, TransactionEstimationError>
{
logger.info("toSendAmount: \(redacting: amount), feeLevel: \(feeLevel)")
guard amount > 0 else {
let errorMessage = "Cannot spend 0 MOB"
logger.error(errorMessage)
let errorMessage = "requiresDefragmentation failure: Cannot spend 0 MOB"
logger.error(errorMessage, logFunction: false)
return .failure(.invalidInput(errorMessage))
}

let txOuts = account.readSync { $0.unspentTxOuts }
let feeStrategy = feeLevel.defaultFeeStrategy
let requiresDefragmentation = txOutSelector
let txOuts = account.readSync { $0.unspentTxOuts }
logger.info(
"Calculation defragmentation required: amount: \(redacting: amount), feeLevel: " +
"\(feeLevel), unspentTxOutValues: \(redacting: txOuts.map { $0.value })",
logFunction: false)
return txOutSelector
.estimateTotalFee(toSendAmount: amount, feeStrategy: feeStrategy, txOuts: txOuts)
.mapError { _ -> TransactionEstimationError in .insufficientBalance() }
.map { $0.requiresDefrag }
logger.info("requiresDefragmentation result: \(redacting: requiresDefragmentation)")
return requiresDefragmentation
.map {
logger.info(
"requiresDefragmentation: \($0.requiresDefrag), totalFee: " +
"\(redacting: $0.totalFee)",
logFunction: false)
return $0.requiresDefrag
}
}
}
Loading

0 comments on commit f2d864a

Please sign in to comment.