Hyperledger source code Analysis of Fabric
Broadcast means that the client sends the request message (for example, after the endorsement transaction is completed) to the ordering service via the Grpc interface.
These request messages are given to the broadcast (SRV ab) of the server structure in the Orderer.common.server package. Atomicbroadcast_broadcastserver) Error method processing. This method will mainly call the Handle (SRV ab) of the HANDLERIMPL structure in the Orderer.common.broadcast package. Atomicbroadcast_broadcastserver) Error method for processing.
Handlerimpl structure is very important to complete the process of broadcast request.
Type Handlerimpl struct {
error
Handle (SRV ab. Atomicbroadcast_broadcastserver) The error method opens a loop to read the request message from the SRV and process it until the end. The core code is shown below.
For {
error : = srv. RECV ()//extracts a Envelope message from the request
CHDR, Isconfig, processor, err: = Bh.sm.BroadcastChannelSupport (msg)//Parse message: Is configuration message, elimination What chain should be processed
//corresponding to the message processing
if!isconfig {
configseq, err: = processor. PROCESSNORMALMSG (MSG)//message check
processor. Order (MSG, CONFIGSEQ)//into queue operations
} else {
config, configseq, err: = processor. PROCESSCONFIGUPDATEMSG (MSG)//merge configuration update message
processor. Configure (config, configseq)//into queue operations
}
srv. Send (&ab. BROADCASTRESPONSE{STATUS:CB. STATUS_SUCCESS})//return response message}
The overall process of the broadcast request is shown in the following figure.
parsing Messages
First, parse the message, get the header, configure the message, get the corresponding processor structure (chain structure).
CHDR, Isconfig, processor, err: = Bh.sm.BroadcastChannelSupport (msg)
In fact, the Broadcastchannelsupport (msg *CB) of the broadcastsupport structure in the Orderer.common.server package is mapped. Envelope) (*CB. Channelheader, BOOL, broadcast. Channelsupport, error) method, further calls the corresponding method of registrar structure body in Orderer.common.multichannel package.
BOOL error) {
Chdr, err: = Utils. Channelheader (msg)
If Err!= nil {return
nil, false, nil, FMT. Errorf ("Could not determine channel ID:%s", err)
}
cs, OK: = R.chains[chdr. Channelid]//Application channel, System channel
if!ok {
cs = R.systemchannel
}
isconfig: = False
switch cs. Classifymsg (CHDR) {case
msgprocessor. Configupdatemsg:
Isconfig = True
default:
} return
CHDR, Isconfig, CS, nil}
Channel the header is parsed from the message envelope structure and whether the configuration information is judged by the type in the message header (CB). headertype_config_update); The corresponding chainsupport structure (application channel, System channel) is identified by the dictionary as the processor.
After that, the different types of messages (ordinary messages, configuration messages) are treated differently with the result of parsing. The following is an introduction to the Chainsupport structure of the application channel as a processor. handling non-configuration messages
For non-configuration messages, the following two actions are mainly performed: message checking and queue operation.
Configseq, Err: = processor. PROCESSNORMALMSG (MSG)//message check processor. Order (MSG, CONFIGSEQ)//into queue operations
The message checking method maps to the PROCESSNORMALMSG (Env *CB) of the Standardchannel structure in the Orderer.common.msgprocessor package. Envelope) (Configseq UInt64, err Error) method, implemented as follows.
UInt64 error) {
configseq = s.support.sequence ()//Get the serial number of the configuration, map to the corresponding method of ConfigManager structure in COMMON.CONFIGTX package
err = S.filters.apply (env)//filter Check to achieve the corresponding method of RuleSet structure in Orderer.common.msgprocessor package. Return
}
Where the filter is initialized when the Chainsupport structure is created:
Application Channel: The Createstandardchannelfilters in the Orderer.common.mspprocessor package (Filtersupport channelconfig. Resources) *ruleset methods, including Emptyrejectrule, Sizefilter, and Sigfilter (channelwriters roles).
System channel: Createsystemchannelfilters in the Orderer.common.mspprocessor package (Chaincreator chaincreator, ledgerresources Channelconfig. Resources) *ruleset methods, including Emptyrejectrule, Sizefilter, Sigfilter (channelwriters role), and Systemchannelfilter.
The queue operation maps to methods in the Orderer.consensus.solo package or Orderer.consensus.kafka package based on the different consensus configuration.
Taking Kafka case as an example, the corresponding method is mapped to the CHAINIMPL structure. This method will further encapsulate the message as Sarama. The Producermessage type message is sent to the Kafka backend by the Enqueue method.
UInt64 Error {
marshaledenv, err: = Utils. Marshal (env)
If Err!= nil {return
FMT. Errorf ("Cannot enqueue, unable to marshal envelope =%s", err)
}
if because (!chain.enqueue ( Marshaledenv, Configseq)) {return
FMT. Errorf ("Cannot enqueue")
} return
nil}
Processing Configuration Messages
For configuration messages, the process differs slightly from the normal message, including the merge configuration update message and the queue operation.
Config, configseq, err: = processor. PROCESSCONFIGUPDATEMSG (MSG)//merge configuration Update message processor. Configure (config, configseq)//into queue operations
The merge Configuration update message method maps to the PROCESSCONFIGUPDATEMSG (Env *CB) of the Standardchannel structure in the Orderer.common.msgprocessor package. Envelope) (Configseq UInt64, err Error) method, calculates the merged configuration and configuration number, as follows.
Func (S *standardchannel) processconfigupdatemsg (env *CB. Envelope) (config *