-
Notifications
You must be signed in to change notification settings - Fork 8
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
Network specific storage paths #149
Conversation
Warning Rate limit exceeded@danielnordh has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 30 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
WalkthroughThe changes in the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🔭 Outside diff range comments (2)
LDKNodeMonday/Service/Lightning Service/LightningNodeService.swift (2)
Line range hint
102-102
: Remove forced unwrap in node initialization.The forced unwrap (
try!
) in node initialization is dangerous and could crash the app. This is particularly risky as node initialization could fail for various reasons (e.g., file system issues, permission problems, network conflicts).Replace with proper error handling:
- let ldkNode = try! nodeBuilder.build() // Handle error instead of "!" + do { + self.ldkNode = try nodeBuilder.build() + } catch { + logger.error("Failed to initialize Lightning node: \(error)") + // Consider implementing a recovery strategy or user notification + throw error + } - self.ldkNode = ldkNodeAlso, consider adding a custom error type to handle specific initialization failures:
enum LightningNodeError: Error { case initializationFailed(underlying: Error) case directoryCreationFailed(path: String) }
Network switching cleanup needs to be more targeted
The current implementation of
deleteAllContentsInDocumentsDirectory()
performs a complete wipe of the documents directory, which could unintentionally delete data from other networks. Consider implementing network-specific cleanup by:
- Only deleting the specific network directory path:
documents/<network>/
- Adding a
deleteNetworkDirectory(network: Network)
method for targeted cleanup🔗 Analysis chain
Line range hint
1-102
: Verify network switching cleanup process.While the network-specific paths provide good isolation, we should verify the cleanup process during network switching to prevent data leaks or corruption.
Let's check for potential issues:
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Check for network-specific directory handling and potential cleanup issues # Check for any hardcoded network paths that might bypass the new network-specific structure rg -i "documents.*/(bitcoin|testnet|signet|regtest)" --type swift # Look for any file operations that might not respect network isolation ast-grep --pattern 'FileManager.default.$_($$$)' --lang swift # Check for proper cleanup in deleteDocuments method ast-grep --pattern 'func deleteDocuments() { $$$ }' --lang swiftLength of output: 661
Script:
#!/bin/bash # Check the implementation of deleteAllContentsInDocumentsDirectory # Look for the extension implementation rg -A 10 "deleteAllContentsInDocumentsDirectory" --type swift # Check for any other cleanup related methods ast-grep --pattern 'extension FileManager { $$$ }' --lang swiftLength of output: 1816
config.storageDirPath = FileManager.default.getDocumentsDirectoryPath().appending( | ||
"/\(network.description)/" | ||
) | ||
config.logDirPath = FileManager.default.getDocumentsDirectoryPath().appending( | ||
"/\(network.description)/" | ||
) |
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.
🛠️ Refactor suggestion
Ensure proper directory handling for network-specific paths.
While the network-specific paths align with the PR objective, there are a few improvements needed:
- The code should ensure directories exist before use
- Use URL path components instead of string concatenation for robust path handling
- Consider data migration strategy for network switching
Here's a safer implementation:
- config.storageDirPath = FileManager.default.getDocumentsDirectoryPath().appending(
- "/\(network.description)/"
- )
- config.logDirPath = FileManager.default.getDocumentsDirectoryPath().appending(
- "/\(network.description)/"
- )
+ let documentsPath = FileManager.default.getDocumentsDirectoryPath()
+ let networkPath = URL(fileURLWithPath: documentsPath)
+ .appendingPathComponent(network.description)
+ .path
+
+ try? FileManager.default.createDirectory(
+ atPath: networkPath,
+ withIntermediateDirectories: true
+ )
+
+ config.storageDirPath = networkPath
+ config.logDirPath = networkPath
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
config.storageDirPath = FileManager.default.getDocumentsDirectoryPath().appending( | |
"/\(network.description)/" | |
) | |
config.logDirPath = FileManager.default.getDocumentsDirectoryPath().appending( | |
"/\(network.description)/" | |
) | |
let documentsPath = FileManager.default.getDocumentsDirectoryPath() | |
let networkPath = URL(fileURLWithPath: documentsPath) | |
.appendingPathComponent(network.description) | |
.path | |
try? FileManager.default.createDirectory( | |
atPath: networkPath, | |
withIntermediateDirectories: true | |
) | |
config.storageDirPath = networkPath | |
config.logDirPath = networkPath |
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
LDKNodeMonday/Service/Lightning Service/LightningNodeService.swift (1)
31-44
: Enhance error handling for directory creation.While the implementation correctly uses URL path components and creates network-specific directories, the error handling could be improved:
- Silent error handling with
try?
could mask directory creation issues- No validation that the directory exists before use
- Missing error logging for directory creation failures
Consider this safer implementation:
let documentsPath = FileManager.default.getDocumentsDirectoryPath() let networkPath = URL(fileURLWithPath: documentsPath) .appendingPathComponent(network.description) .path -try? FileManager.default.createDirectory( - atPath: networkPath, - withIntermediateDirectories: true -) +do { + try FileManager.default.createDirectory( + atPath: networkPath, + withIntermediateDirectories: true + ) + + guard FileManager.default.fileExists(atPath: networkPath) else { + throw NSError( + domain: "LightningNodeService", + code: -1, + userInfo: [NSLocalizedDescriptionKey: "Failed to create network directory"] + ) + } + + config.storageDirPath = networkPath + config.logDirPath = networkPath +} catch { + os_log( + .error, + "Failed to create network directory: %{public}@", + error.localizedDescription + ) + throw error +} -config.storageDirPath = networkPath -config.logDirPath = networkPath
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
LDKNodeMonday/Service/Lightning Service/LightningNodeService.swift
(1 hunks)
🔇 Additional comments (1)
LDKNodeMonday/Service/Lightning Service/LightningNodeService.swift (1)
Line range hint
102-102
: Consider improving error handling in node initialization in a future PR.The current implementation uses forced unwrapping (
try!
) for node initialization which could lead to runtime crashes. While this is outside the scope of the current PR's network-specific storage paths objective, consider addressing this in a future PR.Run this script to assess the impact of potential initialization failures:
Great! |
This PR introduces network specific storage paths, to enable switching networks in the future.
It also uses a separate sub-directory for logs.
Broken out from #145
Summary by CodeRabbit