-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathpayload.go
More file actions
274 lines (249 loc) · 8.7 KB
/
payload.go
File metadata and controls
274 lines (249 loc) · 8.7 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
// 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 blockchain
import (
"context"
"fmt"
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
engineprimitives "github.com/berachain/beacon-kit/engine-primitives/engine-primitives"
engineerrors "github.com/berachain/beacon-kit/engine-primitives/errors"
"github.com/berachain/beacon-kit/errors"
"github.com/berachain/beacon-kit/primitives/math"
statedb "github.com/berachain/beacon-kit/state-transition/core/state"
)
// forceSyncUponProcess sends a force head FCU to the execution client.
func (s *Service) forceSyncUponProcess(
ctx context.Context,
st *statedb.StateDB,
) {
lph, err := st.GetLatestExecutionPayloadHeader()
if err != nil {
s.logger.Error(
"failed to get latest execution payload header",
"error", err,
)
return
}
s.logger.Info(
"Sending startup forkchoice update to execution client",
"head_eth1_hash", lph.GetBlockHash(),
"safe_eth1_hash", lph.GetParentHash(),
"finalized_eth1_hash", lph.GetParentHash(),
"for_slot", lph.GetNumber(),
)
// Submit the forkchoice update to the execution client.
req := ctypes.BuildForkchoiceUpdateRequestNoAttrs(
&engineprimitives.ForkchoiceStateV1{
HeadBlockHash: lph.GetBlockHash(),
SafeBlockHash: lph.GetParentHash(),
FinalizedBlockHash: lph.GetParentHash(),
},
s.chainSpec.ActiveForkVersionForTimestamp(lph.GetTimestamp()),
)
if _, err = s.executionEngine.NotifyForkchoiceUpdate(ctx, req); err != nil {
s.logger.Error(
"failed to send force head FCU",
"error", err,
)
}
}
// forceSyncUponFinalize sends a new payload and force startup FCU to the Execution
// Layer client. This informs the EL client of the new head and forces a SYNC
// if blocks are missing. This function should only be run once at startup.
func (s *Service) forceSyncUponFinalize(
ctx context.Context,
beaconBlock *ctypes.BeaconBlock,
) error {
// NewPayload call first to load payload into EL client.
executionPayload := beaconBlock.GetBody().GetExecutionPayload()
payloadReq, err := ctypes.BuildNewPayloadRequestFromFork(beaconBlock)
if err != nil {
return err
}
if err = payloadReq.HasValidVersionedAndBlockHashes(); err != nil {
return err
}
// We set retryOnSyncingStatus to false here. We can ignore SYNCING status and proceed
// to the FCU.
err = s.executionEngine.NotifyNewPayload(ctx, payloadReq, false)
if err != nil {
return fmt.Errorf("startSyncUponFinalize NotifyNewPayload failed: %w", err)
}
// Submit the forkchoice update to the EL client. This will ensure that it is either synced or
// starts up a sync.
req := ctypes.BuildForkchoiceUpdateRequestNoAttrs(
&engineprimitives.ForkchoiceStateV1{
HeadBlockHash: executionPayload.GetBlockHash(),
SafeBlockHash: executionPayload.GetParentHash(),
FinalizedBlockHash: executionPayload.GetParentHash(),
},
s.chainSpec.ActiveForkVersionForTimestamp(executionPayload.GetTimestamp()),
)
switch _, err = s.executionEngine.NotifyForkchoiceUpdate(ctx, req); {
case err == nil:
return nil
case errors.IsAny(err,
engineerrors.ErrSyncingPayloadStatus,
engineerrors.ErrAcceptedPayloadStatus):
s.logger.Warn(
//nolint:lll // long message on one line for readability.
`Your execution client is syncing. It should be downloading eth blocks from its peers. Restart the beacon node once the execution client is caught up.`,
)
return err
default:
return fmt.Errorf("force startup NotifyForkchoiceUpdate failed: %w", err)
}
}
// handleRebuildPayloadForRejectedBlock handles the case where the incoming
// block was rejected and we need to rebuild the payload for the current slot.
func (s *Service) handleRebuildPayloadForRejectedBlock(
ctx context.Context,
st *statedb.StateDB,
nextPayloadTimestamp math.U64,
) {
if err := s.rebuildPayloadForRejectedBlock(
ctx,
st,
nextPayloadTimestamp,
); err != nil {
s.logger.Error(
"failed to rebuild payload for nil block",
"error", err,
)
}
}
// rebuildPayloadForRejectedBlock rebuilds a payload for the current
// slot, if the incoming block was rejected.
//
// NOTE: We cannot use any data off the incoming block and must recompute
// any required information from our local state. We do this since we have
// rejected the incoming block and it would be unsafe to use any
// information from it.
func (s *Service) rebuildPayloadForRejectedBlock(
ctx context.Context,
st *statedb.StateDB,
nextPayloadTimestamp math.U64,
) error {
s.logger.Info("Rebuilding payload for rejected block ⏳ ")
// In order to rebuild a payload for the current slot, we need to know the
// previous block root, since we know that this is an unmodified state.
// We can safely get the latest block header and then rebuild the
// previous block and it's root.
latestHeader, err := st.GetLatestBlockHeader()
if err != nil {
return err
}
stateSlot, err := st.GetSlot()
if err != nil {
return err
}
// Set the previous state root on the header.
latestHeader.SetStateRoot(st.HashTreeRoot())
// We need to get the *last* finalized execution payload, thus
// the BeaconState that was passed in must be `unmodified`.
lph, err := st.GetLatestExecutionPayloadHeader()
if err != nil {
return err
}
// Submit a request for a new payload.
if _, _, err = s.localBuilder.RequestPayloadAsync(
ctx,
st,
// We are rebuilding for the current slot.
stateSlot,
nextPayloadTimestamp,
// We set the parent root to the previous block root. The HashTreeRoot
// of the header is the same as the HashTreeRoot of the block.
latestHeader.HashTreeRoot(),
// We set the head of our chain to the previous finalized block.
lph.GetBlockHash(),
// We can say that the payload from the previous block is *finalized*,
// TODO: This is making an assumption about the consensus rules
// and possibly should be made more explicit later on.
lph.GetParentHash(),
); err != nil {
s.metrics.markRebuildPayloadForRejectedBlockFailure(stateSlot, err)
return err
}
s.metrics.markRebuildPayloadForRejectedBlockSuccess(stateSlot)
return nil
}
// handleOptimisticPayloadBuild handles optimistically
// building for the next slot.
func (s *Service) handleOptimisticPayloadBuild(
ctx context.Context,
st *statedb.StateDB,
blk *ctypes.BeaconBlock,
nextPayloadTimestamp math.U64,
) {
if err := s.optimisticPayloadBuild(
ctx,
st,
blk,
nextPayloadTimestamp,
); err != nil {
s.logger.Error(
"Failed to build optimistic payload",
"for_slot", (blk.GetSlot() + 1).Base10(),
"error", err,
)
}
}
// optimisticPayloadBuild builds a payload for the next slot.
func (s *Service) optimisticPayloadBuild(
ctx context.Context,
st *statedb.StateDB,
blk *ctypes.BeaconBlock,
nextPayloadTimestamp math.U64,
) error {
// We are building for the next slot, so we increment the slot relative
// to the block we just processed.
slot := blk.GetSlot() + 1
s.logger.Info(
"Optimistically triggering payload build for next slot 🛩️ ",
"next_slot", slot.Base10(),
)
// We process the slot to update any RANDAO values.
if _, err := s.stateProcessor.ProcessSlots(st, slot); err != nil {
return err
}
// We then trigger a request for the next payload.
payload := blk.GetBody().GetExecutionPayload()
if _, _, err := s.localBuilder.RequestPayloadAsync(
ctx, st,
slot,
nextPayloadTimestamp,
// The previous block root is simply the root of the block we just
// processed.
blk.HashTreeRoot(),
// We set the head of our chain to the block we just processed.
payload.GetBlockHash(),
// We can say that the payload from the previous block is *finalized*,
// This is safe to do since this block was accepted and the thus the
// parent hash was deemed valid by the state transition function we
// just processed.
payload.GetParentHash(),
); err != nil {
s.metrics.markOptimisticPayloadBuildFailure(slot, err)
return err
}
s.metrics.markOptimisticPayloadBuildSuccess(slot)
return nil
}