Skip to content

Commit a869a2a

Browse files
committed
more compilation fixes
1 parent 951ac04 commit a869a2a

30 files changed

+297
-237
lines changed

src/config/beacon_config.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ pub const BeaconConfig = struct {
162162
return switch (fork) {
163163
.deneb => self.chain.MAX_BLOBS_PER_BLOCK,
164164
.electra => self.chain.MAX_BLOBS_PER_BLOCK_ELECTRA,
165-
else => {
166-
// For forks before Deneb, we assume no blobs
167-
0;
168-
},
165+
else =>
166+
// For forks before Deneb, we assume no blobs
167+
0,
169168
};
170169
}
171170

src/state_transition/block/initiate_validator_exit.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const computeExitEpochAndUpdateChurn = @import("../utils/epoch.zig").computeExit
2525
/// ```
2626
/// Forcing consumers to pass the SubTree of `validator` directly mitigates this issue.
2727
///
28-
pub fn initiateValidatorExit(cached_state: *CachedBeaconStateAllForks, validator: *Validator) !void {
28+
pub fn initiateValidatorExit(cached_state: *const CachedBeaconStateAllForks, validator: *Validator) !void {
2929
const config = cached_state.config.chain;
3030
const epoch_cache = cached_state.getEpochCache();
3131
const state = cached_state.state;

src/state_transition/block/is_valid_indexed_attestation.zig

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1+
const std = @import("std");
12
const CachedBeaconStateAllForks = @import("../cache/state_cache.zig").CachedBeaconStateAllForks;
23
const ValidatorIndex = @import("../type.zig").ValidatorIndex;
34
const ForkSeq = @import("params").ForkSeq;
45
const ssz = @import("consensus_types");
56
const preset = ssz.preset;
6-
const IndexedAttestation = @import("../types/attestation.zig").IndexedAttestation;
77
const verifySingleSignatureSet = @import("../utils/signature_sets.zig").verifySingleSignatureSet;
88
const verifyAggregatedSignatureSet = @import("../utils/signature_sets.zig").verifyAggregatedSignatureSet;
99
const getIndexedAttestationSignatureSet = @import("../signature_sets/indexed_attestation.zig").getIndexedAttestationSignatureSet;
1010

11-
pub fn isValidIndexedAttestation(cached_state: *const CachedBeaconStateAllForks, indexed_attestation: *const IndexedAttestation, verify_signature: ?bool) !bool {
11+
pub fn isValidIndexedAttestation(comptime IA: type, allocator: std.mem.Allocator, cached_state: *const CachedBeaconStateAllForks, indexed_attestation: *const IA, verify_signature: ?bool) !bool {
1212
if (!isValidIndexedAttestationIndices(cached_state, indexed_attestation.attesting_indices.items)) {
1313
return false;
1414
}
1515

16-
if (verify_signature orelse true) {
17-
const signature_set = try getIndexedAttestationSignatureSet(cached_state.allocator, cached_state, indexed_attestation);
18-
return verifyAggregatedSignatureSet(signature_set);
16+
if (verify_signature) |_| {
17+
const signature_set = try getIndexedAttestationSignatureSet(IA, cached_state.allocator, cached_state, indexed_attestation);
18+
return verifyAggregatedSignatureSet(allocator, &signature_set);
19+
} else {
20+
return true;
1921
}
2022
}
2123

2224
pub fn isValidIndexedAttestationIndices(cached_state: *const CachedBeaconStateAllForks, indices: []const ValidatorIndex) bool {
2325
// verify max number of indices
24-
const fork_seq = cached_state.state.config.getForkSeq(cached_state.state.slot);
25-
const max_indices = if (fork_seq >= ForkSeq.electra) {
26-
preset.MAX_VALIDATORS_PER_COMMITTEE * preset.MAX_COMMITTEES_PER_SLOT;
27-
} else {
26+
const fork_seq = cached_state.state.getForkSeq();
27+
const max_indices: usize = if (fork_seq.isPostElectra())
28+
preset.MAX_VALIDATORS_PER_COMMITTEE * preset.MAX_COMMITTEES_PER_SLOT
29+
else
2830
preset.MAX_VALIDATORS_PER_COMMITTEE;
29-
};
3031

3132
if (!(indices.len > 0 and indices.len <= max_indices)) {
3233
return false;
@@ -35,7 +36,7 @@ pub fn isValidIndexedAttestationIndices(cached_state: *const CachedBeaconStateAl
3536
// verify indices are sorted and unique.
3637
// Just check if they are monotonically increasing,
3738
// instead of creating a set and sorting it. Should be (O(n)) instead of O(n log(n))
38-
var prev: ValidatorIndex = -1;
39+
var prev: ValidatorIndex = 0;
3940
for (indices) |index| {
4041
if (index <= prev) return false;
4142
prev = index;

src/state_transition/block/process_attestation_altair.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn processAttestationsAltair(allocator: Allocator, cached_state: *const Cach
4949
try validateAttestation(AT, cached_state, attestation);
5050

5151
// Retrieve the validator indices from the attestation participation bitfield
52-
const attesting_indices = if (AT == Phase0Attestation) epoch_cache.getAttestingIndicesPhase0(&attestation) else epoch_cache.getAttestingIndicesElectra(&attestation);
52+
const attesting_indices = try if (AT == Phase0Attestation) epoch_cache.getAttestingIndicesPhase0(&attestation) else epoch_cache.getAttestingIndicesElectra(&attestation);
5353
defer attesting_indices.deinit();
5454

5555
// this check is done last because its the most expensive (if signature verification is toggled on)

src/state_transition/block/process_attestation_phase0.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn validateAttestation(comptime AT: type, cached_state: *const CachedBeaconS
5353
const data = attestation.data;
5454
const computed_epoch = computeEpochAtSlot(data.slot);
5555
const committee_count = try epoch_cache.getCommitteeCountPerSlot(computed_epoch);
56-
if (data.target.epoch != epoch_cache.previous_shuffling.epoch and data.target.epoch != epoch_cache.epoch) {
56+
if (data.target.epoch != epoch_cache.previous_shuffling.get().epoch and data.target.epoch != epoch_cache.epoch) {
5757
// TODO: print to stderr?
5858
return error.InvalidAttestationTargetEpochNotInPreviousOrCurrentEpoch;
5959
}

src/state_transition/block/process_attestations.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Attestations = @import("../types/attestation.zig").Attestations;
1010
const processAttestationPhase0 = @import("./process_attestation_phase0.zig").processAttestationPhase0;
1111
const processAttestationsAltair = @import("./process_attestation_altair.zig").processAttestationsAltair;
1212

13-
pub fn processAttestations(allocator: Allocator, cached_state: *CachedBeaconStateAllForks, attestations: Attestations, verify_signatures: ?bool) !void {
13+
pub fn processAttestations(allocator: Allocator, cached_state: *const CachedBeaconStateAllForks, attestations: Attestations, verify_signatures: ?bool) !void {
1414
const state = cached_state.state;
1515
switch (attestations) {
1616
.phase0 => |attestations_phase0| {

src/state_transition/block/process_attester_slashing.zig

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const std = @import("std");
12
const CachedBeaconStateAllForks = @import("../cache/state_cache.zig").CachedBeaconStateAllForks;
23
const ForkSeq = @import("params").ForkSeq;
34
const ssz = @import("consensus_types");
@@ -11,10 +12,10 @@ const slashValidator = @import("./slash_validator.zig").slashValidator;
1112
/// AS is the AttesterSlashing type
1213
/// - for phase0 it is `ssz.phase0.AttesterSlashing.Type`
1314
/// - for electra it is `ssz.electra.AttesterSlashing.Type`
14-
pub fn processAttesterSlashing(comptime AS: type, cached_state: *CachedBeaconStateAllForks, attester_slashing: *const AS, verify_signature: ?bool) !void {
15+
pub fn processAttesterSlashing(comptime AS: type, allocator: std.mem.Allocator, cached_state: *const CachedBeaconStateAllForks, attester_slashing: *const AS, verify_signature: ?bool) !void {
1516
const state = cached_state.state;
1617
const epoch = cached_state.getEpochCache().epoch;
17-
try assertValidAttesterSlashing(AS, cached_state, attester_slashing, verify_signature);
18+
try assertValidAttesterSlashing(AS, allocator, cached_state, attester_slashing, verify_signature);
1819

1920
const intersecting_indices = try getAttesterSlashableIndices(cached_state.allocator, attester_slashing);
2021
defer intersecting_indices.deinit();
@@ -23,7 +24,7 @@ pub fn processAttesterSlashing(comptime AS: type, cached_state: *CachedBeaconSta
2324
// Spec requires to sort indices beforehand but we validated sorted asc AttesterSlashing in the above functions
2425
for (intersecting_indices.items) |validator_index| {
2526
const validator = state.getValidator(validator_index);
26-
if (isSlashableValidator(&validator, epoch)) {
27+
if (isSlashableValidator(validator, epoch)) {
2728
try slashValidator(cached_state, validator_index, null);
2829
slashed_any = true;
2930
}
@@ -37,14 +38,15 @@ pub fn processAttesterSlashing(comptime AS: type, cached_state: *CachedBeaconSta
3738
/// AS is the AttesterSlashing type
3839
/// - for phase0 it is `ssz.phase0.AttesterSlashing.Type`
3940
/// - for electra it is `ssz.electra.AttesterSlashing.Type`
40-
pub fn assertValidAttesterSlashing(comptime AS: type, cached_state: *CachedBeaconStateAllForks, attester_slashing: *const AS, verify_signatures: ?bool) !void {
41+
pub fn assertValidAttesterSlashing(comptime AS: type, allocator: std.mem.Allocator, cached_state: *const CachedBeaconStateAllForks, attester_slashing: *const AS, verify_signatures: ?bool) !void {
4142
const attestations = &.{ attester_slashing.attestation_1, attester_slashing.attestation_2 };
4243
if (!isSlashableAttestationData(&attestations[0].data, &attestations[1].data)) {
4344
return error.InvalidAttesterSlashingNotSlashable;
4445
}
4546

46-
inline for (0..2) |i| {
47-
if (!try isValidIndexedAttestation(cached_state, attestations[i], verify_signatures)) {
47+
inline for (@typeInfo(AS).@"struct".fields, 0..2) |f, i| {
48+
@compileLog(f.type);
49+
if (!try isValidIndexedAttestation(f.type, allocator, cached_state, &attestations[i], verify_signatures)) {
4850
return error.InvalidAttesterSlashingAttestationInvalid;
4951
}
5052
}

src/state_transition/block/process_block.zig

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const processOperations = @import("./process_operations.zig").processOperations;
1515
const processRandao = @import("./process_randao.zig").processRandao;
1616
const processSyncAggregate = @import("./process_sync_committee.zig").processSyncAggregate;
1717
const processWithdrawals = @import("./process_withdrawals.zig").processWithdrawals;
18+
const getExpectedWithdrawals = @import("./process_withdrawals.zig").getExpectedWithdrawals;
1819
const ProcessBlockOpts = @import("./types.zig").ProcessBlockOpts;
1920
const isExecutionEnabled = @import("../utils/execution.zig").isExecutionEnabled;
2021
// TODO: proposer reward api
@@ -27,7 +28,7 @@ pub fn processBlock(
2728
// TODO: support BlindedBeaconBlock
2829
block: *const SignedBlock,
2930
external_data: BlockExternalData,
30-
opts: ?ProcessBlockOpts,
31+
opts: ProcessBlockOpts,
3132
// TODO: metrics
3233
) !void {
3334
const state = cached_state.state;
@@ -40,33 +41,45 @@ pub fn processBlock(
4041
// TODO Deneb: Allow to disable withdrawals for interop testing
4142
// https://github.com/ethereum/consensus-specs/blob/b62c9e877990242d63aa17a2a59a49bc649a2f2e/specs/eip4844/beacon-chain.md#disabling-withdrawals
4243
if (state.isPostCapella()) {
44+
const expected_withdrawals_result = try getExpectedWithdrawals(allocator, cached_state);
45+
const body = block.getBeaconBlockBody();
46+
switch (body) {
47+
.unblinded => |b| {
48+
const actual_withdrawals = b.getExecutionPayload().getWithdrawals();
49+
std.debug.assert(expected_withdrawals_result.withdrawals.items.len == actual_withdrawals.items.len);
50+
for (expected_withdrawals_result.withdrawals.items, actual_withdrawals.items) |ew, pw| {
51+
_ = ew;
52+
_ = pw;
53+
// TODO(bing): equals API
54+
// assert(ew == pw)
55+
}
56+
},
57+
.blinded => |b| {
58+
const header = b.getExecutionPayloadHeader();
59+
var expected: [32]u8 = undefined;
60+
try ssz.capella.Withdrawals.hashTreeRoot(allocator, &expected_withdrawals_result.withdrawals, &expected);
61+
var actual = header.getWithdrawalsRoot();
62+
std.debug.assert(std.mem.eql(u8, &expected, &actual));
63+
},
64+
}
4365
try processWithdrawals(
4466
allocator,
4567
cached_state,
46-
&block.getBeaconBlockBody().getExecutionPayload(),
68+
expected_withdrawals_result,
4769
);
4870
}
4971

50-
switch (block) {
51-
.signed_beacon_block => |b| try processExecutionPayload(
52-
allocator,
53-
cached_state,
54-
b.getBeaconBlockBody(),
55-
external_data,
56-
),
57-
.signed_blinded_beacon_block => |b| try processExecutionPayloadHeader(
58-
allocator,
59-
cached_state,
60-
b,
61-
external_data,
62-
),
63-
}
64-
try processExecutionPayload();
72+
try processExecutionPayload(
73+
allocator,
74+
cached_state,
75+
block.getBeaconBlockBody(),
76+
external_data,
77+
);
6578
}
6679

67-
try processRandao(state, block, opts.verify_signature);
68-
try processEth1Data(state, block.getBeaconBlockBody().getEth1Data());
69-
try processOperations(cached_state, block.getBeaconBlockBody(), external_data);
80+
try processRandao(cached_state, &block.getBeaconBlockBody(), block.getProposerIndex(), opts.verify_signature);
81+
try processEth1Data(allocator, cached_state, block.getBeaconBlockBody().getEth1Data());
82+
try processOperations(allocator, cached_state, &block.getBeaconBlockBody(), opts);
7083
if (state.isPostAltair()) {
7184
try processSyncAggregate(cached_state, block, opts.verify_signature);
7285
}

src/state_transition/block/process_eth1_data.zig

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
const std = @import("std");
12
const ssz = @import("consensus_types");
23
const Eth1Data = ssz.phase0.Eth1Data.Type;
34
const CachedBeaconStateAllForks = @import("../cache/state_cache.zig").CachedBeaconStateAllForks;
45
const preset = ssz.preset;
56

6-
pub fn processEth1Data(cached_state: *const CachedBeaconStateAllForks, eth1_data: *const Eth1Data) void {
7+
pub fn processEth1Data(allocator: std.mem.Allocator, cached_state: *const CachedBeaconStateAllForks, eth1_data: *const Eth1Data) !void {
78
const state = cached_state.state;
89
if (becomesNewEth1Data(cached_state, eth1_data)) {
910
state.setEth1Data(eth1_data.*);
1011
}
1112

12-
state.getEth1DataVotes().append(eth1_data.*);
13+
try state.getEth1DataVotes().append(allocator, eth1_data.*);
1314
}
1415

1516
pub fn becomesNewEth1Data(cached_state: *const CachedBeaconStateAllForks, new_eth1_data: *const Eth1Data) bool {
@@ -18,12 +19,12 @@ pub fn becomesNewEth1Data(cached_state: *const CachedBeaconStateAllForks, new_et
1819

1920
// If there are not more than 50% votes, then we do not have to count to find a winner.
2021
const state_eth1_data_votes = state.getEth1DataVotes().items;
21-
if ((state_eth1_data_votes.length + 1) * 2 <= SLOTS_PER_ETH1_VOTING_PERIOD) {
22+
if ((state_eth1_data_votes.len + 1) * 2 <= SLOTS_PER_ETH1_VOTING_PERIOD) {
2223
return false;
2324
}
2425

2526
// Nothing to do if the state already has this as eth1data (happens a lot after majority vote is in)
26-
if (isEqualEth1DataView(state.getEth1Data(), new_eth1_data.*)) {
27+
if (isEqualEth1DataView(state.getEth1Data().*, new_eth1_data.*)) {
2728
return false;
2829
}
2930

@@ -47,7 +48,8 @@ pub fn becomesNewEth1Data(cached_state: *const CachedBeaconStateAllForks, new_et
4748
}
4849

4950
// TODO: should have a different implement in TreeView
50-
fn isEqualEth1DataView(eth1_data_a: Eth1Data, eth1_data_b: Eth1Data) bool {
51-
// TODO(ssz): implement equals api
52-
return ssz.phase0.Eth1Data.equals(eth1_data_a, eth1_data_b);
51+
fn isEqualEth1DataView(_: Eth1Data, _: Eth1Data) bool {
52+
// TODO(bing): implement equals api, for now return true
53+
// return ssz.phase0.Eth1Data.equals(eth1_data_a, eth1_data_b);
54+
return true;
5355
}

0 commit comments

Comments
 (0)