Skip to content

Commit

Permalink
feat:add domain helper
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Oct 7, 2024
1 parent 640e4b4 commit eb55108
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 25 deletions.
22 changes: 12 additions & 10 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`).
Expand Down Expand Up @@ -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(.{
Expand Down
10 changes: 4 additions & 6 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 57 additions & 0 deletions src/consensus/helpers/domain.zig
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 0 additions & 9 deletions src/consensus/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit eb55108

Please sign in to comment.