|
| 1 | +import Blockchain |
| 2 | +import Foundation |
| 3 | +import JAMTests |
| 4 | +import TracingUtils |
| 5 | +import Utils |
| 6 | + |
| 7 | +private let logger = Logger(label: "FuzzGeneratorTraces") |
| 8 | + |
| 9 | +/// A fuzz generator that loads test cases from JAM conformance fuzz traces |
| 10 | +public class FuzzGeneratorTraces: FuzzGenerator { |
| 11 | + private let testCases: [JamTestnetTestcase] |
| 12 | + |
| 13 | + public init(tracesDir: String) throws { |
| 14 | + logger.info("Loading test vectors from directory: \(tracesDir)") |
| 15 | + |
| 16 | + testCases = try Self.loadTestCases(from: tracesDir) |
| 17 | + |
| 18 | + logger.info("Loaded \(testCases.count) test cases") |
| 19 | + |
| 20 | + if testCases.isEmpty { |
| 21 | + throw FuzzGeneratorError.invalidTestData("No test cases found in directory: \(tracesDir)") |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public func generatePreState( |
| 26 | + timeslot: TimeslotIndex, |
| 27 | + config _: ProtocolConfigRef |
| 28 | + ) async throws -> (stateRoot: Data32, keyValues: [FuzzKeyValue]) { |
| 29 | + let testIndex = Int(timeslot) |
| 30 | + guard testIndex >= 0, testIndex < testCases.count else { |
| 31 | + throw FuzzGeneratorError.stateGenerationFailed("No test case available for timeslot \(timeslot) (index \(testIndex))") |
| 32 | + } |
| 33 | + |
| 34 | + let testCase = testCases[testIndex] |
| 35 | + |
| 36 | + logger.debug("Generating pre-state for timeslot \(timeslot) (test case \(testIndex + 1)/\(testCases.count))") |
| 37 | + |
| 38 | + let keyValues = testCase.preState.keyvals.map { keyval in |
| 39 | + FuzzKeyValue(key: keyval.key, value: keyval.value) |
| 40 | + } |
| 41 | + |
| 42 | + return (stateRoot: testCase.preState.root, keyValues: keyValues) |
| 43 | + } |
| 44 | + |
| 45 | + public func generatePostState( |
| 46 | + timeslot: TimeslotIndex, |
| 47 | + config _: ProtocolConfigRef |
| 48 | + ) async throws -> (stateRoot: Data32, keyValues: [FuzzKeyValue]) { |
| 49 | + let testIndex = Int(timeslot) |
| 50 | + guard testIndex >= 0, testIndex < testCases.count else { |
| 51 | + throw FuzzGeneratorError.stateGenerationFailed("No test case available for timeslot \(timeslot) (index \(testIndex))") |
| 52 | + } |
| 53 | + |
| 54 | + let testCase = testCases[testIndex] |
| 55 | + |
| 56 | + logger.debug("Generating expected post-state for timeslot \(timeslot) (test case \(testIndex + 1)/\(testCases.count))") |
| 57 | + |
| 58 | + let keyValues = testCase.postState.keyvals.map { keyval in |
| 59 | + FuzzKeyValue(key: keyval.key, value: keyval.value) |
| 60 | + } |
| 61 | + |
| 62 | + return (stateRoot: testCase.postState.root, keyValues: keyValues) |
| 63 | + } |
| 64 | + |
| 65 | + public func generateBlock(timeslot: UInt32, currentStateRef _: StateRef, config _: ProtocolConfigRef) async throws -> BlockRef { |
| 66 | + let testIndex = Int(timeslot) |
| 67 | + guard testIndex >= 0, testIndex < testCases.count else { |
| 68 | + throw FuzzGeneratorError.blockGenerationFailed("No test case available for timeslot \(timeslot) (index \(testIndex))") |
| 69 | + } |
| 70 | + |
| 71 | + let testCase = testCases[testIndex] |
| 72 | + |
| 73 | + logger.debug("Generating block for timeslot \(timeslot) (test case \(testIndex + 1)/\(testCases.count))") |
| 74 | + |
| 75 | + let block = testCase.block |
| 76 | + |
| 77 | + return block.asRef() |
| 78 | + } |
| 79 | + |
| 80 | + private static func loadTestCases(from directory: String) throws -> [JamTestnetTestcase] { |
| 81 | + logger.info("Loading test cases from directory: \(directory)") |
| 82 | + |
| 83 | + let basePath = directory |
| 84 | + |
| 85 | + guard FileManager.default.fileExists(atPath: basePath) else { |
| 86 | + throw FuzzGeneratorError.invalidTestData("Directory does not exist: \(basePath)") |
| 87 | + } |
| 88 | + |
| 89 | + var allDecodedTestCases: [JamTestnetTestcase] = [] |
| 90 | + |
| 91 | + // Find all .bin files with depth of 2 |
| 92 | + func findBinFiles(in path: String, currentDepth: Int = 0) throws -> [String] { |
| 93 | + guard currentDepth <= 2 else { return [] } |
| 94 | + |
| 95 | + var binFiles: [String] = [] |
| 96 | + let contents = try FileManager.default.contentsOfDirectory(atPath: path) |
| 97 | + |
| 98 | + for item in contents { |
| 99 | + guard !item.starts(with: ".") else { continue } |
| 100 | + |
| 101 | + let itemPath = "\(path)/\(item)" |
| 102 | + var isDirectory: ObjCBool = false |
| 103 | + |
| 104 | + guard FileManager.default.fileExists(atPath: itemPath, isDirectory: &isDirectory) else { |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + if isDirectory.boolValue, currentDepth < 2 { |
| 109 | + // Recurse into subdirectory |
| 110 | + binFiles += try findBinFiles(in: itemPath, currentDepth: currentDepth + 1) |
| 111 | + } else if !isDirectory.boolValue, item.hasSuffix(".bin") { |
| 112 | + // Found a .bin file |
| 113 | + binFiles.append(itemPath) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return binFiles.sorted() |
| 118 | + } |
| 119 | + |
| 120 | + let testFiles = try findBinFiles(in: basePath) |
| 121 | + |
| 122 | + for testFilePath in testFiles { |
| 123 | + do { |
| 124 | + let testData = try Data(contentsOf: URL(fileURLWithPath: testFilePath)) |
| 125 | + let rawTestCase = Testcase(description: URL(fileURLWithPath: testFilePath).lastPathComponent, data: testData) |
| 126 | + |
| 127 | + let decoded = try JamTestnet.decodeTestcase(rawTestCase) |
| 128 | + allDecodedTestCases.append(decoded) |
| 129 | + logger.debug("Successfully loaded test case: \(testFilePath)") |
| 130 | + } catch { |
| 131 | + logger.warning("Failed to decode test case \(testFilePath): \(error)") |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + logger.info("Successfully loaded \(allDecodedTestCases.count) test cases") |
| 136 | + return allDecodedTestCases |
| 137 | + } |
| 138 | +} |
0 commit comments