github.com/ethereum/go-ethereum@v1.16.1/beacon/types/exec_header.go (about)

     1  // Copyright 2024 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package types
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/ethereum/go-ethereum/beacon/merkle"
    24  	"github.com/ethereum/go-ethereum/common"
    25  	zrntcommon "github.com/protolambda/zrnt/eth2/beacon/common"
    26  	"github.com/protolambda/ztyp/tree"
    27  
    28  	// beacon chain forks
    29  	"github.com/protolambda/zrnt/eth2/beacon/capella"
    30  	"github.com/protolambda/zrnt/eth2/beacon/deneb"
    31  )
    32  
    33  type headerObject interface {
    34  	HashTreeRoot(hFn tree.HashFn) zrntcommon.Root
    35  }
    36  
    37  type ExecutionHeader struct {
    38  	obj headerObject
    39  }
    40  
    41  // ExecutionHeaderFromJSON decodes an execution header from JSON data provided by
    42  // the beacon chain API.
    43  func ExecutionHeaderFromJSON(forkName string, data []byte) (*ExecutionHeader, error) {
    44  	var obj headerObject
    45  	switch forkName {
    46  	case "capella":
    47  		obj = new(capella.ExecutionPayloadHeader)
    48  	case "deneb", "electra": // note: the payload type was not changed in electra
    49  		obj = new(deneb.ExecutionPayloadHeader)
    50  	default:
    51  		return nil, fmt.Errorf("unsupported fork: %s", forkName)
    52  	}
    53  	if err := json.Unmarshal(data, obj); err != nil {
    54  		return nil, err
    55  	}
    56  	return &ExecutionHeader{obj: obj}, nil
    57  }
    58  
    59  func NewExecutionHeader(obj headerObject) *ExecutionHeader {
    60  	switch obj.(type) {
    61  	case *capella.ExecutionPayloadHeader:
    62  	case *deneb.ExecutionPayloadHeader:
    63  	default:
    64  		panic(fmt.Errorf("unsupported ExecutionPayloadHeader type %T", obj))
    65  	}
    66  	return &ExecutionHeader{obj: obj}
    67  }
    68  
    69  func (eh *ExecutionHeader) PayloadRoot() merkle.Value {
    70  	return merkle.Value(eh.obj.HashTreeRoot(tree.GetHashFn()))
    71  }
    72  
    73  func (eh *ExecutionHeader) BlockHash() common.Hash {
    74  	switch obj := eh.obj.(type) {
    75  	case *capella.ExecutionPayloadHeader:
    76  		return common.Hash(obj.BlockHash)
    77  	case *deneb.ExecutionPayloadHeader:
    78  		return common.Hash(obj.BlockHash)
    79  	default:
    80  		panic(fmt.Errorf("unsupported ExecutionPayloadHeader type %T", obj))
    81  	}
    82  }