github.com/onflow/flow-go@v0.33.17/fvm/evm/types/block.go (about)

     1  package types
     2  
     3  import (
     4  	gethCommon "github.com/ethereum/go-ethereum/common"
     5  	gethTypes "github.com/ethereum/go-ethereum/core/types"
     6  	"github.com/ethereum/go-ethereum/rlp"
     7  )
     8  
     9  // Block represents a evm block.
    10  // It captures block info such as height and state
    11  type Block struct {
    12  	// the hash of the parent block
    13  	ParentBlockHash gethCommon.Hash
    14  
    15  	// Height returns the height of this block
    16  	Height uint64
    17  
    18  	// holds the total amount of the native token deposited in the evm side.
    19  	TotalSupply uint64
    20  
    21  	// ReceiptRoot returns the root hash of the receipts emitted in this block
    22  	ReceiptRoot gethCommon.Hash
    23  
    24  	// transaction hashes
    25  	TransactionHashes []gethCommon.Hash
    26  }
    27  
    28  // ToBytes encodes the block into bytes
    29  func (b *Block) ToBytes() ([]byte, error) {
    30  	return rlp.EncodeToBytes(b)
    31  }
    32  
    33  // Hash returns the hash of the block
    34  func (b *Block) Hash() (gethCommon.Hash, error) {
    35  	data, err := b.ToBytes()
    36  	return gethCommon.BytesToHash(data), err
    37  }
    38  
    39  // AppendTxHash appends a transaction hash to the list of transaction hashes of the block
    40  func (b *Block) AppendTxHash(txHash gethCommon.Hash) {
    41  	b.TransactionHashes = append(b.TransactionHashes, txHash)
    42  }
    43  
    44  // NewBlock constructs a new block
    45  func NewBlock(height, uuidIndex, totalSupply uint64,
    46  	stateRoot, receiptRoot gethCommon.Hash,
    47  	txHashes []gethCommon.Hash,
    48  ) *Block {
    49  	return &Block{
    50  		Height:            height,
    51  		TotalSupply:       totalSupply,
    52  		ReceiptRoot:       receiptRoot,
    53  		TransactionHashes: txHashes,
    54  	}
    55  }
    56  
    57  // NewBlockFromBytes constructs a new block from encoded data
    58  func NewBlockFromBytes(encoded []byte) (*Block, error) {
    59  	res := &Block{}
    60  	err := rlp.DecodeBytes(encoded, res)
    61  	return res, err
    62  }
    63  
    64  // GenesisBlock is the genesis block in the EVM environment
    65  var GenesisBlock = &Block{
    66  	ParentBlockHash: gethCommon.Hash{},
    67  	Height:          uint64(0),
    68  	ReceiptRoot:     gethTypes.EmptyRootHash,
    69  }