Skip to content

Commit

Permalink
feat:use ptr
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <281165273grape@gmail.com>
  • Loading branch information
GrapeBaBa committed Nov 3, 2024
1 parent dfbaa6d commit e7fbf03
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
20 changes: 10 additions & 10 deletions src/consensus/helpers/domain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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].*;
}
Expand All @@ -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);

Expand Down Expand Up @@ -106,21 +106,21 @@ 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);
}

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);
}
Expand All @@ -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);
}
Expand Down
18 changes: 9 additions & 9 deletions src/consensus/helpers/genesis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -176,15 +176,15 @@ 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{
.phase0 = std.mem.zeroInit(phase0.BeaconState, .{
.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,
},
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/helpers/voluntary_exit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e7fbf03

Please sign in to comment.