Skip to content

Commit

Permalink
feat:add more spec type
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Oct 3, 2024
1 parent 1dba948 commit 4770fb8
Show file tree
Hide file tree
Showing 7 changed files with 1,301 additions and 308 deletions.
276 changes: 246 additions & 30 deletions src/consensus/altair/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,95 @@ const primitives = @import("../../primitives/types.zig");
const preset = @import("../../presets/preset.zig");
const consensus = @import("../../consensus/types.zig");

pub const LightClientHeader = struct {
beacon: ?*consensus.BeaconBlockHeader,
};

pub const LightClientOptimisticUpdate = struct {
attested_header: ?*consensus.LightClientHeader,
sync_aggregate: ?*consensus.SyncAggregate,
signature_slot: primitives.Slot,
};

pub const LightClientFinalityUpdate = struct {
attested_header: ?*consensus.LightClientHeader,
finalized_header: ?*consensus.LightClientHeader,
finality_branch: primitives.FinalityBranch,
sync_aggregate: ?*consensus.SyncAggregate,
signature_slot: primitives.Slot,
};

pub const LightClientUpdate = struct {
attested_header: ?*consensus.LightClientHeader,
next_sync_committee: ?*consensus.SyncCommittee,
next_sync_committee_branch: primitives.NextSyncCommitteeBranch,
finalized_header: ?*consensus.LightClientHeader,
finality_branch: primitives.FinalityBranch,
sync_aggregate: ?*consensus.SyncAggregate,
signature_slot: primitives.Slot,
};

pub const LightClientBootstrap = struct {
header: ?*consensus.LightClientHeader,
current_sync_committee: ?*consensus.SyncCommittee,
current_sync_committee_branch: primitives.CurrentSyncCommitteeBranch,
};

pub const SignedVoluntaryExit = struct {
message: ?*consensus.VoluntaryExit,
signature: primitives.BLSSignature,
};

pub const SyncAggregate = struct {
sync_committee_bits: []bool,
sync_committee_signature: primitives.BLSSignature,
};

pub const SyncCommittee = struct {
pubkeys: []primitives.BLSPubkey,
aggregate_pubkey: primitives.BLSPubkey,
};

pub const SyncCommitteeMessage = struct {
slot: primitives.Slot,
beacon_block_root: primitives.Root,
validator_index: primitives.ValidatorIndex,
signature: primitives.BLSSignature,
};

pub const SyncCommitteeContribution = struct {
slot: primitives.Slot,
beacon_block_root: primitives.Root,
subcommittee_index: u64,
aggregation_bits: []bool,
signature: primitives.BLSSignature,
};

pub const ContributionAndProof = struct {
aggregator_index: primitives.ValidatorIndex,
aggregate: ?*consensus.SyncCommitteeContribution,
selection_proof: primitives.BLSSignature,
};

