github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/genesis/genesis.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package genesis 18 19 import ( 20 "github.com/golang/protobuf/proto" 21 "github.com/hyperledger/fabric/common/configtx" 22 cb "github.com/hyperledger/fabric/protos/common" 23 "github.com/hyperledger/fabric/protos/utils" 24 ) 25 26 const ( 27 msgVersion = int32(1) 28 29 // These values are fixed for the genesis block. 30 lastModified = 0 31 epoch = 0 32 ) 33 34 type Factory interface { 35 Block(chainID string) (*cb.Block, error) 36 } 37 38 type factory struct { 39 template configtx.Template 40 } 41 42 func NewFactoryImpl(template configtx.Template) Factory { 43 return &factory{template: template} 44 } 45 46 func (f *factory) Block(chainID string) (*cb.Block, error) { 47 configEnv, err := f.template.Envelope(chainID) 48 if err != nil { 49 return nil, err 50 } 51 52 configUpdate := &cb.ConfigUpdate{} 53 err = proto.Unmarshal(configEnv.ConfigUpdate, configUpdate) 54 if err != nil { 55 return nil, err 56 } 57 58 payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG, msgVersion, chainID, epoch) 59 payloadSignatureHeader := utils.MakeSignatureHeader(nil, utils.CreateNonceOrPanic()) 60 payloadHeader := utils.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader) 61 payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(&cb.ConfigEnvelope{Config: &cb.Config{Header: configUpdate.Header, Channel: configUpdate.WriteSet}})} 62 envelope := &cb.Envelope{Payload: utils.MarshalOrPanic(payload), Signature: nil} 63 64 block := cb.NewBlock(0, nil) 65 block.Data = &cb.BlockData{Data: [][]byte{utils.MarshalOrPanic(envelope)}} 66 block.Header.DataHash = block.Data.Hash() 67 block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = utils.MarshalOrPanic(&cb.Metadata{ 68 Value: utils.MarshalOrPanic(&cb.LastConfig{Index: 0}), 69 }) 70 return block, nil 71 }