Fabric 1.0原始碼分析(33) Peer #peer channel命令及子命令實現
# Fabric 1.0原始碼筆記 之 Peer #peer channel命令及子命令實現
## 1、peer channel create子命令實現(建立通道)
### 1.1、初始化Orderer客戶端
```go
const (
EndorserRequired EndorserRequirement = true
EndorserNotRequired EndorserRequirement = false
OrdererRequired OrdererRequirement = true
OrdererNotRequired OrdererRequirement = false
)
cf, err = InitCmdFactory(EndorserNotRequired, OrdererRequired)
//程式碼在peer/channel/create.go
```
cf, err = InitCmdFactory(EndorserNotRequired, OrdererRequired)程式碼如下:
```go
type ChannelCmdFactory struct {
EndorserClient pb.EndorserClient //EndorserClient
Signer msp.SigningIdentity //Signer
BroadcastClient common.BroadcastClient //BroadcastClient
DeliverClient deliverClientIntf //DeliverClient
BroadcastFactory BroadcastClientFactory //BroadcastClientFactory,type BroadcastClientFactory func() (common.BroadcastClient, error)
}
func InitCmdFactory(isEndorserRequired EndorserRequirement, isOrdererRequired OrdererRequirement) (*ChannelCmdFactory, error) {
var err error
cmdFact := &ChannelCmdFactory{}
cmdFact.Signer, err = common.GetDefaultSignerFnc() //GetDefaultSignerFnc = GetDefaultSigner
cmdFact.BroadcastFactory = func() (common.BroadcastClient, error) {
return common.GetBroadcastClientFnc(orderingEndpoint, tls, caFile) //GetBroadcastClientFnc = GetBroadcastClient
}
//peer channel join或list需要endorser,本處暫未使用
if isEndorserRequired {
cmdFact.EndorserClient, err = common.GetEndorserClientFnc()
}
//peer channel create或fetch需要orderer
if isOrdererRequired {
var opts []grpc.DialOption
if tls {
if caFile != "" {
creds, err := credentials.NewClientTLSFromFile(caFile, "")
opts = append(opts, grpc.WithTransportCredentials(creds))
}
} else {
opts = append(opts, grpc.WithInsecure())
}
conn, err := grpc.Dial(orderingEndpoint, opts...)
client, err := ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO())
cmdFact.DeliverClient = newDeliverClient(conn, client, chainID) //構造deliverClient
}
return cmdFact, nil
}
//程式碼在peer/channel/channel.go
```
### 1.2、傳送建立區塊鏈的交易
```go
err = sendCreateChainTransaction(cf)
//程式碼在peer/channel/create.go
```
sendCreateChainTransaction(cf)程式碼如下:
```go
func sendCreateChainTransaction(cf *ChannelCmdFactory) error {
var err error
var chCrtEnv *cb.Envelope
if channelTxFile != "" { //peer channel create -f指定通道交易配置檔案
chCrtEnv, err = createChannelFromConfigTx(channelTxFile) //獲取創世區塊
} else {
chCrtEnv, err = createChannelFromDefaults(cf) //沒有指定通道交易配置檔案
}
chCrtEnv, err = sanityCheckAndSignConfigTx(chCrtEnv) //檢查和簽名交易配置
var broadcastClient common.BroadcastClient
broadcastClient, err = cf.BroadcastFactory()
defer broadcastClient.Close()
err = broadcastClient.Send(chCrtEnv)
return err
}
//程式碼在peer/channel/create.go
```
### 1.3、獲取創世區塊並寫入檔案
```go
var block *cb.Block
block, err = getGenesisBlock(cf) //獲取創世區塊
b, err := proto.Marshal(block) //塊序列化
err = ioutil.WriteFile(file, b, 0644) //寫入檔案
//程式碼在peer/channel/create.go
```
## 2、peer channel join子命令實現(加入通道)
### 2.1、初始化Endorser客戶端
```go
cf, err = InitCmdFactory(EndorserRequired, OrdererNotRequired)
//程式碼在peer/channel/join.go
```
### 2.2、構造ChaincodeInvocationSpec訊息(cscc.JoinChain)
```go
spec, err := getJoinCCSpec()
invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}
//程式碼在peer/channel/join.go
```
spec, err := getJoinCCSpec()程式碼如下:
```go
func getJoinCCSpec() (*pb.ChaincodeSpec, error) {
gb, err := ioutil.ReadFile(genesisBlockPath)
input := &pb.ChaincodeInput{Args: [][]byte{[]byte(cscc.JoinChain), gb}}
spec := &pb.ChaincodeSpec{
Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]),
ChaincodeId: &pb.ChaincodeID{Name: "cscc"},
Input: input,
}
return spec, nil
}
//程式碼在peer/channel/join.go
```
### 2.3、建立cscc Proposal並簽名
```go
creator, err := cf.Signer.Serialize()
//從CIS建立Proposal,CIS即ChaincodeInvocationSpec
//呼叫CreateChaincodeProposal(typ, chainID, cis, creator)
//而後呼叫CreateChaincodeProposalWithTransient(typ, chainID, cis, creator, nil)
prop, _, err = putils.CreateProposalFromCIS(pcommon.HeaderType_CONFIG, "", invocation, creator)
var signedProp *pb.SignedProposal
signedProp, err = putils.GetSignedProposal(prop, cf.Signer)
//程式碼在peer/channel/join.go
```
CreateChaincodeProposalWithTransient(typ, chainID, cis, creator, nil)程式碼如下:
```go
func CreateChaincodeProposalWithTransient(typ common.HeaderType, chainID string, cis *peer.ChaincodeInvocationSpec, creator []byte, transientMap map[string][]byte) (*peer.Proposal, str ing, error) {
//建立隨機Nonce
nonce, err := crypto.GetRandomNonce()
//計算交易ID
//digest, err := factory.GetDefault().Hash(append(nonce, creator...),&bccsp.SHA256Opts{})
txid, err := ComputeProposalTxID(nonce, creator)
return CreateChaincodeProposalWithTxIDNonceAndTransient(txid, typ, chainID, cis, nonce, creator, transientMap)
}
//程式碼在protos/utils/proputils.go
```
CreateChaincodeProposalWithTxIDNonceAndTransient(txid, typ, chainID, cis, nonce, creator, transientMap)程式碼如下:
```go
func CreateChaincodeProposalWithTxIDNonceAndTransient(txid string, typ common.HeaderType, chainID string, cis *peer.ChaincodeInvocationSpec, nonce, creator []byte, transientMap map[string][]byte) (*peer.Proposal, string, error) {
ccHdrExt := &peer.ChaincodeHeaderExtension{ChaincodeId: cis.ChaincodeSpec.ChaincodeId}
ccHdrExtBytes, err := proto.Marshal(ccHdrExt)
cisBytes, err := proto.Marshal(cis)
ccPropPayload := &peer.ChaincodeProposalPayload{Input: cisBytes, TransientMap: transientMap}
ccPropPayloadBytes, err := proto.Marshal(ccPropPayload)
var epoch uint64 = 0
timestamp := util.CreateUtcTimestamp()
hdr := &common.Header{ChannelHeader: MarshalOrPanic(&common.ChannelHeader{
Type: int32(typ),
TxId: txid,
Timestamp: timestamp,
ChannelId: chainID,
Extension: ccHdrExtBytes,
Epoch: epoch}),
SignatureHeader: MarshalOrPanic(&common.SignatureHeader{Nonce: nonce, Creator: creator})}
hdrBytes, err := proto.Marshal(hdr)
return &peer.Proposal{Header: hdrBytes, Payload: ccPropPayloadBytes}, txid, nil
}
//程式碼在protos/utils/proputils.go
```
### 2.4、提交併處理Proposal
```go
var proposalResp *pb.ProposalResponse
proposalResp, err = cf.EndorserClient.ProcessProposal(context.Background(), signedProp)
//程式碼在peer/channel/join.go
```
## 3、peer channel update(更新錨節點配置)
```go
cf, err = InitCmdFactory(EndorserNotRequired, OrdererRequired)
fileData, err := ioutil.ReadFile(channelTxFile)
ctxEnv, err := utils.UnmarshalEnvelope(fileData)
sCtxEnv, err := sanityCheckAndSignConfigTx(ctxEnv)
var broadcastClient common.BroadcastClient
broadcastClient, err = cf.BroadcastFactory()
defer broadcastClient.Close()
return broadcastClient.Send(sCtxEnv)
//程式碼在peer/channel
```
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
網址:http://www.qukuailianxueyuan.io/
欲領取造幣技術與全套虛擬機器資料
區塊鏈技術交流QQ群:756146052 備註:CSDN
尹成學院微信:備註:CSDN
相關文章
- Fabric 1.0原始碼分析(34) Peer #peer chaincode命令及子命令實現原始碼AI
- Fabric 1.0原始碼分析(32)Peer #peer根命令入口及載入子命令原始碼
- Fabric 1.0原始碼分析(32) Peer #peer node start命令實現原始碼
- Fabric 1.0原始碼分析(31) Peer原始碼
- Fabric 1.0原始碼分析(35)Peer #EndorserServer(Endorser服務端)原始碼Server服務端
- Fabric 1.0原始碼分析(36) Peer #EndorserClient(Endorser客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(37) Peer #DeliverClient(Deliver客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(38) Peer #BroadcastClient(Broadcast客戶端)原始碼ASTclient客戶端
- Fabric1.4原始碼解析:Peer節點加入通道原始碼
- 以太坊原始碼分析(31)eth-downloader-peer原始碼分析原始碼
- 以太坊原始碼分析(32)eth-downloader-peer原始碼分析原始碼
- 以太坊原始碼分析(46)p2p-peer.go原始碼分析原始碼Go
- Fabric 1.0原始碼分析(25) Orderer原始碼
- Fabric 1.0原始碼分析(3)Chaincode(鏈碼)原始碼AI
- Fabric 1.0原始碼分析(40) Proposal(提案)原始碼
- Jaeger tchannel-go原始碼閱讀——PeerGo原始碼
- Fabric 1.0原始碼分析(14) flogging(Fabric日誌系統)原始碼
- Fabric 1.0原始碼分析(18) Ledger(賬本)原始碼
- Fabric 1.0原始碼分析(43) Tx(Transaction 交易)原始碼
- Fabric 1.0原始碼分析(47)Fabric 1.0.4 go程式碼量統計原始碼Go
- Fabric 1.0原始碼分析(42)scc(系統鏈碼)原始碼
- Fabric 1.0原始碼分析(13)events(事件服務)原始碼事件
- Fabric 1.0原始碼分析(26)Orderer #ledger(Orderer Ledger)原始碼
- Fabric 1.0原始碼分析(39) policy(背書策略)原始碼
- Fabric 1.0原始碼分析(45)gRPC(Fabric中註冊的gRPC Service)原始碼RPC
- Fabric 1.0原始碼分析(10)consenter(共識外掛)原始碼
- Fabric 1.0原始碼分析(15)gossip(流言演算法)原始碼Go演算法
- Fabric 1.0原始碼分析(23)LevelDB(KV資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(44)Tx #RWSet(讀寫集)原始碼
- Fabric 1.0原始碼分析(5)Chaincode(鏈碼)體系總結原始碼AI
- Fabric 1.0原始碼分析(6)configtx(配置交易) #ChannelConfig(通道配置)原始碼
- Fabric 1.0原始碼分析(20) Ledger #idStore(ledgerID資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(29) Orderer #multichain(多鏈支援包)原始碼AI
- Fabric 1.0原始碼分析(30) Orderer #BroadcastServer(Broadcast服務端)原始碼ASTServer服務端
- Fabric 1.0原始碼分析(41)putils(protos/utils工具包)原始碼
- Fabric 1.0原始碼分析(2) blockfile(區塊檔案儲存)原始碼BloC
- Fabric 1.0原始碼分析(7)configtx(配置交易) #configtxgen(生成通道配置)原始碼
- Fabric 1.0原始碼分析(9)configtx(配置交易)體系介紹原始碼