From 6f01670602c5261fa654e5a3b20bf34b328c493f Mon Sep 17 00:00:00 2001 From: fearlessfe <505380967@qq.com> Date: Fri, 11 Oct 2024 23:48:48 +0800 Subject: [PATCH 1/2] fix: hashTreeRoot error --- build.zig | 9 +++++++++ src/ssz/ssz.zig | 2 +- src/tests.zig | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/tests.zig diff --git a/build.zig b/build.zig index 6a89e87..593c1a6 100644 --- a/build.zig +++ b/build.zig @@ -95,10 +95,19 @@ pub fn build(b: *std.Build) void { const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + const test_cases = b.addTest(.{ + .root_source_file = b.path("src/tests.zig"), + .target = target, + .optimize = optimize, + }); + + const run_test_cases = b.addRunArtifact(test_cases); + // Similar to creating the run step earlier, this exposes a `test` step to // the `zig build --help` menu, providing a way for the user to request // running the unit tests. const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_lib_unit_tests.step); test_step.dependOn(&run_exe_unit_tests.step); + test_step.dependOn(&run_test_cases.step); } diff --git a/src/ssz/ssz.zig b/src/ssz/ssz.zig index 282e4a2..315ec33 100644 --- a/src/ssz/ssz.zig +++ b/src/ssz/ssz.zig @@ -1001,7 +1001,7 @@ pub fn hashTreeRoot(value: anytype, out: *[32]u8, allocator: Allocator) !void { var chunks = ArrayList(chunk).init(allocator); defer chunks.deinit(); var tmp: chunk = undefined; - inline for (type_info.Struct.fields) |f| { + inline for (type_info.@"struct".fields) |f| { try hashTreeRoot(@field(value, f.name), &tmp, allocator); try chunks.append(tmp); } diff --git a/src/tests.zig b/src/tests.zig new file mode 100644 index 0000000..28254b3 --- /dev/null +++ b/src/tests.zig @@ -0,0 +1,16 @@ +const std = @import("std"); +const testing = std.testing; +const ssz = @import("./ssz/ssz.zig"); +const types = @import("./consensus/types.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, + }; + 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 }; + try std.testing.expect(std.mem.eql(u8, out[0..], expect[0..])); +} From 2a6f186657a8b3dd3f5807a3144e2f8ec9bff0b4 Mon Sep 17 00:00:00 2001 From: PengZhen Date: Sat, 12 Oct 2024 15:52:48 +0800 Subject: [PATCH 2/2] feat: use std.log.debug replace std.debug --- build.zig | 9 --------- src/consensus/helpers/balance.zig | 4 ++-- src/consensus/helpers/committee.zig | 4 ++-- src/consensus/helpers/domain.zig | 2 +- src/consensus/helpers/signing_root.zig | 4 ++-- src/consensus/helpers/validator.zig | 2 +- src/root.zig | 1 + src/spec_tests/root.zig | 3 +++ src/{tests.zig => spec_tests/ssz_static/root.zig} | 4 ++-- 9 files changed, 14 insertions(+), 19 deletions(-) create mode 100644 src/spec_tests/root.zig rename src/{tests.zig => spec_tests/ssz_static/root.zig} (87%) diff --git a/build.zig b/build.zig index 593c1a6..6a89e87 100644 --- a/build.zig +++ b/build.zig @@ -95,19 +95,10 @@ pub fn build(b: *std.Build) void { const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); - const test_cases = b.addTest(.{ - .root_source_file = b.path("src/tests.zig"), - .target = target, - .optimize = optimize, - }); - - const run_test_cases = b.addRunArtifact(test_cases); - // Similar to creating the run step earlier, this exposes a `test` step to // the `zig build --help` menu, providing a way for the user to request // running the unit tests. const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_lib_unit_tests.step); test_step.dependOn(&run_exe_unit_tests.step); - test_step.dependOn(&run_test_cases.step); } diff --git a/src/consensus/helpers/balance.zig b/src/consensus/helpers/balance.zig index acdb5ff..8c9fd0b 100644 --- a/src/consensus/helpers/balance.zig +++ b/src/consensus/helpers/balance.zig @@ -25,8 +25,8 @@ pub fn getTotalBalance(state: *const consensus.BeaconState, indices: std.AutoHas var total: primitives.Gwei = 0; var iterator = indices.keyIterator(); while (iterator.next()) |index| { - std.debug.print("index: {}\n", .{index}); - std.debug.print("state.validators()[index.*].effective_balance: {}\n", .{state.validators()[index.*].effective_balance}); + std.log.debug("index: {}\n", .{index}); + std.log.debug("state.validators()[index.*].effective_balance: {}\n", .{state.validators()[index.*].effective_balance}); total += state.validators()[index.*].effective_balance; } return @max(preset.ActivePreset.get().EFFECTIVE_BALANCE_INCREMENT, total); diff --git a/src/consensus/helpers/committee.zig b/src/consensus/helpers/committee.zig index 495247a..65e20e7 100644 --- a/src/consensus/helpers/committee.zig +++ b/src/consensus/helpers/committee.zig @@ -28,7 +28,7 @@ pub fn getCommitteeCountPerSlot(state: *const consensus.BeaconState, epoch: prim defer allocator.free(active_validator_indices); const active_validator_count = active_validator_indices.len; - std.debug.print("active_validator_count: {}\n", .{active_validator_count}); + std.log.debug("active_validator_count: {}\n", .{active_validator_count}); const slots_per_epoch = preset.ActivePreset.get().SLOTS_PER_EPOCH; const target_committee_size = preset.ActivePreset.get().TARGET_COMMITTEE_SIZE; @@ -119,7 +119,7 @@ pub fn getBeaconCommittee(state: *const consensus.BeaconState, slot: primitives. pub fn getBeaconProposerIndex(state: *const consensus.BeaconState, allocator: std.mem.Allocator) !primitives.ValidatorIndex { const epoch = epoch_helper.getCurrentEpoch(state); const seed_origin = seed_helper.getSeed(state, epoch, constants.DOMAIN_BEACON_PROPOSER) ++ std.mem.asBytes(&state.slot()); - std.debug.print("seed_origin: {any}\n", .{seed_origin}); + std.log.debug("seed_origin: {any}\n", .{seed_origin}); var seed: primitives.Bytes32 = undefined; sha256.hash(seed_origin, &seed, .{}); const indices = try validator_helper.getActiveValidatorIndices(state, epoch, allocator); diff --git a/src/consensus/helpers/domain.zig b/src/consensus/helpers/domain.zig index b1ec2ec..9bfda97 100644 --- a/src/consensus/helpers/domain.zig +++ b/src/consensus/helpers/domain.zig @@ -21,7 +21,7 @@ pub fn computeForkDataRoot(current_version: primitives.Version, genesis_validato .genesis_validators_root = genesis_validators_root, }; - std.debug.print("ForkData: {}\n", .{fork_data}); + std.log.debug("ForkData: {}\n", .{fork_data}); // todo: implement hash_tree_root return @as(primitives.Root, .{0} ** 32); } diff --git a/src/consensus/helpers/signing_root.zig b/src/consensus/helpers/signing_root.zig index 327707d..fd14856 100644 --- a/src/consensus/helpers/signing_root.zig +++ b/src/consensus/helpers/signing_root.zig @@ -15,14 +15,14 @@ const configs = @import("../../configs/config.zig"); /// )) pub fn computeSigningRoot(comptime sszObject: type, domain: primitives.Domain) primitives.Root { // const objectRoot = hashTreeRoot(sszObject); - std.debug.print("sszObject: {}\n", .{sszObject}); + std.log.debug("sszObject: {}\n", .{sszObject}); const objectRoot = @as(primitives.Root, .{0} ** 32); const signingData = consensus.SigningData{ .object_root = objectRoot, .domain = domain, }; - std.debug.print("signingData: {}\n", .{signingData}); + std.log.debug("signingData: {}\n", .{signingData}); // return hashTreeRoot(signingData); return @as(primitives.Root, .{0} ** 32); } diff --git a/src/consensus/helpers/validator.zig b/src/consensus/helpers/validator.zig index 8d99899..73630c8 100644 --- a/src/consensus/helpers/validator.zig +++ b/src/consensus/helpers/validator.zig @@ -158,7 +158,7 @@ pub fn computeProposerIndex(state: *const consensus.BeaconState, indices: []cons var seed_plus: [40]u8 = undefined; @memcpy(seed_plus[0..32], &seed); std.mem.writeInt(u64, seed_plus[32..40], @divFloor(i, 32), .little); - std.debug.print("seed_plus: {any}, i: {}\n", .{ seed_plus, i }); + std.log.debug("seed_plus: {any}, i: {}\n", .{ seed_plus, i }); std.crypto.hash.sha2.Sha256.hash(&seed_plus, &hash_result, .{}); const randomByte = hash_result[@mod(i, 32)]; const effectiveBalance = state.validators()[candidate_index].effective_balance; diff --git a/src/root.zig b/src/root.zig index 7b29ef7..f8f396d 100644 --- a/src/root.zig +++ b/src/root.zig @@ -25,4 +25,5 @@ pub const ssz = @import("./ssz/ssz.zig"); test { @import("std").testing.refAllDeclsRecursive(@This()); + _ = @import("./spec_tests/root.zig"); } diff --git a/src/spec_tests/root.zig b/src/spec_tests/root.zig new file mode 100644 index 0000000..a6adb30 --- /dev/null +++ b/src/spec_tests/root.zig @@ -0,0 +1,3 @@ +test { + _ = @import("./ssz_static/root.zig"); +} diff --git a/src/tests.zig b/src/spec_tests/ssz_static/root.zig similarity index 87% rename from src/tests.zig rename to src/spec_tests/ssz_static/root.zig index 28254b3..a993895 100644 --- a/src/tests.zig +++ b/src/spec_tests/ssz_static/root.zig @@ -1,7 +1,7 @@ const std = @import("std"); const testing = std.testing; -const ssz = @import("./ssz/ssz.zig"); -const types = @import("./consensus/types.zig"); +const ssz = @import("../../ssz/ssz.zig"); +const types = @import("../../consensus/types.zig"); test "hash tree root" { const fork = types.Fork{