-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathbatch_commit.go
More file actions
117 lines (100 loc) · 4.43 KB
/
batch_commit.go
File metadata and controls
117 lines (100 loc) · 4.43 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
package node
import (
"bytes"
"fmt"
"math/big"
"morph-l2/node/types"
"github.com/morph-l2/go-ethereum/accounts/abi/bind"
eth "github.com/morph-l2/go-ethereum/core/types"
"github.com/tendermint/tendermint/l2node"
tmtypes "github.com/tendermint/tendermint/types"
)
// CommitBatch commits the sealed batch. It does nothing if no batch header is sealed.
// It should be called when the current block is confirmed.
func (e *Executor) CommitBatch(currentBlockBytes []byte, currentTxs tmtypes.Txs, blsDatas []l2node.BlsData) error {
// If no batch data is available, do nothing
if e.batchingCache.IsEmpty() || e.batchingCache.sealedBatchHeader == nil {
return nil
}
// Reconstruct current block context if needed
if !bytes.Equal(currentBlockBytes, e.batchingCache.currentBlockBytes) || !bytes.Equal(currentTxs.Hash(), e.batchingCache.currentTxsHash) {
e.logger.Info("Current block has changed. Reconstructing current context...")
if err := e.setCurrentBlock(currentBlockBytes, currentTxs); err != nil {
return fmt.Errorf("failed to set current block: %w", err)
}
}
// Get current block height
curHeight, err := types.HeightFromBlockBytes(e.batchingCache.currentBlockBytes)
if err != nil {
return fmt.Errorf("failed to parse current block height: %w", err)
}
// Convert BlsData to batch signatures (if applicable)
var batchSigs []eth.BatchSignature
if !e.devSequencer {
batchSigs, err = e.ConvertBlsDatas(blsDatas)
if err != nil {
return fmt.Errorf("failed to convert BLS data: %w", err)
}
}
// Get the sequencer set at current height - 1
callOpts := &bind.CallOpts{BlockNumber: big.NewInt(int64(curHeight - 1))}
sequencerSetBytes, err := e.sequencerCaller.GetSequencerSetBytes(callOpts)
if err != nil {
e.logger.Error("Failed to GetSequencerSetBytes", "blockHeight", curHeight-1, "error", err)
return fmt.Errorf("failed to get sequencer set bytes: %w", err)
}
// Encode batch data and commit batch to L2 client
blockContexts, err := e.batchingCache.batchData.Encode()
if err != nil {
return fmt.Errorf("failed to encode block contexts: %w", err)
}
version, err := e.batchingCache.sealedBatchHeader.Version()
if err != nil {
return fmt.Errorf("failed to get batch version: %w", err)
}
parentBatchIndex, _ := e.batchingCache.parentBatchHeader.BatchIndex()
hash, _ := e.batchingCache.sealedBatchHeader.Hash()
l1MessagePopped, _ := e.batchingCache.sealedBatchHeader.L1MessagePopped()
// batchsigs type convert
var ptrBatchSigs []*eth.BatchSignature
for _, sig := range batchSigs {
ptrBatchSigs = append(ptrBatchSigs, &sig)
}
if err = e.nodeDB.ImportBatch(ð.RollupBatch{
Version: uint(version),
Index: parentBatchIndex + 1,
Hash: hash,
ParentBatchHeader: *e.batchingCache.parentBatchHeader,
CurrentSequencerSetBytes: sequencerSetBytes,
BlockContexts: blockContexts,
PrevStateRoot: e.batchingCache.prevStateRoot,
PostStateRoot: e.batchingCache.postStateRoot,
WithdrawRoot: e.batchingCache.withdrawRoot,
Sidecar: e.batchingCache.sealedSidecar,
LastBlockNumber: e.batchingCache.lastPackedBlockHeight,
NumL1Messages: uint16(l1MessagePopped),
}, ptrBatchSigs); err != nil {
return fmt.Errorf("failed to store batch: %w", err)
}
// Update batch index metric
e.metrics.BatchIndex.Set(float64(parentBatchIndex + 1))
// Commit the batch and reset the cache for the next batch
e.commitSealedBatch(curHeight)
e.logger.Info("Committed batch", "batchIndex", parentBatchIndex+1)
return nil
}
// commitSealedBatch commits the sealed batch and resets cache for the next batch.
func (e *Executor) commitSealedBatch(curHeight uint64) {
e.batchingCache.parentBatchHeader = e.batchingCache.sealedBatchHeader
e.batchingCache.prevStateRoot = e.batchingCache.postStateRoot
e.batchingCache.sealedBatchHeader = nil
e.batchingCache.sealedSidecar = nil
e.batchingCache.totalL1MessagePopped = e.batchingCache.totalL1MessagePoppedAfterCurBlock
e.batchingCache.postStateRoot = e.batchingCache.currentStateRoot
e.batchingCache.withdrawRoot = e.batchingCache.currentWithdrawRoot
e.batchingCache.lastPackedBlockHeight = curHeight
// Reset batch data and current context
e.batchingCache.batchData = types.NewBatchData()
e.batchingCache.batchData.Append(e.batchingCache.currentBlockContext, e.batchingCache.currentTxsPayload, e.batchingCache.currentL1TxsHashes)
e.batchingCache.ClearCurrent()
}