Skip to content

Commit

Permalink
Merge pull request #6 from optimism-java/ssz-test
Browse files Browse the repository at this point in the history
ssz test
  • Loading branch information
GrapeBaBa authored Oct 12, 2024
2 parents f64c972 + 2a6f186 commit e2fa6ca
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/consensus/helpers/balance.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/consensus/helpers/committee.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/helpers/domain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/consensus/helpers/signing_root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/helpers/validator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ pub const ssz = @import("./ssz/ssz.zig");

test {
@import("std").testing.refAllDeclsRecursive(@This());
_ = @import("./spec_tests/root.zig");
}
3 changes: 3 additions & 0 deletions src/spec_tests/root.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test {
_ = @import("./ssz_static/root.zig");
}
16 changes: 16 additions & 0 deletions src/spec_tests/ssz_static/root.zig
Original file line number Diff line number Diff line change
@@ -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..]));
}
2 changes: 1 addition & 1 deletion src/ssz/ssz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit e2fa6ca

Please sign in to comment.