Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #7464: Remove legacy ledger wallet migrations
Browse files Browse the repository at this point in the history
The Ledger code in brave-core behaves like a singleton as it uses a global pointer to log, so we can't have more than 1 created. Creating a legacy wallet to do migrations was now crashing because ledger changes in 1.52 to use mojo now expose threading errors.
  • Loading branch information
kylehickinson committed May 16, 2023
1 parent 9ac98b8 commit 1b29ce0
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class BraveRewardsViewController: UIViewController, PopoverContentComponent {

let tab: Tab
let rewards: BraveRewards
let legacyWallet: BraveLedger?
var actionHandler: ((Action) -> Void)?

private var ledgerObserver: LedgerObserver?
Expand All @@ -45,10 +44,9 @@ class BraveRewardsViewController: UIViewController, PopoverContentComponent {

private var supportedListCount: Int = 0

init(tab: Tab, rewards: BraveRewards, legacyWallet: BraveLedger?) {
init(tab: Tab, rewards: BraveRewards) {
self.tab = tab
self.rewards = rewards
self.legacyWallet = legacyWallet

super.init(nibName: nil, bundle: nil)
}
Expand Down Expand Up @@ -117,13 +115,7 @@ class BraveRewardsViewController: UIViewController, PopoverContentComponent {
}
}
}
if let legacyWallet = self.legacyWallet, !legacyWallet.isInitialized {
legacyWallet.initializeLedgerService({
self.reloadData()
})
} else {
self.reloadData()
}
self.reloadData()
}

