Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/consensus_types/altair.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ pub const SignedBeaconBlockHeader = ssz.FixedContainerType(struct {
signature: p.BLSSignature,
});

pub const InactivityScores = ssz.FixedListType(p.Uint64, preset.VALIDATOR_REGISTRY_LIMIT);
pub const EpochParticipation = ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT);

pub const BeaconState = ssz.VariableContainerType(struct {
genesis_time: p.Uint64,
genesis_validators_root: p.Root,
Expand All @@ -78,21 +81,21 @@ pub const BeaconState = ssz.VariableContainerType(struct {
latest_block_header: BeaconBlockHeader,
block_roots: HistoricalBlockRoots,
state_roots: HistoricalStateRoots,
historical_roots: ssz.FixedListType(p.Root, preset.HISTORICAL_ROOTS_LIMIT),
historical_roots: phase0.HistoricalRoots,
eth1_data: Eth1Data,
eth1_data_votes: phase0.Eth1DataVotes,
eth1_deposit_index: p.Uint64,
validators: ssz.FixedListType(Validator, preset.VALIDATOR_REGISTRY_LIMIT),
balances: ssz.FixedListType(p.Gwei, preset.VALIDATOR_REGISTRY_LIMIT),
randao_mixes: ssz.FixedVectorType(p.Bytes32, preset.EPOCHS_PER_HISTORICAL_VECTOR),
slashings: ssz.FixedVectorType(p.Gwei, preset.EPOCHS_PER_SLASHINGS_VECTOR),
previous_epoch_participation: ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT),
current_epoch_participation: ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT),
justification_bits: ssz.BitVectorType(c.JUSTIFICATION_BITS_LENGTH),
validators: phase0.Validators,
balances: phase0.Balances,
randao_mixes: phase0.RandaoMixes,
slashings: phase0.Slashings,
previous_epoch_participation: EpochParticipation,
current_epoch_participation: EpochParticipation,
justification_bits: phase0.JustificationBits,
previous_justified_checkpoint: Checkpoint,
current_justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
inactivity_scores: ssz.FixedListType(p.Uint64, preset.VALIDATOR_REGISTRY_LIMIT),
inactivity_scores: InactivityScores,
current_sync_committee: SyncCommittee,
next_sync_committee: SyncCommittee,
});
Expand All @@ -102,8 +105,6 @@ pub const SignedBeaconBlock = ssz.VariableContainerType(struct {
signature: p.BLSSignature,
});

pub const EpochParticipation = ssz.FixedListType(p.ParticipationFlags, preset.VALIDATOR_REGISTRY_LIMIT);

