2019-10-18 11:45:19 -07:00
|
|
|
//
|
|
|
|
// CompactBlockStorageTests.swift
|
|
|
|
// ZcashLightClientKitTests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 10/13/19.
|
|
|
|
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import XCTest
|
|
|
|
@testable import ZcashLightClientKit
|
|
|
|
class CompactBlockStorageTests: XCTestCase {
|
|
|
|
|
2019-11-18 11:49:36 -08:00
|
|
|
var compactBlockDao: CompactBlockRepository = try! TestDbBuilder.inMemoryCompactBlockStorage()
|
2019-10-18 11:45:19 -07:00
|
|
|
|
|
|
|
func testEmptyStorage() {
|
2019-10-30 13:18:57 -07:00
|
|
|
XCTAssertEqual(try! compactBlockDao.latestHeight(), BlockHeight.empty())
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func testStoreThousandBlocks() {
|
2019-10-30 13:18:57 -07:00
|
|
|
let initialHeight = try! compactBlockDao.latestHeight()
|
2020-01-14 14:25:14 -08:00
|
|
|
let startHeight = ZcashSDK.SAPLING_ACTIVATION_HEIGHT
|
2019-10-18 11:45:19 -07:00
|
|
|
let blockCount = Int(1_000)
|
|
|
|
let finalHeight = startHeight + blockCount
|
|
|
|
|
|
|
|
do {
|
2020-02-26 08:54:48 -08:00
|
|
|
try TestDbBuilder.seed(db: compactBlockDao, with: startHeight...finalHeight)
|
2019-10-18 11:45:19 -07:00
|
|
|
} catch {
|
|
|
|
XCTFail("seed faild with error: \(error)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:18:57 -07:00
|
|
|
let latestHeight = try! compactBlockDao.latestHeight()
|
2019-10-18 11:45:19 -07:00
|
|
|
XCTAssertNotEqual(initialHeight, latestHeight)
|
|
|
|
XCTAssertEqual(latestHeight, finalHeight)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testStoreOneBlockFromEmpty() {
|
|
|
|
|
2019-10-30 13:18:57 -07:00
|
|
|
let initialHeight = try! compactBlockDao.latestHeight()
|
2019-10-18 11:45:19 -07:00
|
|
|
guard initialHeight == BlockHeight.empty() else {
|
|
|
|
XCTFail("database not empty, latest height: \(initialHeight)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let expectedHeight = BlockHeight(123_456)
|
|
|
|
guard let block = StubBlockCreator.createRandomDataBlock(with: expectedHeight) else {
|
|
|
|
XCTFail("could not create randem block with height: \(expectedHeight)")
|
|
|
|
return
|
|
|
|
}
|
2019-10-30 13:18:57 -07:00
|
|
|
XCTAssertNoThrow(try compactBlockDao.write(blocks: [block]))
|
2019-10-18 11:45:19 -07:00
|
|
|
|
|
|
|
do {
|
2019-10-30 13:18:57 -07:00
|
|
|
let result = try compactBlockDao.latestHeight()
|
2019-10-18 11:45:19 -07:00
|
|
|
XCTAssertEqual(result, expectedHeight)
|
|
|
|
} catch {
|
|
|
|
XCTFail("latestBlockHeight failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRewindTo() {
|
|
|
|
|
2020-01-14 14:25:14 -08:00
|
|
|
let startHeight = ZcashSDK.SAPLING_ACTIVATION_HEIGHT
|
2019-10-18 11:45:19 -07:00
|
|
|
let blockCount = Int(1_000)
|
|
|
|
let finalHeight = startHeight + blockCount
|
|
|
|
|
|
|
|
do {
|
2020-02-26 08:54:48 -08:00
|
|
|
try TestDbBuilder.seed(db: compactBlockDao, with: startHeight...finalHeight)
|
2019-10-18 11:45:19 -07:00
|
|
|
} catch {
|
|
|
|
XCTFail("seed faild with error: \(error)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let rewindHeight = BlockHeight(finalHeight - 233)
|
|
|
|
|
2019-10-30 13:18:57 -07:00
|
|
|
XCTAssertNoThrow(try compactBlockDao.rewind(to: rewindHeight))
|
2019-10-18 11:45:19 -07:00
|
|
|
do {
|
2019-10-30 13:18:57 -07:00
|
|
|
let latestHeight = try compactBlockDao.latestHeight()
|
2019-10-18 11:45:19 -07:00
|
|
|
XCTAssertEqual(latestHeight, rewindHeight - 1)
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
XCTFail("Rewind latest block failed with error: \(error)")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|