Skip to content

Commit

Permalink
Merge pull request #2136 from OffchainLabs/gate-4844-posting-arbos-20
Browse files Browse the repository at this point in the history
Gate 4844 Batch Posting on ArbOS 20
  • Loading branch information
Tristan-Wilson authored Feb 14, 2024
2 parents d6251df + 14d59b3 commit 20dec92
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 40 deletions.
88 changes: 49 additions & 39 deletions arbnode/batch_poster.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/offchainlabs/nitro/cmd/chaininfo"
"github.com/offchainlabs/nitro/cmd/genericconf"
"github.com/offchainlabs/nitro/das"
"github.com/offchainlabs/nitro/execution"
"github.com/offchainlabs/nitro/solgen/go/bridgegen"
"github.com/offchainlabs/nitro/util"
"github.com/offchainlabs/nitro/util/arbmath"
Expand Down Expand Up @@ -73,22 +74,23 @@ type batchPosterPosition struct {

type BatchPoster struct {
stopwaiter.StopWaiter
l1Reader *headerreader.HeaderReader
inbox *InboxTracker
streamer *TransactionStreamer
config BatchPosterConfigFetcher
seqInbox *bridgegen.SequencerInbox
bridge *bridgegen.Bridge
syncMonitor *SyncMonitor
seqInboxABI *abi.ABI
seqInboxAddr common.Address
bridgeAddr common.Address
gasRefunderAddr common.Address
building *buildingBatch
daWriter das.DataAvailabilityServiceWriter
dataPoster *dataposter.DataPoster
redisLock *redislock.Simple
messagesPerBatch *arbmath.MovingAverage[uint64]
l1Reader *headerreader.HeaderReader
inbox *InboxTracker
streamer *TransactionStreamer
arbOSVersionGetter execution.FullExecutionClient
config BatchPosterConfigFetcher
seqInbox *bridgegen.SequencerInbox
bridge *bridgegen.Bridge
syncMonitor *SyncMonitor
seqInboxABI *abi.ABI
seqInboxAddr common.Address
bridgeAddr common.Address
gasRefunderAddr common.Address
building *buildingBatch
daWriter das.DataAvailabilityServiceWriter
dataPoster *dataposter.DataPoster
redisLock *redislock.Simple
messagesPerBatch *arbmath.MovingAverage[uint64]
// This is an atomic variable that should only be accessed atomically.
// An estimate of the number of batches we want to post but haven't yet.
// This doesn't include batches which we don't want to post yet due to the L1 bounds.
Expand Down Expand Up @@ -255,6 +257,7 @@ type BatchPosterOpts struct {
L1Reader *headerreader.HeaderReader
Inbox *InboxTracker
Streamer *TransactionStreamer
VersionGetter execution.FullExecutionClient
SyncMonitor *SyncMonitor
Config BatchPosterConfigFetcher
DeployInfo *chaininfo.RollupAddresses
Expand Down Expand Up @@ -293,19 +296,20 @@ func NewBatchPoster(ctx context.Context, opts *BatchPosterOpts) (*BatchPoster, e
return nil, err
}
b := &BatchPoster{
l1Reader: opts.L1Reader,
inbox: opts.Inbox,
streamer: opts.Streamer,
syncMonitor: opts.SyncMonitor,
config: opts.Config,
bridge: bridge,
seqInbox: seqInbox,
seqInboxABI: seqInboxABI,
seqInboxAddr: opts.DeployInfo.SequencerInbox,
gasRefunderAddr: opts.Config().gasRefunder,
bridgeAddr: opts.DeployInfo.Bridge,
daWriter: opts.DAWriter,
redisLock: redisLock,
l1Reader: opts.L1Reader,
inbox: opts.Inbox,
streamer: opts.Streamer,
arbOSVersionGetter: opts.VersionGetter,
syncMonitor: opts.SyncMonitor,
config: opts.Config,
bridge: bridge,
seqInbox: seqInbox,
seqInboxABI: seqInboxABI,
seqInboxAddr: opts.DeployInfo.SequencerInbox,
gasRefunderAddr: opts.Config().gasRefunder,
bridgeAddr: opts.DeployInfo.Bridge,
daWriter: opts.DAWriter,
redisLock: redisLock,
}
b.messagesPerBatch, err = arbmath.NewMovingAverage[uint64](20)
if err != nil {
Expand Down Expand Up @@ -947,7 +951,6 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error)
if dbBatchCount > batchPosition.NextSeqNum {
return false, fmt.Errorf("attempting to post batch %v, but the local inbox tracker database already has %v batches", batchPosition.NextSeqNum, dbBatchCount)
}

if b.building == nil || b.building.startMsgCount != batchPosition.MessageCount {
latestHeader, err := b.l1Reader.LastHeader(ctx)
if err != nil {
Expand All @@ -956,17 +959,24 @@ func (b *BatchPoster) maybePostSequencerBatch(ctx context.Context) (bool, error)
var use4844 bool
config := b.config()
if config.Post4844Blobs && latestHeader.ExcessBlobGas != nil && latestHeader.BlobGasUsed != nil {
if config.ForcePost4844Blobs {
use4844 = true
} else {
blobFeePerByte := eip4844.CalcBlobFee(eip4844.CalcExcessBlobGas(*latestHeader.ExcessBlobGas, *latestHeader.BlobGasUsed))
blobFeePerByte.Mul(blobFeePerByte, blobTxBlobGasPerBlob)
blobFeePerByte.Div(blobFeePerByte, usableBytesInBlob)

calldataFeePerByte := arbmath.BigMulByUint(latestHeader.BaseFee, 16)
use4844 = arbmath.BigLessThan(blobFeePerByte, calldataFeePerByte)
arbOSVersion, err := b.arbOSVersionGetter.ArbOSVersionForMessageNumber(arbutil.MessageIndex(arbmath.SaturatingUSub(uint64(batchPosition.MessageCount), 1)))
if err != nil {
return false, err
}
if arbOSVersion >= 20 {
if config.ForcePost4844Blobs {
use4844 = true
} else {
blobFeePerByte := eip4844.CalcBlobFee(eip4844.CalcExcessBlobGas(*latestHeader.ExcessBlobGas, *latestHeader.BlobGasUsed))
blobFeePerByte.Mul(blobFeePerByte, blobTxBlobGasPerBlob)
blobFeePerByte.Div(blobFeePerByte, usableBytesInBlob)

calldataFeePerByte := arbmath.BigMulByUint(latestHeader.BaseFee, 16)
use4844 = arbmath.BigLessThan(blobFeePerByte, calldataFeePerByte)
}
}
}

b.building = &buildingBatch{
segments: newBatchSegments(batchPosition.DelayedMessageCount, b.config(), b.GetBacklogEstimate(), use4844),
msgCount: batchPosition.MessageCount,
Expand Down
1 change: 1 addition & 0 deletions arbnode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ func createNodeImpl(
L1Reader: l1Reader,
Inbox: inboxTracker,
Streamer: txStreamer,
VersionGetter: exec,
SyncMonitor: syncMonitor,
Config: func() *BatchPosterConfig { return &configFetcher.Get().BatchPoster },
DeployInfo: deployInfo,
Expand Down
11 changes: 10 additions & 1 deletion execution/gethexec/executionengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (s *ExecutionEngine) Reorg(count arbutil.MessageIndex, newMessages []arbost
resequencing := false
defer func() {
// if we are resequencing old messages - don't release the lock
// lock will be relesed by thread listening to resequenceChan
// lock will be released by thread listening to resequenceChan
if !resequencing {
s.createBlocksMutex.Unlock()
}
Expand Down Expand Up @@ -601,6 +601,15 @@ func (s *ExecutionEngine) digestMessageWithBlockMutex(num arbutil.MessageIndex,
return nil
}

func (s *ExecutionEngine) ArbOSVersionForMessageNumber(messageNum arbutil.MessageIndex) (uint64, error) {
block := s.bc.GetBlockByNumber(s.MessageIndexToBlockNumber(messageNum))
if block == nil {
return 0, fmt.Errorf("couldn't find block for message number %d", messageNum)
}
extra := types.DeserializeHeaderExtraInformation(block.Header())
return extra.ArbOSFormatVersion, nil
}

func (s *ExecutionEngine) Start(ctx_in context.Context) {
s.StopWaiter.Start(ctx_in, s)
s.LaunchThread(func(ctx context.Context) {
Expand Down
3 changes: 3 additions & 0 deletions execution/gethexec/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ func (n *ExecutionNode) SequenceDelayedMessage(message *arbostypes.L1IncomingMes
func (n *ExecutionNode) ResultAtPos(pos arbutil.MessageIndex) (*execution.MessageResult, error) {
return n.ExecEngine.ResultAtPos(pos)
}
func (n *ExecutionNode) ArbOSVersionForMessageNumber(messageNum arbutil.MessageIndex) (uint64, error) {
return n.ExecEngine.ArbOSVersionForMessageNumber(messageNum)
}

func (n *ExecutionNode) RecordBlockCreation(
ctx context.Context,
Expand Down
2 changes: 2 additions & 0 deletions execution/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type FullExecutionClient interface {

// TODO: only used to get safe/finalized block numbers
MessageIndexToBlockNumber(messageNum arbutil.MessageIndex) uint64

ArbOSVersionForMessageNumber(messageNum arbutil.MessageIndex) (uint64, error)
}

// not implemented in execution, used as input
Expand Down
1 change: 1 addition & 0 deletions system_tests/batch_poster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func testBatchPosterParallel(t *testing.T, useRedis bool) {
L1Reader: builder.L2.ConsensusNode.L1Reader,
Inbox: builder.L2.ConsensusNode.InboxTracker,
Streamer: builder.L2.ConsensusNode.TxStreamer,
VersionGetter: builder.L2.ExecNode,
SyncMonitor: builder.L2.ConsensusNode.SyncMonitor,
Config: func() *arbnode.BatchPosterConfig { return &batchPosterConfig },
DeployInfo: builder.L2.ConsensusNode.DeployInfo,
Expand Down

0 comments on commit 20dec92

Please sign in to comment.