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/spec_tests/ssz_static/root.zig b/src/spec_tests/ssz_static/root.zig new file mode 100644 index 0000000..a993895 --- /dev/null +++ b/src/spec_tests/ssz_static/root.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..])); +} 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); }