From 1dba948abdf12f296c49eeb0f08ca74998ff1a78 Mon Sep 17 00:00:00 2001 From: Chen Kai <281165273grape@gmail.com> Date: Tue, 1 Oct 2024 23:20:48 +0800 Subject: [PATCH] feat:add more types Signed-off-by: Chen Kai <281165273grape@gmail.com> --- src/consensus/altair/types.zig | 111 ++++++++++++++++++++++++++++++ src/consensus/bellatrix/types.zig | 92 +++++++++++++++++++++++++ src/consensus/capella/types.zig | 99 ++++++++++++++++++++++++++ src/consensus/deneb/types.zig | 101 +++++++++++++++++++++++++++ src/consensus/phase0/types.zig | 103 +++++++++++++++++++++++++++ src/main.zig | 24 ------- 6 files changed, 506 insertions(+), 24 deletions(-) diff --git a/src/consensus/altair/types.zig b/src/consensus/altair/types.zig index e69de29..47951ff 100644 --- a/src/consensus/altair/types.zig +++ b/src/consensus/altair/types.zig @@ -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); +} diff --git a/src/consensus/bellatrix/types.zig b/src/consensus/bellatrix/types.zig index 37e2784..dd329b7 100644 --- a/src/consensus/bellatrix/types.zig +++ b/src/consensus/bellatrix/types.zig @@ -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, @@ -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, @@ -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); +} diff --git a/src/consensus/capella/types.zig b/src/consensus/capella/types.zig index e9de4bc..ccc6706 100644 --- a/src/consensus/capella/types.zig +++ b/src/consensus/capella/types.zig @@ -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, @@ -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); +} diff --git a/src/consensus/deneb/types.zig b/src/consensus/deneb/types.zig index e68e8e5..7338c13 100644 --- a/src/consensus/deneb/types.zig +++ b/src/consensus/deneb/types.zig @@ -46,6 +46,53 @@ pub const ExecutionPayload = struct { excess_blob_gas: u64, }; +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, + blob_kzg_commitments: []primitives.KZGCommitment, +}; + +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, @@ -95,3 +142,57 @@ 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, + .blob_kzg_commitments = 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_validators_root.len, 32); + try std.testing.expectEqual(state.genesis_time, 0); +} diff --git a/src/consensus/phase0/types.zig b/src/consensus/phase0/types.zig index e69de29..58a6801 100644 --- a/src/consensus/phase0/types.zig +++ b/src/consensus/phase0/types.zig @@ -0,0 +1,103 @@ +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, +}; + +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, +}; + +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, + }; + + 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, + }; + + try std.testing.expectEqual(body.randao_reveal.len, 96); +} diff --git a/src/main.zig b/src/main.zig index c3a44e3..c42ce8e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -19,28 +19,4 @@ pub fn main() !void { // print mainnet preset try stdout.print("{}\n", .{preset.mainnet_preset}); try bw.flush(); // don't forget to flush! - - const a = types.HistoricalBatchMainnet{ - .block_roots = undefined, - .state_roots = undefined, - }; - - const b = bellatrix.ExecutionPayloadHeaderMainnet{ - .parent_hash = undefined, - .fee_recipient = undefined, - .state_root = undefined, - .receipts_root = undefined, - .logs_bloom = undefined, - .prev_randao = undefined, - .block_number = 21, - .gas_used = 0, - .gas_limit = 0, - .timestamp = 0, - .extra_data = undefined, - .base_fee_per_gas = 0, - .block_hash = undefined, - .transactions_root = undefined, - }; - try stdout.print("{}\n", .{a}); - try stdout.print("{}\n", .{b}); }