-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutils.zig
More file actions
37 lines (31 loc) · 1.58 KB
/
utils.zig
File metadata and controls
37 lines (31 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//! Shared utilities for state transition benchmarks
const std = @import("std");
const Node = @import("persistent_merkle_tree").Node;
const types = @import("consensus_types");
const config = @import("config");
const fork_types = @import("fork_types");
const ForkSeq = config.ForkSeq;
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)
pub fn slotFromStateBytes(state_bytes: []const u8) Slot {
std.debug.assert(state_bytes.len >= 48);
return std.mem.readInt(u64, state_bytes[40..48], .little);
}
/// Read slot from raw SignedBeaconBlock SSZ bytes (offset 100)
pub fn slotFromBlockBytes(block_bytes: []const u8) Slot {
std.debug.assert(block_bytes.len >= 108);
return std.mem.readInt(u64, block_bytes[100..108], .little);
}
/// 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) !*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) !AnySignedBeaconBlock {
return try AnySignedBeaconBlock.deserialize(allocator, .full, fork, block_bytes);
}