Skip to content

Commit e7fbf03

Browse files
committed
feat:use ptr
Signed-off-by: Chen Kai <[email protected]>
1 parent dfbaa6d commit e7fbf03

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

src/consensus/helpers/domain.zig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ const ssz = @import("../../ssz/ssz.zig");
2323
/// current_version=current_version,
2424
/// genesis_validators_root=genesis_validators_root,
2525
/// ))
26-
pub fn computeForkDataRoot(current_version: primitives.Version, genesis_validators_root: primitives.Root, allocator: std.mem.Allocator) !primitives.Root {
26+
pub fn computeForkDataRoot(current_version: primitives.Version, genesis_validators_root: *const primitives.Root, allocator: std.mem.Allocator) !primitives.Root {
2727
const fork_data = consensus.ForkData{
2828
.current_version = current_version,
29-
.genesis_validators_root = genesis_validators_root,
29+
.genesis_validators_root = genesis_validators_root.*,
3030
};
3131

3232
var out: primitives.Root = undefined;
33-
try ssz.hashTreeRoot(fork_data, &out, allocator);
33+
try ssz.hashTreeRoot(&fork_data, &out, allocator);
3434
return out;
3535
}
3636

@@ -49,7 +49,7 @@ pub fn computeForkDataRoot(current_version: primitives.Version, genesis_validato
4949
/// 4-bytes suffices for practical separation of forks/chains.
5050
/// """
5151
/// return ForkDigest(compute_fork_data_root(current_version, genesis_validators_root)[:4])
52-
pub fn computeForkDigest(currentVersion: primitives.Version, genesisValidatorsRoot: primitives.Root, allocator: std.mem.Allocator) !primitives.ForkDigest {
52+
pub fn computeForkDigest(currentVersion: primitives.Version, genesisValidatorsRoot: *const primitives.Root, allocator: std.mem.Allocator) !primitives.ForkDigest {
5353
const forkDataRoot = try computeForkDataRoot(currentVersion, genesisValidatorsRoot, allocator);
5454
return forkDataRoot[0..4].*;
5555
}
@@ -71,12 +71,12 @@ pub fn computeForkDigest(currentVersion: primitives.Version, genesisValidatorsRo
7171
/// genesis_validators_root = Root() # all bytes zero by default
7272
/// fork_data_root = compute_fork_data_root(fork_version, genesis_validators_root)
7373
/// return Domain(domain_type + fork_data_root[:28])
74-
pub fn computeDomain(domain_type: primitives.DomainType, fork_version: ?primitives.Version, genesis_validators_root: ?primitives.Root, allocator: std.mem.Allocator) !primitives.Domain {
74+
pub fn computeDomain(domain_type: primitives.DomainType, fork_version: ?primitives.Version, genesis_validators_root: ?*const primitives.Root, allocator: std.mem.Allocator) !primitives.Domain {
7575
const DOMAIN_TYPE_LENGTH: usize = 4;
7676
const FORK_DATA_ROOT_LENGTH: usize = 28;
7777

7878
const effective_fork_version = fork_version orelse configs.ActiveConfig.get().GENESIS_FORK_VERSION;
79-
const effective_genesis_validators_root = genesis_validators_root orelse @as(primitives.Root, .{0} ** 32);
79+
const effective_genesis_validators_root = genesis_validators_root orelse &@as(primitives.Root, .{0} ** 32);
8080

8181
const fork_data_root = try computeForkDataRoot(effective_fork_version, effective_genesis_validators_root, allocator);
8282

@@ -106,21 +106,21 @@ pub fn computeDomain(domain_type: primitives.DomainType, fork_version: ?primitiv
106106
pub fn getDomain(state: *const consensus.BeaconState, domainType: primitives.DomainType, epoch: ?primitives.Epoch, allocator: std.mem.Allocator) !primitives.Domain {
107107
const current_epoch = epoch orelse epoch_helper.getCurrentEpoch(state);
108108
const fork_version = if (current_epoch < state.fork().epoch) state.fork().previous_version else state.fork().current_version;
109-
return try computeDomain(domainType, fork_version, state.genesisValidatorsRoot(), allocator);
109+
return try computeDomain(domainType, fork_version, &state.genesisValidatorsRoot(), allocator);
110110
}
111111

112112
test "test computeForkDigest" {
113113
const currentVersion = .{3} ** 4;
114114
const genesisValidatorsRoot = .{2} ** 32;
115-
const forkDigest = try computeForkDigest(currentVersion, genesisValidatorsRoot, std.testing.allocator);
115+
const forkDigest = try computeForkDigest(currentVersion, &genesisValidatorsRoot, std.testing.allocator);
116116
try std.testing.expectEqual(4, forkDigest.len);
117117
try std.testing.expectEqual([4]u8{ 164, 100, 54, 186 }, forkDigest);
118118
}
119119

120120
test "test computeForkDataRoot" {
121121
const currentVersion = .{0} ** 4;
122122
const genesisValidatorsRoot = .{0} ** 32;
123-
const forkDataRoot = try computeForkDataRoot(currentVersion, genesisValidatorsRoot, std.testing.allocator);
123+
const forkDataRoot = try computeForkDataRoot(currentVersion, &genesisValidatorsRoot, std.testing.allocator);
124124
try std.testing.expectEqual(32, forkDataRoot.len);
125125
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);
126126
}
@@ -129,7 +129,7 @@ test "test computeDomain" {
129129
const domainType = .{2} ** 4;
130130
const forkVersion = .{4} ** 4;
131131
const genesisValidatorsRoot = .{5} ** 32;
132-
const domain = try computeDomain(domainType, forkVersion, genesisValidatorsRoot, std.testing.allocator);
132+
const domain = try computeDomain(domainType, forkVersion, &genesisValidatorsRoot, std.testing.allocator);
133133
try std.testing.expectEqual(32, domain.len);
134134
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);
135135
}

src/consensus/helpers/genesis.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const ssz = @import("../../ssz/ssz.zig");
2727
/// if len(get_active_validator_indices(state, GENESIS_EPOCH)) < config.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT:
2828
/// return False
2929
/// return True
30-
pub fn isValidGenesisState(state: *consensus.BeaconState, allocator: std.mem.Allocator) !bool {
30+
pub fn isValidGenesisState(state: *const consensus.BeaconState, allocator: std.mem.Allocator) !bool {
3131
if (state.genesisTime() < configs.ActiveConfig.get().MIN_GENESIS_TIME) {
3232
return false;
3333
}
@@ -103,7 +103,7 @@ pub fn isValidGenesisState(state: *consensus.BeaconState, allocator: std.mem.All
103103
/// return state
104104
pub fn initializeBeaconStateFromEth1(
105105
fork_type: primitives.ForkType,
106-
eth1_block_hash: primitives.Hash32,
106+
eth1_block_hash: *const primitives.Hash32,
107107
eth1_timestamp: u64,
108108
deposits: []const consensus.Deposit,
109109
execution_payload_header: ?*const consensus.ExecutionPayloadHeader,
@@ -176,15 +176,15 @@ pub fn initializeBeaconStateFromEth1(
176176
try ssz.hashTreeRoot(beacon_block_body, &body_root, allocator);
177177

178178
const randao_mixes_slice = try allocator.alloc(primitives.Bytes32, preset.ActivePreset.get().EPOCHS_PER_HISTORICAL_VECTOR);
179-
@memset(randao_mixes_slice, eth1_block_hash);
179+
@memset(randao_mixes_slice, eth1_block_hash.*);
180180

181181
var state = switch (fork_type) {
182182
.phase0 => consensus.BeaconState{
183183
.phase0 = std.mem.zeroInit(phase0.BeaconState, .{
184184
.genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY,
185185
.fork = fork,
186186
.eth1_data = consensus.Eth1Data{
187-
.block_hash = eth1_block_hash,
187+
.block_hash = eth1_block_hash.*,
188188
.deposit_count = @as(u64, deposits.len),
189189
.deposit_root = undefined,
190190
},
@@ -199,7 +199,7 @@ pub fn initializeBeaconStateFromEth1(
199199
.genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY,
200200
.fork = fork,
201201
.eth1_data = consensus.Eth1Data{
202-
.block_hash = eth1_block_hash,
202+
.block_hash = eth1_block_hash.*,
203203
.deposit_count = @as(u64, deposits.len),
204204
.deposit_root = undefined,
205205
},
@@ -216,7 +216,7 @@ pub fn initializeBeaconStateFromEth1(
216216
.genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY,
217217
.fork = fork,
218218
.eth1_data = consensus.Eth1Data{
219-
.block_hash = eth1_block_hash,
219+
.block_hash = eth1_block_hash.*,
220220
.deposit_count = @as(u64, deposits.len),
221221
.deposit_root = undefined,
222222
},
@@ -234,7 +234,7 @@ pub fn initializeBeaconStateFromEth1(
234234
.genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY,
235235
.fork = fork,
236236
.eth1_data = consensus.Eth1Data{
237-
.block_hash = eth1_block_hash,
237+
.block_hash = eth1_block_hash.*,
238238
.deposit_count = @as(u64, deposits.len),
239239
.deposit_root = undefined,
240240
},
@@ -252,7 +252,7 @@ pub fn initializeBeaconStateFromEth1(
252252
.genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY,
253253
.fork = fork,
254254
.eth1_data = consensus.Eth1Data{
255-
.block_hash = eth1_block_hash,
255+
.block_hash = eth1_block_hash.*,
256256
.deposit_count = @as(u64, deposits.len),
257257
.deposit_root = undefined,
258258
},
@@ -270,7 +270,7 @@ pub fn initializeBeaconStateFromEth1(
270270
.genesis_time = eth1_timestamp + configs.ActiveConfig.get().GENESIS_DELAY,
271271
.fork = fork,
272272
.eth1_data = consensus.Eth1Data{
273-
.block_hash = eth1_block_hash,
273+
.block_hash = eth1_block_hash.*,
274274
.deposit_count = @as(u64, deposits.len),
275275
.deposit_root = undefined,
276276
},

src/consensus/helpers/voluntary_exit.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn processVoluntaryExit(state: *consensus.BeaconState, signed_voluntary_exit
7878
null;
7979

8080
// Verify signature
81-
const domain = try domain_helper.computeDomain(constants.DOMAIN_VOLUNTARY_EXIT, fork_version, state.genesisValidatorsRoot(), allocator);
81+
const domain = try domain_helper.computeDomain(constants.DOMAIN_VOLUNTARY_EXIT, fork_version, &state.genesisValidatorsRoot(), allocator);
8282
const signing_root = try signing_root_helper.computeSigningRoot(&voluntary_exit, &domain, allocator);
8383
if (!bls_helper.verify(&validator.pubkey, &signing_root, &signed_voluntary_exit.signature)) {
8484
return error.InvalidSignature;

0 commit comments

Comments
 (0)