github.com/ethereum/go-ethereum@v1.14.3/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  	"github.com/protolambda/zrnt/eth2/beacon/capella"
    26  	zrntcommon "github.com/protolambda/zrnt/eth2/beacon/common"
    27  	"github.com/protolambda/zrnt/eth2/beacon/deneb"
    28  	"github.com/protolambda/ztyp/tree"
    29  )
    30  
    31  type headerObject interface {
    32  	HashTreeRoot(hFn tree.HashFn) zrntcommon.Root
    33  }
    34  
    35  type ExecutionHeader struct {
    36  	obj headerObject
    37  }
    38  
    39  // ExecutionHeaderFromJSON decodes an execution header from JSON data provided by
    40  // the beacon chain API.
    41  func ExecutionHeaderFromJSON(forkName string, data []byte) (*ExecutionHeader, error) {
    42  	var obj headerObject
    43  	switch forkName {
    44  	case "capella":
    45  		obj = new(capella.ExecutionPayloadHeader)
    46  	case "deneb":
    47  		obj = new(deneb.ExecutionPayloadHeader)
    48  	default:
    49  		return nil, fmt.Errorf("unsupported fork: " + forkName)
    50  	}
    51  	if err := json.Unmarshal(data, obj); err != nil {
    52  		return nil, err
    53  	}
    54  	return &ExecutionHeader{obj: obj}, nil
    55  }
    56  
    57  func NewExecutionHeader(obj headerObject) *ExecutionHeader {
    58  	switch obj.(type) {
    59  	case *capella.ExecutionPayloadHeader:
    60  	case *deneb.ExecutionPayloadHeader:
    61  	default:
    62  		panic(fmt.Errorf("unsupported ExecutionPayloadHeader type %T", obj))
    63  	}
    64  	return &ExecutionHeader{obj: obj}
    65  }
    66  
    67  func (eh *ExecutionHeader) PayloadRoot() merkle.Value {
    68  	return merkle.Value(eh.obj.HashTreeRoot(tree.GetHashFn()))
    69  }
    70  
    71  func (eh *ExecutionHeader) BlockHash() common.Hash {
    72  	switch obj := eh.obj.(type) {
    73  	case *capella.ExecutionPayloadHeader:
    74  		return common.Hash(obj.BlockHash)
    75  	case *deneb.ExecutionPayloadHeader:
    76  		return common.Hash(obj.BlockHash)
    77  	default:
    78  		panic(fmt.Errorf("unsupported ExecutionPayloadHeader type %T", obj))
    79  	}
    80  }