Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ jobs:
if: matrix.os == 'ubuntu-latest'
run: zig fmt --check .

- name: Run hex tests
run: |
zig build test:hex
- name: Run config tests
run: |
zig build test:config
Expand Down
23 changes: 0 additions & 23 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ pub fn build(b: *std.Build) void {
.target = target,
});

const module_hex = b.createModule(.{
.root_source_file = b.path("src/hex.zig"),
.target = target,
.optimize = optimize,
});
b.modules.put(b.dupe("hex"), module_hex) catch @panic("OOM");

const module_constants = b.createModule(.{
.root_source_file = b.path("src/constants/root.zig"),
.target = target,
Expand Down Expand Up @@ -125,20 +118,6 @@ pub fn build(b: *std.Build) void {

const tls_run_test = b.step("test", "Run all tests");

const test_hex = b.addTest(.{
.name = "hex",
.root_module = module_hex,
.filters = b.option([][]const u8, "hex.filters", "hex test filters") orelse &[_][]const u8{},
});
const install_test_hex = b.addInstallArtifact(test_hex, .{});
const tls_install_test_hex = b.step("build-test:hex", "Install the hex test");
tls_install_test_hex.dependOn(&install_test_hex.step);

const run_test_hex = b.addRunArtifact(test_hex);
const tls_run_test_hex = b.step("test:hex", "Run the hex test");
tls_run_test_hex.dependOn(&run_test_hex.step);
tls_run_test.dependOn(&run_test_hex.step);

const test_constants = b.addTest(.{
.name = "constants",
.root_module = module_constants,
Expand Down Expand Up @@ -282,7 +261,6 @@ pub fn build(b: *std.Build) void {
module_config.addImport("build_options", options_module_build_options);
module_config.addImport("preset", module_preset);
module_config.addImport("consensus_types", module_consensus_types);
module_config.addImport("hex", module_hex);
module_config.addImport("constants", module_constants);

module_consensus_types.addImport("build_options", options_module_build_options);
Expand All @@ -300,7 +278,6 @@ pub fn build(b: *std.Build) void {
module_state_transition.addImport("blst", dep_blst.module("blst"));
module_state_transition.addImport("preset", module_preset);
module_state_transition.addImport("constants", module_constants);
module_state_transition.addImport("hex", module_hex);

module_download_spec_tests.addImport("spec_test_options", options_module_spec_test_options);

Expand Down
3 changes: 1 addition & 2 deletions src/config/chain/networks/chiado.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const std = @import("std");
const hex_utils = @import("hex");
const Preset = @import("preset").Preset;
const ChainConfig = @import("../chain_config.zig").ChainConfig;
const BlobScheduleEntry = @import("../chain_config.zig").BlobScheduleEntry;
const b = hex_utils.hexToBytesComptime;
const b = @import("../../root.zig").hexToBytesComptime;

const gnosis = @import("./gnosis.zig").gnosis_chain_config;

Expand Down
3 changes: 1 addition & 2 deletions src/config/chain/networks/gnosis.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const std = @import("std");
const hex_utils = @import("hex");
const Preset = @import("preset").Preset;
const ChainConfig = @import("../chain_config.zig").ChainConfig;
const BlobScheduleEntry = @import("../chain_config.zig").BlobScheduleEntry;
const b = hex_utils.hexToBytesComptime;
const b = @import("../../root.zig").hexToBytesComptime;

pub const gnosis_chain_config = ChainConfig{
.PRESET_BASE = Preset.gnosis,
Expand Down
3 changes: 1 addition & 2 deletions src/config/chain/networks/hoodi.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const std = @import("std");
const hex_utils = @import("hex");
const preset = @import("preset").preset;
const ChainConfig = @import("../chain_config.zig").ChainConfig;
const BlobScheduleEntry = @import("../chain_config.zig").BlobScheduleEntry;
const b = hex_utils.hexToBytesComptime;
const b = @import("../../root.zig").hexToBytesComptime;

const mainnet = @import("./mainnet.zig").mainnet_chain_config;

Expand Down
3 changes: 1 addition & 2 deletions src/config/chain/networks/mainnet.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const std = @import("std");
const hex_utils = @import("hex");
const Preset = @import("preset").Preset;
const ChainConfig = @import("../chain_config.zig").ChainConfig;
const BlobScheduleEntry = @import("../chain_config.zig").BlobScheduleEntry;
const b = hex_utils.hexToBytesComptime;
const b = @import("../../root.zig").hexToBytesComptime;

pub const mainnet_chain_config = ChainConfig{
.PRESET_BASE = Preset.mainnet,
Expand Down
3 changes: 1 addition & 2 deletions src/config/chain/networks/minimal.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const std = @import("std");
const hex_utils = @import("hex");
const Preset = @import("preset").Preset;
const ChainConfig = @import("../chain_config.zig").ChainConfig;
const BlobScheduleEntry = @import("../chain_config.zig").BlobScheduleEntry;
const b = hex_utils.hexToBytesComptime;
const b = @import("../../root.zig").hexToBytesComptime;

pub const minimal_chain_config = ChainConfig{
.PRESET_BASE = Preset.minimal,
Expand Down
3 changes: 1 addition & 2 deletions src/config/chain/networks/sepolia.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const std = @import("std");
const hex_utils = @import("hex");
const preset = @import("preset").preset;
const ChainConfig = @import("../chain_config.zig").ChainConfig;
const BlobScheduleEntry = @import("../chain_config.zig").BlobScheduleEntry;
const b = hex_utils.hexToBytesComptime;
const b = @import("../../root.zig").hexToBytesComptime;

const mainnet = @import("./mainnet.zig").mainnet_chain_config;

Expand Down
12 changes: 12 additions & 0 deletions src/config/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ pub const chiado_chain_config = @import("./chain/networks/chiado.zig").chiado_ch
pub const sepolia_chain_config = @import("./chain/networks/sepolia.zig").sepolia_chain_config;
pub const hoodi_chain_config = @import("./chain/networks/hoodi.zig").hoodi_chain_config;

pub fn hexToBytesComptime(comptime n: usize, comptime input: []const u8) [n]u8 {
var out: [n]u8 = undefined;
const input_slice = if (std.mem.startsWith(u8, input, "0x"))
input[2..]
else
input;

_ = std.fmt.hexToBytes(&out, input_slice) catch
@compileError(std.fmt.comptimePrint("Failed to convert hex {s} to bytes at comptime", .{input}));
return out;
}

test {
testing.refAllDecls(@This());
}
91 changes: 0 additions & 91 deletions src/hex.zig

This file was deleted.

4 changes: 2 additions & 2 deletions src/state_transition/test_utils/generate_block.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const ssz = @import("consensus_types");
const s = @import("ssz");
const hex = @import("hex");
const hexToRoot = @import("root.zig").hexToRoot;
const Slot = ssz.primitive.Slot.Type;
const preset = @import("preset").preset;
const state_transition = @import("../root.zig");
Expand Down Expand Up @@ -67,7 +67,7 @@ pub fn generateElectraBlock(allocator: Allocator, cached_state: *const CachedBea
.slot = state.slot() + 1,
// value is generated after running real state transition int test
.proposer_index = 41,
.parent_root = try hex.hexToRoot("0x0833505580088dab43dab615abbdaa7c914a5f4ebeca79332a9373d5b25daeac"),
.parent_root = try hexToRoot("0x0833505580088dab43dab615abbdaa7c914a5f4ebeca79332a9373d5b25daeac"),
// this could be computed later
.state_root = [_]u8{0} ** 32,
.body = .{
Expand Down
23 changes: 12 additions & 11 deletions src/state_transition/test_utils/generate_state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const Allocator = std.mem.Allocator;
const mainnet_chain_config = @import("config").mainnet_chain_config;
const minimal_chain_config = @import("config").minimal_chain_config;
const ssz = @import("consensus_types");
const hex = @import("hex");
const hexToRoot = @import("root.zig").hexToRoot;
const hexToBytes = @import("root.zig").hexToBytes;
const ElectraBeaconState = ssz.electra.BeaconState.Type;
const BLSPubkey = ssz.primitive.BLSPubkey.Type;
const ValidatorIndex = ssz.primitive.ValidatorIndex.Type;
Expand Down Expand Up @@ -32,12 +33,12 @@ pub fn generateElectraState(allocator: Allocator, chain_config: ChainConfig, val
errdefer allocator.destroy(electra_state);
electra_state.* = ssz.electra.BeaconState.default_value;
electra_state.genesis_time = 1596546008;
electra_state.genesis_validators_root = try hex.hexToRoot("0x8a8b3f1f1e2d3c4b5a697887766554433221100ffeeddccbbaa9988776655443");
electra_state.genesis_validators_root = try hexToRoot("0x8a8b3f1f1e2d3c4b5a697887766554433221100ffeeddccbbaa9988776655443");
// set the slot to be ready for the next epoch transition
electra_state.slot = chain_config.ELECTRA_FORK_EPOCH * preset.SLOTS_PER_EPOCH + 2025 * preset.SLOTS_PER_EPOCH - 1;
const current_epoch = @divFloor(electra_state.slot, preset.SLOTS_PER_EPOCH);
var version: [4]u8 = undefined;
_ = try hex.hexToBytes(&version, "0x00000001");
_ = try hexToBytes(&version, "0x00000001");
electra_state.fork = .{
.previous_version = version,
.current_version = version,
Expand All @@ -46,9 +47,9 @@ pub fn generateElectraState(allocator: Allocator, chain_config: ChainConfig, val
electra_state.latest_block_header = .{
.slot = electra_state.slot - 1,
.proposer_index = 80882,
.parent_root = try hex.hexToRoot("0x5b83c3078e474b86af60043eda82a34c3c2e5ebf83146b14d9d909aea4163ef2"),
.state_root = try hex.hexToRoot("0x2761ae355e8a53c11e0e37d5e417f8984db0c53fa83f1bc65f89c6af35a196a7"),
.body_root = try hex.hexToRoot("0x249a1962eef90e122fa2447040bfac102798b1dba9c73e5593bc5aa32eb92bfd"),
.parent_root = try hexToRoot("0x5b83c3078e474b86af60043eda82a34c3c2e5ebf83146b14d9d909aea4163ef2"),
.state_root = try hexToRoot("0x2761ae355e8a53c11e0e37d5e417f8984db0c53fa83f1bc65f89c6af35a196a7"),
.body_root = try hexToRoot("0x249a1962eef90e122fa2447040bfac102798b1dba9c73e5593bc5aa32eb92bfd"),
};
electra_state.block_roots = [_][32]u8{[_]u8{1} ** 32} ** preset.SLOTS_PER_HISTORICAL_ROOT;
electra_state.state_roots = [_][32]u8{[_]u8{2} ** 32} ** preset.SLOTS_PER_HISTORICAL_ROOT;
Expand Down Expand Up @@ -76,9 +77,9 @@ pub fn generateElectraState(allocator: Allocator, chain_config: ChainConfig, val
}

electra_state.eth1_data = .{
.deposit_root = try hex.hexToRoot("0xcb1f89a924cfd31224823db5a41b1643f10faa7aedf231f1e28887f6ee98c047"),
.deposit_root = try hexToRoot("0xcb1f89a924cfd31224823db5a41b1643f10faa7aedf231f1e28887f6ee98c047"),
.deposit_count = pubkeys.len,
.block_hash = try hex.hexToRoot("0x701fb2869ce16d0f1d14f6705725adb0dec6799da29006dfc6fff83960298f21"),
.block_hash = try hexToRoot("0x701fb2869ce16d0f1d14f6705725adb0dec6799da29006dfc6fff83960298f21"),
};

// populate sync committee
Expand All @@ -103,15 +104,15 @@ pub fn generateElectraState(allocator: Allocator, chain_config: ChainConfig, val
}
electra_state.previous_justified_checkpoint = .{
.epoch = current_epoch - 2,
.root = try hex.hexToRoot("0x3fe60bf06a57b0956cd1f8181d26649cf8bf79e48bf82f55562e04b33d4785d4"),
.root = try hexToRoot("0x3fe60bf06a57b0956cd1f8181d26649cf8bf79e48bf82f55562e04b33d4785d4"),
};
electra_state.current_justified_checkpoint = .{
.epoch = current_epoch - 1,
.root = try hex.hexToRoot("0x3ba0913d2fb5e4cbcfb0d39eb15803157c1e769d63b8619285d8fdabbd8181c7"),
.root = try hexToRoot("0x3ba0913d2fb5e4cbcfb0d39eb15803157c1e769d63b8619285d8fdabbd8181c7"),
};
electra_state.finalized_checkpoint = .{
.epoch = current_epoch - 3,
.root = try hex.hexToRoot("0x122b8ff579d0c8f8a8b66326bdfec3f685007d2842f01615a0768870961ccc17"),
.root = try hexToRoot("0x122b8ff579d0c8f8a8b66326bdfec3f685007d2842f01615a0768870961ccc17"),
};

// the same logic to processSyncCommitteeUpdates
Expand Down
16 changes: 16 additions & 0 deletions src/state_transition/test_utils/root.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
const std = @import("std");
const fmt = std.fmt;
const testing = std.testing;

/// Utils that could be used for different kinds of tests like int, perf
pub const TestCachedBeaconStateAllForks = @import("./generate_state.zig").TestCachedBeaconStateAllForks;
pub const generateElectraBlock = @import("./generate_block.zig").generateElectraBlock;

/// Convert hex to bytes with 0x-prefix support
pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 {
if (std.mem.startsWith(u8, input, "0x")) {
return try fmt.hexToBytes(out, input[2..]);
}

return try fmt.hexToBytes(out, input);
}

pub fn hexToRoot(input: *const [66]u8) ![32]u8 {
var out: [32]u8 = undefined;
_ = try hexToBytes(&out, input);
return out;
}

test {
testing.refAllDecls(@This());
}
6 changes: 1 addition & 5 deletions zbuild.zon
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@
},
},
.modules = .{
.hex = .{
.root_source_file = "src/hex.zig",
},
.constants = .{
.root_source_file = "src/constants/root.zig",
},
.config = .{
.root_source_file = "src/config/root.zig",
.imports = .{ .build_options, .preset, .consensus_types, .hex, .constants },
.imports = .{ .build_options, .preset, .consensus_types, .constants },
},
.consensus_types = .{
.root_source_file = "src/consensus_types/root.zig",
Expand All @@ -70,7 +67,6 @@
.blst,
.preset,
.constants,
.hex,
},
},
},
Expand Down