Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Epoch = types.primitive.Epoch.Type;
const preset = @import("preset").preset;
const ForkSeq = @import("config").ForkSeq;
const c = @import("constants");
const RootCache = @import("../utils/root_cache.zig").RootCache;
const RootCache = @import("../cache/RootCache.zig");
const validateAttestation = @import("./process_attestation_phase0.zig").validateAttestation;
const getAttestationWithIndicesSignatureSet = @import("../signature_sets/indexed_attestation.zig").getAttestationWithIndicesSignatureSet;
const verifyAggregatedSignatureSet = @import("../utils/signature_sets.zig").verifyAggregatedSignatureSet;
Expand Down
69 changes: 69 additions & 0 deletions src/state_transition/cache/RootCache.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const RootCache = @This();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The filename RootCache.zig does not follow the snake_case convention specified in the style guide. Please rename it to root_cache.zig.

This change will also require updating the import paths in src/state_transition/block/process_attestation_altair.zig and src/state_transition/slot/upgrade_state_to_altair.zig.

References
  1. Use snake_case for file names. We don't use Zig's CamelCase.zig style for "struct" files to keep the convention simple and consistent. (link)


const std = @import("std");
const Allocator = std.mem.Allocator;
const CachedBeaconState = @import("../cache/state_cache.zig").CachedBeaconState;
const BeaconState = @import("../types/beacon_state.zig").BeaconState;
const getBlockRootFn = @import("../utils/block_root.zig").getBlockRoot;
const getBlockRootAtSlotFn = @import("../utils/block_root.zig").getBlockRootAtSlot;
const types = @import("consensus_types");
const Checkpoint = types.phase0.Checkpoint.Type;
const Epoch = types.primitive.Epoch.Type;
const Slot = types.primitive.Slot.Type;
const Root = types.primitive.Root.Type;

allocator: Allocator,
current_justified_checkpoint: Checkpoint,
previous_justified_checkpoint: Checkpoint,
state: *BeaconState,
block_root_epoch_cache: std.AutoHashMap(Epoch, *const Root),
block_root_slot_cache: std.AutoHashMap(Slot, *const Root),

pub fn init(allocator: Allocator, cached_state: *CachedBeaconState) !*RootCache {
const instance = try allocator.create(RootCache);
errdefer allocator.destroy(instance);
const state = cached_state.state;

var current_justified_checkpoint: Checkpoint = undefined;
var previous_justified_checkpoint: Checkpoint = undefined;
try state.currentJustifiedCheckpoint(&current_justified_checkpoint);
try state.previousJustifiedCheckpoint(&previous_justified_checkpoint);
instance.* = RootCache{
.allocator = allocator,
.current_justified_checkpoint = current_justified_checkpoint,
.previous_justified_checkpoint = previous_justified_checkpoint,
.state = state,
.block_root_epoch_cache = std.AutoHashMap(Epoch, *const Root).init(allocator),
.block_root_slot_cache = std.AutoHashMap(Slot, *const Root).init(allocator),
};

return instance;
}

pub fn getBlockRoot(self: *RootCache, epoch: Epoch) !*const Root {
if (self.block_root_epoch_cache.get(epoch)) |root| {
return root;
} else {
const root = try getBlockRootFn(self.state, epoch);
try self.block_root_epoch_cache.put(epoch, root);
return root;
}
}

pub fn getBlockRootAtSlot(self: *RootCache, slot: Slot) !*const Root {
if (self.block_root_slot_cache.get(slot)) |root| {
return root;
} else {
const root = try getBlockRootAtSlotFn(self.state, slot);
try self.block_root_slot_cache.put(slot, root);
return root;
}
}

pub fn deinit(self: *RootCache) void {
self.block_root_epoch_cache.deinit();
self.block_root_slot_cache.deinit();
self.allocator.destroy(self);
}

// TODO: unit tests
2 changes: 1 addition & 1 deletion src/state_transition/slot/upgrade_state_to_altair.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const sumTargetUnslashedBalanceIncrements = @import("../utils/target_unslashed_b
const computePreviousEpoch = @import("../utils/epoch.zig").computePreviousEpoch;
const types = @import("consensus_types");
const ValidatorIndex = types.primitive.ValidatorIndex.Type;
const RootCache = @import("../utils/root_cache.zig").RootCache;
const RootCache = @import("../cache/RootCache.zig");
const getAttestationParticipationStatus = @import("../block//process_attestation_altair.zig").getAttestationParticipationStatus;

pub fn upgradeStateToAltair(allocator: Allocator, cached_state: *CachedBeaconState) !void {
Expand Down
69 changes: 0 additions & 69 deletions src/state_transition/utils/root_cache.zig

This file was deleted.