Skip to content

Commit

Permalink
feat:add more types
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Oct 1, 2024
1 parent e5de39f commit 1dba948
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 24 deletions.
111 changes: 111 additions & 0 deletions src/consensus/altair/types.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const std = @import("std");
const primitives = @import("../../primitives/types.zig");
const preset = @import("../../presets/preset.zig");
const consensus = @import("../../consensus/types.zig");

pub const BeaconBlockBody = struct {
randao_reveal: primitives.BLSSignature,
eth1_data: *consensus.Eth1Data, // Eth1 data vote
graffiti: primitives.Bytes32, // Arbitrary data
// Operations
proposer_slashings: []*consensus.ProposerSlashing,
attester_slashings: []*consensus.AttesterSlashing,
attestations: []*consensus.Attestation,
deposits: []*consensus.Deposit,
voluntary_exits: []*consensus.SignedVoluntaryExit,
sync_aggregate: ?*consensus.SyncAggregate,
};

pub const BeaconBlock = struct {
slot: primitives.Slot,
proposer_index: primitives.ValidatorIndex,
parent_root: primitives.Root,
state_root: primitives.Root,
body: *BeaconBlockBody,
};

const BeaconState = struct {
genesis_time: u64,
genesis_validators_root: primitives.Root,
slot: primitives.Slot,
fork: *consensus.Fork,
latest_block_header: consensus.BeaconBlockHeader,
block_roots: []primitives.Root,
state_roots: []primitives.Root,
historical_roots: []primitives.Root,
eth1_data: ?*consensus.Eth1Data,
eth1_data_votes: []*consensus.Eth1Data,
eth1_deposit_index: u64,
validators: []*consensus.Validator,
balances: []primitives.Gwei,
randao_mixes: []primitives.Bytes32,
slashings: []primitives.Gwei,
previous_epoch_attestations: []*consensus.PendingAttestation,
current_epoch_attestations: []*consensus.PendingAttestation,
justification_bits: []bool,
previous_justified_checkpoint: *consensus.Checkpoint,
current_justified_checkpoint: *consensus.Checkpoint,
finalized_checkpoint: *consensus.Checkpoint,
inactivity_scores: []u64,
current_sync_committee: ?*consensus.SyncCommittee,
next_sync_committee: ?*consensus.SyncCommittee,
};

test "test BeaconBlock" {
const block = BeaconBlock{
.slot = 0,
.proposer_index = 0,
.parent_root = undefined,
.state_root = undefined,
.body = undefined,
};

try std.testing.expectEqual(block.slot, 0);
}

test "test BeaconState" {
const state = BeaconState{
.genesis_time = 0,
.genesis_validators_root = undefined,
.slot = 0,
.fork = undefined,
.latest_block_header = undefined,
.block_roots = undefined,
.state_roots = undefined,
.historical_roots = undefined,
.eth1_data = undefined,
.eth1_data_votes = undefined,
.eth1_deposit_index = 0,
.validators = undefined,
.balances = undefined,
.randao_mixes = undefined,
.slashings = undefined,
.previous_epoch_attestations = undefined,
.current_epoch_attestations = undefined,
.justification_bits = undefined,
.previous_justified_checkpoint = undefined,
.current_justified_checkpoint = undefined,
.finalized_checkpoint = undefined,
.inactivity_scores = undefined,
.current_sync_committee = undefined,
.next_sync_committee = undefined,
};

try std.testing.expectEqual(state.genesis_time, 0);
}

test "test BeaconBlockBody" {
const body = BeaconBlockBody{
.randao_reveal = undefined,
.eth1_data = undefined,
.graffiti = undefined,
.proposer_slashings = undefined,
.attester_slashings = undefined,
.attestations = undefined,
.deposits = undefined,
.voluntary_exits = undefined,
.sync_aggregate = undefined,
};

try std.testing.expectEqual(body.randao_reveal.len, 96);
}
92 changes: 92 additions & 0 deletions src/consensus/bellatrix/types.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
pub const primitives = @import("../../primitives/types.zig");
const preset = @import("../../presets/preset.zig");
const consensus = @import("../../consensus/types.zig");

