-
Notifications
You must be signed in to change notification settings - Fork 0
fuzz tests debug fix #352
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
Merged
fuzz tests debug fix #352
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4c93213
add tests
qiweiii d5edd2f
run traces in fuzzer
qiweiii ca834a4
add tests
qiweiii 9cd75fd
move tests
qiweiii a577853
state root wrong
qiweiii eaea5eb
a temp fix for state root
qiweiii 5d9bca1
remove bad test
qiweiii 4a0d1f3
test fuzzer with target using traces
qiweiii 0185115
pas all tests
qiweiii dad9402
fix tests
qiweiii b68771b
comment
qiweiii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
Fuzzing/Sources/Fuzzing/FuzzGenerator/FuzzGeneratorTraces.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import Blockchain | ||
| import Foundation | ||
| import JAMTests | ||
| import TracingUtils | ||
| import Utils | ||
|
|
||
| private let logger = Logger(label: "FuzzGeneratorTraces") | ||
|
|
||
| /// A fuzz generator that loads test cases from JAM conformance fuzz traces | ||
| public class FuzzGeneratorTraces: FuzzGenerator { | ||
| private let testCases: [JamTestnetTestcase] | ||
|
|
||
| public init(tracesDir: String) throws { | ||
| logger.info("Loading test vectors from directory: \(tracesDir)") | ||
|
|
||
| testCases = try Self.loadTestCases(from: tracesDir) | ||
|
|
||
| logger.info("Loaded \(testCases.count) test cases") | ||
|
|
||
| if testCases.isEmpty { | ||
| throw FuzzGeneratorError.invalidTestData("No test cases found in directory: \(tracesDir)") | ||
| } | ||
| } | ||
|
|
||
| public func generatePreState( | ||
| timeslot: TimeslotIndex, | ||
| config _: ProtocolConfigRef | ||
| ) async throws -> (stateRoot: Data32, keyValues: [FuzzKeyValue]) { | ||
| let testIndex = Int(timeslot) | ||
| guard testIndex >= 0, testIndex < testCases.count else { | ||
| throw FuzzGeneratorError.stateGenerationFailed("No test case available for timeslot \(timeslot) (index \(testIndex))") | ||
| } | ||
|
|
||
| let testCase = testCases[testIndex] | ||
|
|
||
| logger.debug("Generating pre-state for timeslot \(timeslot) (test case \(testIndex + 1)/\(testCases.count))") | ||
|
|
||
| let keyValues = testCase.preState.keyvals.map { keyval in | ||
| FuzzKeyValue(key: keyval.key, value: keyval.value) | ||
| } | ||
|
|
||
| return (stateRoot: testCase.preState.root, keyValues: keyValues) | ||
| } | ||
|
|
||
| public func generatePostState( | ||
| timeslot: TimeslotIndex, | ||
| config _: ProtocolConfigRef | ||
| ) async throws -> (stateRoot: Data32, keyValues: [FuzzKeyValue]) { | ||
| let testIndex = Int(timeslot) | ||
| guard testIndex >= 0, testIndex < testCases.count else { | ||
| throw FuzzGeneratorError.stateGenerationFailed("No test case available for timeslot \(timeslot) (index \(testIndex))") | ||
| } | ||
|
|
||
| let testCase = testCases[testIndex] | ||
|
|
||
| logger.debug("Generating expected post-state for timeslot \(timeslot) (test case \(testIndex + 1)/\(testCases.count))") | ||
|
|
||
| let keyValues = testCase.postState.keyvals.map { keyval in | ||
| FuzzKeyValue(key: keyval.key, value: keyval.value) | ||
| } | ||
|
|
||
| return (stateRoot: testCase.postState.root, keyValues: keyValues) | ||
| } | ||
|
|
||
| public func generateBlock(timeslot: UInt32, currentStateRef _: StateRef, config _: ProtocolConfigRef) async throws -> BlockRef { | ||
| let testIndex = Int(timeslot) | ||
| guard testIndex >= 0, testIndex < testCases.count else { | ||
| throw FuzzGeneratorError.blockGenerationFailed("No test case available for timeslot \(timeslot) (index \(testIndex))") | ||
| } | ||
|
|
||
| let testCase = testCases[testIndex] | ||
|
|
||
| logger.debug("Generating block for timeslot \(timeslot) (test case \(testIndex + 1)/\(testCases.count))") | ||
|
|
||
| let block = testCase.block | ||
|
|
||
| return block.asRef() | ||
| } | ||
|
|
||
| private static func loadTestCases(from directory: String) throws -> [JamTestnetTestcase] { | ||
| logger.info("Loading test cases from directory: \(directory)") | ||
|
|
||
| let basePath = directory | ||
|
|
||
| guard FileManager.default.fileExists(atPath: basePath) else { | ||
| throw FuzzGeneratorError.invalidTestData("Directory does not exist: \(basePath)") | ||
| } | ||
|
|
||
| var allDecodedTestCases: [JamTestnetTestcase] = [] | ||
|
|
||
| // Find all .bin files with depth of 2 | ||
| func findBinFiles(in path: String, currentDepth: Int = 0) throws -> [String] { | ||
| guard currentDepth <= 2 else { return [] } | ||
|
|
||
| var binFiles: [String] = [] | ||
| let contents = try FileManager.default.contentsOfDirectory(atPath: path) | ||
|
|
||
| for item in contents { | ||
| guard !item.starts(with: ".") else { continue } | ||
|
|
||
| let itemPath = "\(path)/\(item)" | ||
| var isDirectory: ObjCBool = false | ||
|
|
||
| guard FileManager.default.fileExists(atPath: itemPath, isDirectory: &isDirectory) else { | ||
| continue | ||
| } | ||
|
|
||
| if isDirectory.boolValue, currentDepth < 2 { | ||
| // Recurse into subdirectory | ||
| binFiles += try findBinFiles(in: itemPath, currentDepth: currentDepth + 1) | ||
| } else if !isDirectory.boolValue, item.hasSuffix(".bin") { | ||
| // Found a .bin file | ||
| binFiles.append(itemPath) | ||
| } | ||
| } | ||
|
|
||
| return binFiles.sorted() | ||
| } | ||
|
|
||
| let testFiles = try findBinFiles(in: basePath) | ||
|
|
||
| for testFilePath in testFiles { | ||
| do { | ||
| let testData = try Data(contentsOf: URL(fileURLWithPath: testFilePath)) | ||
| let rawTestCase = Testcase(description: URL(fileURLWithPath: testFilePath).lastPathComponent, data: testData) | ||
|
|
||
| let decoded = try JamTestnet.decodeTestcase(rawTestCase) | ||
| allDecodedTestCases.append(decoded) | ||
| logger.debug("Successfully loaded test case: \(testFilePath)") | ||
| } catch { | ||
| logger.warning("Failed to decode test case \(testFilePath): \(error)") | ||
| } | ||
| } | ||
|
|
||
| logger.info("Successfully loaded \(allDecodedTestCases.count) test cases") | ||
| return allDecodedTestCases | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#353