Blockchain tutorial Fabric1.0 source code analysis configtx × genesis

Posted by richei on Fri, 13 Dec 2019 21:41:45 +0100

The blockchain tutorial Fabric1.0 source code analysis configtx ා genesis, in the second half of 2018, the blockchain industry is gradually fading away from the impetuosity and returning to rationality at the beginning of development. On the surface, the demand and value of relevant talents seem to be falling. But in fact, it is the gradual decline of the initial bubble that gives people more attention to the real technology of the block chain.

configtx (configuration transaction) of Fabric 1.0 source code note (genesis block of system channel)

1. genesis overview

genesis block refers to genesis block of system channel.
The relevant code is in common/genesis/genesis.go, that is, Factory interface and implementation.

2. Factory interface definition

type Factory interface {
    Block(channelID string) (*cb.Block, error)
}
//The code is in common/genesis/genesis.go

3. Factory interface implementation

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
}

//The code is in common/genesis/genesis.go

Topics: Go Blockchain