github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/genesis/genesis.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package genesis
     8  
     9  import (
    10  	cb "github.com/hyperledger/fabric-protos-go/common"
    11  	"github.com/hyperledger/fabric/protoutil"
    12  )
    13  
    14  const (
    15  	msgVersion = int32(1)
    16  
    17  	// These values are fixed for the genesis block.
    18  	epoch = 0
    19  )
    20  
    21  // Factory facilitates the creation of genesis blocks.
    22  type Factory interface {
    23  	// Block returns a genesis block for a given channel ID.
    24  	Block(channelID string) *cb.Block
    25  }
    26  
    27  type factory struct {
    28  	channelGroup *cb.ConfigGroup
    29  }
    30  
    31  // NewFactoryImpl creates a new Factory.
    32  func NewFactoryImpl(channelGroup *cb.ConfigGroup) Factory {
    33  	return &factory{channelGroup: channelGroup}
    34  }
    35  
    36  // Block constructs and returns a genesis block for a given channel ID.
    37  func (f *factory) Block(channelID string) *cb.Block {
    38  	payloadChannelHeader := protoutil.MakeChannelHeader(cb.HeaderType_CONFIG, msgVersion, channelID, epoch)
    39  	payloadSignatureHeader := protoutil.MakeSignatureHeader(nil, protoutil.CreateNonceOrPanic())
    40  	protoutil.SetTxID(payloadChannelHeader, payloadSignatureHeader)
    41  	payloadHeader := protoutil.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader)
    42  	payload := &cb.Payload{Header: payloadHeader, Data: protoutil.MarshalOrPanic(&cb.ConfigEnvelope{Config: &cb.Config{ChannelGroup: f.channelGroup}})}
    43  	envelope := &cb.Envelope{Payload: protoutil.MarshalOrPanic(payload), Signature: nil}
    44  
    45  	block := protoutil.NewBlock(0, nil)
    46  	block.Data = &cb.BlockData{Data: [][]byte{protoutil.MarshalOrPanic(envelope)}}
    47  	block.Header.DataHash = protoutil.BlockDataHash(block.Data)
    48  	block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = protoutil.MarshalOrPanic(&cb.Metadata{
    49  		Value: protoutil.MarshalOrPanic(&cb.LastConfig{Index: 0}),
    50  	})
    51  	return block
    52  }