Skip to content

Commit

Permalink
Merge pull request #17 from node-real/gastrack
Browse files Browse the repository at this point in the history
average base fee
  • Loading branch information
keroro520 authored Apr 19, 2023
2 parents e07e80c + a582e6f commit 6695f82
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
58 changes: 57 additions & 1 deletion op-node/rollup/derive/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package derive
import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -11,12 +12,14 @@ import (
"github.com/ethereum-optimism/optimism/op-bindings/predeploys"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/rollup"
opservice "github.com/ethereum-optimism/optimism/op-service"
)

// L1ReceiptsFetcher fetches L1 header info and receipts for the payload attributes derivation (the info tx and deposits)
type L1ReceiptsFetcher interface {
InfoByHash(ctx context.Context, hash common.Hash) (eth.BlockInfo, error)
FetchReceipts(ctx context.Context, blockHash common.Hash) (eth.BlockInfo, types.Receipts, error)
InfoAndTxsByHash(ctx context.Context, hash common.Hash) (eth.BlockInfo, types.Transactions, error)
}

type SystemConfigL2Fetcher interface {
Expand Down Expand Up @@ -53,6 +56,14 @@ func (ba *FetchingAttributesBuilder) PreparePayloadAttributes(ctx context.Contex
return nil, NewTemporaryError(fmt.Errorf("failed to retrieve L2 parent block: %w", err))
}

var gasPrice *big.Int
if opservice.ForBSC {
gasPrice, err = ba.prepareAverageGasPrice(ctx, epoch)
if err != nil {
return nil, NewTemporaryError(fmt.Errorf("failed to prepare average gas price: %w", err))
}
}

// If the L1 origin changed this block, then we are in the first block of the epoch. In this
// case we need to fetch all transaction receipts from the L1 origin block so we can scan for
// user deposits.
Expand All @@ -76,7 +87,9 @@ func (ba *FetchingAttributesBuilder) PreparePayloadAttributes(ctx context.Contex
if err := UpdateSystemConfigWithL1Receipts(&sysConfig, receipts, ba.cfg); err != nil {
return nil, NewCriticalError(fmt.Errorf("failed to apply derived L1 sysCfg updates: %w", err))
}

if opservice.ForBSC {
info = newGasPriceWrapper(info, gasPrice)
}
l1Info = info
depositTxs = deposits
seqNumber = 0
Expand All @@ -88,6 +101,9 @@ func (ba *FetchingAttributesBuilder) PreparePayloadAttributes(ctx context.Contex
if err != nil {
return nil, NewTemporaryError(fmt.Errorf("failed to fetch L1 block info: %w", err))
}
if opservice.ForBSC {
info = newGasPriceWrapper(info, gasPrice)
}
l1Info = info
depositTxs = nil
seqNumber = l2Parent.SequenceNumber + 1
Expand Down Expand Up @@ -118,3 +134,43 @@ func (ba *FetchingAttributesBuilder) PreparePayloadAttributes(ctx context.Contex
GasLimit: (*eth.Uint64Quantity)(&sysConfig.GasLimit),
}, nil
}

var bscDefaultGasPrice = big.NewInt(5000000000)

func (ba *FetchingAttributesBuilder) prepareAverageGasPrice(ctx context.Context, epoch eth.BlockID) (*big.Int, error) {
_, txs, err := ba.l1.InfoAndTxsByHash(ctx, epoch.Hash)
if err != nil {
return nil, err
}
count := 0
var sum big.Int
for _, tx := range txs {
if tx.GasPrice().Cmp(common.Big0) <= 0 {
continue
}
sum.Add(&sum, tx.GasPrice())
count += 1
}
if count == 0 {
return bscDefaultGasPrice, nil
}
return sum.Div(&sum, big.NewInt(int64(count))), nil
}

type gasPriceWrapper struct {
eth.BlockInfo
gasprice *big.Int
}

var _ (eth.BlockInfo) = (*gasPriceWrapper)(nil)

func newGasPriceWrapper(info eth.BlockInfo, gasprice *big.Int) *gasPriceWrapper {
return &gasPriceWrapper{
BlockInfo: info,
gasprice: gasprice,
}
}

func (w *gasPriceWrapper) BaseFee() *big.Int {
return w.gasprice
}
2 changes: 2 additions & 0 deletions op-node/rollup/derive/attributes_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/testlog"
"github.com/ethereum-optimism/optimism/op-node/testutils"
observice "github.com/ethereum-optimism/optimism/op-service"
)

// TestAttributesQueue checks that it properly uses the PreparePayloadAttributes function
// (which is well tested) and that it properly sets NoTxPool and adds in the candidate
// transactions.
func TestAttributesQueue(t *testing.T) {
observice.ForBSC = false
// test config, only init the necessary fields
cfg := &rollup.Config{
BlockTime: 2,
Expand Down

0 comments on commit 6695f82

Please sign in to comment.