github.com/ethereum/go-ethereum@v1.14.3/beacon/types/beacon_block.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/common"
    24  	"github.com/ethereum/go-ethereum/core/types"
    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/zrnt/eth2/configs"
    29  	"github.com/protolambda/ztyp/tree"
    30  )
    31  
    32  type blockObject interface {
    33  	HashTreeRoot(spec *zrntcommon.Spec, hFn tree.HashFn) zrntcommon.Root
    34  	Header(spec *zrntcommon.Spec) *zrntcommon.BeaconBlockHeader
    35  }
    36  
    37  // BeaconBlock represents a full block in the beacon chain.
    38  type BeaconBlock struct {
    39  	blockObj blockObject
    40  }
    41  
    42  // BlockFromJSON decodes a beacon block from JSON.
    43  func BlockFromJSON(forkName string, data []byte) (*BeaconBlock, error) {
    44  	var obj blockObject
    45  	switch forkName {
    46  	case "deneb":
    47  		obj = new(deneb.BeaconBlock)
    48  	case "capella":
    49  		obj = new(capella.BeaconBlock)
    50  	default:
    51  		return nil, fmt.Errorf("unsupported fork: " + forkName)
    52  	}
    53  	if err := json.Unmarshal(data, obj); err != nil {
    54  		return nil, err
    55  	}
    56  	return &BeaconBlock{obj}, nil
    57  }
    58  
    59  // NewBeaconBlock wraps a ZRNT block.
    60  func NewBeaconBlock(obj blockObject) *BeaconBlock {
    61  	switch obj := obj.(type) {
    62  	case *capella.BeaconBlock:
    63  		return &BeaconBlock{obj}
    64  	case *deneb.BeaconBlock:
    65  		return &BeaconBlock{obj}
    66  	default:
    67  		panic(fmt.Errorf("unsupported block type %T", obj))
    68  	}
    69  }
    70  
    71  // Slot returns the slot number of the block.
    72  func (b *BeaconBlock) Slot() uint64 {
    73  	switch obj := b.blockObj.(type) {
    74  	case *capella.BeaconBlock:
    75  		return uint64(obj.Slot)
    76  	case *deneb.BeaconBlock:
    77  		return uint64(obj.Slot)
    78  	default:
    79  		panic(fmt.Errorf("unsupported block type %T", b.blockObj))
    80  	}
    81  }
    82  
    83  // ExecutionPayload parses and returns the execution payload of the block.
    84  func (b *BeaconBlock) ExecutionPayload() (*types.Block, error) {
    85  	switch obj := b.blockObj.(type) {
    86  	case *capella.BeaconBlock:
    87  		return convertPayload(&obj.Body.ExecutionPayload, &obj.ParentRoot)
    88  	case *deneb.BeaconBlock:
    89  		return convertPayload(&obj.Body.ExecutionPayload, &obj.ParentRoot)
    90  	default:
    91  		panic(fmt.Errorf("unsupported block type %T", b.blockObj))
    92  	}
    93  }
    94  
    95  // Header returns the block's header data.
    96  func (b *BeaconBlock) Header() Header {
    97  	switch obj := b.blockObj.(type) {
    98  	case *capella.BeaconBlock:
    99  		return headerFromZRNT(obj.Header(configs.Mainnet))
   100  	case *deneb.BeaconBlock:
   101  		return headerFromZRNT(obj.Header(configs.Mainnet))
   102  	default:
   103  		panic(fmt.Errorf("unsupported block type %T", b.blockObj))
   104  	}
   105  }
   106  
   107  // Root computes the SSZ root hash of the block.
   108  func (b *BeaconBlock) Root() common.Hash {
   109  	return common.Hash(b.blockObj.HashTreeRoot(configs.Mainnet, tree.GetHashFn()))
   110  }