|
| 1 | +const std = @import("std"); |
| 2 | +const Allocator = std.mem.Allocator; |
| 3 | + |
| 4 | +const ssz = @import("consensus_types"); |
| 5 | +const preset = ssz.preset; |
| 6 | +const Root = ssz.primitive.Root.Type; |
| 7 | +const ZERO_HASH = @import("../constants.zig").ZERO_HASH; |
| 8 | +const Slot = ssz.primitive.Slot.Type; |
| 9 | + |
| 10 | +const CachedBeaconStateAllForks = @import("cache/state_cache.zig").CachedBeaconStateAllForks; |
| 11 | +pub const SignedBeaconBlock = @import("types/beacon_block.zig").SignedBeaconBlock; |
| 12 | +const BeaconBlock = @import("types/beacon_block.zig").BeaconBlock; |
| 13 | +const BlindedBeaconBlock = @import("types/beacon_block.zig").BlindedBeaconBlock; |
| 14 | +const SignedBlindedBeaconBlock = @import("types/beacon_block.zig").SignedBlindedBeaconBlock; |
| 15 | +const TestCachedBeaconStateAllForks = @import("../../test/int/generate_state.zig").TestCachedBeaconStateAllForks; |
| 16 | +const EpochTransitionCacheOpts = @import("cache/epoch_transition_cache.zig").EpochTransitionCacheOpts; |
| 17 | +const EpochTransitionCache = @import("cache/epoch_transition_cache.zig").EpochTransitionCache; |
| 18 | +const process_epoch = @import("epoch/process_epoch.zig").process_epoch; |
| 19 | +const computeEpochAtSlot = @import("utils/epoch.zig").computeEpochAtSlot; |
| 20 | +const processSlot = @import("slot/process_slot.zig").processSlot; |
| 21 | + |
| 22 | +const Options = struct { |
| 23 | + verify_state_root: bool = true, |
| 24 | + verify_proposer: bool = true, |
| 25 | + verify_signatures: bool = false, |
| 26 | + do_not_transfer_cache: bool = false, |
| 27 | +}; |
| 28 | + |
| 29 | +const Block = union(enum) { |
| 30 | + block: BeaconBlock, |
| 31 | + blinded_block: BlindedBeaconBlock, |
| 32 | +}; |
| 33 | + |
| 34 | +pub const SignedBlock = union(enum) { |
| 35 | + signed_beacon_block: *const SignedBeaconBlock, |
| 36 | + signed_blinded_beacon_block: *const SignedBlindedBeaconBlock, |
| 37 | + |
| 38 | + fn getMessage(self: *const SignedBlock) Block { |
| 39 | + return switch (self.*) { |
| 40 | + .signed_beacon_block => |b| Block{ .block = b.getBeaconBlock() }, |
| 41 | + .signed_blinded_beacon_block => |b| Block{ .blinded_block = b.getBeaconBlock() }, |
| 42 | + }; |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +fn processSlotsWithTransientCache( |
| 47 | + allocator: std.mem.Allocator, |
| 48 | + post_state: *CachedBeaconStateAllForks, |
| 49 | + slot: Slot, |
| 50 | + _: EpochTransitionCacheOpts, |
| 51 | +) !void { |
| 52 | + var post_state_slot = post_state.state.getSlot(); |
| 53 | + if (post_state_slot > slot) return error.outdatedSlot; |
| 54 | + |
| 55 | + while (post_state_slot < slot) { |
| 56 | + try processSlot(allocator, post_state); |
| 57 | + |
| 58 | + if ((post_state_slot + 1) % preset.SLOTS_PER_EPOCH == 0) { |
| 59 | + _ = post_state.config.getForkSeq(post_state_slot); |
| 60 | + // TODO(bing): implement |
| 61 | + // const epochTransitionTimer = metrics?.epochTransitionTime.startTimer(); |
| 62 | + // var epoch_transition_cache = beforeProcessEpoch(post_state, epoch_transition_cache_opts); |
| 63 | + // process_epoch(allocator, post_state, epoch_transition_cache); |
| 64 | + |
| 65 | + // registerValidatorStatuses |
| 66 | + |
| 67 | + post_state_slot += 1; |
| 68 | + |
| 69 | + // afterProcessEpoch |
| 70 | + // post_state.commit |
| 71 | + } |
| 72 | + |
| 73 | + //epochTransitionTimer |
| 74 | + // upgrade state |
| 75 | + _ = computeEpochAtSlot(post_state_slot); |
| 76 | + _ = post_state.config; |
| 77 | + |
| 78 | + //TODO(bing): upgradeState to forks |
| 79 | + //switch (true) { |
| 80 | + // state_epoch == config.chain.DENEB_FORK_EPOCH => post_state = upgradeState(); |
| 81 | + // |
| 82 | + //} |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +pub fn stateTransition( |
| 87 | + allocator: std.mem.Allocator, |
| 88 | + state: *CachedBeaconStateAllForks, |
| 89 | + signed_block: SignedBlock, |
| 90 | + options: Options, |
| 91 | +) !*CachedBeaconStateAllForks { |
| 92 | + _ = options; |
| 93 | + |
| 94 | + //TODO(bing): deep clone |
| 95 | + // const post_state = state.clone(); |
| 96 | + // |
| 97 | + const post_state = state; |
| 98 | + const block = signed_block.getMessage(); |
| 99 | + const block_slot = switch (block) { |
| 100 | + .block => |b| b.getSlot(), |
| 101 | + .blinded_block => |b| b.getSlot(), |
| 102 | + }; |
| 103 | + |
| 104 | + try processSlotsWithTransientCache(allocator, post_state, block_slot, .{}); |
| 105 | + |
| 106 | + return state; |
| 107 | +} |
0 commit comments