Fabric 1.0原始碼分析(8)configtx(配置交易) #genesis(系統通道創世區塊)

尹成發表於2018-05-20
# Fabric 1.0原始碼筆記 之 configtx(配置交易) #genesis(系統通道創世區塊)

## 1、genesis概述

genesis,即創世區塊,此處特指系統通道的創世區塊。
相關程式碼在common/genesis/genesis.go,即Factory介面及實現。

## 2、Factory介面定義

```go
type Factory interface {
    Block(channelID string) (*cb.Block, error)
}
//程式碼在common/genesis/genesis.go
```

## 3、Factory介面實現

```go
msgVersion = int32(1)
epoch = 0

type factory struct {
    template configtx.Template
}

func NewFactoryImpl(template configtx.Template) Factory {
    return &factory{template: template}
}

func (f *factory) Block(channelID string) (*cb.Block, error) {
    configEnv, err := f.template.Envelope(channelID)
    configUpdate := &cb.ConfigUpdate{}
    err = proto.Unmarshal(configEnv.ConfigUpdate, configUpdate)

    payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG, msgVersion, channelID, epoch)
    payloadSignatureHeader := utils.MakeSignatureHeader(nil, utils.CreateNonceOrPanic())
    utils.SetTxID(payloadChannelHeader, payloadSignatureHeader)
    payloadHeader := utils.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader)
    payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(&cb.ConfigEnvelope{Config: &cb.Config{ChannelGroup: configUpdate.WriteSet}})}
    envelope := &cb.Envelope{Payload: utils.MarshalOrPanic(payload), Signature: nil}

    block := cb.NewBlock(0, nil)
    block.Data = &cb.BlockData{Data: [][]byte{utils.MarshalOrPanic(envelope)}}
    block.Header.DataHash = block.Data.Hash()
    block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = utils.MarshalOrPanic(&cb.Metadata{
        Value: utils.MarshalOrPanic(&cb.LastConfig{Index: 0}),
    })
    return block, nil
}

//程式碼在common/genesis/genesis.go
```





網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN







網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN

相關文章