Skip to content

Commit

Permalink
fix: crash on fresh boot up, fix rpcuser not being saved when bitcoin…
Browse files Browse the repository at this point in the history
… conf exists, set blockchaininfo to nil when switching chains, autostart tor, show prune as a checked box.
  • Loading branch information
Fonta1n3 committed Nov 4, 2024
1 parent 4e3fb43 commit be7627c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 78 deletions.
6 changes: 1 addition & 5 deletions FullyNoded-Server/Helpers/BitcoinRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ class BitcoinRPC {
let port = UserDefaults.standard.string(forKey: "port") ?? "8332"

let nodeIp = "127.0.0.1:\(port)"

guard let user = UserDefaults.standard.string(forKey: "rpcuser") else {
completion((nil, "No rpc user saved."))
return
}
let user = UserDefaults.standard.string(forKey: "rpcuser") ?? "FullyNoded-Server"

DataManager.retrieve(entityName: "BitcoinRPCCreds") { [weak self] creds in
guard let self = self else { return }
Expand Down
18 changes: 11 additions & 7 deletions FullyNoded-Server/Helpers/InstallBtcCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class CreateFNDirConfigureCore {
if let existingBitcoinConf = conf {
for item in existingBitcoinConf {
let arr = item.split(separator: "=")
guard arr.count > 1 else { return }
guard let value = Int(arr[1]) else { return }
if item.hasPrefix("prune=") {
UserDefaults.standard.setValue(value, forKey: "prune")
}
if item.hasPrefix("txindex=") {
UserDefaults.standard.setValue(value, forKey: "txindex")
if arr.count > 1 {
if let value = Int(arr[1]) {
if item.hasPrefix("prune=") {
UserDefaults.standard.setValue(value, forKey: "prune")
}
if item.hasPrefix("txindex=") {
UserDefaults.standard.setValue(value, forKey: "txindex")
}
}
}
}
let rpcuser = "FullyNoded-Server"
Expand Down Expand Up @@ -121,10 +123,12 @@ class CreateFNDirConfigureCore {
DataManager.retrieve(entityName: "BitcoinRPCCreds") { existingCreds in
if let _ = existingCreds {
DataManager.update(keyToUpdate: "password", newValue: encryptedPass, entity: "BitcoinRPCCreds") { updated in
UserDefaults.standard.set("FullyNoded-Server", forKey: "rpcuser")
completion(updated)
}
} else {
DataManager.saveEntity(entityName: "BitcoinRPCCreds", dict: ["password": encryptedPass]) { saved in
UserDefaults.standard.set("FullyNoded-Server", forKey: "rpcuser")
completion(saved)
}
}
Expand Down
57 changes: 28 additions & 29 deletions FullyNoded-Server/Views/BitcoinCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ struct BitcoinCore: View {
} label: {
Text("bitcoin.conf")
}
Button {
openFile(file: "debug.log")
} label: {
Text("Log")
if let debugPath = debugLogPath() {
Button {
openFile(file: debugPath)
} label: {
Text("debug.log")
}
}
Button {
refreshRPCAuth()
Expand Down Expand Up @@ -364,7 +366,7 @@ struct BitcoinCore: View {
}

private func initialLoad() {
selectedChain = UserDefaults.standard.string(forKey: "chain") ?? "main"
selectedChain = UserDefaults.standard.string(forKey: "chain") ?? "signet"
DataManager.retrieve(entityName: "BitcoinEnv") { env in
guard let env = env else { return }
let envValues = BitcoinEnvValues(dictionary: env)
Expand Down Expand Up @@ -442,6 +444,7 @@ struct BitcoinCore: View {
UserDefaults.standard.setValue(port, forKey: "port")
UserDefaults.standard.setValue(chain.lowercased(), forKey: "chain")
self.env["CHAIN"] = chain
self.blockchainInfo = nil
DataManager.update(keyToUpdate: "chain", newValue: chain, entity: "BitcoinEnv") { updated in
guard updated else {
showMessage(message: "There was an issue updating your network...")
Expand Down Expand Up @@ -556,28 +559,9 @@ struct BitcoinCore: View {


private func showBitcoinLog() {
let chain = Defaults.shared.chain
var path: URL?
guard let debugPath = debugLogPath() else { return }

print(Defaults.shared.dataDir)

switch chain {
case "main":
path = URL(fileURLWithPath: "\(Defaults.shared.dataDir)/debug.log")
case "test":
path = URL(fileURLWithPath: "\(Defaults.shared.dataDir)/testnet3/debug.log")
case "regtest":
path = URL(fileURLWithPath: "\(Defaults.shared.dataDir)/regtest/debug.log")
case "signet":
path = URL(fileURLWithPath: "\(Defaults.shared.dataDir)/signet/debug.log")
default:
break
}

guard let path = path else {
print("can not get path")
return
}
let path = URL(fileURLWithPath: debugPath)

guard let log = try? String(contentsOf: path, encoding: .utf8) else {
print("can't get log, path: \(path)")
Expand All @@ -593,13 +577,28 @@ struct BitcoinCore: View {
if lastLogItem.contains("Shutdown: done") {
isRunning = false
}
// if lastLogItem.contains("ThreadRPCServer incorrect password") {
// showMessage(message: lastLogItem)
// }
}
}
}

private func debugLogPath() -> String? {
let chain = Defaults.shared.chain
var debugLogPath: String?
switch chain {
case "main":
debugLogPath = "\(Defaults.shared.dataDir)/debug.log"
case "test":
debugLogPath = "\(Defaults.shared.dataDir)/testnet3/debug.log"
case "regtest":
debugLogPath = "\(Defaults.shared.dataDir)/regtest/debug.log"
case "signet":
debugLogPath = "\(Defaults.shared.dataDir)/signet/debug.log"
default:
break
}
return debugLogPath
}

private func isBitcoinCoreRunning() {
isAnimating = true
BitcoinRPC.shared.command(method: "getblockchaininfo", params: [:]) { (result, error) in
Expand Down
2 changes: 2 additions & 0 deletions FullyNoded-Server/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ struct ContentView: View {
.onAppear(perform: {
if TorClient.sharedInstance.state == .connected {
torProgress = 100.0
} else {
TorClient.sharedInstance.start(delegate: nil)
}
TorClient.sharedInstance.showProgress = { progress in
torProgress = Double(progress)
Expand Down
2 changes: 1 addition & 1 deletion FullyNoded-Server/Views/JoinMarketTaggedReleases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ struct JoinMarketTaggedReleasesView: View {

init(taggedReleases: TaggedReleases, existingVersion: String) {
self.taggedReleases = taggedReleases
self.taggedRelease = taggedReleases[0]
self.existingVersion = existingVersion
self.taggedRelease = taggedReleases[0]
}

var body: some View {
Expand Down
79 changes: 43 additions & 36 deletions FullyNoded-Server/Views/TaggedReleasesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
struct TaggedReleasesView: View {

let timerForBitcoinInstall = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
@State private var prune = false
@State private var bitcoinCoreInstallComplete = false
@State private var startCheckingForBitcoinInstall = false
@State private var description = ""
Expand All @@ -25,8 +26,8 @@ struct TaggedReleasesView: View {

init(taggedReleases: TaggedReleases, existingVersion: String) {
self.taggedReleases = taggedReleases
self.taggedRelease = taggedReleases[0]
self.existingVersion = existingVersion
self.taggedRelease = taggedReleases[0]
}

var body: some View {
Expand All @@ -40,42 +41,7 @@ struct TaggedReleasesView: View {
}
.padding([.top, .leading, .trailing])

HStack() {
Text("Data Directory:")
Label(dataDir, systemImage: "")
Button("Update") {
chooseDataDir()
}
}
.padding([.leading, .trailing])
.frame(maxWidth: .infinity, alignment: .leading)

Text("Do not update the data directory unless you want to save your Bitcoin Core data in a custom location like an external hard drive.")
.padding([.leading])
.foregroundStyle(.secondary)

HStack() {
if txIndex == 0 {
Label("Pruned node", systemImage: "")


Button("Do not prune") {
UserDefaults.standard.setValue(1, forKey: "txindex")
UserDefaults.standard.setValue(0, forKey: "prune")
txIndex = 1
}
} else {
Label("Full node", systemImage: "")

Button("Prune") {
UserDefaults.standard.setValue(0, forKey: "txindex")
UserDefaults.standard.setValue(1000, forKey: "prune")
txIndex = 0
}
}
}
.padding([.leading, .trailing])
.frame(maxWidth: .infinity, alignment: .leading)

if let author = taggedRelease.author, let login = author.login, let tagName = taggedRelease.tagName {
let processedVersion = tagName.replacingOccurrences(of: "v", with: "")
Expand All @@ -100,6 +66,45 @@ struct TaggedReleasesView: View {
.padding([.leading, .bottom])
.frame(maxWidth: .infinity, alignment: .leading)

Label("Configuration options", systemImage: "gear")
.padding(.leading)
.frame(maxWidth: .infinity, alignment: .leading)

HStack() {
Text("Data Directory:")
Label(dataDir, systemImage: "")
Button("Update") {
chooseDataDir()
}
}
.padding([.leading, .trailing])
.frame(maxWidth: .infinity, alignment: .leading)

Text("Do not update the data directory unless you want to save your Bitcoin Core data in a custom location like an external hard drive.")
.padding([.leading])
.foregroundStyle(.secondary)

HStack() {
Toggle("Prune", isOn: $prune)
.onChange(of: prune) { oldValue, newValue in
if newValue {
UserDefaults.standard.setValue(0, forKey: "txindex")
UserDefaults.standard.setValue(1000, forKey: "prune")
txIndex = 0
} else {
UserDefaults.standard.setValue(1, forKey: "txindex")
UserDefaults.standard.setValue(0, forKey: "prune")
}
}
}
.padding([.leading, .trailing])
.frame(maxWidth: .infinity, alignment: .leading)

Text("Pruning your node reduces the amount of disc space Bitcoin Core will use.")
.padding([.leading])
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity, alignment: .leading)

HStack() {
if isAnimating {
ProgressView()
Expand Down Expand Up @@ -160,6 +165,7 @@ struct TaggedReleasesView: View {
}
Spacer()
.onAppear {
prune = !((Defaults.shared.prune) == 0)
taggedRelease = .init(url: nil, assetsURL: nil, uploadURL: nil, htmlURL: nil, id: nil, author: nil, nodeID: nil, tagName: "", targetCommitish: nil, name: nil, draft: nil, prerelease: nil, createdAt: nil, publishedAt: nil, tarballURL: "", zipballURL: nil, body: nil)
}
.alert(message, isPresented: $showError) {
Expand Down Expand Up @@ -264,6 +270,7 @@ struct TaggedReleasesView: View {
}
description = "Downloading Bitcoin Core tarball from \(macOSUrl)"
CreateFNDirConfigureCore.checkForExistingConf { startDownload in
print("startDownload")
if startDownload {
isAnimating = true
downloadTask(url: URL(string: macOSUrl)!) { data in
Expand Down

0 comments on commit be7627c

Please sign in to comment.