-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathReader.zig
More file actions
227 lines (193 loc) · 8.83 KB
/
Reader.zig
File metadata and controls
227 lines (193 loc) · 8.83 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
///! Reader is responsible for reading and validating ERA files.
///! See https://github.com/eth-clients/e2store-format-specs/blob/main/formats/era.md
const std = @import("std");
const c = @import("config");
const preset = @import("preset").preset;
const Node = @import("persistent_merkle_tree").Node;
const fork_types = @import("fork_types");
const snappy = @import("snappy").frame;
const e2s = @import("e2s.zig");
const era = @import("era.zig");
config: c.BeaconConfig,
/// The file being read
file: std.fs.File,
/// The era number retrieved from the file name
era_number: u64,
/// The short historical root retrieved from the file name
short_historical_root: [8]u8,
/// An array of state and block indices, one per group
group_indices: []era.GroupIndex,
/// Persistent merkle tree pool used by TreeViews (must outlive returned views)
pool: *Node.Pool,
const Reader = @This();
pub fn open(allocator: std.mem.Allocator, config: c.BeaconConfig, path: []const u8) !Reader {
const file = try std.fs.cwd().openFile(path, .{});
errdefer file.close();
const era_file_name = try era.EraFileName.parse(path);
const group_indices = try era.readAllGroupIndices(allocator, file);
errdefer {
for (group_indices) |group_index| {
allocator.free(group_index.state_index.offsets);
if (group_index.blocks_index) |bi| {
allocator.free(bi.offsets);
}
}
allocator.free(group_indices);
}
const pool = try allocator.create(Node.Pool);
errdefer allocator.destroy(pool);
pool.* = try Node.Pool.init(allocator, 500_000);
errdefer pool.deinit();
return .{
.config = config,
.file = file,
.era_number = era_file_name.era_number,
.short_historical_root = era_file_name.short_historical_root,
.group_indices = group_indices,
.pool = pool,
};
}
pub fn close(self: *Reader, allocator: std.mem.Allocator) void {
self.file.close();
for (self.group_indices) |group_index| {
allocator.free(group_index.state_index.offsets);
if (group_index.blocks_index) |bi| {
allocator.free(bi.offsets);
}
}
allocator.free(self.group_indices);
self.pool.deinit();
allocator.destroy(self.pool);
self.* = undefined;
}
pub fn readCompressedState(self: Reader, allocator: std.mem.Allocator, era_number: ?u64) ![]const u8 {
const state_era_number = era_number orelse self.era_number;
const group_index = try std.math.sub(u64, state_era_number, self.era_number);
if (group_index >= self.group_indices.len) {
return error.InvalidEraNumber;
}
const index = self.group_indices[group_index];
const offset: u64 = @intCast(try std.math.add(i64, @intCast(index.state_index.record_start), index.state_index.offsets[0]));
const entry = try e2s.readEntry(allocator, self.file, offset);
errdefer allocator.free(entry.data);
if (entry.entry_type != .CompressedBeaconState) {
return error.InvalidE2SHeader;
}
return entry.data;
}
pub fn readSerializedState(self: Reader, allocator: std.mem.Allocator, era_number: ?u64) ![]const u8 {
const compressed = try self.readCompressedState(allocator, era_number);
defer allocator.free(compressed);
return try snappy.uncompress(allocator, compressed) orelse error.InvalidE2SHeader;
}
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 fork_types.AnyBeaconState.deserialize(allocator, self.pool, state_fork, serialized);
}
pub fn readCompressedBlock(self: Reader, allocator: std.mem.Allocator, slot: u64) !?[]const u8 {
const slot_era = era.computeEraNumberFromBlockSlot(slot);
const group_index = try std.math.sub(u64, slot_era, self.era_number);
if (group_index >= self.group_indices.len) {
return error.InvalidEraNumber;
}
const index = self.group_indices[group_index];
const blocks_index = index.blocks_index orelse return error.NoBlockIndex;
// Calculate offset within the index
const slot_offset = try std.math.sub(u64, slot, blocks_index.start_slot);
const offset: u64 = @intCast(try std.math.add(i64, @intCast(blocks_index.record_start), blocks_index.offsets[slot_offset]));
if (offset == 0) {
return null; // Empty slot
}
const entry = try e2s.readEntry(allocator, self.file, offset);
errdefer allocator.free(entry.data);
if (entry.entry_type != .CompressedSignedBeaconBlock) {
return error.InvalidE2SHeader;
}
return entry.data;
}
pub fn readSerializedBlock(self: Reader, allocator: std.mem.Allocator, slot: u64) !?[]const u8 {
const compressed = try self.readCompressedBlock(allocator, slot) orelse return null;
defer allocator.free(compressed);
return try snappy.uncompress(allocator, compressed) orelse error.InvalidE2SHeader;
}
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 fork_types.AnySignedBeaconBlock.deserialize(allocator, .full, fork_seq, serialized);
}
/// Validate the era file.
/// - e2s format correctness
/// - era range correctness
/// - network correctness for state and blocks
/// - TODO block root and signature matches
pub fn validate(self: Reader, allocator: std.mem.Allocator) !void {
for (self.group_indices, 0..) |index, group_index| {
const era_number = self.era_number + group_index;
// validate version entry
const start: i64 = if (index.blocks_index) |bi|
@as(i64, @intCast(bi.record_start)) + bi.offsets[0] - e2s.header_size
else
@as(i64, @intCast(index.state_index.record_start)) + index.state_index.offsets[0] - e2s.header_size;
if (start < 0) {
return error.InvalidGroupStartIndex;
}
try e2s.readVersion(self.file, @intCast(start));
// Genesis era cannot have a block index
if (era_number == 0 and index.blocks_index != null) {
return error.GenesisEraHasBlockIndex;
}
// validate state
// the state is loadable and consistent with the given config
var state = try self.readState(allocator, era_number);
defer state.deinit();
if (!std.mem.eql(u8, &self.config.genesis_validator_root, try state.genesisValidatorsRoot())) {
return error.GenesisValidatorRootMismatch;
}
// validate blocks
if (era_number > 0) {
if (index.blocks_index == null) {
return error.MissingBlockIndex;
}
const start_slot = index.blocks_index.?.start_slot;
const end_slot = start_slot + index.blocks_index.?.offsets.len;
if (start_slot % preset.SLOTS_PER_HISTORICAL_ROOT != 0) {
return error.InvalidBlockIndex;
}
if (end_slot != start_slot + preset.SLOTS_PER_HISTORICAL_ROOT) {
return error.InvalidBlockIndex;
}
var blockRoots = try state.blockRoots();
for (start_slot..end_slot) |slot| {
const block = try self.readBlock(allocator, slot) orelse {
if (slot == start_slot) {
// first slot in the era can't be easily validated
continue;
}
var prev_root: [32]u8 = undefined;
var curr_root: [32]u8 = undefined;
var prev_view = try blockRoots.get(@intCast((slot - 1) % preset.SLOTS_PER_HISTORICAL_ROOT));
try prev_view.toValue(allocator, &prev_root);
var curr_view = try blockRoots.get(@intCast(slot % preset.SLOTS_PER_HISTORICAL_ROOT));
try curr_view.toValue(allocator, &curr_root);
if (std.mem.eql(u8, &prev_root, &curr_root)) {
continue;
}
return error.MissingBlock;
};
defer block.deinit(allocator);
var block_root: [32]u8 = undefined;
try block.beaconBlock().hashTreeRoot(allocator, &block_root);
var expected_root: [32]u8 = undefined;
var expected_view = try blockRoots.get(@intCast(slot % preset.SLOTS_PER_HISTORICAL_ROOT));
try expected_view.toValue(allocator, &expected_root);
if (!std.mem.eql(u8, &expected_root, &block_root)) {
return error.BlockRootMismatch;
}
}
}
}
}