Skip to content

Commit

Permalink
feat: add more validator helper method
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Oct 5, 2024
1 parent 56ebf9a commit 3c100e7
Show file tree
Hide file tree
Showing 11 changed files with 824 additions and 341 deletions.
11 changes: 11 additions & 0 deletions src/configs/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ pub const ActiveConfig = struct {
}
return config;
}

pub fn reset() void {
if (!is_initialized.load(.acquire)) return;

is_initialized.store(false, .release);

mutex.lock();
defer mutex.unlock();
config = undefined;
}
};

pub const Config = struct {
Expand Down Expand Up @@ -269,6 +279,7 @@ test "minimal config has correct MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA" {

test "test ActiveConfig" {
ActiveConfig.set(preset.Presets.mainnet);
defer ActiveConfig.reset();
const active_config = ActiveConfig.get();
try std.testing.expectEqual(active_config.PRESET_BASE, "mainnet");
}
51 changes: 48 additions & 3 deletions src/consensus/altair/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ pub const BeaconBlockBody = @Type(
},
);

pub const BeaconState = @Type(
pub const BeaconStateSSZ = @Type(
.{
.@"struct" = .{
.layout = .auto,
.fields = @typeInfo(phase0.BeaconState).@"struct".fields ++ &[_]std.builtin.Type.StructField{
.fields = @typeInfo(phase0.BeaconStateSSZ).@"struct".fields ++ &[_]std.builtin.Type.StructField{
.{
.name = "inactivity_scores",
.type = []u64,
Expand Down Expand Up @@ -131,8 +131,53 @@ pub const BeaconState = @Type(
},
);

pub const BeaconState = struct {
beacon_state_ssz: BeaconStateSSZ,
allocator: std.mem.Allocator,

pub fn init(allocator: std.mem.Allocator, beacon_state_ssz: BeaconStateSSZ) !BeaconState {
return BeaconState{
.beacon_state_ssz = beacon_state_ssz,
.allocator = allocator,
};
}

pub fn deinit(self: *BeaconState) void {
self.allocator.free(self.beacon_state_ssz.validators);
self.allocator.free(self.beacon_state_ssz.balances);
self.allocator.free(self.beacon_state_ssz.randao_mixes);
self.allocator.free(self.beacon_state_ssz.slashings);
self.allocator.free(self.beacon_state_ssz.previous_epoch_attestations);
self.allocator.free(self.beacon_state_ssz.current_epoch_attestations);
self.allocator.free(self.beacon_state_ssz.justification_bits);
self.allocator.destroy(self.beacon_state_ssz.previous_justified_checkpoint);
self.allocator.destroy(self.beacon_state_ssz.current_justified_checkpoint);
if (self.beacon_state_ssz.finalized_checkpoint) |checkpoint| {
self.allocator.destroy(checkpoint);
}
self.allocator.destroy(self.beacon_state_ssz.fork);
if (self.beacon_state_ssz.latest_block_header) |latest_block_header| {
self.allocator.destroy(latest_block_header);
}
self.allocator.free(self.beacon_state_ssz.block_roots);
self.allocator.free(self.beacon_state_ssz.state_roots);
self.allocator.free(self.beacon_state_ssz.historical_roots);
if (self.beacon_state_ssz.eth1_data) |eth1_data| {
self.allocator.destroy(eth1_data);
}
self.allocator.free(self.beacon_state_ssz.eth1_data_votes);
self.allocator.free(self.beacon_state_ssz.inactivity_scores);
if (self.beacon_state_ssz.current_sync_committee) |current_sync_committee| {
self.allocator.destroy(current_sync_committee);
}
if (self.beacon_state_ssz.next_sync_committee) |next_sync_committee| {
self.allocator.destroy(next_sync_committee);
}
}
};

test "test BeaconState" {
const state = BeaconState{
const state = BeaconStateSSZ{
.genesis_time = 0,
.genesis_validators_root = undefined,
.slot = 0,
Expand Down
54 changes: 51 additions & 3 deletions src/consensus/bellatrix/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ pub const BeaconBlockBody = @Type(
},
);

pub const BeaconState = @Type(
pub const BeaconStateSSZ = @Type(
.{
.@"struct" = .{
.layout = .auto,
.fields = @typeInfo(altair.BeaconState).@"struct".fields ++ &[_]std.builtin.Type.StructField{
.fields = @typeInfo(altair.BeaconStateSSZ).@"struct".fields ++ &[_]std.builtin.Type.StructField{
.{
.name = "latest_execution_payload_header",
.type = ?*consensus.ExecutionPayloadHeader,
Expand All @@ -85,6 +85,54 @@ pub const BeaconState = @Type(
},
);

pub const BeaconState = struct {
beacon_state_ssz: BeaconStateSSZ,
allocator: std.mem.Allocator,

pub fn init(allocator: std.mem.Allocator, beacon_state_ssz: BeaconStateSSZ) !BeaconState {
return BeaconState{
.beacon_state_ssz = beacon_state_ssz,
.allocator = allocator,
};
}

pub fn deinit(self: *BeaconState) void {
self.allocator.free(self.beacon_state_ssz.validators);
self.allocator.free(self.beacon_state_ssz.balances);
self.allocator.free(self.beacon_state_ssz.randao_mixes);
self.allocator.free(self.beacon_state_ssz.slashings);
self.allocator.free(self.beacon_state_ssz.previous_epoch_attestations);
self.allocator.free(self.beacon_state_ssz.current_epoch_attestations);
self.allocator.free(self.beacon_state_ssz.justification_bits);
self.allocator.destroy(self.beacon_state_ssz.previous_justified_checkpoint);
self.allocator.destroy(self.beacon_state_ssz.current_justified_checkpoint);
if (self.beacon_state_ssz.finalized_checkpoint) |checkpoint| {
self.allocator.destroy(checkpoint);
}
self.allocator.destroy(self.beacon_state_ssz.fork);
if (self.beacon_state_ssz.latest_block_header) |latest_block_header| {
self.allocator.destroy(latest_block_header);
}
self.allocator.free(self.beacon_state_ssz.block_roots);
self.allocator.free(self.beacon_state_ssz.state_roots);
self.allocator.free(self.beacon_state_ssz.historical_roots);
if (self.beacon_state_ssz.eth1_data) |eth1_data| {
self.allocator.destroy(eth1_data);
}
self.allocator.free(self.beacon_state_ssz.eth1_data_votes);
self.allocator.free(self.beacon_state_ssz.inactivity_scores);
if (self.beacon_state_ssz.current_sync_committee) |current_sync_committee| {
self.allocator.destroy(current_sync_committee);
}
if (self.beacon_state_ssz.next_sync_committee) |next_sync_committee| {
self.allocator.destroy(next_sync_committee);
}
if (self.beacon_state_ssz.latest_execution_payload_header) |latest_execution_payload_header| {
self.allocator.destroy(latest_execution_payload_header);
}
}
};

test "test ExecutionPayloadHeader" {
const header = ExecutionPayloadHeader{
.parent_hash = undefined,
Expand Down Expand Up @@ -146,7 +194,7 @@ test "test BeaconBlockBody" {
}

test "test BeaconState" {
const state = BeaconState{
const state = BeaconStateSSZ{
.genesis_time = 0,
.genesis_validators_root = undefined,
.slot = 0,
Expand Down
59 changes: 54 additions & 5 deletions src/consensus/capella/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,22 @@ pub const BeaconBlockBody = @Type(
},
);

pub const BeaconState = @Type(
pub const BeaconStateSSZ = @Type(
.{
.@"struct" = .{
.layout = .auto,
.fields = @typeInfo(bellatrix.BeaconState).@"struct".fields ++ &[_]std.builtin.Type.StructField{
.fields = @typeInfo(bellatrix.BeaconStateSSZ).@"struct".fields ++ &[_]std.builtin.Type.StructField{
.{
.name = "next_withdrawal_index",
.type = primitives.WithdrawalIndex,
.default_value = @ptrCast(&@as(primitives.WithdrawalIndex, 0)),
.default_value = &@as(primitives.WithdrawalIndex, 0),
.is_comptime = false,
.alignment = @alignOf(primitives.WithdrawalIndex),
},
.{
.name = "next_withdrawal_validator_index",
.type = primitives.ValidatorIndex,
.default_value = @ptrCast(&@as(primitives.ValidatorIndex, 0)),
.default_value = &@as(primitives.ValidatorIndex, 0),
.is_comptime = false,
.alignment = @alignOf(primitives.ValidatorIndex),
},
Expand All @@ -142,6 +142,55 @@ pub const BeaconState = @Type(
},
);

pub const BeaconState = struct {
beacon_state_ssz: BeaconStateSSZ,
allocator: std.mem.Allocator,

pub fn init(allocator: std.mem.Allocator, beacon_state_ssz: BeaconStateSSZ) !BeaconState {
return BeaconState{
.beacon_state_ssz = beacon_state_ssz,
.allocator = allocator,
};
}

pub fn deinit(self: *BeaconState) void {
self.allocator.free(self.beacon_state_ssz.validators);
self.allocator.free(self.beacon_state_ssz.balances);
self.allocator.free(self.beacon_state_ssz.randao_mixes);
self.allocator.free(self.beacon_state_ssz.slashings);
self.allocator.free(self.beacon_state_ssz.previous_epoch_attestations);
self.allocator.free(self.beacon_state_ssz.current_epoch_attestations);
self.allocator.free(self.beacon_state_ssz.justification_bits);
self.allocator.destroy(self.beacon_state_ssz.previous_justified_checkpoint);
self.allocator.destroy(self.beacon_state_ssz.current_justified_checkpoint);
if (self.beacon_state_ssz.finalized_checkpoint) |checkpoint| {
self.allocator.destroy(checkpoint);
}
self.allocator.destroy(self.beacon_state_ssz.fork);
if (self.beacon_state_ssz.latest_block_header) |latest_block_header| {
self.allocator.destroy(latest_block_header);
}
self.allocator.free(self.beacon_state_ssz.block_roots);
self.allocator.free(self.beacon_state_ssz.state_roots);
self.allocator.free(self.beacon_state_ssz.historical_roots);
if (self.beacon_state_ssz.eth1_data) |eth1_data| {
self.allocator.destroy(eth1_data);
}
self.allocator.free(self.beacon_state_ssz.eth1_data_votes);
self.allocator.free(self.beacon_state_ssz.inactivity_scores);
if (self.beacon_state_ssz.current_sync_committee) |current_sync_committee| {
self.allocator.destroy(current_sync_committee);
}
if (self.beacon_state_ssz.next_sync_committee) |next_sync_committee| {
self.allocator.destroy(next_sync_committee);
}
if (self.beacon_state_ssz.latest_execution_payload_header) |latest_execution_payload_header| {
self.allocator.destroy(latest_execution_payload_header);
}
self.allocator.free(self.beacon_state_ssz.historical_summaries);
}
};

test "test ExecutionPayloadHeader" {
const header = ExecutionPayloadHeader{
.parent_hash = undefined,
Expand Down Expand Up @@ -207,7 +256,7 @@ test "test BeaconBlockBody" {
}

test "test BeaconState" {
const state = BeaconState{
const state = BeaconStateSSZ{
.genesis_time = 0,
.genesis_validators_root = undefined,
.slot = 0,
Expand Down
68 changes: 60 additions & 8 deletions src/consensus/electra/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -155,50 +155,50 @@ test "test ExecutionPayloadHeader" {
try std.testing.expectEqual(header.block_number, 21);
}

pub const BeaconState = @Type(
pub const BeaconStateSSZ = @Type(
.{
.@"struct" = .{
.layout = .auto,
.fields = @typeInfo(capella.BeaconState).@"struct".fields ++ &[_]std.builtin.Type.StructField{
.fields = @typeInfo(capella.BeaconStateSSZ).@"struct".fields ++ &[_]std.builtin.Type.StructField{
.{
.name = "deposit_requests_start_index", // # [New in Electra:EIP6110]
.type = u64,
.default_value = @ptrCast(&@as(u64, 0)),
.default_value = &@as(u64, 0),
.is_comptime = false,
.alignment = @alignOf(u64),
},
.{
.name = "deposit_balance_to_consume", // # [New in Electra:EIP7251]
.type = primitives.Gwei,
.default_value = @ptrCast(&@as(primitives.Gwei, 0)),
.default_value = &@as(primitives.Gwei, 0),
.is_comptime = false,
.alignment = @alignOf(primitives.Gwei),
},
.{
.name = "exit_balance_to_consume", // # [New in Electra:EIP7251]
.type = primitives.Gwei,
.default_value = @ptrCast(&@as(primitives.Gwei, 0)),
.default_value = &@as(primitives.Gwei, 0),
.is_comptime = false,
.alignment = @alignOf(primitives.Gwei),
},
.{
.name = "earliest_exit_epoch", // # [New in Electra:EIP7251]
.type = primitives.Epoch,
.default_value = @ptrCast(&@as(primitives.Epoch, 0)),
.default_value = &@as(primitives.Epoch, 0),
.is_comptime = false,
.alignment = @alignOf(primitives.Epoch),
},
.{
.name = "consolidation_balance_to_consume", // # [New in Electra:EIP7251]
.type = primitives.Gwei,
.default_value = @ptrCast(&@as(primitives.Gwei, 0)),
.default_value = &@as(primitives.Gwei, 0),
.is_comptime = false,
.alignment = @alignOf(primitives.Gwei),
},
.{
.name = "earliest_consolidation_epoch", // # [New in Electra:EIP7251]
.type = primitives.Epoch,
.default_value = @ptrCast(&@as(primitives.Epoch, 0)),
.default_value = &@as(primitives.Epoch, 0),
.is_comptime = false,
.alignment = @alignOf(primitives.Epoch),
},
Expand Down Expand Up @@ -230,6 +230,58 @@ pub const BeaconState = @Type(
},
);

pub const BeaconState = struct {
beacon_state_ssz: BeaconStateSSZ,
allocator: std.mem.Allocator,

pub fn init(allocator: std.mem.Allocator, beacon_state_ssz: BeaconStateSSZ) !BeaconState {
return BeaconState{
.beacon_state_ssz = beacon_state_ssz,
.allocator = allocator,
};
}

pub fn deinit(self: *BeaconState) void {
self.allocator.free(self.beacon_state_ssz.validators);
self.allocator.free(self.beacon_state_ssz.balances);
self.allocator.free(self.beacon_state_ssz.randao_mixes);
self.allocator.free(self.beacon_state_ssz.slashings);
self.allocator.free(self.beacon_state_ssz.previous_epoch_attestations);
self.allocator.free(self.beacon_state_ssz.current_epoch_attestations);
self.allocator.free(self.beacon_state_ssz.justification_bits);
self.allocator.destroy(self.beacon_state_ssz.previous_justified_checkpoint);
self.allocator.destroy(self.beacon_state_ssz.current_justified_checkpoint);
if (self.beacon_state_ssz.finalized_checkpoint) |checkpoint| {
self.allocator.destroy(checkpoint);
}
self.allocator.destroy(self.beacon_state_ssz.fork);
if (self.beacon_state_ssz.latest_block_header) |latest_block_header| {
self.allocator.destroy(latest_block_header);
}
self.allocator.free(self.beacon_state_ssz.block_roots);
self.allocator.free(self.beacon_state_ssz.state_roots);
self.allocator.free(self.beacon_state_ssz.historical_roots);
if (self.beacon_state_ssz.eth1_data) |eth1_data| {
self.allocator.destroy(eth1_data);
}
self.allocator.free(self.beacon_state_ssz.eth1_data_votes);
self.allocator.free(self.beacon_state_ssz.inactivity_scores);
if (self.beacon_state_ssz.current_sync_committee) |current_sync_committee| {
self.allocator.destroy(current_sync_committee);
}
if (self.beacon_state_ssz.next_sync_committee) |next_sync_committee| {
self.allocator.destroy(next_sync_committee);
}
if (self.beacon_state_ssz.latest_execution_payload_header) |latest_execution_payload_header| {
self.allocator.destroy(latest_execution_payload_header);
}
self.allocator.free(self.beacon_state_ssz.historical_summaries);
self.allocator.free(self.beacon_state_ssz.pending_balance_deposits);
self.allocator.free(self.beacon_state_ssz.pending_partial_withdrawals);
self.allocator.free(self.beacon_state_ssz.pending_consolidations);
}
};

test "test Attestation" {
const attestation = Attestation{
.aggregation_bits = &[_]bool{},
Expand Down
Loading

0 comments on commit 3c100e7

Please sign in to comment.