generated from rollkit/template-da-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexecution.go
360 lines (312 loc) · 10.7 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package execution
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
"net/http"
"strings"
"time"
"github.com/golang-jwt/jwt/v5"
"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"
execution "github.com/rollkit/go-execution"
execution_types "github.com/rollkit/go-execution/types"
)
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 = (*EngineAPIExecutionClient)(nil)
// EngineAPIExecutionClient implements the execution.Execute interface
type EngineAPIExecutionClient struct {
engineClient *rpc.Client // engine api
ethClient *ethclient.Client
genesisHash common.Hash
initialHeight uint64
feeRecipient common.Address
}
// NewEngineAPIExecutionClient creates a new instance of EngineAPIExecutionClient
func NewEngineAPIExecutionClient(
ethURL,
engineURL string,
jwtSecret string,
genesisHash common.Hash,
feeRecipient common.Address,
) (*EngineAPIExecutionClient, 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 {
ethClient.Close() // Clean up eth client if engine client fails
return nil, err
}
return &EngineAPIExecutionClient{
engineClient: engineClient,
ethClient: ethClient,
genesisHash: genesisHash,
initialHeight: 1, // set to 1 and updated in InitChain
feeRecipient: feeRecipient,
}, nil
}
// Start starts the execution client
func (c *EngineAPIExecutionClient) Start() error {
return nil
}
// Stop stops the execution client and closes all connections
func (c *EngineAPIExecutionClient) Stop() {
if c.engineClient != nil {
c.engineClient.Close()
}
if c.ethClient != nil {
c.ethClient.Close()
}
}
// InitChain initializes the blockchain with genesis information
func (c *EngineAPIExecutionClient) InitChain(ctx context.Context, genesisTime time.Time, initialHeight uint64, chainID string) (execution_types.Hash, uint64, error) {
var forkchoiceResult engine.ForkChoiceResponse
err := c.engineClient.CallContext(ctx, &forkchoiceResult, "engine_forkchoiceUpdatedV3",
engine.ForkchoiceStateV1{
HeadBlockHash: c.genesisHash,
SafeBlockHash: c.genesisHash,
FinalizedBlockHash: c.genesisHash,
},
engine.PayloadAttributes{
Timestamp: uint64(genesisTime.Unix()), //nolint:gosec // disable G115
Random: common.Hash{},
SuggestedFeeRecipient: c.feeRecipient,
BeaconRoot: &c.genesisHash,
Withdrawals: []*types.Withdrawal{},
},
)
if err != nil {
return execution_types.Hash{}, 0, fmt.Errorf("engine_forkchoiceUpdatedV3 failed: %w", err)
}
if forkchoiceResult.PayloadID == nil {
return execution_types.Hash{}, 0, ErrNilPayloadStatus
}
// Retrieve the Genesis Execution Payload
// Ensures the execution client recognizes the genesis block.
var payloadResult engine.ExecutionPayloadEnvelope
err = c.engineClient.CallContext(ctx, &payloadResult, "engine_getPayloadV3", *forkchoiceResult.PayloadID)
if err != nil {
return execution_types.Hash{}, 0, fmt.Errorf("engine_getPayloadV3 failed: %w", err)
}
// Confirm Finalization Using engine_forkchoiceUpdatedV3 Again
// Ensures that the genesis block is permanently finalized.
blockHash := c.genesisHash
err = c.setFinal(ctx, blockHash)
if err != nil {
return execution_types.Hash{}, 0, err
}
stateRoot := payloadResult.ExecutionPayload.StateRoot
rollkitStateRoot := execution_types.Hash(stateRoot[:])
gasLimit := payloadResult.ExecutionPayload.GasLimit
c.initialHeight = initialHeight
return rollkitStateRoot, gasLimit, nil
}
// GetTxs retrieves transactions from the transaction pool
func (c *EngineAPIExecutionClient) GetTxs(ctx context.Context) ([]execution_types.Tx, error) {
var result struct {
Pending map[string]map[string]*types.Transaction `json:"pending"`
Queued map[string]map[string]*types.Transaction `json:"queued"`
}
err := c.ethClient.Client().CallContext(ctx, &result, "txpool_content")
if err != nil {
return nil, fmt.Errorf("failed to get tx pool content: %w", err)
}
var txs []execution_types.Tx
// add pending txs
for _, accountTxs := range result.Pending {
for _, tx := range accountTxs {
txBytes, err := tx.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("failed to marshal transaction: %w", err)
}
txs = append(txs, execution_types.Tx(txBytes))
}
}
// add queued txs
for _, accountTxs := range result.Queued {
for _, tx := range accountTxs {
txBytes, err := tx.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("failed to marshal transaction: %w", err)
}
txs = append(txs, execution_types.Tx(txBytes))
}
}
return txs, nil
}
func (c *EngineAPIExecutionClient) getBlockHash(ctx context.Context, height uint64) (common.Hash, error) {
var block map[string]interface{}
err := c.engineClient.CallContext(
ctx, &block, "eth_getBlockByNumber", rpc.BlockNumber(height), false) //nolint:gosec // disable G115
if err != nil {
return common.Hash{}, fmt.Errorf("failed to get block at height %d: %w", height, err)
}
// Extract the block hash
blockHashStr, ok := block["hash"].(string)
if !ok {
return common.Hash{}, fmt.Errorf("failed to get block hash at height %d", height)
}
return common.HexToHash(blockHashStr), nil
}
// ExecuteTxs executes the given transactions and returns the new state root and gas used
func (c *EngineAPIExecutionClient) ExecuteTxs(ctx context.Context, txs []execution_types.Tx, height uint64, timestamp time.Time, prevStateRoot execution_types.Hash) (execution_types.Hash, uint64, error) {
// convert rollkit tx to eth tx
ethTxs := make([]*types.Transaction, len(txs))
for i, tx := range txs {
ethTxs[i] = new(types.Transaction)
err := ethTxs[i].UnmarshalBinary(tx)
if err != nil {
return execution_types.Hash{}, 0, fmt.Errorf("failed to unmarshal transaction: %w", err)
}
}
// encode
txsPayload := make([][]byte, len(txs))
for i, tx := range ethTxs {
buf := bytes.Buffer{}
err := tx.EncodeRLP(&buf)
if err != nil {
return execution_types.Hash{}, 0, fmt.Errorf("failed to RLP encode tx: %w", err)
}
txsPayload[i] = buf.Bytes()
}
// fetch previous block hash to update forkchoice for the next payload id
var err error
var prevBlockHash common.Hash
if height == c.initialHeight {
prevBlockHash = c.genesisHash
} else {
prevBlockHash, err = c.getBlockHash(ctx, height-1)
if err != nil {
return execution_types.Hash{}, 0, err
}
}
// update forkchoice to get the next payload id
var forkchoiceResult engine.ForkChoiceResponse
err = c.engineClient.CallContext(ctx, &forkchoiceResult, "engine_forkchoiceUpdatedV3",
engine.ForkchoiceStateV1{
HeadBlockHash: prevBlockHash,
SafeBlockHash: prevBlockHash,
FinalizedBlockHash: prevBlockHash,
},
&engine.PayloadAttributes{
Timestamp: uint64(timestamp.Unix()), //nolint:gosec // disable G115
Random: c.derivePrevRandao(height),
SuggestedFeeRecipient: c.feeRecipient,
Withdrawals: []*types.Withdrawal{},
BeaconRoot: &c.genesisHash,
},
)
if err != nil {
return execution_types.Hash{}, 0, fmt.Errorf("forkchoice update failed: %w", err)
}
if forkchoiceResult.PayloadID == nil {
return execution_types.Hash{}, 0, ErrNilPayloadStatus
}
// get payload
var payloadResult engine.ExecutionPayloadEnvelope
err = c.engineClient.CallContext(ctx, &payloadResult, "engine_getPayloadV3", *forkchoiceResult.PayloadID)
if err != nil {
return execution_types.Hash{}, 0, fmt.Errorf("get payload failed: %w", err)
}
// submit payload
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 execution_types.Hash{}, 0, fmt.Errorf("new payload submission failed: %w", err)
}
if newPayloadResult.Status != engine.VALID {
return execution_types.Hash{}, 0, ErrInvalidPayloadStatus
}
// forkchoice update
blockHash := payloadResult.ExecutionPayload.BlockHash
err = c.setFinal(ctx, blockHash)
if err != nil {
return execution_types.Hash{}, 0, err
}
return payloadResult.ExecutionPayload.StateRoot.Bytes(), payloadResult.ExecutionPayload.GasUsed, nil
}
func (c *EngineAPIExecutionClient) setFinal(ctx context.Context, blockHash common.Hash) error {
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
}
// SetFinal marks a block at the given height as final
func (c *EngineAPIExecutionClient) SetFinal(ctx context.Context, height uint64) error {
blockHash, err := c.getBlockHash(ctx, height)
if err != nil {
return err
}
err = c.setFinal(ctx, blockHash)
if err != nil {
return err
}
return nil
}
// derivePrevRandao generates a deterministic prevRandao value based on block height
func (c *EngineAPIExecutionClient) derivePrevRandao(blockHeight uint64) common.Hash {
return common.BigToHash(big.NewInt(int64(blockHeight))) //nolint:gosec // disable G115
}
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
}