Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ddbc486

Browse files
authoredJul 2, 2024
Merge branch 'develop' into jt/eth-68
2 parents 95b6b92 + ef980a2 commit ddbc486

File tree

5 files changed

+90
-13
lines changed

5 files changed

+90
-13
lines changed
 

‎core/rawdb/accessors_rollup_event.go

+27
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,30 @@ func ReadFinalizedL2BlockNumber(db ethdb.Reader) *uint64 {
144144
finalizedL2BlockNumber := number.Uint64()
145145
return &finalizedL2BlockNumber
146146
}
147+
148+
// WriteLastFinalizedBatchIndex stores the last finalized batch index in the database.
149+
func WriteLastFinalizedBatchIndex(db ethdb.KeyValueWriter, lastFinalizedBatchIndex uint64) {
150+
value := big.NewInt(0).SetUint64(lastFinalizedBatchIndex).Bytes()
151+
if err := db.Put(lastFinalizedBatchIndexKey, value); err != nil {
152+
log.Crit("failed to store last finalized batch index for rollup event", "batch index", lastFinalizedBatchIndex, "value", value, "err", err)
153+
}
154+
}
155+
156+
// ReadLastFinalizedBatchIndex fetches the last finalized batch index from the database.
157+
func ReadLastFinalizedBatchIndex(db ethdb.Reader) *uint64 {
158+
data, err := db.Get(lastFinalizedBatchIndexKey)
159+
if err != nil && isNotFoundErr(err) {
160+
return nil
161+
}
162+
if err != nil {
163+
log.Crit("failed to read last finalized batch index from database", "key", lastFinalizedBatchIndexKey, "err", err)
164+
}
165+
166+
number := new(big.Int).SetBytes(data)
167+
if !number.IsUint64() {
168+
log.Crit("unexpected finalized batch index in database", "data", data, "number", number)
169+
}
170+
171+
lastFinalizedBatchIndex := number.Uint64()
172+
return &lastFinalizedBatchIndex
173+
}

