Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
480 changes: 301 additions & 179 deletions bench/state_transition/process_block.zig

Large diffs are not rendered by default.

869 changes: 505 additions & 364 deletions bench/state_transition/process_epoch.zig

Large diffs are not rendered by default.

29 changes: 9 additions & 20 deletions bench/state_transition/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const std = @import("std");
const Node = @import("persistent_merkle_tree").Node;
const types = @import("consensus_types");
const config = @import("config");
const state_transition = @import("state_transition");
const fork_types = @import("fork_types");

const ForkSeq = config.ForkSeq;
const BeaconState = state_transition.BeaconState;
const AnyBeaconState = fork_types.AnyBeaconState;
const AnySignedBeaconBlock = fork_types.AnySignedBeaconBlock;
const Slot = types.primitive.Slot.Type;

/// Read slot from raw BeaconState SSZ bytes (offset 40)
Expand All @@ -23,26 +24,14 @@ pub fn slotFromBlockBytes(block_bytes: []const u8) Slot {
}

/// Load and deserialize BeaconState from SSZ bytes for a specific fork
pub fn loadState(comptime fork: ForkSeq, allocator: std.mem.Allocator, pool: *Node.Pool, state_bytes: []const u8) !*BeaconState {
const BeaconStateST = @field(types, @tagName(fork)).BeaconState;
var state_data = try BeaconStateST.TreeView.init(
allocator,
pool,
try BeaconStateST.tree.deserializeFromBytes(pool, state_bytes),
);
errdefer state_data.deinit();

const beacon_state = try allocator.create(BeaconState);
beacon_state.* = @unionInit(BeaconState, @tagName(fork), state_data);
pub fn loadState(comptime fork: ForkSeq, allocator: std.mem.Allocator, pool: *Node.Pool, state_bytes: []const u8) !*AnyBeaconState {
const beacon_state = try allocator.create(AnyBeaconState);
errdefer allocator.destroy(beacon_state);
beacon_state.* = try AnyBeaconState.deserialize(allocator, pool, fork, state_bytes);
return beacon_state;
}

/// Load and deserialize SignedBeaconBlock from SSZ bytes for a specific fork
pub fn loadBlock(comptime fork: ForkSeq, allocator: std.mem.Allocator, block_bytes: []const u8) !state_transition.SignedBeaconBlock {
const SignedBeaconBlock = @field(types, @tagName(fork)).SignedBeaconBlock;
const block_data = try allocator.create(SignedBeaconBlock.Type);
errdefer allocator.destroy(block_data);
block_data.* = SignedBeaconBlock.default_value;
try SignedBeaconBlock.deserializeFromBytes(allocator, block_bytes, block_data);
return @unionInit(state_transition.SignedBeaconBlock, @tagName(fork), block_data);
pub fn loadBlock(comptime fork: ForkSeq, allocator: std.mem.Allocator, block_bytes: []const u8) !AnySignedBeaconBlock {
return try AnySignedBeaconBlock.deserialize(allocator, .full, fork, block_bytes);
}
32 changes: 32 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ pub fn build(b: *std.Build) void {
});
b.modules.put(b.dupe("hex"), module_hex) catch @panic("OOM");

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

const module_persistent_merkle_tree = b.createModule(.{
.root_source_file = b.path("src/persistent_merkle_tree/root.zig"),
.target = target,
Expand Down Expand Up @@ -513,6 +520,20 @@ pub fn build(b: *std.Build) void {
tls_run_test_hex.dependOn(&run_test_hex.step);
tls_run_test.dependOn(&run_test_hex.step);

const test_fork_types = b.addTest(.{
.name = "fork_types",
.root_module = module_fork_types,
.filters = b.option([][]const u8, "fork_types.filters", "fork_types test filters") orelse &[_][]const u8{},
});
const install_test_fork_types = b.addInstallArtifact(test_fork_types, .{});
const tls_install_test_fork_types = b.step("build-test:fork_types", "Install the fork_types test");
tls_install_test_fork_types.dependOn(&install_test_fork_types.step);

const run_test_fork_types = b.addRunArtifact(test_fork_types);
const tls_run_test_fork_types = b.step("test:fork_types", "Run the fork_types test");
tls_run_test_fork_types.dependOn(&run_test_fork_types.step);
tls_run_test.dependOn(&run_test_fork_types.step);

const test_persistent_merkle_tree = b.addTest(.{
.name = "persistent_merkle_tree",
.root_module = module_persistent_merkle_tree,
Expand Down Expand Up @@ -848,6 +869,7 @@ pub fn build(b: *std.Build) void {

module_era.addImport("consensus_types", module_consensus_types);
module_era.addImport("config", module_config);
module_era.addImport("fork_types", module_fork_types);
module_era.addImport("preset", module_preset);
module_era.addImport("state_transition", module_state_transition);
module_era.addImport("snappy", dep_snappy.module("snappy"));
Expand All @@ -857,6 +879,12 @@ pub fn build(b: *std.Build) void {
module_hashing.addImport("hex", module_hex);
module_hashing.addImport("hashtree", dep_hashtree.module("hashtree"));

module_fork_types.addImport("consensus_types", module_consensus_types);
module_fork_types.addImport("config", module_config);
module_fork_types.addImport("persistent_merkle_tree", module_persistent_merkle_tree);
module_fork_types.addImport("preset", module_preset);
module_fork_types.addImport("ssz", module_ssz);

module_persistent_merkle_tree.addImport("build_options", options_module_build_options);
module_persistent_merkle_tree.addImport("hex", module_hex);
module_persistent_merkle_tree.addImport("hashing", module_hashing);
Expand All @@ -874,6 +902,7 @@ pub fn build(b: *std.Build) void {
module_state_transition.addImport("config", module_config);
module_state_transition.addImport("consensus_types", module_consensus_types);
module_state_transition.addImport("blst", dep_blst.module("blst"));
module_state_transition.addImport("fork_types", module_fork_types);
module_state_transition.addImport("preset", module_preset);
module_state_transition.addImport("constants", module_constants);
module_state_transition.addImport("hex", module_hex);
Expand Down Expand Up @@ -916,6 +945,7 @@ pub fn build(b: *std.Build) void {
module_bench_hashing.addImport("zbench", dep_zbench.module("zbench"));

module_bench_process_block.addImport("state_transition", module_state_transition);
module_bench_process_block.addImport("fork_types", module_fork_types);
module_bench_process_block.addImport("consensus_types", module_consensus_types);
module_bench_process_block.addImport("config", module_config);
module_bench_process_block.addImport("zbench", dep_zbench.module("zbench"));
Expand All @@ -924,6 +954,7 @@ pub fn build(b: *std.Build) void {
module_bench_process_block.addImport("era", module_era);

module_bench_process_epoch.addImport("state_transition", module_state_transition);
module_bench_process_epoch.addImport("fork_types", module_fork_types);
module_bench_process_epoch.addImport("consensus_types", module_consensus_types);
module_bench_process_epoch.addImport("config", module_config);
module_bench_process_epoch.addImport("zbench", dep_zbench.module("zbench"));
Expand All @@ -938,6 +969,7 @@ pub fn build(b: *std.Build) void {
module_spec_tests.addImport("spec_test_options", options_module_spec_test_options);
module_spec_tests.addImport("consensus_types", module_consensus_types);
module_spec_tests.addImport("config", module_config);
module_spec_tests.addImport("fork_types", module_fork_types);
module_spec_tests.addImport("preset", module_preset);
module_spec_tests.addImport("snappy", dep_snappy.module("snappy"));
module_spec_tests.addImport("state_transition", module_state_transition);
Expand Down
13 changes: 6 additions & 7 deletions src/config/BeaconConfig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,17 @@ pub fn getMaxRequestBlobSidecars(self: *const BeaconConfig, fork: ForkSeq) u64 {
///
/// When the message epoch is before the state's active fork epoch, the domain is computed
/// using the previous fork sequence (per spec rules around fork boundaries).
pub fn getDomain(self: *const BeaconConfig, state_slot: Slot, domain_type: DomainType, message_slot: ?Slot) !*const [32]u8 {
const slot = if (message_slot) |s| s else state_slot;
const epoch = @divFloor(slot, preset.SLOTS_PER_EPOCH);
const state_fork_info = self.forkInfo(state_slot);
pub fn getDomain(self: *const BeaconConfig, state_epoch: Epoch, domain_type: DomainType, message_slot: ?Slot) !*const [32]u8 {
const epoch = if (message_slot) |s| @divFloor(s, preset.SLOTS_PER_EPOCH) else state_epoch;
const state_fork_info = self.forkInfoAtEpoch(state_epoch);
const fork_seq = if (epoch < state_fork_info.epoch) state_fork_info.prev_fork_seq else state_fork_info.fork_seq;

return self.domain_cache.get(fork_seq, domain_type);
}

pub fn getDomainForVoluntaryExit(self: *const BeaconConfig, state_slot: Slot, message_slot: ?Slot) !*const [32]u8 {
if (@divFloor(state_slot, preset.SLOTS_PER_EPOCH) < self.chain.DENEB_FORK_EPOCH) {
return self.getDomain(state_slot, DOMAIN_VOLUNTARY_EXIT, message_slot);
pub fn getDomainForVoluntaryExit(self: *const BeaconConfig, state_epoch: Epoch, message_slot: ?Slot) !*const [32]u8 {
if (state_epoch < self.chain.DENEB_FORK_EPOCH) {
return self.getDomain(state_epoch, DOMAIN_VOLUNTARY_EXIT, message_slot);
} else {
return self.domain_cache.get(.capella, DOMAIN_VOLUNTARY_EXIT);
}
Expand Down
3 changes: 0 additions & 3 deletions src/config/fork_seq.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const std = @import("std");
const types = @import("consensus_types");
const Epoch = types.primitive.Epoch.Type;
const Version = types.primitive.Version.Type;

/// Ordered consensus fork identifiers used throughout the client.
///
Expand Down
2 changes: 0 additions & 2 deletions src/config/networks/chiado.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const std = @import("std");
const Preset = @import("preset").Preset;
const ChainConfig = @import("../ChainConfig.zig");
const BeaconConfig = @import("../BeaconConfig.zig");
const b = @import("hex").hexToBytesComptime;
const gnosis = @import("./gnosis.zig");
Expand Down
2 changes: 0 additions & 2 deletions src/config/networks/hoodi.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const std = @import("std");
const preset = @import("preset").preset;
const ChainConfig = @import("../ChainConfig.zig");
const BeaconConfig = @import("../BeaconConfig.zig");
const b = @import("hex").hexToBytesComptime;
Expand Down
2 changes: 0 additions & 2 deletions src/config/networks/sepolia.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const std = @import("std");
const preset = @import("preset").preset;
const ChainConfig = @import("../ChainConfig.zig");
const BeaconConfig = @import("../BeaconConfig.zig");
const b = @import("hex").hexToBytesComptime;
Expand Down
5 changes: 5 additions & 0 deletions src/consensus_types/altair.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ pub const Fork = phase0.Fork;
pub const ForkData = phase0.ForkData;
pub const Checkpoint = phase0.Checkpoint;
pub const Validator = phase0.Validator;
pub const Validators = phase0.Validators;
pub const AttestationData = phase0.AttestationData;
pub const IndexedAttestation = phase0.IndexedAttestation;
pub const PendingAttestation = phase0.PendingAttestation;
pub const Eth1Data = phase0.Eth1Data;
pub const Eth1DataVotes = phase0.Eth1DataVotes;
pub const HistoricalBatch = phase0.HistoricalBatch;
pub const DepositMessage = phase0.DepositMessage;
pub const DepositData = phase0.DepositData;
Expand All @@ -32,6 +34,9 @@ pub const HistoricalBlockRoots = phase0.HistoricalBlockRoots;
pub const HistoricalStateRoots = phase0.HistoricalStateRoots;
pub const ProposerSlashings = phase0.ProposerSlashings;
pub const AttesterSlashings = phase0.AttesterSlashings;
pub const Slashings = phase0.Slashings;
pub const Balances = phase0.Balances;
pub const RandaoMixes = phase0.RandaoMixes;
pub const Attestations = phase0.Attestations;
pub const Deposits = phase0.Deposits;
pub const VoluntaryExits = phase0.VoluntaryExits;
Expand Down
7 changes: 5 additions & 2 deletions src/consensus_types/bellatrix.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const std = @import("std");
const ssz = @import("ssz");
const p = @import("primitive.zig");
const c = @import("constants");
const preset = @import("preset").preset;
const phase0 = @import("phase0.zig");
const altair = @import("altair.zig");
Expand All @@ -10,10 +8,12 @@ pub const Fork = phase0.Fork;
pub const ForkData = phase0.ForkData;
pub const Checkpoint = phase0.Checkpoint;
pub const Validator = phase0.Validator;
pub const Validators = phase0.Validators;
pub const AttestationData = phase0.AttestationData;
pub const IndexedAttestation = phase0.IndexedAttestation;
pub const PendingAttestation = phase0.PendingAttestation;
pub const Eth1Data = phase0.Eth1Data;
pub const Eth1DataVotes = phase0.Eth1DataVotes;
pub const HistoricalBatch = phase0.HistoricalBatch;
pub const DepositMessage = phase0.DepositMessage;
pub const DepositData = phase0.DepositData;
Expand All @@ -33,6 +33,9 @@ pub const HistoricalBlockRoots = phase0.HistoricalBlockRoots;
pub const HistoricalStateRoots = phase0.HistoricalStateRoots;
pub const ProposerSlashings = phase0.ProposerSlashings;
pub const AttesterSlashings = phase0.AttesterSlashings;
pub const Slashings = phase0.Slashings;
pub const Balances = phase0.Balances;
pub const RandaoMixes = phase0.RandaoMixes;
pub const Attestations = phase0.Attestations;
pub const Deposits = phase0.Deposits;
pub const VoluntaryExits = phase0.VoluntaryExits;
Expand Down
5 changes: 5 additions & 0 deletions src/consensus_types/capella.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ pub const Fork = phase0.Fork;
pub const ForkData = phase0.ForkData;
pub const Checkpoint = phase0.Checkpoint;
pub const Validator = phase0.Validator;
pub const Validators = phase0.Validators;
pub const AttestationData = phase0.AttestationData;
pub const IndexedAttestation = phase0.IndexedAttestation;
pub const PendingAttestation = phase0.PendingAttestation;
pub const Eth1Data = phase0.Eth1Data;
pub const Eth1DataVotes = phase0.Eth1DataVotes;
pub const HistoricalBatch = phase0.HistoricalBatch;
pub const DepositMessage = phase0.DepositMessage;
pub const DepositData = phase0.DepositData;
Expand All @@ -34,6 +36,9 @@ pub const HistoricalBlockRoots = phase0.HistoricalBlockRoots;
pub const HistoricalStateRoots = phase0.HistoricalStateRoots;
pub const ProposerSlashings = phase0.ProposerSlashings;
pub const AttesterSlashings = phase0.AttesterSlashings;
pub const Slashings = phase0.Slashings;
pub const Balances = phase0.Balances;
pub const RandaoMixes = phase0.RandaoMixes;
pub const Attestations = phase0.Attestations;
pub const Deposits = phase0.Deposits;
pub const VoluntaryExits = phase0.VoluntaryExits;
Expand Down
5 changes: 5 additions & 0 deletions src/consensus_types/deneb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ pub const Fork = phase0.Fork;
pub const ForkData = phase0.ForkData;
pub const Checkpoint = phase0.Checkpoint;
pub const Validator = phase0.Validator;
pub const Validators = phase0.Validators;
pub const AttestationData = phase0.AttestationData;
pub const IndexedAttestation = phase0.IndexedAttestation;
pub const PendingAttestation = phase0.PendingAttestation;
pub const Eth1Data = phase0.Eth1Data;
pub const Eth1DataVotes = phase0.Eth1DataVotes;
pub const HistoricalBatch = phase0.HistoricalBatch;
pub const DepositMessage = phase0.DepositMessage;
pub const DepositData = phase0.DepositData;
Expand All @@ -35,6 +37,9 @@ pub const HistoricalBlockRoots = phase0.HistoricalBlockRoots;
pub const HistoricalStateRoots = phase0.HistoricalStateRoots;
pub const ProposerSlashings = phase0.ProposerSlashings;
pub const AttesterSlashings = phase0.AttesterSlashings;
pub const Slashings = phase0.Slashings;
pub const Balances = phase0.Balances;
pub const RandaoMixes = phase0.RandaoMixes;
pub const Attestations = phase0.Attestations;
pub const Deposits = phase0.Deposits;
pub const VoluntaryExits = phase0.VoluntaryExits;
Expand Down
5 changes: 5 additions & 0 deletions src/consensus_types/electra.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ pub const Fork = phase0.Fork;
pub const ForkData = phase0.ForkData;
pub const Checkpoint = phase0.Checkpoint;
pub const Validator = phase0.Validator;
pub const Validators = phase0.Validators;
pub const AttestationData = phase0.AttestationData;
pub const PendingAttestation = phase0.PendingAttestation;
pub const Eth1Data = phase0.Eth1Data;
pub const Eth1DataVotes = phase0.Eth1DataVotes;
pub const HistoricalBatch = phase0.HistoricalBatch;
pub const DepositMessage = phase0.DepositMessage;
pub const DepositData = phase0.DepositData;
Expand All @@ -31,6 +33,9 @@ pub const HistoricalBlockRoots = phase0.HistoricalBlockRoots;
pub const HistoricalStateRoots = phase0.HistoricalStateRoots;
pub const ProposerSlashings = phase0.ProposerSlashings;
pub const AttesterSlashings = ssz.VariableListType(AttesterSlashing, preset.MAX_ATTESTER_SLASHINGS_ELECTRA);
pub const Slashings = phase0.Slashings;
pub const Balances = phase0.Balances;
pub const RandaoMixes = phase0.RandaoMixes;
pub const Deposits = phase0.Deposits;
pub const VoluntaryExits = phase0.VoluntaryExits;

Expand Down
6 changes: 5 additions & 1 deletion src/consensus_types/fulu.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const std = @import("std");
const ssz = @import("ssz");
const p = @import("primitive.zig");
const c = @import("constants");
Expand All @@ -15,9 +14,11 @@ pub const Fork = phase0.Fork;
pub const ForkData = phase0.ForkData;
pub const Checkpoint = phase0.Checkpoint;
pub const Validator = phase0.Validator;
pub const Validators = phase0.Validators;
pub const AttestationData = phase0.AttestationData;
pub const PendingAttestation = phase0.PendingAttestation;
pub const Eth1Data = phase0.Eth1Data;
pub const Eth1DataVotes = phase0.Eth1DataVotes;
pub const HistoricalBatch = phase0.HistoricalBatch;
pub const DepositMessage = phase0.DepositMessage;
pub const DepositData = phase0.DepositData;
Expand All @@ -33,6 +34,9 @@ pub const HistoricalStateRoots = phase0.HistoricalStateRoots;
pub const ProposerSlashings = phase0.ProposerSlashings;
pub const Deposits = phase0.Deposits;
pub const VoluntaryExits = phase0.VoluntaryExits;
pub const Slashings = phase0.Slashings;
pub const Balances = phase0.Balances;
pub const RandaoMixes = phase0.RandaoMixes;

pub const SyncAggregate = altair.SyncAggregate;
pub const SyncCommittee = altair.SyncCommittee;
Expand Down
1 change: 0 additions & 1 deletion src/consensus_types/phase0.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const std = @import("std");
const ssz = @import("ssz");
const p = @import("primitive.zig");
const c = @import("constants");
Expand Down
10 changes: 5 additions & 5 deletions src/era/Reader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const std = @import("std");
const c = @import("config");
const preset = @import("preset").preset;
const Node = @import("persistent_merkle_tree").Node;
const state_transition = @import("state_transition");
const fork_types = @import("fork_types");
const snappy = @import("snappy").frame;
const e2s = @import("e2s.zig");
const era = @import("era.zig");
Expand Down Expand Up @@ -91,14 +91,14 @@ pub fn readSerializedState(self: Reader, allocator: std.mem.Allocator, era_numbe
return try snappy.uncompress(allocator, compressed) orelse error.InvalidE2SHeader;
}

pub fn readState(self: Reader, allocator: std.mem.Allocator, era_number: ?u64) !state_transition.BeaconState {
pub fn readState(self: Reader, allocator: std.mem.Allocator, era_number: ?u64) !fork_types.AnyBeaconState {
const serialized = try self.readSerializedState(allocator, era_number);
defer allocator.free(serialized);

const state_slot = era.readSlotFromBeaconStateBytes(serialized);
const state_fork = self.config.forkSeq(state_slot);

return try state_transition.BeaconState.deserialize(allocator, self.pool, state_fork, serialized);
return try fork_types.AnyBeaconState.deserialize(allocator, self.pool, state_fork, serialized);
}

pub fn readCompressedBlock(self: Reader, allocator: std.mem.Allocator, slot: u64) !?[]const u8 {
Expand Down Expand Up @@ -131,13 +131,13 @@ pub fn readSerializedBlock(self: Reader, allocator: std.mem.Allocator, slot: u64
return try snappy.uncompress(allocator, compressed) orelse error.InvalidE2SHeader;
}

pub fn readBlock(self: Reader, allocator: std.mem.Allocator, slot: u64) !?state_transition.SignedBeaconBlock {
pub fn readBlock(self: Reader, allocator: std.mem.Allocator, slot: u64) !?fork_types.AnySignedBeaconBlock {
const serialized = try self.readSerializedBlock(allocator, slot) orelse return null;
defer allocator.free(serialized);

const fork_seq = self.config.forkSeq(slot);

return try state_transition.SignedBeaconBlock.deserialize(allocator, fork_seq, serialized);
return try fork_types.AnySignedBeaconBlock.deserialize(allocator, .full, fork_seq, serialized);
}

/// Validate the era file.
Expand Down
Loading