github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/genesis_block.go (about)

     1  // Copyright (c) 2021 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package block
     7  
     8  import (
     9  	"sync"
    10  	"time"
    11  
    12  	"github.com/iotexproject/go-pkgs/hash"
    13  
    14  	"github.com/iotexproject/iotex-core/blockchain/genesis"
    15  	"github.com/iotexproject/iotex-core/pkg/version"
    16  )
    17  
    18  var (
    19  	_loadGenesisHash sync.Once
    20  	_genesisHash     hash.Hash256
    21  )
    22  
    23  // GenesisBlock returns the genesis block
    24  func GenesisBlock() *Block {
    25  	return &Block{
    26  		Header: Header{
    27  			version:          version.ProtocolVersion,
    28  			height:           0,
    29  			timestamp:        time.Unix(genesis.Timestamp(), 0),
    30  			prevBlockHash:    hash.ZeroHash256,
    31  			txRoot:           hash.ZeroHash256,
    32  			deltaStateDigest: hash.ZeroHash256,
    33  			receiptRoot:      hash.ZeroHash256,
    34  		},
    35  	}
    36  }
    37  
    38  // GenesisHash returns the genesis block's hash
    39  func GenesisHash() hash.Hash256 {
    40  	return _genesisHash
    41  }
    42  
    43  // LoadGenesisHash is done once to compute and save the genesis'es hash
    44  func LoadGenesisHash(g *genesis.Genesis) {
    45  	_loadGenesisHash.Do(func() {
    46  		_genesisHash = g.Hash()
    47  	})
    48  }