6 Star 46 Fork 28

Hyperledger/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
genesis.go 2.47 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package genesis
import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/configtx"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)
const (
msgVersion = int32(1)
// These values are fixed for the genesis block.
epoch = 0
)
// Factory facilitates the creation of genesis blocks.
type Factory interface {
// Block returns a genesis block for a given channel ID.
Block(channelID string) (*cb.Block, error)
}
type factory struct {
template configtx.Template
}
// NewFactoryImpl creates a new Factory.
func NewFactoryImpl(template configtx.Template) Factory {
return &factory{template: template}
}
// Block constructs and returns a genesis block for a given channel ID.
func (f *factory) Block(channelID string) (*cb.Block, error) {
configEnv, err := f.template.Envelope(channelID)
if err != nil {
return nil, err
}
configUpdate := &cb.ConfigUpdate{}
err = proto.Unmarshal(configEnv.ConfigUpdate, configUpdate)
if err != nil {
return nil, err
}
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hyperledger/fabric.git
git@gitee.com:hyperledger/fabric.git
hyperledger
fabric
fabric
v1.0.3

搜索帮助