-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add ssz spec test #11
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
|
||
.zig-cache | ||
zig-out | ||
.vscode | ||
.vscode | ||
consensus-spec-tests/ |
This file contains 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 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 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
Binary file not shown.
This file contains 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 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 |
---|---|---|
|
@@ -4,26 +4,138 @@ const ssz = @import("../../ssz/ssz.zig"); | |
const types = @import("../../consensus/types.zig"); | ||
const snappy = @import("../../snappy/snappy.zig"); | ||
|
||
test "hash tree root" { | ||
const fork = types.Fork{ | ||
.previous_version = [4]u8{ 0x75, 0xeb, 0x7f, 0x25 }, | ||
.current_version = [4]u8{ 0x10, 0xd4, 0xe2, 0x7f }, | ||
.epoch = 8876772290899440384, | ||
}; | ||
const Yaml = @import("../../yaml/yaml.zig").Yaml; | ||
|
||
const gpa = testing.allocator; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comment for functions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
/// Loads and parses a YAML file into a Yaml object | ||
/// Parameters: | ||
/// file_path: Path to the YAML file to load | ||
/// Returns: | ||
/// Parsed Yaml object or error | ||
fn loadFromFile(file_path: []const u8) !Yaml { | ||
const file = try std.fs.cwd().openFile(file_path, .{}); | ||
defer file.close(); | ||
|
||
const source = try file.readToEndAlloc(gpa, std.math.maxInt(u32)); | ||
defer gpa.free(source); | ||
|
||
return Yaml.load(gpa, source); | ||
} | ||
// load root.yml in spec test | ||
const Roots = struct { | ||
root: [32]u8, | ||
}; | ||
// test cases for all phases | ||
const CommonUnion = union { | ||
Fork: types.Fork, | ||
}; | ||
|
||
test "ssz static" { | ||
const testPath = "consensus-spec-tests/tests/mainnet"; | ||
const gpa1 = testing.allocator; | ||
const fields = @typeInfo(CommonUnion).@"union".fields; | ||
inline for (fields) |field| { | ||
const fieldType = field.type; | ||
const fieldName = field.name; | ||
const ssz_type_path = try std.fmt.allocPrint(gpa1, "{s}/phase0/ssz_static/{s}", .{ testPath, fieldName }); | ||
|
||
var dirs = try getLeafDirs(gpa1, ssz_type_path); | ||
|
||
// deinit the dirs array | ||
defer { | ||
for (dirs.items) |item| { | ||
gpa1.free(item); | ||
} | ||
dirs.deinit(); | ||
} | ||
|
||
for (dirs.items) |dir| { | ||
try testSSZStatic(dir, fieldType); | ||
} | ||
} | ||
} | ||
|
||
/// Recursively finds all leaf directories (directories with no subdirectories) starting from the given path | ||
/// Parameters: | ||
/// allocator: Memory allocator for dynamic allocations | ||
/// path: Starting directory path to search from | ||
/// Returns: | ||
/// ArrayList containing paths to all leaf directories | ||
fn getLeafDirs(allocator: std.mem.Allocator, path: []const u8) !std.ArrayList([]const u8) { | ||
var leafDirs = std.ArrayList([]const u8).init(allocator); | ||
// defer leafDirs.deinit(); | ||
var list = std.ArrayList([]const u8).init(allocator); | ||
defer { | ||
for (list.items) |item| { | ||
allocator.free(item); | ||
} | ||
list.deinit(); | ||
} | ||
try list.append(path); | ||
|
||
var index: u32 = 0; | ||
|
||
while (index < list.items.len) { | ||
var hasSubDir = false; | ||
const currentPath = list.items[index]; | ||
var dir = try std.fs.cwd().openDir(currentPath, .{ .iterate = true }); | ||
defer dir.close(); | ||
|
||
var iter = dir.iterate(); | ||
|
||
while (try iter.next()) |entry| { | ||
if (entry.kind == .directory) { | ||
hasSubDir = true; | ||
const fullPath = try std.fs.path.join(allocator, &[_][]const u8{ currentPath, entry.name }); | ||
try list.append(fullPath); | ||
} | ||
} | ||
if (!hasSubDir) { | ||
try leafDirs.append(try allocator.dupe(u8, currentPath)); | ||
} | ||
index += 1; | ||
} | ||
|
||
return leafDirs; | ||
} | ||
|
||
/// Tests SSZ (Simple Serialize) static functionality by performing: | ||
/// 1. YAML parsing | ||
/// 2. Hash tree root verification | ||
/// 3. SSZ encoding/decoding with snappy compression | ||
/// Parameters: | ||
/// path: Directory path containing test files | ||
/// t: Type to test SSZ operations against | ||
fn testSSZStatic(path: []const u8, t: type) !void { | ||
// parse from yaml | ||
const valueFile = try std.fmt.allocPrint(testing.allocator, "{s}/value.yaml", .{path}); | ||
defer testing.allocator.free(valueFile); | ||
var parsed = try loadFromFile(valueFile); | ||
defer parsed.deinit(); | ||
const fork = try parsed.parse(t); | ||
// test hash tree root | ||
var out: [32]u8 = [_]u8{0} ** 32; | ||
try ssz.hashTreeRoot(fork, &out, testing.allocator); | ||
const expect: [32]u8 = [_]u8{ 0x98, 0x2a, 0x69, 0x96, 0xc9, 0x2f, 0x86, 0xf6, 0x37, 0x68, 0x3c, 0x72, 0xd9, 0x09, 0xc7, 0xa8, 0x68, 0x11, 0x0e, 0x3b, 0x05, 0xf7, 0xb4, 0x48, 0x44, 0xbc, 0x53, 0x96, 0x0d, 0x89, 0x56, 0xf5 }; | ||
const rootFile = try std.fmt.allocPrint(testing.allocator, "{s}/roots.yaml", .{path}); | ||
defer testing.allocator.free(rootFile); | ||
var rootData = try loadFromFile(rootFile); | ||
defer rootData.deinit(); | ||
const root = try rootData.parse(Roots); | ||
const expect: [32]u8 = root.root; | ||
try std.testing.expect(std.mem.eql(u8, out[0..], expect[0..])); | ||
const file_path = "serialized.ssz_snappy"; | ||
// test ssz encode | ||
const file_path = try std.fmt.allocPrint(testing.allocator, "{s}/serialized.ssz_snappy", .{path}); | ||
defer testing.allocator.free(file_path); | ||
const file_contents = try std.fs.cwd().readFileAlloc(testing.allocator, file_path, std.math.maxInt(usize)); | ||
defer testing.allocator.free(file_contents); | ||
// std.debug.print("Hex: {any}\n", .{std.fmt.fmtSliceHexLower(file_contents)}); | ||
|
||
const decoded_data = try snappy.decode(testing.allocator, file_contents); | ||
defer testing.allocator.free(decoded_data); | ||
|
||
const encode = try ssz.encodeSSZ(testing.allocator, fork); | ||
defer testing.allocator.free(encode); | ||
|
||
try std.testing.expect(std.mem.eql(u8, encode, decoded_data)); | ||
|
||
// test ssz decode | ||
const decode = try ssz.decodeSSZ(t, decoded_data); | ||
try std.testing.expectEqualDeep(decode, fork); | ||
} |
This file contains 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 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
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.
Use a separate the task would be better such as if someone want to build it in local but don't want to do spec tests.
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.
done