-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathprocess_eth1_data.zig
More file actions
69 lines (56 loc) · 3.09 KB
/
process_eth1_data.zig
File metadata and controls
69 lines (56 loc) · 3.09 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
const std = @import("std");
const types = @import("consensus_types");
const Eth1Data = types.phase0.Eth1Data.Type;
const BeaconState = @import("../types/beacon_state.zig").BeaconState;
const CachedBeaconState = @import("../cache/state_cache.zig").CachedBeaconState;
const preset = @import("preset").preset;
pub fn processEth1Data(allocator: std.mem.Allocator, cached_state: *CachedBeaconState, eth1_data: *const Eth1Data) !void {
const state = cached_state.state;
if (try becomesNewEth1Data(allocator, state, eth1_data)) {
try state.setEth1Data(eth1_data);
}
try state.appendEth1DataVote(eth1_data);
}
pub fn becomesNewEth1Data(allocator: std.mem.Allocator, state: *BeaconState, new_eth1_data: *const Eth1Data) !bool {
const SLOTS_PER_ETH1_VOTING_PERIOD = preset.EPOCHS_PER_ETH1_VOTING_PERIOD * preset.SLOTS_PER_EPOCH;
// If there are not more than 50% votes, then we do not have to count to find a winner.
var state_eth1_data_votes_view = try state.eth1DataVotes();
const state_eth1_data_votes_len = try state_eth1_data_votes_view.length();
if ((state_eth1_data_votes_len + 1) * 2 <= SLOTS_PER_ETH1_VOTING_PERIOD) return false;
// Nothing to do if the state already has this as eth1data (happens a lot after majority vote is in)
var state_eth1_data_view = try state.eth1Data();
var state_eth1_data: Eth1Data = undefined;
try state_eth1_data_view.toValue(allocator, &state_eth1_data);
if (types.phase0.Eth1Data.equals(&state_eth1_data, new_eth1_data)) return false;
var new_eth1_data_root: [32]u8 = undefined;
try types.phase0.Eth1Data.hashTreeRoot(new_eth1_data, &new_eth1_data_root);
// Close to half the EPOCHS_PER_ETH1_VOTING_PERIOD it can be expensive to do so many comparisions.
//
// `iteratorReadonly` navigates the tree once to fetch all the LeafNodes efficiently.
// Then isEqualEth1DataView compares cached roots (HashObject as of Jan 2022) which is much cheaper
// than doing structural equality, which requires tree -> value conversions
var same_votes_count: usize = 0;
var eth1_data_votes_it = state_eth1_data_votes_view.iteratorReadonly(0);
for (0..state_eth1_data_votes_len) |_| {
const state_eth1_data_vote_root = try eth1_data_votes_it.nextRoot();
if (std.mem.eql(u8, state_eth1_data_vote_root, &new_eth1_data_root)) {
same_votes_count += 1;
}
}
// The +1 is to account for the `eth1Data` supplied to the function.
if ((same_votes_count + 1) * 2 > SLOTS_PER_ETH1_VOTING_PERIOD) {
return true;
}
return false;
}
const TestCachedBeaconState = @import("../test_utils/root.zig").TestCachedBeaconState;
const Node = @import("persistent_merkle_tree").Node;
test "process eth1 data - sanity" {
const allocator = std.testing.allocator;
var pool = try Node.Pool.init(allocator, 1024);
defer pool.deinit();
var test_state = try TestCachedBeaconState.init(allocator, &pool, 256);
defer test_state.deinit();
const block = types.electra.BeaconBlock.default_value;
try processEth1Data(allocator, test_state.cached_state, &block.body.eth1_data);
}