From e7fbf03c51168462f07dd309979c492a4ed33656 Mon Sep 17 00:00:00 2001 From: Chen Kai <281165273grape@gmail.com> Date: Sun, 3 Nov 2024 21:55:50 +0800 Subject: [PATCH] feat:use ptr Signed-off-by: Chen Kai <281165273grape@gmail.com> --- src/consensus/helpers/domain.zig | 20 ++++++++++---------- src/consensus/helpers/genesis.zig | 18 +++++++++--------- src/consensus/helpers/voluntary_exit.zig | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/consensus/helpers/domain.zig b/src/consensus/helpers/domain.zig index 26e9f17..9f760e7 100644 --- a/src/consensus/helpers/domain.zig +++ b/src/consensus/helpers/domain.zig @@ -23,14 +23,14 @@ const ssz = @import("../../ssz/ssz.zig"); /// current_version=current_version, /// genesis_validators_root=genesis_validators_root, /// )) -pub fn computeForkDataRoot(current_version: primitives.Version, genesis_validators_root: primitives.Root, allocator: std.mem.Allocator) !primitives.Root { +pub fn computeForkDataRoot(current_version: primitives.Version, genesis_validators_root: *const primitives.Root, allocator: std.mem.Allocator) !primitives.Root { const fork_data = consensus.ForkData{ .current_version = current_version, - .genesis_validators_root = genesis_validators_root, + .genesis_validators_root = genesis_validators_root.*, }; var out: primitives.Root = undefined; - try ssz.hashTreeRoot(fork_data, &out, allocator); + try ssz.hashTreeRoot(&fork_data, &out, allocator); return out; } @@ -49,7 +49,7 @@ pub fn computeForkDataRoot(current_version: primitives.Version, genesis_validato /// 4-bytes suffices for practical separation of forks/chains. /// """ /// return ForkDigest(compute_fork_data_root(current_version, genesis_validators_root)[:4]) -pub fn computeForkDigest(currentVersion: primitives.Version, genesisValidatorsRoot: primitives.Root, allocator: std.mem.Allocator) !primitives.ForkDigest { +pub fn computeForkDigest(currentVersion: primitives.Version, genesisValidatorsRoot: *const primitives.Root, allocator: std.mem.Allocator) !primitives.ForkDigest { const forkDataRoot = try computeForkDataRoot(currentVersion, genesisValidatorsRoot, allocator); return forkDataRoot[0..4].*; } @@ -71,12 +71,12 @@ pub fn computeForkDigest(currentVersion: primitives.Version, genesisValidatorsRo /// genesis_validators_root = Root() # all bytes zero by default /// fork_data_root = compute_fork_data_root(fork_version, genesis_validators_root) /// return Domain(domain_type + fork_data_root[:28]) -pub fn computeDomain(domain_type: primitives.DomainType, fork_version: ?primitives.Version, genesis_validators_root: ?primitives.Root, allocator: std.mem.Allocator) !primitives.Domain { +pub fn computeDomain(domain_type: primitives.DomainType, fork_version: ?primitives.Version, genesis_validators_root: ?*const primitives.Root, allocator: std.mem.Allocator) !primitives.Domain { const DOMAIN_TYPE_LENGTH: usize = 4; const FORK_DATA_ROOT_LENGTH: usize = 28; const effective_fork_version = fork_version orelse configs.ActiveConfig.get().GENESIS_FORK_VERSION; - const effective_genesis_validators_root = genesis_validators_root orelse @as(primitives.Root, .{0} ** 32); + const effective_genesis_validators_root = genesis_validators_root orelse &@as(primitives.Root, .{0} ** 32); const fork_data_root = try computeForkDataRoot(effective_fork_version, effective_genesis_validators_root, allocator); @@ -106,13 +106,13 @@ pub fn computeDomain(domain_type: primitives.DomainType, fork_version: ?primitiv pub fn getDomain(state: *const consensus.BeaconState, domainType: primitives.DomainType, epoch: ?primitives.Epoch, allocator: std.mem.Allocator) !primitives.Domain { const current_epoch = epoch orelse epoch_helper.getCurrentEpoch(state); const fork_version = if (current_epoch < state.fork().epoch) state.fork().previous_version else state.fork().current_version; - return try computeDomain(domainType, fork_version, state.genesisValidatorsRoot(), allocator); + return try computeDomain(domainType, fork_version, &state.genesisValidatorsRoot(), allocator); } test "test computeForkDigest" { const currentVersion = .{3} ** 4; const genesisValidatorsRoot = .{2} ** 32; - const forkDigest = try computeForkDigest(currentVersion, genesisValidatorsRoot, std.testing.allocator); + const forkDigest = try computeForkDigest(currentVersion, &genesisValidatorsRoot, std.testing.allocator); try std.testing.expectEqual(4, forkDigest.len); try std.testing.expectEqual([4]u8{ 164, 100, 54, 186 }, forkDigest); } @@ -120,7 +120,7 @@ test "test computeForkDigest" { test "test computeForkDataRoot" { const currentVersion = .{0} ** 4; const genesisValidatorsRoot = .{0} ** 32; - const forkDataRoot = try computeForkDataRoot(currentVersion, genesisValidatorsRoot, std.testing.allocator); + const forkDataRoot = try computeForkDataRoot(currentVersion, &genesisValidatorsRoot, std.testing.allocator); try std.testing.expectEqual(32, forkDataRoot.len); try std.testing.expectEqual([32]u8{ 245, 165, 253, 66, 209, 106, 32, 48, 39, 152, 239, 110, 211, 9, 151, 155, 67, 0, 61, 35, 32, 217, 240, 232, 234, 152, 49, 169, 39, 89, 251, 75 }, forkDataRoot); } @@ -129,7 +129,7 @@ test "test computeDomain" { const domainType = .{2} ** 4; const forkVersion = .{4} ** 4; const genesisValidatorsRoot = .{5} ** 32; - const domain = try computeDomain(domainType, forkVersion, genesisValidatorsRoot, std.testing.allocator); + const domain = try computeDomain(domainType, forkVersion, &genesisValidatorsRoot, std.testing.allocator); try std.testing.expectEqual(32, domain.len); try std.testing.expectEqual([32]u8{ 2, 2, 2, 2, 32, 125, 236, 13, 25, 22, 206, 134, 1, 218, 218, 156, 241, 61, 204, 254, 64, 74, 66, 44, 6, 212, 31, 140, 234, 29, 169, 68 }, domain); } diff --git a/src/consensus/helpers/genesis.zig b/src/consensus/helpers/genesis.zig index 9094809..289ada2 100644 --- a/src/consensus/helpers/genesis.zig +++ b/src/consensus/helpers/genesis.zig @@ -27,7 +27,7 @@ const ssz = @import("../../ssz/ssz.zig"); /// if len(get_active_validator_indices(state, GENESIS_EPOCH)) < config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT: /// return False /// return True -pub fn isValidGenesisState(state: *consensus.BeaconState, allocator: std.mem.Allocator) !bool { +pub fn isValidGenesisState(state: *const consensus.BeaconState, allocator: std.mem.Allocator) !bool { if (state.genesisTime() < configs.ActiveConfig.get().MIN_GENESIS_TIME) { return false; } @@ -103,7 +103,7 @@ pub fn isValidGenesisState(state: *consensus.BeaconState, allocator: std.mem.All /// return state pub fn initializeBeaconStateFromEth1( fork_type: primitives.ForkType, - eth1_block_hash: primitives.Hash32, + eth1_block_hash: *const primitives.Hash32, eth1_timestamp: u64, deposits: []const consensus.Deposit, execution_payload_header: ?*const consensus.ExecutionPayloadHeader, @@ -176,7 +176,7 @@ pub fn initializeBeaconStateFromEth1( try ssz.hashTreeRoot(beacon_block_body, &body_root, allocator); const randao_mixes_slice = try allocator.alloc(primitives.Bytes32, preset.ActivePreset.get().EPOCHS_PER_HISTORICAL_VECTOR); - @memset(randao_mixes_slice, eth1_block_hash); + @memset(randao_mixes_slice, eth1_block_hash.*); var state = switch (fork_type) { .phase0 => consensus.BeaconState{ @@ -184,7 +184,7 @@ pub fn initializeBeaconStateFromEth1( .genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY, .fork = fork, .eth1_data = consensus.Eth1Data{ - .block_hash = eth1_block_hash, + .block_hash = eth1_block_hash.*, .deposit_count = @as(u64, deposits.len), .deposit_root = undefined, }, @@ -199,7 +199,7 @@ pub fn initializeBeaconStateFromEth1( .genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY, .fork = fork, .eth1_data = consensus.Eth1Data{ - .block_hash = eth1_block_hash, + .block_hash = eth1_block_hash.*, .deposit_count = @as(u64, deposits.len), .deposit_root = undefined, }, @@ -216,7 +216,7 @@ pub fn initializeBeaconStateFromEth1( .genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY, .fork = fork, .eth1_data = consensus.Eth1Data{ - .block_hash = eth1_block_hash, + .block_hash = eth1_block_hash.*, .deposit_count = @as(u64, deposits.len), .deposit_root = undefined, }, @@ -234,7 +234,7 @@ pub fn initializeBeaconStateFromEth1( .genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY, .fork = fork, .eth1_data = consensus.Eth1Data{ - .block_hash = eth1_block_hash, + .block_hash = eth1_block_hash.*, .deposit_count = @as(u64, deposits.len), .deposit_root = undefined, }, @@ -252,7 +252,7 @@ pub fn initializeBeaconStateFromEth1( .genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY, .fork = fork, .eth1_data = consensus.Eth1Data{ - .block_hash = eth1_block_hash, + .block_hash = eth1_block_hash.*, .deposit_count = @as(u64, deposits.len), .deposit_root = undefined, }, @@ -270,7 +270,7 @@ pub fn initializeBeaconStateFromEth1( .genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY, .fork = fork, .eth1_data = consensus.Eth1Data{ - .block_hash = eth1_block_hash, + .block_hash = eth1_block_hash.*, .deposit_count = @as(u64, deposits.len), .deposit_root = undefined, }, diff --git a/src/consensus/helpers/voluntary_exit.zig b/src/consensus/helpers/voluntary_exit.zig index 74817dc..813579b 100644 --- a/src/consensus/helpers/voluntary_exit.zig +++ b/src/consensus/helpers/voluntary_exit.zig @@ -78,7 +78,7 @@ pub fn processVoluntaryExit(state: *consensus.BeaconState, signed_voluntary_exit null; // Verify signature - const domain = try domain_helper.computeDomain(constants.DOMAIN_VOLUNTARY_EXIT, fork_version, state.genesisValidatorsRoot(), allocator); + const domain = try domain_helper.computeDomain(constants.DOMAIN_VOLUNTARY_EXIT, fork_version, &state.genesisValidatorsRoot(), allocator); const signing_root = try signing_root_helper.computeSigningRoot(&voluntary_exit, &domain, allocator); if (!bls_helper.verify(&validator.pubkey, &signing_root, &signed_voluntary_exit.signature)) { return error.InvalidSignature;