Skip to content

Commit e814805

Browse files
committed
test(stf): add unit tests for processDepositRequest
Test coverage for Electra deposit request processing: - First request sets depositRequestsStartIndex from UNSET - Subsequent requests preserve existing start index - Pending deposit fields (amount, slot) match input request - Already-set start index is not overwritten 🤖 Generated with AI assistance
1 parent f684a9f commit e814805

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

src/state_transition/block/process_deposit_request.zig

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,121 @@ pub fn processDepositRequest(comptime fork: ForkSeq, state: *BeaconState(fork),
2222
var pending_deposits = try state.pendingDeposits();
2323
try pending_deposits.pushValue(&pending_deposit);
2424
}
25+
26+
// ─── Tests ──────────────────────────────────────────────────────────────────
27+
28+
const std = @import("std");
29+
const testing = std.testing;
30+
const Node = @import("persistent_merkle_tree").Node;
31+
const TestCachedBeaconState = @import("../test_utils/generate_state.zig").TestCachedBeaconState;
32+
33+
fn makeDepositRequest(index: u64) DepositRequest {
34+
return DepositRequest{
35+
.pubkey = [_]u8{@as(u8, @intCast(index & 0xFF))} ** 48,
36+
.withdrawal_credentials = [_]u8{0x01} ++ [_]u8{0} ** 11 ++ [_]u8{0xAA} ** 20,
37+
.amount = 32_000_000_000,
38+
.signature = [_]u8{0} ** 96,
39+
.index = index,
40+
};
41+
}
42+
43+
test "processDepositRequest - first request sets depositRequestsStartIndex" {
44+
const allocator = testing.allocator;
45+
var pool = try Node.Pool.init(allocator, 256 * 5);
46+
defer pool.deinit();
47+
48+
var test_state = try TestCachedBeaconState.init(allocator, &pool, 16);
49+
defer test_state.deinit();
50+
51+
var state = test_state.cached_state.state.castToFork(.electra);
52+
53+
// Set to UNSET value (as electra upgrade does)
54+
try state.setDepositRequestsStartIndex(c.UNSET_DEPOSIT_REQUESTS_START_INDEX);
55+
56+
const request = makeDepositRequest(42);
57+
try processDepositRequest(.electra, state, &request);
58+
59+
// Should have set the start index to the request's index
60+
try testing.expectEqual(@as(u64, 42), try state.depositRequestsStartIndex());
61+
62+
// Should have appended one pending deposit
63+
var pending = try state.pendingDeposits();
64+
try testing.expectEqual(@as(u64, 1), try pending.length());
65+
}
66+
67+
test "processDepositRequest - subsequent request does not change start index" {
68+
const allocator = testing.allocator;
69+
var pool = try Node.Pool.init(allocator, 256 * 5);
70+
defer pool.deinit();
71+
72+
var test_state = try TestCachedBeaconState.init(allocator, &pool, 16);
73+
defer test_state.deinit();
74+
75+
var state = test_state.cached_state.state.castToFork(.electra);
76+
77+
// Set to UNSET, then process first request
78+
try state.setDepositRequestsStartIndex(c.UNSET_DEPOSIT_REQUESTS_START_INDEX);
79+
const first = makeDepositRequest(10);
80+
try processDepositRequest(.electra, state, &first);
81+
82+
// Process second request with different index
83+
const second = makeDepositRequest(20);
84+
try processDepositRequest(.electra, state, &second);
85+
86+
// Start index should still be 10 (from first request)
87+
try testing.expectEqual(@as(u64, 10), try state.depositRequestsStartIndex());
88+
89+
// Should have two pending deposits
90+
var pending = try state.pendingDeposits();
91+
try testing.expectEqual(@as(u64, 2), try pending.length());
92+
}
93+
94+
test "processDepositRequest - pending deposit fields match request" {
95+
const allocator = testing.allocator;
96+
var pool = try Node.Pool.init(allocator, 256 * 5);
97+
defer pool.deinit();
98+
99+
var test_state = try TestCachedBeaconState.init(allocator, &pool, 16);
100+
defer test_state.deinit();
101+
102+
var state = test_state.cached_state.state.castToFork(.electra);
103+
104+
// Use a non-UNSET start index so we skip that branch
105+
try state.setDepositRequestsStartIndex(0);
106+
107+
const request = makeDepositRequest(5);
108+
try processDepositRequest(.electra, state, &request);
109+
110+
var pending = try state.pendingDeposits();
111+
var deposit = try pending.get(0);
112+
113+
// Verify all fields were copied correctly
114+
// Verify amount and slot were correctly copied from the request
115+
116+
const amount_view = try deposit.get("amount");
117+
try testing.expectEqual(@as(u64, 32_000_000_000), amount_view);
118+
119+
const slot_view = try deposit.get("slot");
120+
const state_slot = try state.slot();
121+
try testing.expectEqual(state_slot, slot_view);
122+
}
123+
124+
test "processDepositRequest - already set start index is preserved" {
125+
const allocator = testing.allocator;
126+
var pool = try Node.Pool.init(allocator, 256 * 5);
127+
defer pool.deinit();
128+
129+
var test_state = try TestCachedBeaconState.init(allocator, &pool, 16);
130+
defer test_state.deinit();
131+
132+
var state = test_state.cached_state.state.castToFork(.electra);
133+
134+
// Pre-set to a specific value (not UNSET)
135+
try state.setDepositRequestsStartIndex(100);
136+
137+
const request = makeDepositRequest(200);
138+
try processDepositRequest(.electra, state, &request);
139+
140+
// Start index should remain 100
141+
try testing.expectEqual(@as(u64, 100), try state.depositRequestsStartIndex());
142+
}

0 commit comments

Comments
 (0)