Skip to content
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

Migrate to first pre-release of FFI 0.9.0 #1446

Open
wants to merge 1 commit into
base: ffi-0.9.0-integration
Choose a base branch
from

Conversation

str4d
Copy link
Collaborator

@str4d str4d commented Jun 6, 2024

Includes:

  • Initialization changes to enable log filter customization. We now connect the Rust log level to the Swift log level, and always run other Rust initialization steps.
  • Fetching the ZEC-USD exchange rate over Tor.

Depends on Electric-Coin-Company/zcash-light-client-ffi#142.

This code review checklist is intended to serve as a starting point for the author and reviewer, although it may not be appropriate for all types of changes (e.g. fixing a spelling typo in documentation). For more in-depth discussion of how we think about code review, please see Code Review Guidelines.

Author

  • Self-review: Did you review your own code in GitHub's web interface? Code often looks different when reviewing the diff in a browser, making it easier to spot potential bugs.
  • Automated tests: Did you add appropriate automated tests for any code changes?
  • Code coverage: Did you check the code coverage report for the automated tests? While we are not looking for perfect coverage, the tool can point out potential cases that have been missed.
  • Documentation: Did you update Docs as appropiate? (E.g README.md, etc.)
  • Run the app: Did you run the app and try the changes?
  • Did you provide Screenshots of what the App looks like before and after your changes as part of the description of this PR? (only applicable to UI Changes)
  • Rebase and squash: Did you pull in the latest changes from the main branch and squash your commits before assigning a reviewer? Having your code up to date and squashed will make it easier for others to review. Use best judgement when squashing commits, as some changes (such as refactoring) might be easier to review as a separate commit.

Reviewer

  • Checklist review: Did you go through the code with the Code Review Guidelines checklist?
  • Ad hoc review: Did you perform an ad hoc review? In addition to a first pass using the code review guidelines, do a second pass using your best judgement and experience which may identify additional questions or comments. Research shows that code review is most effective when done in multiple passes, where reviewers look for different things through each pass.
  • Automated tests: Did you review the automated tests?
  • Manual tests: Did you review the manual tests?You will find manual testing guidelines under our manual testing section
  • How is Code Coverage affected by this PR? We encourage you to compare coverage befor and after your changes and when possible, leave it in a better place. Learn More...
  • Documentation: Did you review Docs, README.md, LICENSE.md, and Architecture.md as appropriate?
  • Run the app: Did you run the app and try the changes? While the CI server runs the app to look for build failures or crashes, humans running the app are more likely to notice unexpected log messages, UI inconsistencies, or bad output data.

@@ -56,6 +56,7 @@ actor CompactBlockProcessor {
let saplingParamsSourceURL: SaplingParamsSourceURL
let fsBlockCacheRoot: URL
let dataDb: URL
let torDir: URL
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like there are now too many of these; I originally was passing this through to ZcashRustBackend, but now have a separate TorClient that only the SDKSynchronizer needs to know about. But I didn't have time to figure out which of these could be reverted.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed but not high-prio to deal with it right now I believe. We can file a followup ticket to maybe pass a URLConfiguration or something like that.

@@ -823,33 +847,33 @@ struct ZcashRustBackend: ZcashRustBackendWelding {
}

private extension ZcashRustBackend {
static func enableTracing() {
zcashlc_init_on_load(false)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code here was treating the enableTracing argument to ZcashRustBackend as enabling Rust logging, rather than enabling trace-level logging. The changes I've made so far in this PR have preserved that, but only because OSLogger.LogLevel has no trace level.

case .info:
rustLogging = RustLogging.info
case .event:
rustLogging = RustLogging.info
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust has no EVENT level, so I've mapped that to INFO.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be shortened to

case .info, .event:
     rustLogging = RustLogging.info

@str4d str4d force-pushed the rust-logging-and-exchange-rates branch from ea54bf1 to fca9729 Compare June 6, 2024 02:35
@@ -9,6 +9,21 @@
import Foundation
import libzcashlc

enum RustLogging: String {
/// The logs are completely disabled.
case off = "off"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When enum is defined as String (enum RustLogging: String) the exact value should be omitted unless we want them to be a different value which is not this case. So all cases must be in format case off instead of case off = "off"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, will the string discriminant be taken from the enum variant names in that case? What matters here is they exactly match what tracing expects on the Rust side.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as long as your expected strings match the case names. Which is the case I believe when I reviewed it. The assign of string is used when the name doesn't match the value, usually for key-value scenarios. For example json en-de/coding, example:

enum JsonKeys: String {
  case name = "first-name"
  case surname = "second-name"
}

Dashes and other special characters make it "impossible" to be determined by the case name itself. If json keys are simple ones, value and :String is omitted and case names are used automatically, example:

enum JsonKeys {
  case name // matches 'name' in the json
  case surname // matches 'surname' in the json
}

rustLogging = RustLogging.debug
case .info:
rustLogging = RustLogging.info
case .event:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, case .info, .event: deals with both

Comment on lines 12 to 23
private let fileManager = FileManager()
private let runtime: OpaquePointer

init(torDir: URL) async throws {
// Ensure that the directory exists.
if !fileManager.fileExists(atPath: torDir.path) {
do {
try fileManager.createDirectory(at: torDir, withIntermediateDirectories: true)
} catch {
throw ZcashError.blockRepositoryCreateBlocksCacheDirectory(torDir, error)
}
}
Copy link
Collaborator

@LukasKorba LukasKorba Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary to hold an instance of FileManager for a lifetime of the TorClient as it's used only in the init. Also it not defined as a custom file manager so FileManager.default is sufficient enough for 2 calls or define let fileManager = FileManager() in the init and reuse it.

Copy link
Collaborator

@LukasKorba LukasKorba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few non-blocking comments from me.

@@ -309,6 +309,9 @@ public protocol Synchronizer: AnyObject {
/// - Returns: `AccountBalance`, struct that holds sapling and unshielded balances or `nil` when no account is associated with `accountIndex`
func getAccountBalance(accountIndex: Int) async throws -> AccountBalance?

/// Fetches the latest ZEC-USD exchange rate.
func getExchangeRateUSD() async throws -> Float64
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One note about this proposed SDK method is that it always does a live lookup and has no caching (the caller, e.g. Zashi, would need to cache as desired). Additionally, this method is somewhat slow (a few seconds), particularly on first usage (10+ seconds).

@LukasKorba another approach we could take here is to have a cached value somewhere, and a refreshExchangeRateUSD() method that updates it (so the SDK tracks the last-observed exchange rate). Either approach uses the same backend Rust code, it's just how this gets presented to the SDK user. Thoughts (and suggestions of how to expose if so)?

Includes:
- Initialization changes to enable log filter customization. We now
  connect the Rust log level to the Swift log level, and always run
  other Rust initialization steps.
- Fetching the ZEC-USD exchange rate over Tor.
@str4d str4d force-pushed the rust-logging-and-exchange-rates branch from fca9729 to 929f198 Compare June 19, 2024 15:30
@str4d
Copy link
Collaborator Author

str4d commented Jun 19, 2024

Force-pushed to address comments, and replace Float64 with NSDecimalNumber.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants