Hyperledger source code Analysis of Fabric
The Orderer node startup is implemented by the main () method under the Orderer package and further calls to the main () method in the Orderer/common/server package.
The core code is shown below.
Main is the entry point of Orderer Processfunc main () {
fullcmd: = Kingpin. Mustparse (app. Parse (OS. Args[1:])
//"version" command
if Fullcmd = = version. Fullcommand () {
fmt. Println (metadata. GetVersionInfo ())
return
}
conf: = config. Load ()
initializelogginglevel (conf)
Initializelocalmsp (conf)
Start (Fullcmd, conf)}
Includes the configuration initialization process and two parts of the core boot process:
Config. Load (): Reads configuration information from local configuration files and environment variables to build the configuration tree structure.
Initializelogginglevel (CONF): Configure log level.
Initializelocalmsp (CONF): Configure the MSP structure.
Start (): Completes the core work after startup. Overall Process
The core boot process is in the Orderer/common/server package in the Start () method, as shown in the following figure.
The start () method initializes the structure that the GRPC service requires, and then starts the service.
The core code is shown below.
string, conf *config. TopLevel) {
logger. DEBUGF ("Start ()")
signer: = Localmsp. Newsigner ()
Manager: = Initializemultichannelregistrar (conf, signer)
server: = NewServer (manager, signer, &conf. Debug)
switch cmd {case
start. Fullcommand ()://"Start" Command
Logger.infof ("Starting%s", metadata. GetVersionInfo ())
initializeprofilingservice (conf)
grpcserver: = Initializegrpcserver (conf)
AB. Registeratomicbroadcastserver (Grpcserver.server (), Server)
logger. Info ("Beginning to serve requests")
Grpcserver.start () case
benchmark. Fullcommand ()://"benchmark" command
logger. Info ("Starting orderer in benchmark mode")
benchmarkserver: = performance. Getbenchmarkserver ()
benchmarkserver.registerservice (server)
Benchmarkserver.start ()
}}
Includes two main parts:
Initialization of GRPC service structure;
GRPC Service started. GRPC Service Structure initialization
This includes creating a new MSP signature structure, initializing the registrar structure to manage each ledger structure, initiating a consensus process, and creating a GRPC service-side structure.
Core processes include:
Signer: = Localmsp. Newsigner ()//INIT signature structure Manager: = Initializemultichannelregistrar (conf, signer)//Initialize Ledger Manager (registrar) structure
Among them, the Initializemultichannelregistrar (conf, signer) method is most critical, the core code is as follows:
Func initializemultichannelregistrar (Conf *config. TopLevel, signer crypto. Localsigner) *multichannel. Registrar {
//create the factory structure of the ledger operation
LF, _: = Createledgerfactory (conf)
//If it is a new boot, create the system channel's ledger structure if
len (LF). Chainids ()) = = 0 {
logger. DEBUGF ("There is no chain, hence we must being in bootstrapping")
initializebootstrapchannel (conf, LF)
} else {
l Ogger. Info ("not bootstrapping because of existing chains")
}
//Initialize consensus plug-in
consenters: = Make (map[string ]consensus. Consenter)
consenters["Solo" = Solo. New ()
consenters["Kafka"] = Kafka. New (CONF. KAFKA.TLS, Conf. Kafka.retry, Conf. Kafka.version, Conf. Kafka.verbose)
//Create the Manager (registrar) structure of each ledger and start the consensus process return
multichannel. Newregistrar (LF, consenters, signer)}
Use the incoming configuration information and signature information to complete the following steps:
Create the factory structure of the ledger operation;
In the case of a new start-up, the corresponding structure of the system channel is initialized using the given system initial block file;
Completion of the consensus plug-in (including solo and Kafka two) initialization;
Multichannel. Newregistrar (LF, consenters, signer)
The method scans the local ledger data (at least the system channel already exists), creates the registrar structure, and initiates a consensus (such as a Kafka sort) process for each ledger.
Description: The registrar structure (located in Orderer.common.multichannel package) is the most core structure of the Orderer component, and manages the data structure of all the books and consensus Plug-ins in Orderer. Create a registrar structure and start a consensus process
The Newregistrar (LF, consenters, signer) method is located in the Orderer.common.multichannel package, which is responsible for initializing chain support, message handlers, and other important data structures, and initiating a consensus process for each ledger.
The core code is as follows:
existingchains: = Ledgerfactory.chainids () for _, Chainid: = Range Existingchains {If _, OK: = ledgerresources.co Nsortiumsconfig (); OK {//If the system ledger chain: = Newchainsupport (R, Ledgerresources, Consenters, signer) chain. Processor = Msgprocessor. Newsystemchannel (Chain, R.templator, Msgprocessor. Createsystemchannelfilters (R, Chain)) R.chains[chainid] = Chain R.systemchannelid = Chainid R.syst Emchannel = Chain defer chain.start ()//start consensus process else