Brother Lian Blockchain technical training Fabric 1.0 Source Code Analysis (a) Peer #peer Channel Command and Sub-command implementation
 
# Fabric 1.0 Source Code notes Peer #peer Channel command and subcommand implementation
 
 
# 1, peer channel Create sub-command implementation (create channel)
 
 
# # # 1.1, initialize the Orderer client
 
 
"Go
 
Const (
 
endorserrequired Endorserrequirement = True
 
Endorsernotrequired Endorserrequirement = False
 
ordererrequired Ordererrequirement = True
 
Orderernotrequired Ordererrequirement = False
 
)
 
 
CF, err = initcmdfactory (endorsernotrequired, ordererrequired)
 
Code in Peer/channel/create.go
 
```
 
 
CF, err = initcmdfactory (endorsernotrequired, ordererrequired) code is as follows:
 
 
"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 or list needs to be endorser and is not currently used
 
If isendorserrequired {
 
Cmdfact.endorserclient, err = common. GETENDORSERCLIENTFNC ()
 
}
 
 
Peer channel create or fetch requires 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)//Construction Deliverclient
 
}
 
Return cmdfact, Nil
 
}
 
 
Code in Peer/channel/channel.go
 
```
 
 
# # 1.2, send a transaction to create a blockchain
 
 
"Go
 
Err = sendcreatechaintransaction (CF)
 
Code in Peer/channel/create.go
 
```
 
 
The sendcreatechaintransaction (CF) code is as follows:
 
 
"Go
 
Func sendcreatechaintransaction (cf *channelcmdfactory) error {
 
var err error
 
var chcrtenv *cb. Envelope
 
 
If channeltxfile! = "" "{//peer channel create-f specified channels trading profile
 
Chcrtenv, err = CREATECHANNELFROMCONFIGTX (channeltxfile)//Get Genesis block
 
} else {
 
Chcrtenv, err = createchannelfromdefaults (CF)//No channel trade profile specified
 
}
 
 
Chcrtenv, err = SANITYCHECKANDSIGNCONFIGTX (chcrtenv)//check and sign trading configuration
 
var broadcastclient common. Broadcastclient
 
Broadcastclient, err = cf. Broadcastfactory ()
 
Defer Broadcastclient.close ()
 
Err = Broadcastclient.send (chcrtenv)
 
return err
 
}
 
Code in Peer/channel/create.go
 
```
 
 
# # 1.3, get the Genesis Chunk and write the file
 
 
"Go
 
var block *CB. Block
 
block, err = getgenesisblock (CF)//Get Genesis block
 
B, err: = Proto. Marshal (block)//blocks serialization
 
Err = Ioutil. WriteFile (file, B, 0644)//write files
 
Code in Peer/channel/create.go
 
```
 
 
# 2, Peer channel join Sub-command implementation (join channels)
 
 
# # # 2.1, initialize the endorser client
 
 
"Go
 
CF, err = initcmdfactory (endorserrequired, orderernotrequired)
 
Code in Peer/channel/join.go
 
```
 
 
# # # 2.2, construct Chaincodeinvocationspec message (Cscc.joinchain)
 
 
"Go
 
Spec, err: = Getjoinccspec ()
 
Invocation: = &PB. CHAINCODEINVOCATIONSPEC{CHAINCODESPEC:SPEC}
 
Code in Peer/channel/join.go
 
```
 
 
Spec, err: = Getjoinccspec () code is as follows:
 
 
"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
 
}
 
Code in Peer/channel/join.go
 
```
 
 
# # # 2.3, create CSCC proposal and sign
 
 
"Go
 
Creator, Err: = cf. Signer.serialize ()
 
Create Proposal,cis-Chaincodeinvocationspec from CIS
 
Call Createchaincodeproposal (Typ, Chainid, CIS, creator)
 
Then call 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)
 
Code in Peer/channel/join.go
 
```
 
 
The code for Createchaincodeproposalwithtransient (Typ, Chainid, CIS, creator, nil) is as follows:
 
 
"Go
 
Func createchaincodeproposalwithtransient (Typ common. Headertype, Chainid string, cis *peer. Chaincodeinvocationspec, Creator []byte, Transientmap Map[string][]byte] (*peer. Proposal, str ing, error) {
 
Create Random Nonce
 
Nonce, err: = Crypto. Getrandomnonce ()
 
Calculate Transaction 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)
 
}
 
Code in Protos/utils/proputils.go
 
```
 
 
Createchaincodeproposalwithtxidnonceandtransient (Txid, Typ, Chainid, CIS, nonce, creator, Transientmap) codes are as follows:
 
 
"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
 
}
 
Code in Protos/utils/proputils.go
 
```
 
 
# # 2.4, Submit and process proposal
 
 
"Go
 
var proposalresp *pb. Proposalresponse
 
PROPOSALRESP, err = cf. Endorserclient.processproposal (context. Background (), Signedprop)
 
Code in Peer/channel/join.go
 
```
 
 
# 3, Peer Channel Update (update anchor node configuration)
 
 
"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)
 
Code in Peer/channel
 
 is the technology development too fast, or we have lost the end? From July 7 onwards, two hours a day with the Tsinghua Microsoft Google Daniel team to combat the blockchain.
 
HTTP://WWW.YDMA.CN/OPEN/COURSE/16 to come, Tsinghua Genius Yin Cheng Big Brother take you fly up ~ ~ ~