From 0e733fcff13036085ba4a6e9494e34005ba68a45 Mon Sep 17 00:00:00 2001 From: Chris Buckland Date: Thu, 18 Jan 2024 12:57:29 +0000 Subject: [PATCH] Added both block basefee and blob basefee to batch spending report --- src/bridge/SequencerInbox.sol | 20 ++++++++++-------- test/contract/sequencerInbox.spec.4844.ts | 25 ++++++++++++++++++++--- test/foundry/SequencerInbox.t.sol | 4 +++- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/bridge/SequencerInbox.sol b/src/bridge/SequencerInbox.sol index 7aef44b8..7cecec64 100644 --- a/src/bridge/SequencerInbox.sol +++ b/src/bridge/SequencerInbox.sol @@ -418,6 +418,9 @@ contract SequencerInbox is DelegateCallAware, GasRefundEnabled, ISequencerInbox afterDelayedMessagesRead ); + // we use addSequencerL2BatchImpl for submitting the message + // normally this would also submit a batch spending report but that is skipped if we pass + // an empty call data size, then we submit a separate batch spending report later ( uint256 seqMessageIndex, bytes32 beforeAcc, @@ -453,11 +456,7 @@ contract SequencerInbox is DelegateCallAware, GasRefundEnabled, ISequencerInbox // submit a batch spending report to refund the entity that produced the blob batch data uint256 blobBasefee = blobBasefeeReader.getBlobBaseFee(); - - // TODO: This report the gas spending using the blob basefee, however the actual spending actually involve - // 2 parts: 1. data cost priced in blob basefee, 2. tx cost priced in block basefee - // We might need to change the batch spending report format so both costs are reported. - submitBatchSpendingReport(dataHash, seqMessageIndex, blobBasefee); + submitBatchSpendingReport(dataHash, seqMessageIndex, block.basefee, blobBasefee); } function addSequencerL2Batch( @@ -625,7 +624,8 @@ contract SequencerInbox is DelegateCallAware, GasRefundEnabled, ISequencerInbox function submitBatchSpendingReport( bytes32 dataHash, uint256 seqMessageIndex, - uint256 gasPrice + uint256 gasPrice, + uint256 blobBaseFeePrice ) internal { bytes memory spendingReportMsg; address batchPoster = msg.sender; @@ -646,12 +646,16 @@ contract SequencerInbox is DelegateCallAware, GasRefundEnabled, ISequencerInbox uint64(extraGas) ); } else { + // when a blob base fee is supplied we include it into the batch spending report spendingReportMsg = abi.encodePacked( block.timestamp, batchPoster, dataHash, seqMessageIndex, - gasPrice + gasPrice, + // we add an empty extraGas since the parsing code expects a value here + uint64(0), + blobBaseFeePrice ); } @@ -691,7 +695,7 @@ contract SequencerInbox is DelegateCallAware, GasRefundEnabled, ISequencerInbox totalDelayedMessagesRead = afterDelayedMessagesRead; if (calldataLengthPosted > 0) { - submitBatchSpendingReport(dataHash, seqMessageIndex, block.basefee); + submitBatchSpendingReport(dataHash, seqMessageIndex, block.basefee, 0); } } diff --git a/test/contract/sequencerInbox.spec.4844.ts b/test/contract/sequencerInbox.spec.4844.ts index cb7d51b1..681e78dd 100644 --- a/test/contract/sequencerInbox.spec.4844.ts +++ b/test/contract/sequencerInbox.spec.4844.ts @@ -445,8 +445,11 @@ describe('SequencerInbox', async () => { const batchSendTx = await Toolkit4844.getTx(txHash) const blobHashes = (batchSendTx as any)['blobVersionedHashes'] as string[] const batchSendReceipt = await Toolkit4844.getTxReceipt(txHash) - const { timestamp: blockTimestamp, number: blockNumber } = - await wallet.provider.getBlock(batchSendReceipt.blockNumber) + const { + timestamp: blockTimestamp, + number: blockNumber, + baseFeePerGas, + } = await wallet.provider.getBlock(batchSendReceipt.blockNumber) const timeBounds = await getTimeBounds( blockNumber, @@ -516,8 +519,12 @@ describe('SequencerInbox', async () => { '0x' + inboxMsgDeliveredEvent.data.substring(106, 170) const spendingSeqMessageIndex = '0x' + inboxMsgDeliveredEvent.data.substring(170, 234) - const spendingBlobBasefee = + const spendingBlockBaseFee = '0x' + inboxMsgDeliveredEvent.data.substring(234, 298) + const spendingExtraGas = + '0x' + inboxMsgDeliveredEvent.data.substring(298, 314) + const spendingBlobBasefee = + '0x' + inboxMsgDeliveredEvent.data.substring(314, 378) expect( BigNumber.from(spendingTimestamp).eq(blockTimestamp), @@ -531,6 +538,18 @@ describe('SequencerInbox', async () => { BigNumber.from(spendingSeqMessageIndex).eq(sequenceNumber), 'spending seq message index' ).to.eq(true) + + if (baseFeePerGas == null) { + throw new Error('Missing base fee') + } + expect( + BigNumber.from(spendingBlockBaseFee).eq(baseFeePerGas), + `spending basefee: ${BigNumber.from(spendingBlockBaseFee).toString()}` + ).to.eq(true) + expect( + BigNumber.from(spendingExtraGas).eq(0), + `spending extra gas: ${BigNumber.from(spendingExtraGas).toString()}` + ).to.eq(true) // we expect a very low - 1 - basefee since we havent sent many blobs expect( BigNumber.from(spendingBlobBasefee).eq(1), diff --git a/test/foundry/SequencerInbox.t.sol b/test/foundry/SequencerInbox.t.sol index 7af81507..f947e8ac 100644 --- a/test/foundry/SequencerInbox.t.sol +++ b/test/foundry/SequencerInbox.t.sol @@ -106,7 +106,9 @@ contract SequencerInboxTest is Test { msg.sender, dataHash, sequenceNumber, - block.basefee + block.basefee, + uint64(0), + uint256(0) ); bytes32 beforeAcc = bytes32(0); bytes32 delayedAcc = bridge.delayedInboxAccs(delayedMessagesRead - 1);