-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathprocess_attester_slashing.zig
More file actions
102 lines (95 loc) · 3.8 KB
/
process_attester_slashing.zig
File metadata and controls
102 lines (95 loc) · 3.8 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
const std = @import("std");
const Allocator = std.mem.Allocator;
const BeaconConfig = @import("config").BeaconConfig;
const ForkSeq = @import("config").ForkSeq;
const ForkTypes = @import("fork_types").ForkTypes;
const BeaconState = @import("fork_types").BeaconState;
const types = @import("consensus_types");
const EpochCache = @import("../cache/epoch_cache.zig").EpochCache;
const SlashingsCache = @import("../cache/slashings_cache.zig").SlashingsCache;
const buildSlashingsCacheIfNeeded = @import("../cache/slashings_cache.zig").buildFromStateIfNeeded;
const isSlashableAttestationData = @import("../utils/attestation.zig").isSlashableAttestationData;
const getAttesterSlashableIndices = @import("../utils/attestation.zig").getAttesterSlashableIndices;
const isValidIndexedAttestation = @import("./is_valid_indexed_attestation.zig").isValidIndexedAttestation;
const isSlashableValidator = @import("../utils/validator.zig").isSlashableValidator;
const slashValidator = @import("./slash_validator.zig").slashValidator;
/// AS is the AttesterSlashing type
/// - for phase0 it is `types.phase0.AttesterSlashing.Type`
/// - for electra it is `types.electra.AttesterSlashing.Type`
pub fn processAttesterSlashing(
comptime fork: ForkSeq,
allocator: Allocator,
config: *const BeaconConfig,
epoch_cache: *EpochCache,
state: *BeaconState(fork),
slashings_cache: *SlashingsCache,
current_epoch: u64,
attester_slashing: *const ForkTypes(fork).AttesterSlashing.Type,
verify_signature: bool,
) !void {
try buildSlashingsCacheIfNeeded(allocator, state, slashings_cache);
try assertValidAttesterSlashing(
fork,
allocator,
config,
epoch_cache,
try state.validatorsCount(),
attester_slashing,
verify_signature,
);
const intersecting_indices = try getAttesterSlashableIndices(allocator, attester_slashing);
defer intersecting_indices.deinit();
var slashed_any: bool = false;
var validators = try state.validators();
// Spec requires to sort indices beforehand but we validated sorted asc AttesterSlashing in the above functions
for (intersecting_indices.items) |validator_index| {
var validator: types.phase0.Validator.Type = undefined;
try validators.getValue(undefined, validator_index, &validator);
if (isSlashableValidator(&validator, current_epoch)) {
try slashValidator(fork, config, epoch_cache, state, slashings_cache, validator_index, null);
slashed_any = true;
}
}
if (!slashed_any) {
return error.InvalidAttesterSlashingNoSlashableValidators;
}
}
/// AS is the AttesterSlashing type
/// - for phase0 it is `types.phase0.AttesterSlashing.Type`
/// - for electra it is `types.electra.AttesterSlashing.Type`
pub fn assertValidAttesterSlashing(
comptime fork: ForkSeq,
allocator: Allocator,
config: *const BeaconConfig,
epoch_cache: *const EpochCache,
validators_count: usize,
attester_slashing: *const ForkTypes(fork).AttesterSlashing.Type,
verify_signatures: bool,
) !void {
const attestations = &.{ attester_slashing.attestation_1, attester_slashing.attestation_2 };
if (!isSlashableAttestationData(&attestations[0].data, &attestations[1].data)) {
return error.InvalidAttesterSlashingNotSlashable;
}
if (!try isValidIndexedAttestation(
fork,
allocator,
config,
epoch_cache,
validators_count,
&attestations[0],
verify_signatures,
)) {
return error.InvalidAttesterSlashingAttestationInvalid;
}
if (!try isValidIndexedAttestation(
fork,
allocator,
config,
epoch_cache,
validators_count,
&attestations[1],
verify_signatures,
)) {
return error.InvalidAttesterSlashingAttestationInvalid;
}
}