Skip to content

Commit 5922564

Browse files
committed
main stf
fix import undo panic dedup deletion
1 parent f8e92fd commit 5922564

12 files changed

+183
-10
lines changed

build.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,14 @@ pub fn build(b: *std.Build) void {
161161

162162
const run_config_unit_tests = b.addRunArtifact(config_unit_tests);
163163

164+
const filters = b.option([]const []const u8, "filters", "Test filters");
164165
// Creates a step for unit testing. This only builds the test executable
165166
// but does not run it.
166167
const state_transition_unit_tests = b.addTest(.{
167168
.root_source_file = b.path("src/state_transition/root.zig"),
168169
.target = target,
169170
.optimize = optimize,
171+
.filters = filters orelse &.{},
170172
});
171173
state_transition_unit_tests.root_module.addImport("ssz", module_ssz);
172174
state_transition_unit_tests.root_module.addImport("consensus_types", module_consensus_types);
@@ -210,7 +212,7 @@ pub fn build(b: *std.Build) void {
210212
const test_int_tests = b.addTest(.{
211213
.name = "int",
212214
.root_module = module_test_int,
213-
.filters = &[_][]const u8{},
215+
.filters = filters orelse &.{},
214216
});
215217

216218
const run_test_int_tests = b.addRunArtifact(test_int_tests);

src/state_transition/cache/epoch_transition_cache.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ pub const ReusedEpochTransitionCache = struct {
9595
}
9696
};
9797

98+
pub const EpochTransitionCacheOpts = struct {
99+
/// Assert progressive balances the same in the cache.
100+
assert_correct_progressive_balances: bool = false,
101+
/// Do not queue shuffling calculation async. Forces sync JIT calculation in afterProcessEpoch
102+
async_shuffling_calculation: bool = false,
103+
};
104+
98105
pub const EpochTransitionCache = struct {
99106
prev_epoch: Epoch,
100107
current_epoch: Epoch,

src/state_transition/epoch/process_inactivity_updates.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const CachedBeaconStateAllForks = @import("../cache/state_cache.zig").CachedBeaconStateAllForks;
2-
const ssz = @import("consensus_types");
3-
const Epoch = ssz.primitive.Epoch.Type;
2+
3+
const types = @import("../type.zig");
4+
const Epoch = types.Epoch;
45
const EpochTransitionCache = @import("../cache/epoch_transition_cache.zig").EpochTransitionCache;
56
const params = @import("params");
67
const GENESIS_EPOCH = params.GENESIS_EPOCH;

src/state_transition/epoch/process_justification_and_finalization.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const CachedBeaconStateAllForks = @import("../cache/state_cache.zig").CachedBeaconStateAllForks;
2+
const types = @import("../type.zig");
3+
const Epoch = types.Epoch;
24
const EpochTransitionCache = @import("../cache/epoch_transition_cache.zig").EpochTransitionCache;
35
const params = @import("params");
46
const GENESIS_EPOCH = params.GENESIS_EPOCH;

src/state_transition/epoch/process_registry_updates.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const CachedBeaconStateAllForks = @import("../cache/state_cache.zig").CachedBeaconStateAllForks;
2-
const ssz = @import("consensus_types");
3-
const Epoch = ssz.primitive.Epoch.Type;
2+
const types = @import("../type.zig");
3+
const Epoch = types.Epoch;
44
const EpochTransitionCache = @import("../cache/epoch_transition_cache.zig").EpochTransitionCache;
55
const ForkSeq = @import("params").ForkSeq;
66
const computeActivationExitEpoch = @import("../utils/epoch.zig").computeActivationExitEpoch;

src/state_transition/root.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ pub const processSyncCommitteeUpdates = @import("./epoch/process_sync_committee_
3636

3737
pub const bls = @import("utils/bls.zig");
3838
const seed = @import("./utils/seed.zig");
39+
pub const state_transition = @import("./state_transition.zig");
3940
const EpochShuffling = @import("./utils/epoch_shuffling.zig");
4041

4142
test {
4243
testing.refAllDecls(@This());
4344
testing.refAllDecls(seed);
45+
testing.refAllDecls(state_transition);
4446
testing.refAllDecls(EpochShuffling);
4547
}

src/state_transition/slot/process_slot.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const preset = ssz.preset;
66
const Root = ssz.primitive.Root.Type;
77
const ZERO_HASH = @import("../constants.zig").ZERO_HASH;
88

9-
pub fn processSlot(allocator: Allocator, cached_state: *CachedBeaconStateAllForks) void {
9+
pub fn processSlot(allocator: Allocator, cached_state: *CachedBeaconStateAllForks) !void {
1010
const state = cached_state.state;
1111

1212
// Cache state root
@@ -16,7 +16,7 @@ pub fn processSlot(allocator: Allocator, cached_state: *CachedBeaconStateAllFork
1616

1717
// Cache latest block header state root
1818
var latest_block_header = state.getLatestBlockHeader();
19-
if (!std.mem.allEqual(u8, &latest_block_header.state_root, &ZERO_HASH)) {
19+
if (!std.mem.eql(u8, &latest_block_header.state_root, &ZERO_HASH)) {
2020
latest_block_header.state_root = previous_state_root;
2121
}
2222

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const CachedBeaconStateAllForks = @import("../cache/state_cache.zig").CachedBeaconStateAllForks;
2+
const BeaconStateAllForks = @import("../types/beacon_state.zig").BeaconStateAllForks;
3+
4+
const std = @import("std");
5+
const Allocator = std.mem.Allocator;
6+
7+
const assert = std.debug.assert;
8+
9+
pub fn upgradeStateToAltair(_: *CachedBeaconStateAllForks) CachedBeaconStateAllForks {
10+
@panic("unimplemented");
11+
}
12+
pub fn upgradeStateToBellatrix(_: *CachedBeaconStateAllForks) CachedBeaconStateAllForks {
13+
@panic("unimplemented");
14+
}
15+
pub fn upgradeStateToCapella(_: *CachedBeaconStateAllForks) CachedBeaconStateAllForks {
16+
@panic("unimplemented");
17+
}
18+
pub fn upgradeStateToDeneb(_: *CachedBeaconStateAllForks) CachedBeaconStateAllForks {
19+
@panic("unimplemented");
20+
}
21+
pub fn upgradeStateToElectra(_: *CachedBeaconStateAllForks) CachedBeaconStateAllForks {
22+
@panic("unimplemented");
23+
}
24+
pub fn upgradeStateToFulu(_: *CachedBeaconStateAllForks) CachedBeaconStateAllForks {
25+
@panic("unimplemented");
26+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}

src/state_transition/types/beacon_state.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const std = @import("std");
2+
const panic = std.debug.panic;
23
const Allocator = std.mem.Allocator;
34
const expect = std.testing.expect;
4-
const panic = std.debug.panic;
55
const ssz = @import("consensus_types");
66
const preset = ssz.preset;
77
const BeaconStatePhase0 = ssz.phase0.BeaconState.Type;
@@ -301,7 +301,7 @@ pub const BeaconStateAllForks = union(enum) {
301301

302302
pub fn setBlockRoot(self: *BeaconStateAllForks, index: usize, root: Root) void {
303303
switch (self.*) {
304-
inline .phase0, .altair, .bellatrix, .capella, .deneb, .electra => |state| state.block_roots.items[index] = root,
304+
inline .phase0, .altair, .bellatrix, .capella, .deneb, .electra => |state| state.block_roots[index] = root,
305305
}
306306
}
307307

@@ -319,7 +319,7 @@ pub const BeaconStateAllForks = union(enum) {
319319

320320
pub fn setStateRoot(self: *BeaconStateAllForks, index: usize, root: Root) void {
321321
switch (self.*) {
322-
inline .phase0, .altair, .bellatrix, .capella, .deneb, .electra => |state| state.state_roots.items[index] = root,
322+
inline .phase0, .altair, .bellatrix, .capella, .deneb, .electra => |state| state.state_roots[index] = root,
323323
}
324324
}
325325

0 commit comments

Comments
 (0)