Skip to content

Commit f4b274c

Browse files
committed
implement estimate functions
1 parent 4d46aad commit f4b274c

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

encoding/codecv7.go

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -261,41 +261,78 @@ func (d *DACodecV7) CheckBatchCompressedDataCompatibility(b *Batch) (bool, error
261261
return compatible, nil
262262
}
263263

264-
// TODO: which of the Estimate* functions are needed?
264+
func (d *DACodecV7) estimateL1CommitBatchSizeAndBlobSize(batch *Batch) (uint64, uint64, error) {
265+
blobBytes := make([]byte, blobEnvelopeV7OffsetPayload)
266+
267+
payloadBytes, err := d.constructBlobPayload(batch)
268+
if err != nil {
269+
return 0, 0, fmt.Errorf("failed to construct blob payload: %w", err)
270+
}
271+
272+
compressedPayloadBytes, enableCompression, err := d.checkCompressedDataCompatibility(payloadBytes)
273+
if err != nil {
274+
return 0, 0, fmt.Errorf("failed to check batch compressed data compatibility: %w", err)
275+
}
276+
277+
if enableCompression {
278+
blobBytes = append(blobBytes, compressedPayloadBytes...)
279+
} else {
280+
blobBytes = append(blobBytes, payloadBytes...)
281+
}
265282

283+
return blobEnvelopeV7OffsetPayload + uint64(len(payloadBytes)), calculatePaddedBlobSize(uint64(len(blobBytes))), nil
284+
}
285+
286+
// EstimateChunkL1CommitBatchSizeAndBlobSize estimates the L1 commit batch size and blob size for a single chunk.
266287
func (d *DACodecV7) EstimateChunkL1CommitBatchSizeAndBlobSize(chunk *Chunk) (uint64, uint64, error) {
267-
//TODO implement me after contracts are implemented
268-
panic("implement me")
288+
return d.estimateL1CommitBatchSizeAndBlobSize(&Batch{Blocks: chunk.Blocks})
269289
}
270290

291+
// EstimateBatchL1CommitBatchSizeAndBlobSize estimates the L1 commit batch size and blob size for a batch.
271292
func (d *DACodecV7) EstimateBatchL1CommitBatchSizeAndBlobSize(batch *Batch) (uint64, uint64, error) {
272-
//TODO implement me after contracts are implemented
273-
panic("implement me")
293+
return d.estimateL1CommitBatchSizeAndBlobSize(batch)
274294
}
275295

296+
// EstimateBlockL1CommitCalldataSize calculates the calldata size in l1 commit for this block approximately.
297+
// Note: For CodecV7 calldata is constant independently of how many blocks or batches are submitted.
276298
func (d *DACodecV7) EstimateBlockL1CommitCalldataSize(block *Block) (uint64, error) {
277-
//TODO implement me after contracts are implemented
278-
panic("implement me")
299+
return 0, nil
279300
}
280301

302+
// EstimateChunkL1CommitCalldataSize calculates the calldata size needed for committing a chunk to L1 approximately.
303+
// Note: For CodecV7 calldata is constant independently of how many blocks or batches are submitted. There is no notion
304+
// of chunks in this version.
281305
func (d *DACodecV7) EstimateChunkL1CommitCalldataSize(chunk *Chunk) (uint64, error) {
282-
//TODO implement me after contracts are implemented
283-
panic("implement me")
306+
return 0, nil
307+
}
308+
309+
// EstimateBatchL1CommitCalldataSize calculates the calldata size in l1 commit for this batch approximately.
310+
// Note: For CodecV7 calldata is constant independently of how many blocks or batches are submitted.
311+
// Version + BatchHeader
312+
func (d *DACodecV7) EstimateBatchL1CommitCalldataSize(batch *Batch) (uint64, error) {
313+
return 1 + daBatchV7EncodedLength, nil
284314
}
285315

316+
// EstimateChunkL1CommitGas calculates the total L1 commit gas for this chunk approximately.
317+
// Note: For CodecV7 calldata is constant independently of how many blocks or batches are submitted. There is no notion
318+
// of chunks in this version.
286319
func (d *DACodecV7) EstimateChunkL1CommitGas(chunk *Chunk) (uint64, error) {
287-
//TODO implement me after contracts are implemented
288-
panic("implement me")
320+
return 0, nil
289321
}
290322

323+
// EstimateBatchL1CommitGas calculates the total L1 commit gas for this batch approximately.
291324
func (d *DACodecV7) EstimateBatchL1CommitGas(batch *Batch) (uint64, error) {
292-
//TODO implement me after contracts are implemented
293-
panic("implement me")
294-
}
325+
// TODO: adjust this after contracts are implemented
326+
var totalL1CommitGas uint64
295327

296-
func (d *DACodecV7) EstimateBatchL1CommitCalldataSize(batch *Batch) (uint64, error) {
297-
//TODO implement me after contracts are implemented
298-
panic("implement me")
328+
// Add extra gas costs
329+
totalL1CommitGas += extraGasCost // constant to account for ops like _getAdmin, _implementation, _requireNotPaused, etc
330+
totalL1CommitGas += 4 * coldSloadGas // 4 one-time cold sload for commitBatch
331+
totalL1CommitGas += sstoreGas // 1 time sstore
332+
totalL1CommitGas += baseTxGas // base gas for tx
333+
totalL1CommitGas += calldataNonZeroByteGas // version in calldata
334+
335+
return totalL1CommitGas, nil
299336
}
300337

301338
// JSONFromBytes converts the bytes to a DABatch and then marshals it to JSON.

0 commit comments

Comments
 (0)