generated from rollkit/template-da-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexecution.go
337 lines (291 loc) · 10.8 KB
/
execution.go
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
package execution
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"net/http"
"strings"
"time"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/golang-jwt/jwt/v5"
"github.com/rollkit/rollkit/core/execution"
)
var (
// ErrNilPayloadStatus indicates that PayloadID returned by EVM was nil
ErrNilPayloadStatus = errors.New("nil payload status")
// ErrInvalidPayloadStatus indicates that EVM returned status != VALID
ErrInvalidPayloadStatus = errors.New("invalid payload status")
)
// Ensure EngineAPIExecutionClient implements the execution.Execute interface
var _ execution.Executor = (*PureEngineClient)(nil)
// PureEngineClient represents a client that interacts with an Ethereum execution engine
// through the Engine API. It manages connections to both the engine and standard Ethereum
// APIs, and maintains state related to block processing.
type PureEngineClient struct {
engineClient *rpc.Client // Client for Engine API calls
ethClient *ethclient.Client // Client for standard Ethereum API calls
genesisHash common.Hash // Hash of the genesis block
feeRecipient common.Address // Address to receive transaction fees
payloadID *engine.PayloadID // ID of the current execution payload being processed
}
// NewPureEngineExecutionClient creates a new instance of EngineAPIExecutionClient
func NewPureEngineExecutionClient(
ethURL,
engineURL string,
jwtSecret string,
genesisHash common.Hash,
feeRecipient common.Address,
) (*PureEngineClient, error) {
ethClient, err := ethclient.Dial(ethURL)
if err != nil {
return nil, err
}
secret, err := decodeSecret(jwtSecret)
if err != nil {
return nil, err
}
engineClient, err := rpc.DialOptions(context.Background(), engineURL,
rpc.WithHTTPAuth(func(h http.Header) error {
authToken, err := getAuthToken(secret)
if err != nil {
return err
}
if authToken != "" {
h.Set("Authorization", "Bearer "+authToken)
}
return nil
}))
if err != nil {
return nil, err
}
return &PureEngineClient{
engineClient: engineClient,
ethClient: ethClient,
genesisHash: genesisHash,
feeRecipient: feeRecipient,
}, nil
}
// InitChain initializes the blockchain with the given genesis parameters
func (c *PureEngineClient) InitChain(ctx context.Context, genesisTime time.Time, initialHeight uint64, chainID string) ([]byte, uint64, error) {
if initialHeight != 1 {
return nil, 0, fmt.Errorf("initialHeight must be 1, got %d", initialHeight)
}
// Acknowledge the genesis block
var forkchoiceResult engine.ForkChoiceResponse
err := c.engineClient.CallContext(ctx, &forkchoiceResult, "engine_forkchoiceUpdatedV3",
engine.ForkchoiceStateV1{
HeadBlockHash: c.genesisHash,
SafeBlockHash: c.genesisHash,
FinalizedBlockHash: c.genesisHash,
},
nil,
)
if err != nil {
return nil, 0, fmt.Errorf("engine_forkchoiceUpdatedV3 failed: %w", err)
}
// Start building the first block
err = c.engineClient.CallContext(ctx, &forkchoiceResult, "engine_forkchoiceUpdatedV3",
engine.ForkchoiceStateV1{
HeadBlockHash: c.genesisHash,
SafeBlockHash: c.genesisHash,
FinalizedBlockHash: c.genesisHash,
},
engine.PayloadAttributes{
Timestamp: uint64(genesisTime.Add(-1 * time.Second).Unix()), //nolint:gosec // disable G115
Random: common.Hash{}, // TODO(tzdybal): this probably shouldn't be 0
SuggestedFeeRecipient: c.feeRecipient,
BeaconRoot: &c.genesisHash,
Withdrawals: []*types.Withdrawal{},
},
)
if err != nil {
return nil, 0, fmt.Errorf("engine_forkchoiceUpdatedV3 failed: %w", err)
}
if forkchoiceResult.PayloadID == nil {
return nil, 0, ErrNilPayloadStatus
}
c.payloadID = forkchoiceResult.PayloadID
_, stateRoot, _, err := c.getBlockInfo(ctx, 0)
if err != nil {
return nil, 0, fmt.Errorf("failed to get genesis block info: %w", err)
}
// for rollkit compatibility, create one empty block
payload, err := c.GetTxs(ctx)
if err != nil {
return nil, 0, fmt.Errorf("failed to get txs: %w", err)
}
return c.ExecuteTxs(ctx, payload, 1, genesisTime, stateRoot[:])
}
// GetTxs retrieves transactions from the current execution payload
func (c *PureEngineClient) GetTxs(ctx context.Context) ([][]byte, error) {
if c.payloadID == nil { // this happens when rollkit is restarted
latestHeight, err := c.ethClient.BlockNumber(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get latest block height: %w", err)
}
block, err := c.ethClient.BlockByNumber(ctx, new(big.Int).SetUint64(latestHeight))
if err != nil {
return nil, fmt.Errorf("failed to get latest block: %w", err)
}
blockHash := block.Hash()
timestamp := block.Time() + 1
var forkchoiceResult engine.ForkChoiceResponse
err = c.engineClient.CallContext(ctx, &forkchoiceResult, "engine_forkchoiceUpdatedV3",
engine.ForkchoiceStateV1{
HeadBlockHash: blockHash,
SafeBlockHash: blockHash,
// FinalizedBlockHash: blockHash,
},
&engine.PayloadAttributes{
Timestamp: timestamp,
Random: c.derivePrevRandao(latestHeight + 1),
SuggestedFeeRecipient: c.feeRecipient,
BeaconRoot: &c.genesisHash,
Withdrawals: []*types.Withdrawal{},
},
)
if err != nil {
return nil, fmt.Errorf("forkchoice update failed with error: %w", err)
}
if forkchoiceResult.PayloadStatus.Status != engine.VALID {
return nil, ErrInvalidPayloadStatus
}
c.payloadID = forkchoiceResult.PayloadID
}
var payloadResult engine.ExecutionPayloadEnvelope
err := c.engineClient.CallContext(ctx, &payloadResult, "engine_getPayloadV3", c.payloadID)
c.payloadID = nil
if err != nil {
return nil, fmt.Errorf("engine_getPayloadV3 failed: %w", err)
}
// Store the original transactions
originalTxs := payloadResult.ExecutionPayload.Transactions
// Clear transactions before serializing
payloadResult.ExecutionPayload.Transactions = [][]byte{}
jsonPayloadResult, err := json.Marshal(payloadResult)
if err != nil {
return nil, fmt.Errorf("failed to serialize payloadResult: %w", err)
}
// Create the result with serialized payload as first tx, followed by original transactions
txs := make([][]byte, len(originalTxs)+1)
txs[0] = jsonPayloadResult
for i, tx := range originalTxs {
txs[i+1] = tx
}
return txs, nil
}
// ExecuteTxs executes the given transactions at the specified block height and timestamp
func (c *PureEngineClient) ExecuteTxs(ctx context.Context, txs [][]byte, blockHeight uint64, timestamp time.Time, prevStateRoot []byte) (updatedStateRoot []byte, maxBytes uint64, err error) {
// special handling of block 1 (rollkit expects this to be genesis block)
if blockHeight == 1 && len(txs) == 0 {
_, stateRoot, gasLimit, err := c.getBlockInfo(ctx, blockHeight)
return stateRoot[:], gasLimit, err
}
var payloadResult engine.ExecutionPayloadEnvelope
// First tx is the serialized payload
firstTx := txs[0]
err = json.Unmarshal(firstTx, &payloadResult)
if err != nil {
return nil, 0, fmt.Errorf("failed to deserialize first transaction as ExecutionPayload: %w", err)
}
// Add transactions from txs to the payload (skip the first one which is the payload itself)
payloadResult.ExecutionPayload.Transactions = make([][]byte, len(txs)-1)
for i := 1; i < len(txs); i++ {
payloadResult.ExecutionPayload.Transactions[i-1] = txs[i]
}
var newPayloadResult engine.PayloadStatusV1
err = c.engineClient.CallContext(ctx, &newPayloadResult, "engine_newPayloadV3",
payloadResult.ExecutionPayload,
[]string{}, // No blob hashes
c.genesisHash.Hex(),
)
if err != nil {
return nil, 0, fmt.Errorf("new payload submission failed: %w", err)
}
if newPayloadResult.Status != engine.VALID {
return nil, 0, fmt.Errorf("new payload submission failed with: %s", *newPayloadResult.ValidationError)
}
// forkchoice update
blockHash := payloadResult.ExecutionPayload.BlockHash
var forkchoiceResult engine.ForkChoiceResponse
err = c.engineClient.CallContext(ctx, &forkchoiceResult, "engine_forkchoiceUpdatedV3",
engine.ForkchoiceStateV1{
HeadBlockHash: blockHash,
SafeBlockHash: blockHash,
},
&engine.PayloadAttributes{
Timestamp: uint64(timestamp.Unix()), //nolint:gosec // disable G115
Random: c.derivePrevRandao(blockHeight),
SuggestedFeeRecipient: c.feeRecipient,
BeaconRoot: &c.genesisHash,
Withdrawals: []*types.Withdrawal{},
},
)
if err != nil {
return nil, 0, fmt.Errorf("forkchoice update failed with error: %w", err)
}
if forkchoiceResult.PayloadStatus.Status != engine.VALID {
return nil, 0, ErrInvalidPayloadStatus
}
c.payloadID = forkchoiceResult.PayloadID
return payloadResult.ExecutionPayload.StateRoot.Bytes(), payloadResult.ExecutionPayload.GasLimit, nil
}
// SetFinal marks the block at the given height as finalized
func (c *PureEngineClient) SetFinal(ctx context.Context, blockHeight uint64) error {
blockHash, _, _, err := c.getBlockInfo(ctx, blockHeight)
if err != nil {
return fmt.Errorf("failed to get block info: %w", err)
}
var forkchoiceResult engine.ForkChoiceResponse
err = c.engineClient.CallContext(ctx, &forkchoiceResult, "engine_forkchoiceUpdatedV3",
engine.ForkchoiceStateV1{
HeadBlockHash: blockHash,
SafeBlockHash: blockHash,
FinalizedBlockHash: blockHash,
},
nil,
)
if err != nil {
return fmt.Errorf("forkchoice update failed with error: %w", err)
}
if forkchoiceResult.PayloadStatus.Status != engine.VALID {
return ErrInvalidPayloadStatus
}
return nil
}
func (c *PureEngineClient) derivePrevRandao(blockHeight uint64) common.Hash {
return common.BigToHash(new(big.Int).SetUint64(blockHeight))
}
func (c *PureEngineClient) getBlockInfo(ctx context.Context, height uint64) (common.Hash, common.Hash, uint64, error) {
header, err := c.ethClient.HeaderByNumber(ctx, new(big.Int).SetUint64(height))
if err != nil {
return common.Hash{}, common.Hash{}, 0, fmt.Errorf("failed to get block at height %d: %w", height, err)
}
return header.Hash(), header.Root, header.GasLimit, nil
}
func decodeSecret(jwtSecret string) ([]byte, error) {
secret, err := hex.DecodeString(strings.TrimPrefix(jwtSecret, "0x"))
if err != nil {
return nil, fmt.Errorf("failed to decode JWT secret: %w", err)
}
return secret, nil
}
func getAuthToken(jwtSecret []byte) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"exp": time.Now().Add(time.Hour * 1).Unix(), // Expires in 1 hour
"iat": time.Now().Unix(),
})
// Sign the token with the decoded secret
authToken, err := token.SignedString(jwtSecret)
if err != nil {
return "", fmt.Errorf("failed to sign JWT token: %w", err)
}
return authToken, nil
}