pub const SignedContributionAndProof = struct {
message: ?*consensus.ContributionAndProof,
signature: primitives.BLSSignature,
};

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,
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 {
pub const BeaconState = struct {
genesis_time: u64,
genesis_validators_root: primitives.Root,
slot: primitives.Slot,
Expand All @@ -34,14 +101,14 @@ const BeaconState = struct {
state_roots: []primitives.Root,
historical_roots: []primitives.Root,
eth1_data: ?*consensus.Eth1Data,
eth1_data_votes: []*consensus.Eth1Data,
eth1_data_votes: []consensus.Eth1Data,
eth1_deposit_index: u64,
validators: []*consensus.Validator,
validators: []consensus.Validator,
balances: []primitives.Gwei,
randao_mixes: []primitives.Bytes32,
slashings: []primitives.Gwei,
previous_epoch_attestations: []*consensus.PendingAttestation,
current_epoch_attestations: []*consensus.PendingAttestation,
previous_epoch_attestations: []consensus.PendingAttestation,
current_epoch_attestations: []consensus.PendingAttestation,
justification_bits: []bool,
previous_justified_checkpoint: *consensus.Checkpoint,
current_justified_checkpoint: *consensus.Checkpoint,
Expand All @@ -51,18 +118,6 @@ const BeaconState = struct {
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,
Expand Down Expand Up @@ -109,3 +164,164 @@ test "test BeaconBlockBody" {

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

test "test SignedVoluntaryExit" {
const exit = SignedVoluntaryExit{
.message = null,
.signature = undefined,
};

try std.testing.expectEqual(exit.message, null);
}

test "test SyncAggregate" {
const aggregate = SyncAggregate{
.sync_committee_bits = &[_]bool{},
.sync_committee_signature = undefined,
};

try std.testing.expectEqual(aggregate.sync_committee_bits.len, 0);
}

test "test SyncCommittee" {
const committee = SyncCommittee{
.pubkeys = &[_]primitives.BLSPubkey{},
.aggregate_pubkey = undefined,
};

try std.testing.expectEqual(committee.pubkeys.len, 0);
}

test "test SyncCommitteeMessage" {
const message = SyncCommitteeMessage{
.slot = 0,
.beacon_block_root = undefined,
.validator_index = 0,
.signature = undefined,
};

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

test "test SyncCommitteeContribution" {
const contribution = SyncCommitteeContribution{
.slot = 0,
.beacon_block_root = undefined,
.subcommittee_index = 0,
.aggregation_bits = &[_]bool{},
.signature = undefined,
};

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

test "test ContributionAndProof" {
const contribution = ContributionAndProof{
.aggregator_index = 0,
.aggregate = null,
.selection_proof = undefined,
};

try std.testing.expectEqual(contribution.aggregator_index, 0);
}

test "test SignedContributionAndProof" {
const contribution = SignedContributionAndProof{
.message = null,
.signature = undefined,
};

try std.testing.expectEqual(contribution.message, null);
}

test "test LightClientHeader" {
const header = LightClientHeader{
.beacon = null,
};

try std.testing.expectEqual(header.beacon, null);
}

test "test LightClientOptimisticUpdate" {
const update = LightClientOptimisticUpdate{
.attested_header = null,
.sync_aggregate = null,
.signature_slot = 0,
};

try std.testing.expectEqual(update.attested_header, null);
try std.testing.expectEqual(update.sync_aggregate, null);
try std.testing.expectEqual(update.signature_slot, 0);
}

test "test LightClientFinalityUpdate" {
const finality_branch = primitives.FinalityBranch{
.altair = [_][32]u8{
[_]u8{0} ** 32,
} ** 6,
};

const update = LightClientFinalityUpdate{
.attested_header = null,
.finalized_header = null,
.finality_branch = finality_branch,
.sync_aggregate = null,
.signature_slot = 0,
};

try std.testing.expectEqual(update.attested_header, null);
try std.testing.expectEqual(update.finalized_header, null);
try std.testing.expect(update.finality_branch.altair.len == 6);
try std.testing.expectEqual(update.sync_aggregate, null);
try std.testing.expectEqual(update.signature_slot, 0);
}

test "test LightClientUpdate" {
const next_sync_committee_branch = primitives.NextSyncCommitteeBranch{
.altair = [_][32]u8{
[_]u8{0} ** 32,
} ** 5,
};

const finality_branch = primitives.FinalityBranch{
.altair = [_][32]u8{
[_]u8{0} ** 32,
} ** 6,
};

const update = LightClientUpdate{
.attested_header = null,
.next_sync_committee = null,
.next_sync_committee_branch = next_sync_committee_branch,
.finalized_header = null,
.finality_branch = finality_branch,
.sync_aggregate = null,
.signature_slot = 0,
};

try std.testing.expectEqual(update.attested_header, null);
try std.testing.expectEqual(update.next_sync_committee, null);
try std.testing.expect(update.next_sync_committee_branch.altair.len == 5);
try std.testing.expectEqual(update.finalized_header, null);
try std.testing.expect(update.finality_branch.altair.len == 6);
try std.testing.expectEqual(update.sync_aggregate, null);
try std.testing.expectEqual(update.signature_slot, 0);
}

test "test LightClientBootstrap" {
const current_sync_committee_branch = primitives.CurrentSyncCommitteeBranch{
.altair = [_][32]u8{
[_]u8{0} ** 32,
} ** 5,
};

const bootstrap = LightClientBootstrap{
.header = null,
.current_sync_committee = null,
.current_sync_committee_branch = current_sync_committee_branch,
};

try std.testing.expectEqual(bootstrap.header, null);
try std.testing.expectEqual(bootstrap.current_sync_committee, null);
try std.testing.expect(bootstrap.current_sync_committee_branch.altair.len == 5);
}
41 changes: 29 additions & 12 deletions src/consensus/bellatrix/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ pub const primitives = @import("../../primitives/types.zig");
const preset = @import("../../presets/preset.zig");
const consensus = @import("../../consensus/types.zig");

pub const PowBlock = struct {
block_hash: primitives.Hash32,
parent_hash: primitives.Hash32,
total_difficulty: u256,
};

pub const ExecutionPayloadHeader = struct {
parent_hash: primitives.Hash32,
fee_recipient: primitives.ExecutionAddress,
Expand Down Expand Up @@ -45,16 +51,16 @@ pub const BeaconBlockBody = struct {
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,
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,
execution_payload: ?*consensus.ExecutionPayload,
};

const BeaconState = struct {
pub const BeaconState = struct {
genesis_time: u64,
genesis_validators_root: primitives.Root,
slot: primitives.Slot,
Expand All @@ -64,22 +70,22 @@ const BeaconState = struct {
state_roots: []primitives.Root,
historical_roots: []primitives.Root,
eth1_data: ?*consensus.Eth1Data,
eth1_data_votes: []*consensus.Eth1Data,
eth1_data_votes: []consensus.Eth1Data,
eth1_deposit_index: u64,
validators: []*consensus.Validator,
validators: []consensus.Validator,
balances: []primitives.Gwei,
randao_mixes: []primitives.Bytes32,
slashings: []primitives.Gwei,
previous_epoch_attestations: []*consensus.PendingAttestation,
current_epoch_attestations: []*consensus.PendingAttestation,
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,
latest_execution_payload_header: ?*consensus.ExecutionPayloadHeader,
};

test "test ExecutionPayloadHeader" {
Expand Down Expand Up @@ -173,3 +179,14 @@ test "test BeaconState" {

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

test "test PowBlock" {
const block = PowBlock{
.block_hash = undefined,
.parent_hash = undefined,
.total_difficulty = 0,
};

try std.testing.expectEqual(block.block_hash.len, 32);
try std.testing.expectEqual(block.total_difficulty, 0);
}
Loading

0 comments on commit 4770fb8

Please sign in to comment.