view.snp.makeConstraints {
Expand Down
61 changes: 1 addition & 60 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ public class BrowserViewController: UIViewController {

let rewards: BraveRewards
var ledgerObserver: LedgerObserver?
let legacyWallet: BraveLedger?
var promotionFetchTimer: Timer?
private var notificationsHandler: AdsNotificationHandler?
let notificationsPresenter = BraveNotificationsPresenter()
Expand Down Expand Up @@ -283,18 +282,6 @@ public class BrowserViewController: UIViewController {
let configuration: BraveRewards.Configuration = .current()

Self.migrateAdsConfirmations(for: configuration)
legacyWallet = Self.legacyWallet(for: configuration)
if let wallet = legacyWallet {
// Legacy ledger is disabled by default
wallet.setAutoContributeEnabled(false)
// Ensure we remove any pending contributions or recurring tips from the legacy wallet
wallet.removeAllPendingContributions { _ in }
wallet.listRecurringTips { publishers in
publishers.forEach {
wallet.removeRecurringTip(publisherId: $0.id)
}
}
}

// Initialize Rewards
self.rewards = BraveRewards(configuration: configuration)
Expand Down Expand Up @@ -344,9 +331,7 @@ public class BrowserViewController: UIViewController {
if shouldStartAds {
// Only start ledger service automatically if ads is enabled
if rewards.isEnabled {
rewards.startLedgerService {
self.legacyWallet?.initializeLedgerService(nil)
}
rewards.startLedgerService(nil)
} else {
rewards.ads.initialize { _ in }
}
Expand Down Expand Up @@ -382,50 +367,6 @@ public class BrowserViewController: UIViewController {
braveCore.sendTabAPI.removeObserver(observer)
}
}

static func legacyWallet(for config: BraveRewards.Configuration) -> BraveLedger? {
let fm = FileManager.default
let stateStorage = config.storageURL
let legacyLedger = stateStorage.appendingPathComponent("legacy_ledger")

// Check if we've already migrated the users wallet to the `legacy_rewards` folder
if fm.fileExists(atPath: legacyLedger.path) {
return BraveLedger(stateStoragePath: legacyLedger.path)
}

// We've already performed an attempt at migration, if there wasn't a legacy folder, then
// we have no legacy wallet.
if Preferences.Rewards.migratedLegacyWallet.value {
return nil
}

// Ledger exists in the state storage under `ledger` folder, if that folder doesn't exist
// then the user hasn't actually launched the app before and doesn't need to migrate
let ledgerFolder = stateStorage.appendingPathComponent("ledger")
if !fm.fileExists(atPath: ledgerFolder.path) {
// No wallet, therefore no legacy folder needed
Preferences.Rewards.migratedLegacyWallet.value = true
return nil
}

do {
// Copy the current `ledger` directory into the new legacy state storage path
try fm.copyItem(at: ledgerFolder, to: legacyLedger)
// Remove the old Rewards DB so that it starts fresh
try fm.removeItem(atPath: ledgerFolder.appendingPathComponent("Rewards.db").path)
// And remove the sqlite journal file if it exists
let journalPath = ledgerFolder.appendingPathComponent("Rewards.db-journal").path
if fm.fileExists(atPath: journalPath) {
try fm.removeItem(atPath: journalPath)
}

Preferences.Rewards.migratedLegacyWallet.value = true
return BraveLedger(stateStoragePath: legacyLedger.path)
} catch {
adsRewardsLog.error("Failed to migrate legacy wallet into a new folder: \(error.localizedDescription)")
return nil
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension BrowserViewController {
func showRewardsDebugSettings() {
if AppConstants.buildChannel.isPublic { return }

let settings = RewardsDebugSettingsViewController(rewards: rewards, legacyWallet: legacyWallet)
let settings = RewardsDebugSettingsViewController(rewards: rewards)
let container = UINavigationController(rootViewController: settings)
present(container, animated: true)
}
Expand Down Expand Up @@ -67,8 +67,7 @@ extension BrowserViewController {

let braveRewardsPanel = BraveRewardsViewController(
tab: tab,
rewards: rewards,
legacyWallet: legacyWallet
rewards: rewards
)
braveRewardsPanel.actionHandler = { [weak self] action in
switch action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ extension BrowserViewController {
tabManager: self.tabManager,
feedDataSource: self.feedDataSource,
rewards: self.rewards,
legacyWallet: self.legacyWallet,
windowProtection: self.windowProtection,
braveCore: self.braveCore,
keyringStore: keyringStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import Combine
class BraveRewardsSettingsViewController: TableViewController {

let rewards: BraveRewards
let legacyWallet: BraveLedger?
var walletTransferLearnMoreTapped: (() -> Void)?

init(_ rewards: BraveRewards, legacyWallet: BraveLedger?) {
init(_ rewards: BraveRewards) {
self.rewards = rewards
self.legacyWallet = legacyWallet

super.init(style: .insetGrouped)
}
Expand Down Expand Up @@ -53,7 +51,7 @@ class BraveRewardsSettingsViewController: TableViewController {
text: Strings.RewardsInternals.title,
selection: {
guard let self = self else { return }
let controller = RewardsInternalsViewController(ledger: ledger, legacyLedger: self.legacyWallet)
let controller = RewardsInternalsViewController(ledger: ledger)
self.navigationController?.pushViewController(controller, animated: true)
}, accessory: .disclosureIndicator)
])
Expand All @@ -70,13 +68,7 @@ class BraveRewardsSettingsViewController: TableViewController {

rewards.startLedgerService { [weak self] in
guard let self = self else { return }
if let legacyWallet = self.legacyWallet, !legacyWallet.isInitialized {
legacyWallet.initializeLedgerService {
self.reloadSections()
}
} else {
self.reloadSections()
}
self.reloadSections()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ private typealias EnvironmentOverride = Preferences.Rewards.EnvironmentOverride
class RewardsDebugSettingsViewController: TableViewController {

let rewards: BraveRewards
let legacyWallet: BraveLedger?
private var adsInfo: (viewed: Int, amount: Double, paymentDate: Date?)?

init(rewards: BraveRewards, legacyWallet: BraveLedger?) {
init(rewards: BraveRewards) {
self.rewards = rewards
self.legacyWallet = legacyWallet

super.init(style: .grouped)

Expand Down Expand Up @@ -187,28 +185,6 @@ class RewardsDebugSettingsViewController: TableViewController {
}, cellClass: ButtonCell.self),
]
),
Section(
header: .title("Legacy Wallet"),
rows: [
Row(
text: "Internals",
selection: { [unowned self] in
guard let legacyWallet = legacyWallet else {
let alert = UIAlertController(title: "Legacy Wallet", message: "No Wallet Found. Use \"Create Legacy Wallet\" action below to duplicate the current wallet", preferredStyle: .alert)
alert.addAction(.init(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true)
return
}
let controller = RewardsInternalsDebugViewController(ledger: legacyWallet)
self.navigationController?.pushViewController(controller, animated: true)
}, accessory: .disclosureIndicator),
Row(
text: "Create Legacy Wallet",
selection: { [unowned self] in
self.createLegacyLedger()
}, cellClass: ButtonCell.self),
]
),
Section(
header: .title("Ads"),
rows: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,11 @@ class RewardsInternalsViewController: TableViewController {
private let ledger: BraveLedger
private var internalsInfo: BraveCore.BraveRewards.RewardsInternalsInfo?

private let legacyLedger: BraveLedger?
private var legacyInternalsInfo: BraveCore.BraveRewards.RewardsInternalsInfo?

init(ledger: BraveLedger, legacyLedger: BraveLedger?) {
init(ledger: BraveLedger) {
self.ledger = ledger
self.legacyLedger = legacyLedger
super.init(style: .insetGrouped)
let group = DispatchGroup()
group.enter()
ledger.rewardsInternalInfo { [weak self] info in
self?.internalsInfo = info
group.leave()
}
if let legacyLedger = legacyLedger {
group.enter()
legacyLedger.rewardsInternalInfo { [weak self] info in
self?.legacyInternalsInfo = info
group.leave()
}
}
group.notify(queue: .main) { [weak self] in
self?.reloadSections()
}
}
Expand Down Expand Up @@ -85,7 +69,7 @@ class RewardsInternalsViewController: TableViewController {
$0.dateStyle = .short
}

var sections: [Static.Section] = [
let sections: [Static.Section] = [
.init(
rows: [
Row(text: Strings.RewardsInternals.sharingWarningTitle, detailText: Strings.RewardsInternals.sharingWarningMessage, cellClass: WarningCell.self)
Expand Down Expand Up @@ -115,27 +99,6 @@ class RewardsInternalsViewController: TableViewController {
),
]

if let legacyLedger = legacyLedger, let internals = legacyInternalsInfo, !legacyLedger.isLedgerTransferExpired {
let legacyWalletSection = sections.count
sections.append(
.init(
header: .title(Strings.RewardsInternals.legacyWalletInfoHeader),
rows: [
Row(text: Strings.RewardsInternals.keyInfoSeed, detailText: "\(internals.isKeyInfoSeedValid ? Strings.RewardsInternals.valid : Strings.RewardsInternals.invalid)"),
Row(
text: Strings.RewardsInternals.walletPaymentID, detailText: internals.paymentId,
selection: { [unowned self] in
if let index = self.dataSource.sections[safe: legacyWalletSection]?.rows.firstIndex(where: { $0.cellClass == PaymentIDCell.self }),
let cell = self.tableView.cellForRow(at: IndexPath(item: index, section: legacyWalletSection)) as? PaymentIDCell {
cell.showMenu()
}
}, cellClass: PaymentIDCell.self),
Row(text: Strings.RewardsInternals.walletCreationDate, detailText: dateFormatter.string(from: Date(timeIntervalSince1970: TimeInterval(internals.bootStamp))))
]
)
)
}

dataSource.sections = sections
}
}
Expand Down
7 changes: 2 additions & 5 deletions Sources/Brave/Frontend/Settings/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class SettingsViewController: TableViewController {
private let profile: Profile
private let tabManager: TabManager
private let rewards: BraveRewards?
private let legacyWallet: BraveLedger?
private let feedDataSource: FeedDataSource
private let historyAPI: BraveHistoryAPI
private let passwordAPI: BravePasswordAPI
Expand All @@ -72,7 +71,6 @@ class SettingsViewController: TableViewController {
tabManager: TabManager,
feedDataSource: FeedDataSource,
rewards: BraveRewards? = nil,
legacyWallet: BraveLedger? = nil,
windowProtection: WindowProtection?,
braveCore: BraveCoreMain,
keyringStore: KeyringStore? = nil,
Expand All @@ -82,7 +80,6 @@ class SettingsViewController: TableViewController {
self.tabManager = tabManager
self.feedDataSource = feedDataSource
self.rewards = rewards
self.legacyWallet = legacyWallet
self.windowProtection = windowProtection
self.historyAPI = braveCore.historyAPI
self.passwordAPI = braveCore.passwordAPI
Expand Down Expand Up @@ -117,7 +114,7 @@ class SettingsViewController: TableViewController {

private func displayRewardsDebugMenu() {
guard let rewards = rewards else { return }
let settings = RewardsDebugSettingsViewController(rewards: rewards, legacyWallet: legacyWallet)
let settings = RewardsDebugSettingsViewController(rewards: rewards)
navigationController?.pushViewController(settings, animated: true)
}

Expand Down Expand Up @@ -256,7 +253,7 @@ class SettingsViewController: TableViewController {
Row(
text: Strings.braveRewardsTitle,
selection: { [unowned self] in
let rewardsVC = BraveRewardsSettingsViewController(rewards, legacyWallet: self.legacyWallet)
let rewardsVC = BraveRewardsSettingsViewController(rewards)
rewardsVC.walletTransferLearnMoreTapped = { [weak self] in
guard let self = self else { return }
self.dismiss(animated: true) {
Expand Down
1 change: 0 additions & 1 deletion Sources/BraveStrings/BraveStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3676,7 +3676,6 @@ extension Strings {
public struct RewardsInternals {
public static let title = NSLocalizedString("RewardsInternalsTitle", tableName: "BraveShared", bundle: .module, value: "Rewards Internals", comment: "'Rewards' as in 'Brave Rewards'")
public static let walletInfoHeader = NSLocalizedString("RewardsInternalsWalletInfoHeader", tableName: "BraveShared", bundle: .module, value: "Rewards Profile Info", comment: "")
public static let legacyWalletInfoHeader = NSLocalizedString("RewardsInternalsLegacyWalletInfoHeader", tableName: "BraveShared", bundle: .module, value: "Legacy Wallet Info", comment: "")
public static let keyInfoSeed = NSLocalizedString("RewardsInternalsKeyInfoSeed", tableName: "BraveShared", bundle: .module, value: "Key Info Seed", comment: "")
public static let valid = NSLocalizedString("RewardsInternalsValid", tableName: "BraveShared", bundle: .module, value: "Valid", comment: "")
public static let invalid = NSLocalizedString("RewardsInternalsInvalid", tableName: "BraveShared", bundle: .module, value: "Invalid", comment: "")
Expand Down
1 change: 0 additions & 1 deletion Sources/Preferences/GlobalPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ extension Preferences {
public static let rewardsToggledOnce = Option<Bool>(key: "rewards.rewards-toggled-once", default: false)
public static let isUsingBAP = Option<Bool?>(key: "rewards.is-using-bap", default: nil)
public static let seenDataMigrationFailureError = Option<Bool>(key: "rewards.seen-data-migration-failure-error", default: false)
public static let migratedLegacyWallet = Option<Bool>(key: "rewards.migrated-legacy-wallet", default: false)
public static let adaptiveCaptchaFailureCount = Option<Int>(key: "rewards.adaptive-captcha-failure-count", default: 0)
public static let adsEnabledTimestamp = Option<Date?>(key: "rewards.ads.last-time-enabled", default: nil)
public static let adsDisabledTimestamp = Option<Date?>(key: "rewards.ads.last-time-disabled", default: nil)
Expand Down

0 comments on commit 1b29ce0

Please sign in to comment.