diff --git a/zk/datastream/server/data_stream_server.go b/zk/datastream/server/data_stream_server.go index 6ddb1c3d7d8..b16f1086178 100644 --- a/zk/datastream/server/data_stream_server.go +++ b/zk/datastream/server/data_stream_server.go @@ -124,6 +124,7 @@ func createBlockWithBatchCheckStreamEntriesProto( transactionsToIncludeByIndex []int, // passing nil here will include all transactions in the blocks ) ([]DataStreamEntryProto, error) { var err error + var startEntriesProto, blockEntriesProto, endEntriesProto []DataStreamEntryProto gers, err := reader.GetBatchGlobalExitRootsProto(lastBatchNumber, batchNumber) if err != nil { @@ -139,33 +140,12 @@ func createBlockWithBatchCheckStreamEntriesProto( blockNum := block.NumberU64() - entryCount := 2 // l2 block bookmark + l2 block - entryCount += len(filteredTransactions) // transactions - entryCount += len(gers) - - if isBatchStart { - // we will add in a batch bookmark and a batch start entry - entryCount += 2 - - // a gap of 1 is normal but if greater than we need to account for the empty batches which will each - // have a batch bookmark, batch start and batch end - entryCount += int(3 * (batchGap - 1)) - } - - if isBatchEnd { - entryCount++ - } - - entries := NewDataStreamEntries(entryCount) - // batch start // BATCH BOOKMARK if isBatchStart { - var entriesProto []DataStreamEntryProto - if entriesProto, err = createBatchStartEntriesProto(reader, tx, batchNumber, lastBatchNumber, batchGap, chainId, block.Root(), gers); err != nil { + if startEntriesProto, err = createBatchStartEntriesProto(reader, tx, batchNumber, lastBatchNumber, batchGap, chainId, block.Root(), gers); err != nil { return nil, err } - entries.AddMany(entriesProto) } forkId, err := reader.GetForkId(batchNumber) @@ -183,16 +163,19 @@ func createBlockWithBatchCheckStreamEntriesProto( if err != nil { return nil, err } - entries.AddMany(blockEntries.Entries()) + blockEntriesProto = blockEntries.Entries() if isBatchEnd { - var batchEndEntries []DataStreamEntryProto - if batchEndEntries, err = addBatchEndEntriesProto(reader, tx, batchNumber, lastBatchNumber, block.Root(), gers); err != nil { + if endEntriesProto, err = addBatchEndEntriesProto(reader, tx, batchNumber, lastBatchNumber, block.Root(), gers); err != nil { return nil, err } - entries.AddMany(batchEndEntries) } + entries := NewDataStreamEntries(len(startEntriesProto) + len(blockEntriesProto) + len(endEntriesProto)) + entries.AddMany(startEntriesProto) + entries.AddMany(blockEntriesProto) + entries.AddMany(endEntriesProto) + return entries.Entries(), nil } diff --git a/zk/datastream/server/data_stream_server_utils.go b/zk/datastream/server/data_stream_server_utils.go index 47cf2f390f3..cb13c7d30fd 100644 --- a/zk/datastream/server/data_stream_server_utils.go +++ b/zk/datastream/server/data_stream_server_utils.go @@ -134,9 +134,12 @@ func createBatchStartEntriesProto( batchNumber, lastBatchNumber, batchGap, chainId uint64, root libcommon.Hash, gers []types.GerUpdateProto, -) (entries []DataStreamEntryProto, err error) { +) ([]DataStreamEntryProto, error) { + var err error var batchStartEntries []DataStreamEntryProto + entries := make([]DataStreamEntryProto, 0, 2+int(3*(batchGap-1))+len(gers)) + // if we have a gap of more than 1 batch then we need to write in the batch start and ends for these empty batches if batchGap > 1 { var localExitRoot libcommon.Hash @@ -183,13 +186,13 @@ func addBatchEndEntriesProto( root libcommon.Hash, gers []types.GerUpdateProto, ) ([]DataStreamEntryProto, error) { - entries := make([]DataStreamEntryProto, len(gers)+1) + entries := make([]DataStreamEntryProto, 0, len(gers)+1) // see if we have any gers to handle - for i, ger := range gers { + for _, ger := range gers { upd := ger.UpdateGER if upd.BatchNumber == batchNumber { - entries[i] = newGerUpdateProto(upd.BatchNumber, upd.Timestamp, libcommon.BytesToHash(upd.GlobalExitRoot), libcommon.BytesToAddress(upd.Coinbase), upd.ForkId, upd.ChainId, libcommon.BytesToHash(upd.StateRoot)) + entries = append(entries, newGerUpdateProto(upd.BatchNumber, upd.Timestamp, libcommon.BytesToHash(upd.GlobalExitRoot), libcommon.BytesToAddress(upd.Coinbase), upd.ForkId, upd.ChainId, libcommon.BytesToHash(upd.StateRoot))) } } @@ -198,7 +201,7 @@ func addBatchEndEntriesProto( return nil, err } // seal off the last batch - entries[len(gers)] = newBatchEndProto(localExitRoot, root, batchNumber) + entries = append(entries, newBatchEndProto(localExitRoot, root, batchNumber)) return entries, nil }