diff --git a/build.zig b/build.zig index a0c4580..aa57813 100644 --- a/build.zig +++ b/build.zig @@ -24,16 +24,15 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); - // // Add ssz.zig as a dependency to the library - // const ssz_dep = b.dependency( - // "ssz.zig", - // .{ - // .target = target, - // .optimize = optimize, - // }, - // ); - // lib.addLibraryPath(ssz_dep.path("src")); - // lib.addIncludePath(ssz_dep.path("src")); + // Add ssz.zig as a dependency to the library + const ssz_dep = b.dependency( + "zabi", + .{ + .target = target, + .optimize = optimize, + }, + ); + lib.root_module.addImport("zabi", ssz_dep.module("zabi")); // This declares intent for the library to be installed into the standard // location when the user invokes the "install" step (the default step when @@ -47,6 +46,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); + exe.root_module.addImport("zabi", ssz_dep.module("zabi")); // This declares intent for the executable to be installed into the // standard location when the user invokes the "install" step (the default // step when running `zig build`). @@ -83,6 +83,8 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); + lib_unit_tests.root_module.addImport("zabi", ssz_dep.module("zabi")); + const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); const exe_unit_tests = b.addTest(.{ diff --git a/build.zig.zon b/build.zig.zon index 3b35062..fdda806 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -22,12 +22,10 @@ // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. // Once all dependencies are fetched, `zig build` no longer requires // internet connectivity. - .dependencies = .{ - .@"ssz.zig" = .{ - .url = "https://github.com/gballet/ssz.zig/archive/808c88aa6bd0f3d2e736774e4439afa24ab5377c.tar.gz", - .hash = "1220c73fadb1988c41aa54be104ce680812f833a6f6d53a5065f6e6db7ca73989d55", - }, - }, + .dependencies = .{ .zabi = .{ + .url = "https://github.com/Raiden1411/zabi/archive/79c77aa4a39d41ed50033fd922f4d37c0f0638bb.tar.gz", + .hash = "122095aaf2d09e3286bf8b00b773b65c1632639de6c5005de881754df7c04efc7e98", + } }, .paths = .{ "build.zig", "build.zig.zon", diff --git a/src/consensus/helpers/domain.zig b/src/consensus/helpers/domain.zig new file mode 100644 index 0000000..d850850 --- /dev/null +++ b/src/consensus/helpers/domain.zig @@ -0,0 +1,57 @@ +const zabi = @import("zabi"); +const std = @import("std"); +const primitives = @import("../../primitives/types.zig"); +const consensus = @import("../../consensus/types.zig"); + +/// computeForkDataRoot return the 32-byte fork data root for the ``current_version`` and ``genesis_validators_root``. +/// This is used primarily in signature domains to avoid collisions across forks/chains. +/// Spec pseudocode definition: +/// def compute_fork_data_root(current_version: Version, genesis_validators_root: Root) -> Root: +/// """ +/// Return the 32-byte fork data root for the ``current_version`` and ``genesis_validators_root``. +/// This is used primarily in signature domains to avoid collisions across forks/chains. +/// """ +/// return hash_tree_root(ForkData( +/// current_version=current_version, +/// genesis_validators_root=genesis_validators_root, +/// )) +pub fn computeForkDataRoot(current_version: primitives.Version, genesis_validators_root: primitives.Root) primitives.Root { + const fork_data = consensus.ForkData{ + .current_version = current_version, + .genesis_validators_root = genesis_validators_root, + }; + + std.debug.print("ForkData: {}\n", .{fork_data}); + // todo: implement hash_tree_root + return .{0} ** 32; +} + +/// computeForkDigest returns the 4-byte fork digest for the `currentVersion` and `genesisValidatorsRoot`. +/// This is a digest primarily used for domain separation on the p2p layer. +/// 4-bytes suffices for practical separation of forks/chains. +/// Spec pseudocode definition: +/// def compute_fork_digest(current_version: Version, genesis_validators_root: Root) -> ForkDigest: +/// """ +/// Return the 4-byte fork digest for the ``current_version`` and ``genesis_validators_root``. +/// This is a digest primarily used for domain separation on the p2p layer. +/// 4-bytes suffices for practical separation of forks/chains. +/// """ +/// return ForkDigest(compute_fork_data_root(current_version, genesis_validators_root)[:4]) +pub fn computeForkDigest(currentVersion: primitives.Version, genesisValidatorsRoot: primitives.Root) primitives.ForkDigest { + const forkDataRoot = computeForkDataRoot(currentVersion, genesisValidatorsRoot); + return forkDataRoot[0..4].*; +} + +test "test computeForkDigest" { + const currentVersion = .{0} ** 4; + const genesisValidatorsRoot = .{0} ** 32; + const forkDigest = computeForkDigest(currentVersion, genesisValidatorsRoot); + try std.testing.expectEqual(4, forkDigest.len); +} + +test "test computeForkDataRoot" { + const currentVersion = .{0} ** 4; + const genesisValidatorsRoot = .{0} ** 32; + const forkDataRoot = computeForkDataRoot(currentVersion, genesisValidatorsRoot); + try std.testing.expectEqual(32, forkDataRoot.len); +} diff --git a/src/consensus/types.zig b/src/consensus/types.zig index 8462929..9d3b0b5 100644 --- a/src/consensus/types.zig +++ b/src/consensus/types.zig @@ -458,15 +458,6 @@ pub const BeaconState = union(primitives.ForkType) { } }; -// pub fn compute_fork_data_root(current_version: primitives.Version, genesis_validators_root: primitives.Root) primitives.Root { -// const fork_data = ForkData{ -// .current_version = current_version, -// .genesis_validators_root = genesis_validators_root, -// }; -// -// return ssz.serialize_root(&fork_data); -// } - test "test Attestation" { const attestation = Attestation{ .phase0 = phase0.Attestation{ diff --git a/src/root.zig b/src/root.zig index 587ab48..b351774 100644 --- a/src/root.zig +++ b/src/root.zig @@ -14,6 +14,7 @@ pub const epoch_helper = @import("consensus/helpers/epoch.zig"); pub const attestation_helper = @import("consensus/helpers/attestation.zig"); pub const weak_subjectivity_helper = @import("consensus/helpers/weak_subjectivity.zig"); pub const validator_helper = @import("consensus/helpers/validator.zig"); +pub const domain_helper = @import("consensus/helpers/domain.zig"); test { @import("std").testing.refAllDeclsRecursive(@This());