-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathvalidation_deposits_test.go
More file actions
382 lines (341 loc) · 11.5 KB
/
validation_deposits_test.go
File metadata and controls
382 lines (341 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//go:build test
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.
package core_test
import (
"fmt"
"testing"
"github.com/berachain/beacon-kit/chain"
"github.com/berachain/beacon-kit/config/spec"
"github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/constants"
"github.com/berachain/beacon-kit/state-transition/core"
statetransition "github.com/berachain/beacon-kit/testing/state-transition"
"github.com/stretchr/testify/require"
)
//nolint:paralleltest // uses envars
func TestInvalidDeposits(t *testing.T) {
cs := setupChain(t)
sp, st, ds, ctx, _, _ := statetransition.SetupTestState(t, cs)
var (
minBalance = cs.MinActivationBalance()
maxBalance = cs.MaxEffectiveBalance()
credentials0 = types.NewCredentialsFromExecutionAddress(common.ExecutionAddress{})
)
// Setup initial state with one validator
var (
genDeposits = types.Deposits{
{
Pubkey: [48]byte{0x00},
Credentials: credentials0,
Amount: maxBalance,
Index: 0,
},
}
genPayloadHeader = &types.ExecutionPayloadHeader{
Versionable: types.NewVersionable(cs.GenesisForkVersion()),
}
totalDepositsCount = uint64(len(genDeposits))
)
require.NoError(t, ds.EnqueueDeposits(ctx.ConsensusCtx(), genDeposits))
_, err := sp.InitializeBeaconStateFromEth1(
st, genDeposits, genPayloadHeader, cs.GenesisForkVersion(),
)
require.NoError(t, err)
// Create the correct deposit for pubkey 1.
correctDeposit := &types.Deposit{
Pubkey: [48]byte{0x01},
Credentials: credentials0,
Amount: minBalance,
Index: 1,
}
totalDepositsCount++
// Create an invalid deposit with extra balance going to pubkey 1
invalidDeposit := &types.Deposit{
Pubkey: [48]byte{0x01},
Credentials: credentials0,
Amount: maxBalance, // Invalid - should be minBalance
Index: 1,
}
blkDeposits := []*types.Deposit{invalidDeposit}
// make sure included deposit is already available in deposit store
require.NoError(t, ds.EnqueueDeposits(ctx.ConsensusCtx(), []*types.Deposit{correctDeposit}))
var depRoot common.Root
_, depRoot, err = ds.GetDepositsByIndex(ctx.ConsensusCtx(), constants.FirstDepositIndex, totalDepositsCount)
require.NoError(t, err)
// Create test block with invalid deposit, BUT the correct deposit for pubkey 1.
blk := buildNextBlock(
t,
cs,
st,
types.NewEth1Data(depRoot),
10,
blkDeposits,
&types.ExecutionRequests{},
st.EVMInflationWithdrawal(10),
)
// Run transition - should fail due to invalid deposit amount.
_, err = sp.Transition(ctx, st, blk)
require.Error(t, err)
require.ErrorContains(t, err, "deposit mismatched")
}
//nolint:paralleltest // uses envars
func TestInvalidDepositsCount(t *testing.T) {
cs := setupChain(t)
sp, st, ds, ctx, _, _ := statetransition.SetupTestState(t, cs)
var (
maxBalance = cs.MaxEffectiveBalance()
credentials0 = types.NewCredentialsFromExecutionAddress(common.ExecutionAddress{})
)
// Setup initial state with one validator
var (
genDeposits = types.Deposits{
{
Pubkey: [48]byte{0x00},
Credentials: credentials0,
Amount: maxBalance,
Index: 0,
},
}
genPayloadHeader = &types.ExecutionPayloadHeader{
Versionable: types.NewVersionable(cs.GenesisForkVersion()),
}
totalDepositsCount = uint64(len(genDeposits))
)
require.NoError(t, ds.EnqueueDeposits(ctx.ConsensusCtx(), genDeposits))
_, err := sp.InitializeBeaconStateFromEth1(
st, genDeposits, genPayloadHeader, cs.GenesisForkVersion(),
)
require.NoError(t, err)
// Create the correct deposits.
correctDeposits := types.Deposits{
{
Pubkey: [48]byte{0x01},
Credentials: credentials0,
Amount: maxBalance,
Index: 1,
},
{
Pubkey: [48]byte{0x02},
Credentials: credentials0,
Amount: maxBalance,
Index: 2,
},
}
totalDepositsCount += uint64(len(correctDeposits))
// Add JUST 1 correct deposit to local store. This node SHOULD fail to verify.
require.NoError(t, ds.EnqueueDeposits(ctx.ConsensusCtx(), []*types.Deposit{correctDeposits[0]}))
var depRoot common.Root
_, depRoot, err = ds.GetDepositsByIndex(ctx.ConsensusCtx(), constants.FirstDepositIndex, totalDepositsCount)
require.NoError(t, err)
// Create test block with the correct deposits.
blk := buildNextBlock(
t,
cs,
st,
types.NewEth1Data(depRoot),
10,
correctDeposits,
&types.ExecutionRequests{},
st.EVMInflationWithdrawal(10),
)
// Run transition.
_, err = sp.Transition(ctx, st, blk)
require.Error(t, err)
require.ErrorContains(t, err, "deposits lengths mismatched")
}
func TestLocalDepositsExceedBlockDeposits(t *testing.T) {
t.Parallel()
csData := spec.DevnetChainSpecData()
csData.MaxDepositsPerBlock = 1 // Set only 1 deposit allowed per block.
cs, err := chain.NewSpec(csData)
require.NoError(t, err)
sp, st, ds, ctx, _, _ := statetransition.SetupTestState(t, cs)
var (
maxBalance = cs.MaxEffectiveBalance()
credentials0 = types.NewCredentialsFromExecutionAddress(common.ExecutionAddress{})
)
// Setup initial state with one validator
var (
genDeposits = types.Deposits{
{
Pubkey: [48]byte{0x00},
Credentials: credentials0,
Amount: maxBalance,
Index: 0,
},
}
genPayloadHeader = &types.ExecutionPayloadHeader{
Versionable: types.NewVersionable(cs.GenesisForkVersion()),
}
totalDepositsCount = uint64(len(genDeposits))
)
require.NoError(t, ds.EnqueueDeposits(ctx.ConsensusCtx(), genDeposits))
_, err = sp.InitializeBeaconStateFromEth1(
st, genDeposits, genPayloadHeader, cs.GenesisForkVersion(),
)
require.NoError(t, err)
// Create the block deposits.
blockDeposit := types.Deposit{
Pubkey: [48]byte{0x01},
Credentials: credentials0,
Amount: maxBalance,
Index: 1,
}
blkDeposits := []*types.Deposit{&blockDeposit}
extraLocalDeposit := &types.Deposit{
Pubkey: [48]byte{0x01},
Credentials: credentials0,
Amount: maxBalance,
Index: 2,
}
totalDepositsCount += 2
// make sure included deposit is already available in deposit store
require.NoError(t, ds.EnqueueDeposits(ctx.ConsensusCtx(), []*types.Deposit{&blockDeposit, extraLocalDeposit}))
var depRoot common.Root
_, depRoot, err = ds.GetDepositsByIndex(ctx.ConsensusCtx(), constants.FirstDepositIndex, totalDepositsCount-1)
require.NoError(t, err)
// Create test block with the correct deposits.
blk := buildNextBlock(
t,
cs,
st,
types.NewEth1Data(depRoot),
10,
blkDeposits,
&types.ExecutionRequests{},
st.EVMInflationWithdrawal(10),
)
// Run transition.
_, err = sp.Transition(ctx, st, blk)
require.NoError(t, err)
}
// TestDepositIndexOutOfOrderErrorMessage verifies that the error message produced by
// ErrDepositIndexOutOfOrder reports the absolute expected index (depositIndex+i),
// not just the loop counter i. This is a regression test for a bug where a chain
// 100 deposits in would report "expected index: 0" instead of "expected index: 100".
//
//nolint:paralleltest // uses envars
func TestDepositIndexOutOfOrderErrorMessage(t *testing.T) {
cs := setupChain(t)
_, st, ds, ctx, _, _ := statetransition.SetupTestState(t, cs)
credentials0 := types.NewCredentialsFromExecutionAddress(common.ExecutionAddress{})
// Simulate a chain that has already processed 5 deposits (indices 0–4).
const processedDeposits = uint64(5)
// Seed the deposit store with processedDeposits+1 entries so that the length
// check passes when we submit a single-deposit block.
allDeposits := make(types.Deposits, processedDeposits+1)
for i := range allDeposits {
allDeposits[i] = &types.Deposit{
Pubkey: [48]byte{byte(i)},
Credentials: credentials0,
Amount: cs.MinActivationBalance(),
Index: uint64(i),
}
}
require.NoError(t, ds.EnqueueDeposits(ctx.ConsensusCtx(), allDeposits))
// Advance the state's eth1 deposit index to reflect the already-processed deposits.
require.NoError(t, st.SetEth1DepositIndex(processedDeposits))
// Build a deposit whose index is wrong (0 instead of the expected processedDeposits).
wrongDeposit := &types.Deposit{
Pubkey: [48]byte{0xff},
Credentials: credentials0,
Amount: cs.MinActivationBalance(),
Index: 0, // should be processedDeposits
}
err := core.ValidateNonGenesisDeposits(
ctx.ConsensusCtx(),
st,
ds,
cs.MaxDepositsPerBlock(),
types.Deposits{wrongDeposit},
common.Root{}, // root check is never reached; index check fires first
)
require.Error(t, err)
require.ErrorIs(t, err, core.ErrDepositIndexOutOfOrder)
// The error must report the absolute expected index, not just the loop counter (0).
require.ErrorContains(t, err, fmt.Sprintf("expected index: %d", processedDeposits))
}
func TestLocalDepositsExceedBlockDepositsBadRoot(t *testing.T) {
t.Parallel()
csData := spec.DevnetChainSpecData()
csData.MaxDepositsPerBlock = 1 // Set only 1 deposit allowed per block.
cs, err := chain.NewSpec(csData)
require.NoError(t, err)
sp, st, ds, ctx, _, _ := statetransition.SetupTestState(t, cs)
var (
maxBalance = cs.MaxEffectiveBalance()
credentials0 = types.NewCredentialsFromExecutionAddress(common.ExecutionAddress{})
)
// Setup initial state with one validator
var (
genDeposits = types.Deposits{
{
Pubkey: [48]byte{0x00},
Credentials: credentials0,
Amount: maxBalance,
Index: 0,
},
}
genPayloadHeader = &types.ExecutionPayloadHeader{
Versionable: types.NewVersionable(cs.GenesisForkVersion()),
}
)
require.NoError(t, ds.EnqueueDeposits(ctx.ConsensusCtx(), genDeposits))
_, err = sp.InitializeBeaconStateFromEth1(
st, genDeposits, genPayloadHeader, cs.GenesisForkVersion(),
)
require.NoError(t, err)
// Create the block deposits.
blockDeposits := types.Deposits{
{
Pubkey: [48]byte{0x01},
Credentials: credentials0,
Amount: maxBalance,
Index: 1,
},
}
extraLocalDeposit := &types.Deposit{
Pubkey: [48]byte{0x01},
Credentials: credentials0,
Amount: maxBalance,
Index: 2,
}
// Now, the block proposer ends up adding the correct 1 deposit per block, BUT spoofs the
// deposits root to use the entire deposits list.
badDepRoot := append(genDeposits, append(blockDeposits, extraLocalDeposit)...).HashTreeRoot()
blk := buildNextBlock(
t,
cs,
st,
types.NewEth1Data(badDepRoot),
10,
blockDeposits,
&types.ExecutionRequests{},
st.EVMInflationWithdrawal(10),
)
// Add both deposits to local store (which includes more than what's in the block).
require.NoError(t, ds.EnqueueDeposits(ctx.ConsensusCtx(), append(blockDeposits, extraLocalDeposit)))
// Run transition.
_, err = sp.Transition(ctx, st, blk)
require.Error(t, err)
require.ErrorContains(t, err, "deposits root mismatch")
}