[#1267] Avoid Division By Zero in ScanProgress type
Although is not the best way to solve it this addresses the issue in a single statement and following existing pattern in the code. The error should be thrown earlier in RustBackend itself if so, or fallback to some specific value that makes sense to the domain Closes #1267
This commit is contained in:
parent
552d31eb64
commit
4f225b5115
|
@ -12,8 +12,11 @@ struct ScanProgress: Equatable {
|
|||
let denominator: UInt64
|
||||
|
||||
func progress() throws -> Float {
|
||||
// division by 0 is not a concern here because `ZcashRustBackend.getScanProgress() -> ScanProgress?`
|
||||
// handles the 0 and returns nil rather than returning nil progress or 0 value here
|
||||
guard denominator != 0 else {
|
||||
// this shouldn't happen but if it does, we need to get notified by clients and work on a fix
|
||||
throw ZcashError.rustScanProgressOutOfRange("\(numerator)/\(denominator)")
|
||||
}
|
||||
|
||||
let value = Float(numerator) / Float(denominator)
|
||||
|
||||
// this shouldn't happen but if it does, we need to get notified by clients and work on a fix
|
||||
|
|
|
@ -157,4 +157,14 @@ class ZcashRustBackendTests: XCTestCase {
|
|||
XCTAssertEqual(metadata?.networkType, .mainnet)
|
||||
XCTAssertEqual(metadata?.addressType, .sapling)
|
||||
}
|
||||
|
||||
func testScanProgressThrowsOnWrongValues() {
|
||||
// Assert that throws on Zero denominator
|
||||
XCTAssertThrowsError(try ScanProgress(numerator: 0, denominator: 0).progress())
|
||||
|
||||
// Assert that throws on numerator > denominator
|
||||
XCTAssertThrowsError(try ScanProgress(numerator: 23, denominator: 2).progress())
|
||||
|
||||
XCTAssertNoThrow(try ScanProgress(numerator: 3, denominator: 4).progress())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue