github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/core/rawdb/accessors_row_consumption.go (about)

     1  package rawdb
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/scroll-tech/go-ethereum/common"
     7  	"github.com/scroll-tech/go-ethereum/core/types"
     8  	"github.com/scroll-tech/go-ethereum/ethdb"
     9  	"github.com/scroll-tech/go-ethereum/log"
    10  	"github.com/scroll-tech/go-ethereum/rlp"
    11  )
    12  
    13  // WriteBlockRowConsumption writes a RowConsumption of the block to the database.
    14  func WriteBlockRowConsumption(db ethdb.KeyValueWriter, l2BlockHash common.Hash, rc *types.RowConsumption) {
    15  	if rc == nil {
    16  		return
    17  	}
    18  
    19  	bytes, err := rlp.EncodeToBytes(&rc)
    20  	if err != nil {
    21  		log.Crit("Failed to RLP encode RowConsumption ", "err", err)
    22  	}
    23  	if err := db.Put(rowConsumptionKey(l2BlockHash), bytes); err != nil {
    24  		log.Crit("Failed to store RowConsumption ", "err", err)
    25  	}
    26  }
    27  
    28  // ReadBlockRowConsumption retrieves the RowConsumption corresponding to the block hash.
    29  func ReadBlockRowConsumption(db ethdb.Reader, l2BlockHash common.Hash) *types.RowConsumption {
    30  	data := ReadBlockRowConsumptionRLP(db, l2BlockHash)
    31  	if len(data) == 0 {
    32  		return nil
    33  	}
    34  	rc := new(types.RowConsumption)
    35  	if err := rlp.Decode(bytes.NewReader(data), rc); err != nil {
    36  		log.Crit("Invalid RowConsumption message RLP", "l2BlockHash", l2BlockHash.String(), "data", data, "err", err)
    37  	}
    38  	return rc
    39  }
    40  
    41  // ReadBlockRowConsumption retrieves the RowConsumption in its raw RLP database encoding.
    42  func ReadBlockRowConsumptionRLP(db ethdb.Reader, l2BlockHash common.Hash) rlp.RawValue {
    43  	data, err := db.Get(rowConsumptionKey(l2BlockHash))
    44  	if err != nil && isNotFoundErr(err) {
    45  		return nil
    46  	}
    47  	if err != nil {
    48  		log.Crit("Failed to load RowConsumption", "l2BlockHash", l2BlockHash.String(), "err", err)
    49  	}
    50  	return data
    51  }