-
Notifications
You must be signed in to change notification settings - Fork 31
/
batch_executor.go
479 lines (412 loc) · 18 KB
/
batch_executor.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package components
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"sort"
"sync"
"github.com/holiman/uint256"
"github.com/ten-protocol/go-ten/go/config"
"github.com/ten-protocol/go-ten/go/common/gethencoding"
"github.com/ten-protocol/go-ten/go/enclave/gas"
"github.com/ten-protocol/go-ten/go/enclave/storage"
gethcommon "github.com/ethereum/go-ethereum/common"
smt "github.com/FantasyJony/openzeppelin-merkle-tree-go/standard_merkle_tree"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/ten-protocol/go-ten/go/common"
"github.com/ten-protocol/go-ten/go/common/errutil"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/common/measure"
"github.com/ten-protocol/go-ten/go/enclave/core"
"github.com/ten-protocol/go-ten/go/enclave/crosschain"
"github.com/ten-protocol/go-ten/go/enclave/evm"
"github.com/ten-protocol/go-ten/go/enclave/genesis"
)
var ErrNoTransactionsToProcess = fmt.Errorf("no transactions to process")
// batchExecutor - the component responsible for executing batches
type batchExecutor struct {
storage storage.Storage
batchRegistry BatchRegistry
config config.EnclaveConfig
gethEncodingService gethencoding.EncodingService
crossChainProcessors *crosschain.Processors
genesis *genesis.Genesis
logger gethlog.Logger
gasOracle gas.Oracle
chainConfig *params.ChainConfig
// stateDBMutex - used to protect calls to stateDB.Commit as it is not safe for async access.
stateDBMutex sync.Mutex
batchGasLimit uint64 // max execution gas allowed in a batch
}
func NewBatchExecutor(
storage storage.Storage,
batchRegistry BatchRegistry,
config config.EnclaveConfig,
gethEncodingService gethencoding.EncodingService,
cc *crosschain.Processors,
genesis *genesis.Genesis,
gasOracle gas.Oracle,
chainConfig *params.ChainConfig,
batchGasLimit uint64,
logger gethlog.Logger,
) BatchExecutor {
return &batchExecutor{
storage: storage,
batchRegistry: batchRegistry,
config: config,
gethEncodingService: gethEncodingService,
crossChainProcessors: cc,
genesis: genesis,
chainConfig: chainConfig,
logger: logger,
gasOracle: gasOracle,
stateDBMutex: sync.Mutex{},
batchGasLimit: batchGasLimit,
}
}
// filterTransactionsWithSufficientFunds - this function estimates hte l1 fees for the transaction in a given batch execution context. It does so by taking the price of the
// pinned L1 block and using it as the cost per gas for the estimated gas of the calldata encoding of a transaction. It filters out any transactions that cannot afford to pay for their L1
// publishing cost.
func (executor *batchExecutor) filterTransactionsWithSufficientFunds(ctx context.Context, stateDB *state.StateDB, context *BatchExecutionContext) (common.L2PricedTransactions, common.L2PricedTransactions) {
transactions := make(common.L2PricedTransactions, 0)
freeTransactions := make(common.L2PricedTransactions, 0)
block, _ := executor.storage.FetchBlock(ctx, context.BlockPtr)
for _, tx := range context.Transactions {
sender, err := core.GetAuthenticatedSender(context.ChainConfig.ChainID.Int64(), tx)
if err != nil {
executor.logger.Error("Unable to extract sender for tx. Should not happen at this point.", log.TxKey, tx.Hash(), log.ErrKey, err)
continue
}
accBalance := stateDB.GetBalance(*sender)
cost, err := executor.gasOracle.EstimateL1StorageGasCost(tx, block)
if err != nil {
executor.logger.Error("Unable to get gas cost for tx. Should not happen at this point.", log.TxKey, tx.Hash(), log.ErrKey, err)
continue
}
// Transactions that are created inside the enclave can have no GasPrice set.
// External transactions are always required to have a gas price set. Thus we filter
// those transactions for separate processing than the normal ones and we run them through the EVM
// with a flag that disables the baseFee logic and wont fail them for having price lower than the base fee.
isFreeTransaction := tx.GasFeeCap().Cmp(gethcommon.Big0) == 0
isFreeTransaction = isFreeTransaction && tx.GasPrice().Cmp(gethcommon.Big0) == 0
if isFreeTransaction {
freeTransactions = append(freeTransactions, common.L2PricedTransaction{
Tx: tx,
PublishingCost: big.NewInt(0),
})
continue
}
if accBalance.Cmp(uint256.MustFromBig(cost)) == -1 {
executor.logger.Info(fmt.Sprintf("insufficient account balance for tx - want: %d have: %d", cost, accBalance), log.TxKey, tx.Hash(), "addr", sender.Hex())
continue
}
transactions = append(transactions, common.L2PricedTransaction{
Tx: tx,
PublishingCost: big.NewInt(0).Set(cost),
})
}
return transactions, freeTransactions
}
func (executor *batchExecutor) ComputeBatch(ctx context.Context, context *BatchExecutionContext, failForEmptyBatch bool) (*ComputedBatch, error) { //nolint:gocognit
defer core.LogMethodDuration(executor.logger, measure.NewStopwatch(), "Batch context processed")
// sanity check that the l1 block exists. We don't have to execute batches of forks.
block, err := executor.storage.FetchBlock(ctx, context.BlockPtr)
if errors.Is(err, errutil.ErrNotFound) {
return nil, errutil.ErrBlockForBatchNotFound
} else if err != nil {
return nil, fmt.Errorf("failed to retrieve block %s for batch. Cause: %w", context.BlockPtr, err)
}
// These variables will be used to create the new batch
parent, err := executor.storage.FetchBatch(ctx, context.ParentPtr)
if errors.Is(err, errutil.ErrNotFound) {
executor.logger.Error(fmt.Sprintf("can't find parent batch %s. Seq %d", context.ParentPtr, context.SequencerNo))
return nil, errutil.ErrAncestorBatchNotFound
}
if err != nil {
return nil, fmt.Errorf("failed to retrieve parent batch %s. Cause: %w", context.ParentPtr, err)
}
parentBlock := block
if parent.Header.L1Proof != block.Hash() {
var err error
parentBlock, err = executor.storage.FetchBlock(ctx, parent.Header.L1Proof)
if err != nil {
executor.logger.Error(fmt.Sprintf("Could not retrieve a proof for batch %s", parent.Hash()), log.ErrKey, err)
return nil, err
}
}
// Create a new batch based on the fromBlock of inclusion of the previous, including all new transactions
batch := core.DeterministicEmptyBatch(parent.Header, block, context.AtTime, context.SequencerNo, context.BaseFee, context.Creator)
stateDB, err := executor.batchRegistry.GetBatchState(ctx, &batch.Header.ParentHash)
if err != nil {
return nil, fmt.Errorf("could not create stateDB. Cause: %w", err)
}
snap := stateDB.Snapshot()
var messages common.CrossChainMessages
var transfers common.ValueTransferEvents
if context.SequencerNo.Int64() > int64(common.L2GenesisSeqNo+1) {
messages, transfers = executor.crossChainProcessors.Local.RetrieveInboundMessages(ctx, parentBlock, block, stateDB)
}
crossChainTransactions := executor.crossChainProcessors.Local.CreateSyntheticTransactions(ctx, messages, stateDB)
executor.crossChainProcessors.Local.ExecuteValueTransfers(ctx, transfers, stateDB)
transactionsToProcess, freeTransactions := executor.filterTransactionsWithSufficientFunds(ctx, stateDB, context)
xchainTxs := make(common.L2PricedTransactions, 0)
for _, xTx := range crossChainTransactions {
xchainTxs = append(xchainTxs, common.L2PricedTransaction{
Tx: xTx,
PublishingCost: big.NewInt(0),
})
}
syntheticTransactions := append(xchainTxs, freeTransactions...)
// fromTxIndex - Here we start from the 0 index. This will be the same for a validator.
successfulTxs, excludedTxs, txReceipts, err := executor.processTransactions(ctx, batch, 0, transactionsToProcess, stateDB, context.ChainConfig, false)
if err != nil {
return nil, fmt.Errorf("could not process transactions. Cause: %w", err)
}
// fromTxIndex - Here we start from the len of the successful transactions; As long as we have the exact same successful transactions in a batch,
// we will start from the same place.
ccSuccessfulTxs, _, ccReceipts, err := executor.processTransactions(ctx, batch, len(successfulTxs), syntheticTransactions, stateDB, context.ChainConfig, true)
if err != nil {
return nil, err
}
if err = executor.verifyInboundCrossChainTransactions(syntheticTransactions, ccSuccessfulTxs, ccReceipts); err != nil {
return nil, fmt.Errorf("batch computation failed due to cross chain messages. Cause: %w", err)
}
if failForEmptyBatch &&
len(txReceipts) == 0 &&
len(ccReceipts) == 0 &&
len(transactionsToProcess)-len(excludedTxs) == 0 &&
len(crossChainTransactions) == 0 &&
len(messages) == 0 &&
len(transfers) == 0 {
if snap > 0 {
//// revert any unexpected mutation to the statedb
stateDB.RevertToSnapshot(snap)
}
return nil, ErrNoTransactionsToProcess
}
// we need to copy the batch to reset the internal hash cache
copyBatch := *batch
copyBatch.Header.Root = stateDB.IntermediateRoot(false)
copyBatch.Transactions = append(successfulTxs, freeTransactions.ToTransactions()...)
copyBatch.ResetHash()
if err = executor.populateOutboundCrossChainData(ctx, ©Batch, block, txReceipts); err != nil {
return nil, fmt.Errorf("failed adding cross chain data to batch. Cause: %w", err)
}
allReceipts := append(txReceipts, ccReceipts...)
executor.populateHeader(©Batch, allReceipts)
// the logs and receipts produced by the EVM have the wrong hash which must be adjusted
for _, receipt := range allReceipts {
receipt.BlockHash = copyBatch.Hash()
for _, l := range receipt.Logs {
l.BlockHash = copyBatch.Hash()
}
}
return &ComputedBatch{
Batch: ©Batch,
Receipts: allReceipts,
Commit: func(deleteEmptyObjects bool) (gethcommon.Hash, error) {
executor.stateDBMutex.Lock()
defer executor.stateDBMutex.Unlock()
h, err := stateDB.Commit(copyBatch.Number().Uint64(), deleteEmptyObjects)
if err != nil {
return gethcommon.Hash{}, fmt.Errorf("commit failure for batch %d. Cause: %w", batch.SeqNo(), err)
}
trieDB := executor.storage.TrieDB()
err = trieDB.Commit(h, false)
return h, err
},
}, nil
}
func (executor *batchExecutor) ExecuteBatch(ctx context.Context, batch *core.Batch) (types.Receipts, error) {
defer core.LogMethodDuration(executor.logger, measure.NewStopwatch(), "Executed batch", log.BatchHashKey, batch.Hash())
// Validators recompute the entire batch using the same batch context
// if they have all necessary prerequisites like having the l1 block processed
// and the parent hash. This recomputed batch is then checked against the incoming batch.
// If the sequencer has tampered with something the hash will not add up and validation will
// produce an error.
cb, err := executor.ComputeBatch(ctx, &BatchExecutionContext{
BlockPtr: batch.Header.L1Proof,
ParentPtr: batch.Header.ParentHash,
Transactions: batch.Transactions,
AtTime: batch.Header.Time,
ChainConfig: executor.chainConfig,
SequencerNo: batch.Header.SequencerOrderNo,
Creator: batch.Header.Coinbase,
BaseFee: batch.Header.BaseFee,
}, false) // this execution is not used when first producing a batch, we never want to fail for empty batches
if err != nil {
return nil, fmt.Errorf("failed computing batch %s. Cause: %w", batch.Hash(), err)
}
if cb.Batch.Hash() != batch.Hash() {
// todo @stefan - generate a validator challenge here and return it
executor.logger.Error(fmt.Sprintf("Error validating batch. Calculated: %+v Incoming: %+v\n", cb.Batch.Header, batch.Header))
return nil, fmt.Errorf("batch is in invalid state. Incoming hash: %s Computed hash: %s", batch.Hash(), cb.Batch.Hash())
}
if _, err := cb.Commit(true); err != nil {
return nil, fmt.Errorf("cannot commit stateDB for incoming valid batch %s. Cause: %w", batch.Hash(), err)
}
return cb.Receipts, nil
}
func (executor *batchExecutor) CreateGenesisState(
ctx context.Context,
blkHash common.L1BlockHash,
timeNow uint64,
coinbase gethcommon.Address,
baseFee *big.Int,
) (*core.Batch, *types.Transaction, error) {
preFundGenesisState, err := executor.genesis.GetGenesisRoot(executor.storage)
if err != nil {
return nil, nil, err
}
genesisBatch := &core.Batch{
Header: &common.BatchHeader{
ParentHash: common.L2BatchHash{},
L1Proof: blkHash,
Root: *preFundGenesisState,
TxHash: types.EmptyRootHash,
Number: big.NewInt(int64(0)),
SequencerOrderNo: big.NewInt(int64(common.L2GenesisSeqNo)), // genesis batch has seq number 1
ReceiptHash: types.EmptyRootHash,
CrossChainRoot: types.EmptyRootHash,
Time: timeNow,
Coinbase: coinbase,
BaseFee: baseFee,
GasLimit: executor.batchGasLimit,
},
Transactions: []*common.L2Tx{},
}
// todo (#1577) - figure out a better way to bootstrap the system contracts
deployTx, err := executor.crossChainProcessors.Local.GenerateMessageBusDeployTx()
if err != nil {
executor.logger.Crit("Could not create message bus deployment transaction", "Error", err)
}
executor.logger.Info("L2 Bus deploy", log.TxKey, deployTx.Hash())
if err = executor.genesis.CommitGenesisState(executor.storage); err != nil {
return nil, nil, fmt.Errorf("could not apply genesis preallocation. Cause: %w", err)
}
return genesisBatch, deployTx, nil
}
func (executor *batchExecutor) populateOutboundCrossChainData(ctx context.Context, batch *core.Batch, block *types.Block, receipts types.Receipts) error {
crossChainMessages, err := executor.crossChainProcessors.Local.ExtractOutboundMessages(ctx, receipts)
if err != nil {
executor.logger.Error("Failed extracting L2->L1 messages", log.ErrKey, err, log.CmpKey, log.CrossChainCmp)
return fmt.Errorf("could not extract cross chain messages. Cause: %w", err)
}
valueTransferMessages, err := executor.crossChainProcessors.Local.ExtractOutboundTransfers(ctx, receipts)
if err != nil {
executor.logger.Error("Failed extracting L2->L1 messages value transfers", log.ErrKey, err, log.CmpKey, log.CrossChainCmp)
return fmt.Errorf("could not extract cross chain value transfers. Cause: %w", err)
}
xchainTree := make([][]interface{}, 0)
hasMessages := false
if len(valueTransferMessages) > 0 {
transfers := crosschain.ValueTransfers(valueTransferMessages).ForMerkleTree()
xchainTree = append(xchainTree, transfers...)
hasMessages = true
}
if len(crossChainMessages) > 0 {
messages := crosschain.MessageStructs(crossChainMessages).ForMerkleTree()
xchainTree = append(xchainTree, messages...)
hasMessages = true
}
var xchainHash gethcommon.Hash = gethcommon.BigToHash(gethcommon.Big0)
if hasMessages {
tree, err := smt.Of(xchainTree, crosschain.CrossChainEncodings)
if err != nil {
executor.logger.Error("Unable to create merkle tree for cross chain messages", log.ErrKey, err)
return fmt.Errorf("unable to create merkle tree for cross chain messages. Cause: %w", err)
}
encodedTree, err := json.Marshal(xchainTree)
if err != nil {
panic(err) // todo: figure out what to do
}
batch.Header.CrossChainTree = encodedTree
xchainHash = gethcommon.BytesToHash(tree.GetRoot())
executor.logger.Info("[CrossChain] adding messages to batch")
}
batch.Header.CrossChainMessages = crossChainMessages
batch.Header.CrossChainRoot = xchainHash
executor.logger.Trace(fmt.Sprintf("Added %d cross chain messages to batch.",
len(batch.Header.CrossChainMessages)), log.CmpKey, log.CrossChainCmp)
batch.Header.LatestInboundCrossChainHash = block.Hash()
batch.Header.LatestInboundCrossChainHeight = block.Number()
return nil
}
func (executor *batchExecutor) populateHeader(batch *core.Batch, receipts types.Receipts) {
if len(receipts) == 0 {
batch.Header.ReceiptHash = types.EmptyRootHash
} else {
batch.Header.ReceiptHash = types.DeriveSha(receipts, trie.NewStackTrie(nil))
}
if len(batch.Transactions) == 0 {
batch.Header.TxHash = types.EmptyRootHash
} else {
batch.Header.TxHash = types.DeriveSha(types.Transactions(batch.Transactions), trie.NewStackTrie(nil))
}
}
func (executor *batchExecutor) verifyInboundCrossChainTransactions(transactions common.L2PricedTransactions, executedTxs types.Transactions, receipts types.Receipts) error {
if len(transactions) != executedTxs.Len() {
return fmt.Errorf("some synthetic transactions have not been executed")
}
for _, rec := range receipts {
if rec.Status == 1 {
continue
}
return fmt.Errorf("found a failed receipt for a synthetic transaction: %s", rec.TxHash.Hex())
}
return nil
}
func (executor *batchExecutor) processTransactions(
ctx context.Context,
batch *core.Batch,
tCount int,
txs common.L2PricedTransactions,
stateDB *state.StateDB,
cc *params.ChainConfig,
noBaseFee bool,
) ([]*common.L2Tx, []*common.L2Tx, []*types.Receipt, error) {
var executedTransactions []*common.L2Tx
var excludedTransactions []*common.L2Tx
var txReceipts []*types.Receipt
txResults := evm.ExecuteTransactions(
ctx,
txs,
stateDB,
batch.Header,
executor.storage,
executor.gethEncodingService,
cc,
executor.config,
tCount,
noBaseFee,
executor.batchGasLimit,
executor.logger,
)
for _, tx := range txs {
result, f := txResults[tx.Tx.Hash()]
if !f {
return nil, nil, nil, fmt.Errorf("there should be an entry for each transaction")
}
rec, foundReceipt := result.(*types.Receipt)
if foundReceipt {
executedTransactions = append(executedTransactions, tx.Tx)
txReceipts = append(txReceipts, rec)
} else {
// Exclude all errors
excludedTransactions = append(excludedTransactions, tx.Tx)
executor.logger.Debug("Excluding transaction from batch", log.TxKey, tx.Tx.Hash(), log.BatchHashKey, batch.Hash(), "cause", result)
}
}
sort.Sort(sortByTxIndex(txReceipts))
return executedTransactions, excludedTransactions, txReceipts, nil
}
type sortByTxIndex []*types.Receipt
func (c sortByTxIndex) Len() int { return len(c) }
func (c sortByTxIndex) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c sortByTxIndex) Less(i, j int) bool { return c[i].TransactionIndex < c[j].TransactionIndex }