pub const SyncCommitteeMessage = ssz.FixedContainerType(struct {
slot: p.Slot,
beacon_block_root: p.Root,
Expand Down
18 changes: 9 additions & 9 deletions src/consensus_types/bellatrix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,21 @@ pub const BeaconState = ssz.VariableContainerType(struct {
latest_block_header: BeaconBlockHeader,
block_roots: HistoricalBlockRoots,
state_roots: HistoricalStateRoots,
historical_roots: ssz.FixedListType(p.Root, preset.HISTORICAL_ROOTS_LIMIT),
historical_roots: phase0.HistoricalRoots,
eth1_data: Eth1Data,
eth1_data_votes: phase0.Eth1DataVotes,
eth1_deposit_index: p.Uint64,
validators: ssz.FixedListType(Validator, preset.VALIDATOR_REGISTRY_LIMIT),
balances: ssz.FixedListType(p.Gwei, preset.VALIDATOR_REGISTRY_LIMIT),
randao_mixes: ssz.FixedVectorType(p.Bytes32, preset.EPOCHS_PER_HISTORICAL_VECTOR),
slashings: ssz.FixedVectorType(p.Gwei, preset.EPOCHS_PER_SLASHINGS_VECTOR),
previous_epoch_participation: ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT),
current_epoch_participation: ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT),
justification_bits: ssz.BitVectorType(c.JUSTIFICATION_BITS_LENGTH),
validators: phase0.Validators,
balances: phase0.Balances,
randao_mixes: phase0.RandaoMixes,
slashings: phase0.Slashings,
previous_epoch_participation: altair.EpochParticipation,
current_epoch_participation: altair.EpochParticipation,
justification_bits: phase0.JustificationBits,
previous_justified_checkpoint: Checkpoint,
current_justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
inactivity_scores: ssz.FixedListType(p.Uint64, preset.VALIDATOR_REGISTRY_LIMIT),
inactivity_scores: altair.InactivityScores,
current_sync_committee: SyncCommittee,
next_sync_committee: SyncCommittee,
latest_execution_payload_header: ExecutionPayloadHeader,
Expand Down
22 changes: 12 additions & 10 deletions src/consensus_types/capella.zig
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ pub const SignedBeaconBlockHeader = ssz.FixedContainerType(struct {
signature: p.BLSSignature,
});

pub const HistoricalSummaries = ssz.FixedListType(HistoricalSummary, preset.HISTORICAL_ROOTS_LIMIT);

pub const BeaconState = ssz.VariableContainerType(struct {
genesis_time: p.Uint64,
genesis_validators_root: p.Root,
Expand All @@ -211,27 +213,27 @@ pub const BeaconState = ssz.VariableContainerType(struct {
latest_block_header: BeaconBlockHeader,
block_roots: HistoricalBlockRoots,
state_roots: HistoricalStateRoots,
historical_roots: ssz.FixedListType(p.Root, preset.HISTORICAL_ROOTS_LIMIT),
historical_roots: phase0.HistoricalRoots,
eth1_data: Eth1Data,
eth1_data_votes: phase0.Eth1DataVotes,
eth1_deposit_index: p.Uint64,
validators: ssz.FixedListType(Validator, preset.VALIDATOR_REGISTRY_LIMIT),
balances: ssz.FixedListType(p.Gwei, preset.VALIDATOR_REGISTRY_LIMIT),
randao_mixes: ssz.FixedVectorType(p.Bytes32, preset.EPOCHS_PER_HISTORICAL_VECTOR),
slashings: ssz.FixedVectorType(p.Gwei, preset.EPOCHS_PER_SLASHINGS_VECTOR),
previous_epoch_participation: ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT),
current_epoch_participation: ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT),
justification_bits: ssz.BitVectorType(c.JUSTIFICATION_BITS_LENGTH),
validators: phase0.Validators,
balances: phase0.Balances,
randao_mixes: phase0.RandaoMixes,
slashings: phase0.Slashings,
previous_epoch_participation: altair.EpochParticipation,
current_epoch_participation: altair.EpochParticipation,
justification_bits: phase0.JustificationBits,
previous_justified_checkpoint: Checkpoint,
current_justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
inactivity_scores: ssz.FixedListType(p.Uint64, preset.VALIDATOR_REGISTRY_LIMIT),
inactivity_scores: altair.InactivityScores,
current_sync_committee: SyncCommittee,
next_sync_committee: SyncCommittee,
latest_execution_payload_header: ExecutionPayloadHeader,
next_withdrawal_index: p.WithdrawalIndex,
next_withdrawal_validator_index: p.ValidatorIndex,
historical_summaries: ssz.FixedListType(HistoricalSummary, preset.HISTORICAL_ROOTS_LIMIT),
historical_summaries: HistoricalSummaries,
});

pub const SignedBeaconBlock = ssz.VariableContainerType(struct {
Expand Down
20 changes: 10 additions & 10 deletions src/consensus_types/deneb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -214,27 +214,27 @@ pub const BeaconState = ssz.VariableContainerType(struct {
latest_block_header: BeaconBlockHeader,
block_roots: HistoricalBlockRoots,
state_roots: HistoricalStateRoots,
historical_roots: ssz.FixedListType(p.Root, preset.HISTORICAL_ROOTS_LIMIT),
historical_roots: phase0.HistoricalRoots,
eth1_data: Eth1Data,
eth1_data_votes: phase0.Eth1DataVotes,
eth1_deposit_index: p.Uint64,
validators: ssz.FixedListType(Validator, preset.VALIDATOR_REGISTRY_LIMIT),
balances: ssz.FixedListType(p.Gwei, preset.VALIDATOR_REGISTRY_LIMIT),
randao_mixes: ssz.FixedVectorType(p.Bytes32, preset.EPOCHS_PER_HISTORICAL_VECTOR),
slashings: ssz.FixedVectorType(p.Gwei, preset.EPOCHS_PER_SLASHINGS_VECTOR),
previous_epoch_participation: ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT),
current_epoch_participation: ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT),
justification_bits: ssz.BitVectorType(c.JUSTIFICATION_BITS_LENGTH),
validators: phase0.Validators,
balances: phase0.Balances,
randao_mixes: phase0.RandaoMixes,
slashings: phase0.Slashings,
previous_epoch_participation: altair.EpochParticipation,
current_epoch_participation: altair.EpochParticipation,
justification_bits: phase0.JustificationBits,
previous_justified_checkpoint: Checkpoint,
current_justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
inactivity_scores: ssz.FixedListType(p.Uint64, preset.VALIDATOR_REGISTRY_LIMIT),
inactivity_scores: altair.InactivityScores,
current_sync_committee: SyncCommittee,
next_sync_committee: SyncCommittee,
latest_execution_payload_header: ExecutionPayloadHeader,
next_withdrawal_index: p.WithdrawalIndex,
next_withdrawal_validator_index: p.ValidatorIndex,
historical_summaries: ssz.FixedListType(HistoricalSummary, preset.HISTORICAL_ROOTS_LIMIT),
historical_summaries: capella.HistoricalSummaries,
});

pub const SignedBeaconBlock = ssz.VariableContainerType(struct {
Expand Down
30 changes: 17 additions & 13 deletions src/consensus_types/electra.zig
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ pub const SignedBlindedBeaconBlock = ssz.VariableContainerType(struct {
signature: p.BLSSignature,
});

pub const PendingDeposits = ssz.FixedListType(PendingDeposit, preset.PENDING_DEPOSITS_LIMIT);
pub const PendingPartialWithdrawals = ssz.FixedListType(PendingPartialWithdrawal, preset.PENDING_PARTIAL_WITHDRAWALS_LIMIT);
pub const PendingConsolidations = ssz.FixedListType(PendingConsolidation, preset.PENDING_CONSOLIDATIONS_LIMIT);

pub const BeaconState = ssz.VariableContainerType(struct {
genesis_time: p.Uint64,
genesis_validators_root: p.Root,
Expand All @@ -248,36 +252,36 @@ pub const BeaconState = ssz.VariableContainerType(struct {
latest_block_header: BeaconBlockHeader,
block_roots: HistoricalBlockRoots,
state_roots: HistoricalStateRoots,
historical_roots: ssz.FixedListType(p.Root, preset.HISTORICAL_ROOTS_LIMIT),
historical_roots: phase0.HistoricalRoots,
eth1_data: Eth1Data,
eth1_data_votes: phase0.Eth1DataVotes,
eth1_deposit_index: p.Uint64,
validators: ssz.FixedListType(Validator, preset.VALIDATOR_REGISTRY_LIMIT),
balances: ssz.FixedListType(p.Gwei, preset.VALIDATOR_REGISTRY_LIMIT),
randao_mixes: ssz.FixedVectorType(p.Bytes32, preset.EPOCHS_PER_HISTORICAL_VECTOR),
slashings: ssz.FixedVectorType(p.Gwei, preset.EPOCHS_PER_SLASHINGS_VECTOR),
previous_epoch_participation: ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT),
current_epoch_participation: ssz.FixedListType(p.Uint8, preset.VALIDATOR_REGISTRY_LIMIT),
justification_bits: ssz.BitVectorType(c.JUSTIFICATION_BITS_LENGTH),
validators: phase0.Validators,
balances: phase0.Balances,
randao_mixes: phase0.RandaoMixes,
slashings: phase0.Slashings,
previous_epoch_participation: altair.EpochParticipation,
current_epoch_participation: altair.EpochParticipation,
justification_bits: phase0.JustificationBits,
previous_justified_checkpoint: Checkpoint,
current_justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
inactivity_scores: ssz.FixedListType(p.Uint64, preset.VALIDATOR_REGISTRY_LIMIT),
inactivity_scores: altair.InactivityScores,
current_sync_committee: SyncCommittee,
next_sync_committee: SyncCommittee,
latest_execution_payload_header: ExecutionPayloadHeader,
next_withdrawal_index: p.WithdrawalIndex,
next_withdrawal_validator_index: p.ValidatorIndex,
historical_summaries: ssz.FixedListType(HistoricalSummary, preset.HISTORICAL_ROOTS_LIMIT),
historical_summaries: capella.HistoricalSummaries,
deposit_requests_start_index: p.Uint64,
deposit_balance_to_consume: p.Gwei,
exit_balance_to_consume: p.Gwei,
earliest_exit_epoch: p.Epoch,
consolidation_balance_to_consume: p.Gwei,
earliest_consolidation_epoch: p.Epoch,
pending_deposits: ssz.FixedListType(PendingDeposit, preset.PENDING_DEPOSITS_LIMIT),
pending_partial_withdrawals: ssz.FixedListType(PendingPartialWithdrawal, preset.PENDING_PARTIAL_WITHDRAWALS_LIMIT),
pending_consolidations: ssz.FixedListType(PendingConsolidation, preset.PENDING_CONSOLIDATIONS_LIMIT),
pending_deposits: PendingDeposits,
pending_partial_withdrawals: PendingPartialWithdrawals,
pending_consolidations: PendingConsolidations,
});

pub const SignedBeaconBlock = ssz.VariableContainerType(struct {
Expand Down
16 changes: 12 additions & 4 deletions src/consensus_types/phase0.zig
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ pub const SignedBeaconBlockHeader = ssz.FixedContainerType(struct {

pub const EpochAttestations = ssz.VariableListType(PendingAttestation, preset.MAX_ATTESTATIONS * preset.SLOTS_PER_EPOCH);

pub const Balances = ssz.FixedListType(p.Gwei, preset.VALIDATOR_REGISTRY_LIMIT);

pub const RandaoMixes = ssz.FixedVectorType(p.Bytes32, preset.EPOCHS_PER_HISTORICAL_VECTOR);

pub const Slashings = ssz.FixedVectorType(p.Gwei, preset.EPOCHS_PER_SLASHINGS_VECTOR);

pub const HistoricalRoots = ssz.FixedListType(p.Root, preset.HISTORICAL_ROOTS_LIMIT);

pub const BeaconState = ssz.VariableContainerType(struct {
genesis_time: p.Uint64,
genesis_validators_root: p.Root,
Expand All @@ -174,14 +182,14 @@ pub const BeaconState = ssz.VariableContainerType(struct {
latest_block_header: BeaconBlockHeader,
block_roots: HistoricalBlockRoots,
state_roots: HistoricalStateRoots,
historical_roots: ssz.FixedListType(p.Root, preset.HISTORICAL_ROOTS_LIMIT),
historical_roots: HistoricalRoots,
eth1_data: Eth1Data,
eth1_data_votes: Eth1DataVotes,
eth1_deposit_index: p.Uint64,
validators: Validators,
balances: ssz.FixedListType(p.Gwei, preset.VALIDATOR_REGISTRY_LIMIT),
randao_mixes: ssz.FixedVectorType(p.Bytes32, preset.EPOCHS_PER_HISTORICAL_VECTOR),
slashings: ssz.FixedVectorType(p.Gwei, preset.EPOCHS_PER_SLASHINGS_VECTOR),
balances: Balances,
randao_mixes: RandaoMixes,
slashings: Slashings,
previous_epoch_attestations: EpochAttestations,
current_epoch_attestations: EpochAttestations,
justification_bits: JustificationBits,
Expand Down
2 changes: 1 addition & 1 deletion src/state_transition/block/process_execution_payload.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ pub fn processExecutionPayload(
.blinded => |b| b.executionPayloadHeader(),
};

state.setLatestExecutionPayloadHeader(&payload_header);
state.setLatestExecutionPayloadHeader(payload_header);
}
14 changes: 8 additions & 6 deletions src/state_transition/cache/epoch_cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,14 @@ pub const EpochCache = struct {
self.next_sync_committee_indexed = try SyncCommitteeCacheRc.init(allocator, next_sync_committee_indexed);
}

// TODO: review the use of this function, use the rotateSyncCommitteeIndexed() instead
// TODO: also increase reference count
// pub fn setSyncCommitteesIndexed(self: *EpochCache, next_sync_committee_indices: std.ArrayList(ValidatorIndex)) !void {
// self.next_sync_committee_indexed = try SyncCommitteeCacheAllForks.initValidatorIndices(self.allocator, next_sync_committee_indices);
// self.current_sync_committee_indexed = self.next_sync_committee_indexed;
// }
/// this is used at fork boundary from phase0 to altair
pub fn setSyncCommitteesIndexed(self: *EpochCache, next_sync_committee_indices: []const ValidatorIndex) !void {
// both current and next sync committee are set to the same value at fork boundary
self.next_sync_committee_indexed.release();
self.next_sync_committee_indexed = try SyncCommitteeCacheRc.init(self.allocator, try SyncCommitteeCacheAllForks.initValidatorIndices(self.allocator, next_sync_committee_indices));
self.current_sync_committee_indexed.release();
self.current_sync_committee_indexed = try SyncCommitteeCacheRc.init(self.allocator, try SyncCommitteeCacheAllForks.initValidatorIndices(self.allocator, next_sync_committee_indices));
}

/// This is different from typescript version: only allocate new EffectiveBalanceIncrements if needed
pub fn effectiveBalanceIncrementsSet(self: *EpochCache, allocator: Allocator, index: usize, effective_balance: u64) !void {
Expand Down
79 changes: 79 additions & 0 deletions src/state_transition/slot/upgrade_state_to_altair.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const CachedBeaconStateAllForks = @import("../cache/state_cache.zig").CachedBeaconStateAllForks;
const BeaconStateAllForks = @import("../types/beacon_state.zig").BeaconStateAllForks;
const getNextSyncCommittee = @import("../utils/sync_committee.zig").getNextSyncCommittee;
const SyncCommitteeInfo = @import("../utils/sync_committee.zig").SyncCommitteeInfo;
const sumTargetUnslashedBalanceIncrements = @import("../utils/target_unslashed_balance.zig").sumTargetUnslashedBalanceIncrements;
const ssz = @import("consensus_types");
const ValidatorIndex = ssz.primitive.ValidatorIndex.Type;
const RootCache = @import("../utils/root_cache.zig").RootCache;
const getAttestationParticipationStatus = @import("../block//process_attestation_altair.zig").getAttestationParticipationStatus;

pub fn upgradeStateToAltair(allocator: Allocator, cached_state: *CachedBeaconStateAllForks) !void {
var state = cached_state.state;
if (!state.isPhase0()) {
// already altair
return error.StateIsNotPhase0;
}
const phase0_state = state.phase0;
defer {
ssz.phase0.BeaconState.deinit(allocator, phase0_state);
allocator.destroy(phase0_state);
}
_ = try state.upgradeUnsafe(allocator);
state.forkPtr().* = .{
.previous_version = phase0_state.fork.current_version,
.current_version = cached_state.config.chain.ALTAIR_FORK_VERSION,
.epoch = cached_state.getEpochCache().epoch,
};

const validator_count = state.validators().items.len;
const previous_epoch_participations = state.previousEpochParticipations();
try previous_epoch_participations.resize(allocator, validator_count);
@memset(previous_epoch_participations.items, 0);

const current_epoch_participations = state.currentEpochParticipations();
try state.currentEpochParticipations().resize(allocator, validator_count);
@memset(current_epoch_participations.items, 0);

const inactivity_scores = state.inactivityScores();
try inactivity_scores.resize(allocator, validator_count);
@memset(inactivity_scores.items, 0);

const epoch_cache = cached_state.getEpochCache();
const active_indices = epoch_cache.next_shuffling.get().active_indices;
var sync_committee_info: SyncCommitteeInfo = undefined;
try getNextSyncCommittee(allocator, state, active_indices, epoch_cache.getEffectiveBalanceIncrements(), &sync_committee_info);
defer sync_committee_info.deinit(allocator);
state.currentSyncCommittee().* = sync_committee_info.sync_committee.*;
state.nextSyncCommittee().* = sync_committee_info.sync_committee.*;

try cached_state.epoch_cache_ref.get().setSyncCommitteesIndexed(sync_committee_info.indices.items);
try translateParticipation(allocator, cached_state, phase0_state.previous_epoch_attestations);

const previous_epoch = epoch_cache.epoch - 1;
epoch_cache.previous_target_unslashed_balance_increments = sumTargetUnslashedBalanceIncrements(state.previousEpochParticipations().items, previous_epoch, state.validators().items);
}

/// Translate_participation in https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/altair/fork.md
fn translateParticipation(allocator: Allocator, cached_state: *CachedBeaconStateAllForks, pending_attestations: ssz.phase0.EpochAttestations.Type) !void {
const epoch_cache = cached_state.getEpochCache();
const root_cache = try RootCache.init(allocator, cached_state);
defer root_cache.deinit();
const state = cached_state.state;
const epoch_participation = state.previousEpochParticipations();
try epoch_participation.resize(allocator, state.validators().items.len);
@memset(epoch_participation.items, 0);

for (pending_attestations.items) |*attestation| {
const data = attestation.data;
const attestation_flag = try getAttestationParticipationStatus(cached_state.state, data, attestation.inclusion_delay, epoch_cache.epoch, root_cache);
const committee_indices = try epoch_cache.getBeaconCommittee(data.slot, data.index);
const attesting_indices = try attestation.aggregation_bits.intersectValues(ValidatorIndex, allocator, committee_indices);
defer attesting_indices.deinit();
for (attesting_indices.items) |validator_index| {
epoch_participation.items[validator_index] |= attestation_flag;
}
}
}
Loading