pub const ExecutionPayloadHeader = struct {
parent_hash: primitives.Hash32,
Expand Down Expand Up @@ -39,6 +40,48 @@ pub const ExecutionPayload = struct {
transactions: []primitives.Transaction, // with a maximum length of MAX_TRANSACTIONS_PER_PAYLOAD
};

pub const BeaconBlockBody = struct {
randao_reveal: primitives.BLSSignature,
eth1_data: *consensus.Eth1Data, // Eth1 data vote
graffiti: primitives.Bytes32, // Arbitrary data
// Operations
proposer_slashings: []*consensus.ProposerSlashing,
attester_slashings: []*consensus.AttesterSlashing,
attestations: []*consensus.Attestation,
deposits: []*consensus.Deposit,
voluntary_exits: []*consensus.SignedVoluntaryExit,
sync_aggregate: ?*consensus.SyncAggregate,
execution_payload: ?*ExecutionPayload,
};

const BeaconState = struct {
genesis_time: u64,
genesis_validators_root: primitives.Root,
slot: primitives.Slot,
fork: *consensus.Fork,
latest_block_header: consensus.BeaconBlockHeader,
block_roots: []primitives.Root,
state_roots: []primitives.Root,
historical_roots: []primitives.Root,
eth1_data: ?*consensus.Eth1Data,
eth1_data_votes: []*consensus.Eth1Data,
eth1_deposit_index: u64,
validators: []*consensus.Validator,
balances: []primitives.Gwei,
randao_mixes: []primitives.Bytes32,
slashings: []primitives.Gwei,
previous_epoch_attestations: []*consensus.PendingAttestation,
current_epoch_attestations: []*consensus.PendingAttestation,
justification_bits: []bool,
previous_justified_checkpoint: *consensus.Checkpoint,
current_justified_checkpoint: *consensus.Checkpoint,
finalized_checkpoint: *consensus.Checkpoint,
inactivity_scores: []u64,
current_sync_committee: ?*consensus.SyncCommittee,
next_sync_committee: ?*consensus.SyncCommittee,
latest_execution_payload_header: ?*ExecutionPayloadHeader,
};

test "test ExecutionPayloadHeader" {
const header = ExecutionPayloadHeader{
.parent_hash = undefined,
Expand Down Expand Up @@ -81,3 +124,52 @@ test "test ExecutionPayload" {

try std.testing.expectEqual(payload.block_number, 21);
}

test "test BeaconBlockBody" {
const body = BeaconBlockBody{
.randao_reveal = undefined,
.eth1_data = undefined,
.graffiti = undefined,
.proposer_slashings = undefined,
.attester_slashings = undefined,
.attestations = undefined,
.deposits = undefined,
.voluntary_exits = undefined,
.sync_aggregate = undefined,
.execution_payload = undefined,
};

try std.testing.expectEqual(body.randao_reveal.len, 96);
}

test "test BeaconState" {
const state = BeaconState{
.genesis_time = 0,
.genesis_validators_root = undefined,
.slot = 0,
.fork = undefined,
.latest_block_header = undefined,
.block_roots = undefined,
.state_roots = undefined,
.historical_roots = undefined,
.eth1_data = undefined,
.eth1_data_votes = undefined,
.eth1_deposit_index = 0,
.validators = undefined,
.balances = undefined,
.randao_mixes = undefined,
.slashings = undefined,
.previous_epoch_attestations = undefined,
.current_epoch_attestations = undefined,
.justification_bits = undefined,
.previous_justified_checkpoint = undefined,
.current_justified_checkpoint = undefined,
.finalized_checkpoint = undefined,
.inactivity_scores = undefined,
.current_sync_committee = undefined,
.next_sync_committee = undefined,
.latest_execution_payload_header = undefined,
};

try std.testing.expectEqual(state.genesis_time, 0);
}
99 changes: 99 additions & 0 deletions src/consensus/capella/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,52 @@ pub const ExecutionPayload = struct {
withdrawals: []consensus.Withdrawal, // with a maximum length of MAX_WITHDRAWALS_PER_PAYLOAD
};

pub const BeaconBlockBody = struct {
randao_reveal: primitives.BLSSignature,
eth1_data: *consensus.Eth1Data, // Eth1 data vote
graffiti: primitives.Bytes32, // Arbitrary data
// Operations
proposer_slashings: []*consensus.ProposerSlashing,
attester_slashings: []*consensus.AttesterSlashing,
attestations: []*consensus.Attestation,
deposits: []*consensus.Deposit,
voluntary_exits: []*consensus.SignedVoluntaryExit,
sync_aggregate: ?*consensus.SyncAggregate,
execution_payload: ?*ExecutionPayload,
bls_to_execution_changes: []*consensus.SignedBLSToExecutionChange,
};

const BeaconState = struct {
genesis_time: u64,
genesis_validators_root: primitives.Root,
slot: primitives.Slot,
fork: *consensus.Fork,
latest_block_header: consensus.BeaconBlockHeader,
block_roots: []primitives.Root,
state_roots: []primitives.Root,
historical_roots: []primitives.Root,
eth1_data: ?*consensus.Eth1Data,
eth1_data_votes: []*consensus.Eth1Data,
eth1_deposit_index: u64,
validators: []*consensus.Validator,
balances: []primitives.Gwei,
randao_mixes: []primitives.Bytes32,
slashings: []primitives.Gwei,
previous_epoch_attestations: []*consensus.PendingAttestation,
current_epoch_attestations: []*consensus.PendingAttestation,
justification_bits: []bool,
previous_justified_checkpoint: *consensus.Checkpoint,
current_justified_checkpoint: *consensus.Checkpoint,
finalized_checkpoint: *consensus.Checkpoint,
inactivity_scores: []u64,
current_sync_committee: ?*consensus.SyncCommittee,
next_sync_committee: ?*consensus.SyncCommittee,
latest_execution_payload_header: ?*ExecutionPayloadHeader,
next_withdrawal_index: primitives.WithdrawalIndex,
next_withdrawal_validator_index: primitives.ValidatorIndex,
historical_summaries: []*consensus.HistoricalSummary,
};

test "test ExecutionPayloadHeader" {
const header = ExecutionPayloadHeader{
.parent_hash = undefined,
Expand Down Expand Up @@ -87,3 +133,56 @@ test "test ExecutionPayload" {
try std.testing.expectEqual(payload.parent_hash.len, 32);
try std.testing.expectEqual(payload.block_number, 21);
}

test "test BeaconBlockBody" {
const body = BeaconBlockBody{
.randao_reveal = undefined,
.eth1_data = undefined,
.graffiti = undefined,
.proposer_slashings = undefined,
.attester_slashings = undefined,
.attestations = undefined,
.deposits = undefined,
.voluntary_exits = undefined,
.sync_aggregate = undefined,
.execution_payload = undefined,
.bls_to_execution_changes = undefined,
};

try std.testing.expectEqual(body.randao_reveal.len, 96);
}

test "test BeaconState" {
const state = BeaconState{
.genesis_time = 0,
.genesis_validators_root = undefined,
.slot = 0,
.fork = undefined,
.latest_block_header = undefined,
.block_roots = undefined,
.state_roots = undefined,
.historical_roots = undefined,
.eth1_data = undefined,
.eth1_data_votes = undefined,
.eth1_deposit_index = 0,
.validators = undefined,
.balances = undefined,
.randao_mixes = undefined,
.slashings = undefined,
.previous_epoch_attestations = undefined,
.current_epoch_attestations = undefined,
.justification_bits = undefined,
.previous_justified_checkpoint = undefined,
.current_justified_checkpoint = undefined,
.finalized_checkpoint = undefined,
.inactivity_scores = undefined,
.current_sync_committee = undefined,
.next_sync_committee = undefined,
.latest_execution_payload_header = undefined,
.next_withdrawal_index = 0,
.next_withdrawal_validator_index = 0,
.historical_summaries = undefined,
};

try std.testing.expectEqual(state.genesis_time, 0);
}
Loading

0 comments on commit 1dba948

Please sign in to comment.