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