-
Notifications
You must be signed in to change notification settings - Fork 12
chore: move utils/root_cache.zig -> cache/RootCache.zig #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| const RootCache = @This(); | ||
|
|
||
| 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(¤t_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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filename
RootCache.zigdoes not follow thesnake_caseconvention specified in the style guide. Please rename it toroot_cache.zig.This change will also require updating the import paths in
src/state_transition/block/process_attestation_altair.zigandsrc/state_transition/slot/upgrade_state_to_altair.zig.References
snake_casefor file names. We don't use Zig'sCamelCase.zigstyle for "struct" files to keep the convention simple and consistent. (link)