github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/eth/gasprice/rollup_gasprice.go (about)

     1  package gasprice
     2  
     3  import (
     4  	"context"
     5  	"math/big"
     6  	"sync"
     7  
     8  	"github.com/ethereum-optimism/optimism/l2geth/log"
     9  	"github.com/ethereum-optimism/optimism/l2geth/rollup/fees"
    10  )
    11  
    12  // RollupOracle holds the L1 and L2 gas prices for fee calculation
    13  type RollupOracle struct {
    14  	l1GasPrice     *big.Int
    15  	l2GasPrice     *big.Int
    16  	overhead       *big.Int
    17  	scalar         *big.Float
    18  	l1GasPriceLock sync.RWMutex
    19  	l2GasPriceLock sync.RWMutex
    20  	overheadLock   sync.RWMutex
    21  	scalarLock     sync.RWMutex
    22  }
    23  
    24  // NewRollupOracle returns an initialized RollupOracle
    25  func NewRollupOracle() *RollupOracle {
    26  	return &RollupOracle{
    27  		l1GasPrice: new(big.Int),
    28  		l2GasPrice: new(big.Int),
    29  		overhead:   new(big.Int),
    30  		scalar:     new(big.Float),
    31  	}
    32  }
    33  
    34  // SuggestL1GasPrice returns the gas price which should be charged per byte of published
    35  // data by the sequencer.
    36  func (gpo *RollupOracle) SuggestL1GasPrice(ctx context.Context) (*big.Int, error) {
    37  	gpo.l1GasPriceLock.RLock()
    38  	defer gpo.l1GasPriceLock.RUnlock()
    39  	return gpo.l1GasPrice, nil
    40  }
    41  
    42  // SetL1GasPrice returns the current L1 gas price
    43  func (gpo *RollupOracle) SetL1GasPrice(gasPrice *big.Int) error {
    44  	gpo.l1GasPriceLock.Lock()
    45  	defer gpo.l1GasPriceLock.Unlock()
    46  	gpo.l1GasPrice = gasPrice
    47  	log.Info("Set L1 Gas Price", "gasprice", gpo.l1GasPrice)
    48  	return nil
    49  }
    50  
    51  // SuggestL2GasPrice returns the gas price which should be charged per unit of gas
    52  // set manually by the sequencer depending on congestion
    53  func (gpo *RollupOracle) SuggestL2GasPrice(ctx context.Context) (*big.Int, error) {
    54  	gpo.l2GasPriceLock.RLock()
    55  	defer gpo.l2GasPriceLock.RUnlock()
    56  	return gpo.l2GasPrice, nil
    57  }
    58  
    59  // SetL2GasPrice returns the current L2 gas price
    60  func (gpo *RollupOracle) SetL2GasPrice(gasPrice *big.Int) error {
    61  	gpo.l2GasPriceLock.Lock()
    62  	defer gpo.l2GasPriceLock.Unlock()
    63  	gpo.l2GasPrice = gasPrice
    64  	log.Info("Set L2 Gas Price", "gasprice", gpo.l2GasPrice)
    65  	return nil
    66  }
    67  
    68  // SuggestOverhead returns the cached overhead value from the
    69  // OVM_GasPriceOracle
    70  func (gpo *RollupOracle) SuggestOverhead(ctx context.Context) (*big.Int, error) {
    71  	gpo.overheadLock.RLock()
    72  	defer gpo.overheadLock.RUnlock()
    73  	return gpo.overhead, nil
    74  }
    75  
    76  // SetOverhead caches the overhead value that is set in the
    77  // OVM_GasPriceOracle
    78  func (gpo *RollupOracle) SetOverhead(overhead *big.Int) error {
    79  	gpo.overheadLock.Lock()
    80  	defer gpo.overheadLock.Unlock()
    81  	gpo.overhead = overhead
    82  	log.Info("Set batch overhead", "overhead", overhead)
    83  	return nil
    84  }
    85  
    86  // SuggestScalar returns the cached scalar value
    87  func (gpo *RollupOracle) SuggestScalar(ctx context.Context) (*big.Float, error) {
    88  	gpo.scalarLock.RLock()
    89  	defer gpo.scalarLock.RUnlock()
    90  	return gpo.scalar, nil
    91  }
    92  
    93  // SetScalar sets the scalar value held in the OVM_GasPriceOracle
    94  func (gpo *RollupOracle) SetScalar(scalar *big.Int, decimals *big.Int) error {
    95  	gpo.scalarLock.Lock()
    96  	defer gpo.scalarLock.Unlock()
    97  	value := fees.ScaleDecimals(scalar, decimals)
    98  	gpo.scalar = value
    99  	log.Info("Set scalar", "scalar", gpo.scalar)
   100  	return nil
   101  }