github.com/ethereum-optimism/optimism@v1.7.2/op-node/rollup/derive/singular_batch.go (about)

     1  package derive
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"io"
     7  
     8  	"github.com/ethereum-optimism/optimism/op-node/rollup"
     9  	"github.com/ethereum-optimism/optimism/op-service/eth"
    10  	"github.com/ethereum/go-ethereum/common"
    11  	"github.com/ethereum/go-ethereum/common/hexutil"
    12  	"github.com/ethereum/go-ethereum/log"
    13  	"github.com/ethereum/go-ethereum/rlp"
    14  )
    15  
    16  // Batch format
    17  //
    18  // SingularBatchType := 0
    19  // singularBatch := SingularBatchType ++ RLP([parent_hash, epoch_number, epoch_hash, timestamp, transaction_list])
    20  
    21  // SingularBatch is an implementation of Batch interface, containing the input to build one L2 block.
    22  type SingularBatch struct {
    23  	ParentHash   common.Hash  // parent L2 block hash
    24  	EpochNum     rollup.Epoch // aka l1 num
    25  	EpochHash    common.Hash  // l1 block hash
    26  	Timestamp    uint64
    27  	Transactions []hexutil.Bytes
    28  }
    29  
    30  // GetBatchType returns its batch type (batch_version)
    31  func (b *SingularBatch) GetBatchType() int {
    32  	return SingularBatchType
    33  }
    34  
    35  // GetTimestamp returns its block timestamp
    36  func (b *SingularBatch) GetTimestamp() uint64 {
    37  	return b.Timestamp
    38  }
    39  
    40  // GetEpochNum returns its epoch number (L1 origin block number)
    41  func (b *SingularBatch) GetEpochNum() rollup.Epoch {
    42  	return b.EpochNum
    43  }
    44  
    45  // LogContext creates a new log context that contains information of the batch
    46  func (b *SingularBatch) LogContext(log log.Logger) log.Logger {
    47  	return log.New(
    48  		"batch_type", "SingularBatch",
    49  		"batch_timestamp", b.Timestamp,
    50  		"parent_hash", b.ParentHash,
    51  		"batch_epoch", b.Epoch(),
    52  		"txs", len(b.Transactions),
    53  	)
    54  }
    55  
    56  // Epoch returns a BlockID of its L1 origin.
    57  func (b *SingularBatch) Epoch() eth.BlockID {
    58  	return eth.BlockID{Hash: b.EpochHash, Number: uint64(b.EpochNum)}
    59  }
    60  
    61  // encode writes the byte encoding of SingularBatch to Writer stream
    62  func (b *SingularBatch) encode(w io.Writer) error {
    63  	return rlp.Encode(w, b)
    64  }
    65  
    66  // decode reads the byte encoding of SingularBatch from Reader stream
    67  func (b *SingularBatch) decode(r *bytes.Reader) error {
    68  	return rlp.Decode(r, b)
    69  }
    70  
    71  // GetSingularBatch retrieves SingularBatch from batchData
    72  func GetSingularBatch(batchData *BatchData) (*SingularBatch, error) {
    73  	singularBatch, ok := batchData.inner.(*SingularBatch)
    74  	if !ok {
    75  		return nil, NewCriticalError(errors.New("failed type assertion to SingularBatch"))
    76  	}
    77  	return singularBatch, nil
    78  }