‎core/rawdb/accessors_rollup_event_test.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestFinalizedL2BlockNumber(t *testing.T) {
4545

4646
// read non-existing value
4747
if got := ReadFinalizedL2BlockNumber(db); got != nil {
48-
t.Fatal("Expected 0 for non-existing value", "got", *got)
48+
t.Fatal("Expected nil for non-existing value", "got", *got)
4949
}
5050

5151
for _, num := range blockNumbers {
@@ -58,6 +58,32 @@ func TestFinalizedL2BlockNumber(t *testing.T) {
5858
}
5959
}
6060

61+
func TestLastFinalizedBatchIndex(t *testing.T) {
62+
batchIndxes := []uint64{
63+
1,
64+
1 << 2,
65+
1 << 8,
66+
1 << 16,
67+
1 << 32,
68+
}
69+
70+
db := NewMemoryDatabase()
71+
72+
// read non-existing value
73+
if got := ReadLastFinalizedBatchIndex(db); got != nil {
74+
t.Fatal("Expected nil for non-existing value", "got", *got)
75+
}
76+
77+
for _, num := range batchIndxes {
78+
WriteLastFinalizedBatchIndex(db, num)
79+
got := ReadLastFinalizedBatchIndex(db)
80+
81+
if *got != num {
82+
t.Fatal("Batch index mismatch", "expected", num, "got", got)
83+
}
84+
}
85+
}
86+
6187
func TestFinalizedBatchMeta(t *testing.T) {
6288
batches := []*FinalizedBatchMeta{
6389
{

‎core/rawdb/schema.go

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ var (
115115
batchChunkRangesPrefix = []byte("R-bcr")
116116
batchMetaPrefix = []byte("R-bm")
117117
finalizedL2BlockNumberKey = []byte("R-finalized")
118+
lastFinalizedBatchIndexKey = []byte("R-finalizedBatchIndex")
118119

119120
// Row consumption
120121
rowConsumptionPrefix = []byte("rc") // rowConsumptionPrefix + hash -> row consumption by block

‎params/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
const (
2525
VersionMajor = 5 // Major version component of the current release
2626
VersionMinor = 5 // Minor version component of the current release
27-
VersionPatch = 1 // Patch version component of the current release
27+
VersionPatch = 2 // Patch version component of the current release
2828
VersionMeta = "mainnet" // Version metadata to append to the version string
2929
)
3030

‎rollup/rollup_sync_service/rollup_sync_service.go

+34-11
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,46 @@ func (s *RollupSyncService) parseAndUpdateRollupEventLogs(logs []types.Log, endB
225225
batchIndex := event.BatchIndex.Uint64()
226226
log.Trace("found new FinalizeBatch event", "batch index", batchIndex)
227227

228-
parentBatchMeta, chunks, err := s.getLocalInfoForBatch(batchIndex)
229-
if err != nil {
230-
return fmt.Errorf("failed to get local node info, batch index: %v, err: %w", batchIndex, err)
228+
lastFinalizedBatchIndex := rawdb.ReadLastFinalizedBatchIndex(s.db)
229+
230+
// After darwin, FinalizeBatch event emitted every bundle, which contains multiple batches.
231+
// Therefore there are a range of finalized batches need to be saved into db.
232+
//
233+
// The range logic also applies to the batches before darwin when FinalizeBatch event emitted
234+
// per single batch. In this situation, `batchIndex` just equals to `*lastFinalizedBatchIndex + 1`
235+
// and only one batch is processed through the for loop.
236+
startBatchIndex := batchIndex
237+
if lastFinalizedBatchIndex != nil {
238+
startBatchIndex = *lastFinalizedBatchIndex + 1
239+
} else {
240+
log.Warn("got nil when reading last finalized batch index. This should happen only once.")
231241
}
232242

233-
endBlock, finalizedBatchMeta, err := validateBatch(event, parentBatchMeta, chunks, s.bc.Config(), s.stack)
234-
if err != nil {
235-
return fmt.Errorf("fatal: validateBatch failed: finalize event: %v, err: %w", event, err)
236-
}
243+
var highestFinalizedBlockNumber uint64
244+
for index := startBatchIndex; index <= batchIndex; index++ {
245+
parentBatchMeta, chunks, err := s.getLocalInfoForBatch(index)
246+
if err != nil {
247+
return fmt.Errorf("failed to get local node info, batch index: %v, err: %w", index, err)
248+
}
237249

238-
rawdb.WriteFinalizedL2BlockNumber(s.db, endBlock)
239-
rawdb.WriteFinalizedBatchMeta(s.db, batchIndex, finalizedBatchMeta)
250+
endBlock, finalizedBatchMeta, err := validateBatch(event, parentBatchMeta, chunks, s.bc.Config(), s.stack)
251+
if err != nil {
252+
return fmt.Errorf("fatal: validateBatch failed: finalize event: %v, err: %w", event, err)
253+
}
240254

241-
if batchIndex%100 == 0 {
242-
log.Info("finalized batch progress", "batch index", batchIndex, "finalized l2 block height", endBlock)
255+
rawdb.WriteFinalizedBatchMeta(s.db, index, finalizedBatchMeta)
256+
257+
if index%100 == 0 {
258+
log.Info("finalized batch progress", "batch index", index, "finalized l2 block height", endBlock)
259+
}
260+
261+
highestFinalizedBlockNumber = endBlock
243262
}
244263

264+
rawdb.WriteFinalizedL2BlockNumber(s.db, highestFinalizedBlockNumber)
265+
rawdb.WriteLastFinalizedBatchIndex(s.db, batchIndex)
266+
log.Debug("write finalized l2 block number", "batch index", batchIndex, "finalized l2 block height", highestFinalizedBlockNumber)
267+
245268
default:
246269
return fmt.Errorf("unknown event, topic: %v, tx hash: %v", vLog.Topics[0].Hex(), vLog.TxHash.Hex())
247270
}

0 commit comments

Comments
 (0)
Please sign in to comment.