-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathexecution.go
More file actions
357 lines (310 loc) · 10.7 KB
/
execution.go
File metadata and controls
357 lines (310 loc) · 10.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
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
package evm
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
"net/http"
"strings"
"sync"
"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 = (*EngineClient)(nil)
// EngineClient 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 EngineClient 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
initialHeight uint64
feeRecipient common.Address // Address to receive transaction fees
mu sync.Mutex // Mutex to protect concurrent access to block hashes
currentHeadBlockHash common.Hash // Store last non-finalized HeadBlockHash
currentSafeBlockHash common.Hash // Store last non-finalized SafeBlockHash
currentFinalizedBlockHash common.Hash // Store last finalized block hash
}
// NewEngineExecutionClient creates a new instance of EngineAPIExecutionClient
func NewEngineExecutionClient(
ethURL,
engineURL string,
jwtSecret string,
genesisHash common.Hash,
feeRecipient common.Address,
) (*EngineClient, 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 &EngineClient{
engineClient: engineClient,
ethClient: ethClient,
genesisHash: genesisHash,
feeRecipient: feeRecipient,
}, nil
}
// InitChain initializes the blockchain with the given genesis parameters
func (c *EngineClient) 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)
}
_, stateRoot, gasLimit, _, err := c.getBlockInfo(ctx, 0)
if err != nil {
return nil, 0, fmt.Errorf("failed to get block info: %w", err)
}
c.initialHeight = initialHeight
return stateRoot[:], gasLimit, nil
}
// GetTxs retrieves transactions from the current execution payload
func (c *EngineClient) GetTxs(ctx context.Context) ([][]byte, 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 [][]byte
// 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, 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, txBytes)
}
}
return txs, nil
}
// ExecuteTxs executes the given transactions at the specified block height and timestamp
func (c *EngineClient) ExecuteTxs(ctx context.Context, txs [][]byte, blockHeight uint64, timestamp time.Time, prevStateRoot []byte) (updatedStateRoot []byte, maxBytes uint64, err 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 nil, 0, fmt.Errorf("failed to unmarshal transaction: %w", err)
}
txHash := ethTxs[i].Hash()
_, isPending, err := c.ethClient.TransactionByHash(context.Background(), txHash)
if err == nil && isPending {
continue // skip SendTransaction
}
err = c.ethClient.SendTransaction(context.Background(), ethTxs[i])
if err != nil {
// Ignore the error if the transaction fails to be sent since transactions can be invalid and an invalid transaction sent by a client should not crash the node.
continue
}
}
// encode
txsPayload := make([][]byte, len(txs))
for i, tx := range ethTxs {
buf := bytes.Buffer{}
err := tx.EncodeRLP(&buf)
if err != nil {
return nil, 0, fmt.Errorf("failed to RLP encode tx: %w", err)
}
txsPayload[i] = buf.Bytes()
}
var (
prevBlockHash common.Hash
prevTimestamp uint64
)
// fetch previous block hash to update forkchoice for the next payload id
// if blockHeight == c.initialHeight {
// prevBlockHash = c.genesisHash
// } else {
prevBlockHash, _, _, prevTimestamp, err = c.getBlockInfo(ctx, blockHeight-1)
if err != nil {
return nil, 0, err
}
// }
// make sure that the timestamp is increasing
ts := uint64(timestamp.Unix())
if ts <= prevTimestamp {
ts = prevTimestamp + 1 // Subsequent blocks must have a higher timestamp.
}
// get current finalized block hash
c.mu.Lock()
finalizedBlockHash := c.currentFinalizedBlockHash
c.mu.Unlock()
// 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: finalizedBlockHash,
},
&engine.PayloadAttributes{
Timestamp: ts,
Random: prevBlockHash, //c.derivePrevRandao(height),
SuggestedFeeRecipient: c.feeRecipient,
Withdrawals: []*types.Withdrawal{},
BeaconRoot: &c.genesisHash,
Transactions: txsPayload,
NoTxPool: true,
},
)
if err != nil {
return nil, 0, fmt.Errorf("forkchoice update failed: %w", err)
}
if forkchoiceResult.PayloadID == nil {
return nil, 0, ErrNilPayloadStatus
}
// get payload
var payloadResult engine.ExecutionPayloadEnvelope
err = c.engineClient.CallContext(ctx, &payloadResult, "engine_getPayloadV4", *forkchoiceResult.PayloadID)
if err != nil {
return nil, 0, fmt.Errorf("get payload failed: %w", err)
}
// submit payload
var newPayloadResult engine.PayloadStatusV1
err = c.engineClient.CallContext(ctx, &newPayloadResult, "engine_newPayloadV4",
payloadResult.ExecutionPayload,
[]string{}, // No blob hashes
c.genesisHash.Hex(),
[][]byte{}, // No execution requests
)
if err != nil {
return nil, 0, fmt.Errorf("new payload submission failed: %w", err)
}
if newPayloadResult.Status != engine.VALID {
return nil, 0, ErrInvalidPayloadStatus
}
// forkchoice update
blockHash := payloadResult.ExecutionPayload.BlockHash
err = c.setFinal(ctx, blockHash, false)
if err != nil {
return nil, 0, err
}
return payloadResult.ExecutionPayload.StateRoot.Bytes(), payloadResult.ExecutionPayload.GasUsed, nil
}
func (c *EngineClient) setFinal(ctx context.Context, blockHash common.Hash, isFinal bool) error {
c.mu.Lock()
// Update block hashes based on finalization status
if isFinal {
c.currentFinalizedBlockHash = blockHash
} else {
c.currentHeadBlockHash = blockHash
c.currentSafeBlockHash = blockHash
}
// Construct forkchoice state
args := engine.ForkchoiceStateV1{
HeadBlockHash: c.currentHeadBlockHash,
SafeBlockHash: c.currentSafeBlockHash,
FinalizedBlockHash: c.currentFinalizedBlockHash,
}
c.mu.Unlock()
var forkchoiceResult engine.ForkChoiceResponse
err := c.engineClient.CallContext(ctx, &forkchoiceResult, "engine_forkchoiceUpdatedV3",
args,
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 the block at the given height as finalized
func (c *EngineClient) 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)
}
return c.setFinal(ctx, blockHash, true)
}
func (c *EngineClient) derivePrevRandao(blockHeight uint64) common.Hash {
return common.BigToHash(new(big.Int).SetUint64(blockHeight))
}
func (c *EngineClient) getBlockInfo(ctx context.Context, height uint64) (common.Hash, common.Hash, uint64, uint64, error) {
header, err := c.ethClient.HeaderByNumber(ctx, new(big.Int).SetUint64(height))
if err != nil {
return common.Hash{}, common.Hash{}, 0, 0, fmt.Errorf("failed to get block at height %d: %w", height, err)
}
return header.Hash(), header.Root, header.GasLimit, header.Time, nil
}
// decodeSecret decodes a hex-encoded JWT secret string into a byte slice.
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
}
// getAuthToken creates a JWT token signed with the provided secret, valid for 1 hour.
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
}