github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/beacon/types/header.go (about) 1 // Copyright 2022 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 implements a few types of the beacon chain for light client usage. 18 package types 19 20 import ( 21 "crypto/sha256" 22 "encoding/binary" 23 24 "github.com/ethereum/go-ethereum/beacon/merkle" 25 "github.com/ethereum/go-ethereum/beacon/params" 26 "github.com/ethereum/go-ethereum/common" 27 zrntcommon "github.com/protolambda/zrnt/eth2/beacon/common" 28 ) 29 30 //go:generate go run github.com/fjl/gencodec -type Header -field-override headerMarshaling -out gen_header_json.go 31 32 const ( 33 headerIndexSlot = 8 34 headerIndexProposerIndex = 9 35 headerIndexParentRoot = 10 36 headerIndexStateRoot = 11 37 headerIndexBodyRoot = 12 38 ) 39 40 // Header defines a beacon header. 41 // 42 // See data structure definition here: 43 // https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#beaconblockheader 44 type Header struct { 45 // Monotonically increasing slot number for the beacon block (may be gapped) 46 Slot uint64 `gencodec:"required" json:"slot"` 47 48 // Index into the validator table who created the beacon block 49 ProposerIndex uint64 `gencodec:"required" json:"proposer_index"` 50 51 // SSZ hash of the parent beacon header 52 ParentRoot common.Hash `gencodec:"required" json:"parent_root"` 53 54 // SSZ hash of the beacon state (https://github.com/ethereum/consensus-specs/blob/dev/specs/bellatrix/beacon-chain.md#beacon-state) 55 StateRoot common.Hash `gencodec:"required" json:"state_root"` 56 57 // SSZ hash of the beacon block body (https://github.com/ethereum/consensus-specs/blob/dev/specs/bellatrix/beacon-chain.md#beaconblockbody) 58 BodyRoot common.Hash `gencodec:"required" json:"body_root"` 59 } 60 61 func headerFromZRNT(zh *zrntcommon.BeaconBlockHeader) Header { 62 return Header{ 63 Slot: uint64(zh.Slot), 64 ProposerIndex: uint64(zh.ProposerIndex), 65 ParentRoot: common.Hash(zh.ParentRoot), 66 StateRoot: common.Hash(zh.StateRoot), 67 BodyRoot: common.Hash(zh.BodyRoot), 68 } 69 } 70 71 // headerMarshaling is a field type overrides for gencodec. 72 type headerMarshaling struct { 73 Slot common.Decimal 74 ProposerIndex common.Decimal 75 } 76 77 // Hash calculates the block root of the header. 78 // 79 // TODO(zsfelfoldi): Remove this when an SSZ encoder lands. 80 func (h *Header) Hash() common.Hash { 81 var values [16]merkle.Value // values corresponding to indices 8 to 15 of the beacon header tree 82 binary.LittleEndian.PutUint64(values[headerIndexSlot][:8], h.Slot) 83 binary.LittleEndian.PutUint64(values[headerIndexProposerIndex][:8], h.ProposerIndex) 84 values[headerIndexParentRoot] = merkle.Value(h.ParentRoot) 85 values[headerIndexStateRoot] = merkle.Value(h.StateRoot) 86 values[headerIndexBodyRoot] = merkle.Value(h.BodyRoot) 87 hasher := sha256.New() 88 for i := 7; i > 0; i-- { 89 hasher.Reset() 90 hasher.Write(values[i*2][:]) 91 hasher.Write(values[i*2+1][:]) 92 hasher.Sum(values[i][:0]) 93 } 94 return common.Hash(values[1]) 95 } 96 97 // Epoch returns the epoch the header belongs to. 98 func (h *Header) Epoch() uint64 { 99 return h.Slot / params.EpochLength 100 } 101 102 // SyncPeriod returns the sync period the header belongs to. 103 func (h *Header) SyncPeriod() uint64 { 104 return SyncPeriod(h.Slot) 105 } 106 107 // SyncPeriodStart returns the first slot of the given period. 108 func SyncPeriodStart(period uint64) uint64 { 109 return period * params.SyncPeriodLength 110 } 111 112 // SyncPeriod returns the sync period that the given slot belongs to. 113 func SyncPeriod(slot uint64) uint64 { 114 return slot / params.SyncPeriodLength 115 } 116 117 // SignedHeader represents a beacon header signed by a sync committee. 118 // 119 // This structure is created from either an optimistic update or an instant update: 120 // - https://github.com/ethereum/consensus-specs/blob/dev/specs/altair/light-client/sync-protocol.md#lightclientoptimisticupdate 121 // - https://github.com/zsfelfoldi/beacon-APIs/blob/instant_update/apis/beacon/light_client/instant_update.yaml 122 type SignedHeader struct { 123 // Beacon header being signed 124 Header Header 125 126 // Sync committee BLS signature aggregate 127 Signature SyncAggregate 128 129 // Slot in which the signature has been created (newer than Header.Slot, 130 // determines the signing sync committee) 131 SignatureSlot uint64 132 }