Skip to content

Commit

Permalink
[Electric-Coin-Company#1204] Expose ZIP 317
Browse files Browse the repository at this point in the history
- the flag can be turned on/off via new public API
- changelog updated
- unit tests for the toggle added
  • Loading branch information
LukasKorba committed Aug 18, 2023
1 parent d446c6d commit 3cdeedc
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ be able to write to this location after it creates this directory. It is suggest
a subdirectory of the `Documents` directory. If this information is stored in `Documents` then the
system itself won't remove these data.

### [#1204] Expose ZIP 317

The SDK has been prepared but untested for ZIP 317 proposal for a while. Internally it uses a flag `useZIP317Fees` but it's been hardcoded to false.
Ticket #1024 exposes new public API that allows users to turn the flag on/off. Synchronizer now offers an async method `useZIP317Fees(Bool)`.

# 0.22.0-beta

## Checkpoints
Expand Down
6 changes: 5 additions & 1 deletion Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import libzcashlc

actor ZcashRustBackend: ZcashRustBackendWelding {
let minimumConfirmations: UInt32 = 10
let useZIP317Fees = false
var useZIP317Fees = false

let dbData: (String, UInt)
let fsBlockDbRoot: (String, UInt)
Expand Down Expand Up @@ -647,6 +647,10 @@ actor ZcashRustBackend: ZcashRustBackendWelding {

return branchId
}

func useZIP317Fees(_ state: Bool) async {
useZIP317Fees = state
}
}

private extension ZcashRustBackend {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,7 @@ protocol ZcashRustBackendWelding {
/// format `{height}-{hash}-block`. This directory has must be granted both write and read permissions.
/// - Returns `BlockHeight` of the latest cached block or `.empty` if no blocks are stored.
func latestCachedBlockHeight() async -> BlockHeight

/// The SDK previously hardcoded the ZIP 317 flag to `false`. This API allows users to turn it on/off.
func useZIP317Fees(_ state: Bool) async
}
3 changes: 3 additions & 0 deletions Sources/ZcashLightClientKit/Synchronizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ public protocol Synchronizer: AnyObject {
/// - Parameter policy: the rewind policy
func rewind(_ policy: RewindPolicy) -> AnyPublisher<Void, Error>

/// The SDK previously hardcoded the ZIP 317 flag to `false`. This API allows users to turn it on/off.
func useZIP317Fees(_ state: Bool) async

/// Wipes out internal data structures of the SDK. After this call, everything is the same as before any sync. The state of the synchronizer is
/// switched to `unprepared`. So before the next sync, it's required to call `prepare()`.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ public class SDKSynchronizer: Synchronizer {
return subject.eraseToAnyPublisher()
}

// MARK: Zip 317 fees

public func useZIP317Fees(_ state: Bool) async {
await initializer.rustBackend.useZIP317Fees(state)
}

// MARK: Wipe

public func wipe() -> AnyPublisher<Void, Error> {
Expand Down
43 changes: 43 additions & 0 deletions Tests/OfflineTests/SynchronizerOfflineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,49 @@ class SynchronizerOfflineTests: ZcashTestCase {
}
}

func testUseZIP317FeesToggle() async throws {
let validFileURL = URL(fileURLWithPath: "/some/valid/path/to.file")
let validDirectoryURL = URL(fileURLWithPath: "/some/valid/path/to/directory")
let invalidPathURL = URL(string: "https://whatever")!

let initializer = Initializer(
cacheDbURL: nil,
fsBlockDbRoot: validDirectoryURL,
generalStorageURL: validDirectoryURL,
dataDbURL: invalidPathURL,
endpoint: LightWalletEndpointBuilder.default,
network: ZcashNetworkBuilder.network(for: .testnet),
spendParamsURL: validFileURL,
outputParamsURL: validFileURL,
saplingParamsSourceURL: .default,
alias: .default,
loggingPolicy: .default(.debug)
)

XCTAssertNotNil(initializer.urlsParsingError)

let synchronizer = SDKSynchronizer(initializer: initializer)

let backend = initializer.container.resolve(ZcashRustBackendWelding.self)

guard let rustBackend = backend as? ZcashRustBackend else {
return XCTFail()
}

let unused = await rustBackend.useZIP317Fees
XCTAssertFalse(unused)

await synchronizer.useZIP317Fees(true)

let used = await rustBackend.useZIP317Fees
XCTAssertTrue(used)

await synchronizer.useZIP317Fees(false)

let unusedAgain = await rustBackend.useZIP317Fees
XCTAssertFalse(unusedAgain)
}

func synchronizerState(for internalSyncStatus: InternalSyncStatus) -> SynchronizerState {
SynchronizerState(
syncSessionID: .nullID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,21 @@ class SynchronizerMock: Synchronizer {
}
}

// MARK: - useZIP317Fees

var useZIP317FeesCallsCount = 0
var useZIP317FeesCalled: Bool {
return useZIP317FeesCallsCount > 0
}
var useZIP317FeesReceivedState: Bool?
var useZIP317FeesClosure: ((Bool) async -> Void)?

func useZIP317Fees(_ state: Bool) async {
useZIP317FeesCallsCount += 1
useZIP317FeesReceivedState = state
await useZIP317FeesClosure!(state)
}

// MARK: - wipe

var wipeCallsCount = 0
Expand Down Expand Up @@ -2767,4 +2782,22 @@ actor ZcashRustBackendWeldingMock: ZcashRustBackendWelding {
}
}

// MARK: - useZIP317Fees

var useZIP317FeesCallsCount = 0
var useZIP317FeesCalled: Bool {
return useZIP317FeesCallsCount > 0
}
var useZIP317FeesReceivedState: Bool?
var useZIP317FeesClosure: ((Bool) async -> Void)?
func setUseZIP317FeesClosure(_ param: ((Bool) async -> Void)?) async {
useZIP317FeesClosure = param
}

func useZIP317Fees(_ state: Bool) async {
useZIP317FeesCallsCount += 1
useZIP317FeesReceivedState = state
await useZIP317FeesClosure!(state)
}

}

0 comments on commit 3cdeedc

Please # to comment.