peer, entry point
Driven by Cobra: A mainCmd is created to start Cobra execution.
func main() {
// For environment variables.
viper.SetEnvPrefix(common.CmdRoot)
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
// Define command-line flags that are valid for all peer commands and
// subcommands.
mainFlags := mainCmd.PersistentFlags()
mainFlags.String("logging-level", "", "Legacy logging level flag")
viper.BindPFlag("logging_level", mainFlags.Lookup("logging-level"))
mainFlags.MarkHidden("logging-level")
cryptoProvider := factory.GetDefault()
mainCmd.AddCommand(version.Cmd())
mainCmd.AddCommand(node.Cmd())
mainCmd.AddCommand(chaincode.Cmd(nil, cryptoProvider))
mainCmd.AddCommand(channel.Cmd(nil))
mainCmd.AddCommand(lifecycle.Cmd(cryptoProvider))
// On failure Cobra prints the usage message and error string, so we only
// need to exit with a non-0 status
if mainCmd.Execute() != nil {
os.Exit(1)
}
}
node cmd
- node has a PersistentPreRun defined, so common.InitCmd has to be executed before node cmd execution. Note that os.Exit(1) if InitConfig(CmdRoot) failed - no configuration files specified.
- Log, legacy CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable
– loggingSpec := os.Getenv(“FABRIC_LOGGING_SPEC”)
– loggingFormat := os.Getenv(“FABRIC_LOGGING_FORMAT”)
var nodeCmd = &cobra.Command{
Use: nodeFuncName,
Short: fmt.Sprint(nodeCmdDes),
Long: fmt.Sprint(nodeCmdDes),
PersistentPreRun: common.InitCmd,
}
func InitCmd(cmd *cobra.Command, args []string) {
err := InitConfig(CmdRoot)
if err != nil { // Handle errors reading the config file
mainLogger.Errorf("Fatal error when initializing %s config : %s", CmdRoot, err)
os.Exit(1)
}
// read in the legacy logging level settings and, if set,
// notify users of the FABRIC_LOGGING_SPEC env variable
var loggingLevel string
if viper.GetString("logging_level") != "" {
loggingLevel = viper.GetString("logging_level")
} else {
loggingLevel = viper.GetString("logging.level")
}
if loggingLevel != "" {
mainLogger.Warning("CORE_LOGGING_LEVEL is no longer supported, please use the FABRIC_LOGGING_SPEC environment variable")
}
loggingSpec := os.Getenv("FABRIC_LOGGING_SPEC")
loggingFormat := os.Getenv("FABRIC_LOGGING_FORMAT")
flogging.Init(flogging.Config{
Format: loggingFormat,
Writer: logOutput,
LogSpec: loggingSpec,
})
// chaincode packaging does not require material from the local MSP
if cmd.CommandPath() == "peer lifecycle chaincode package" {
mainLogger.Debug("peer lifecycle chaincode package does not need to init crypto")
return
}
// Init the MSP
var mspMgrConfigDir = config.GetPath("peer.mspConfigPath")
var mspID = viper.GetString("peer.localMspId")
var mspType = viper.GetString("peer.localMspType")
if mspType == "" {
mspType = msp.ProviderTypeToString(msp.FABRIC)
}
err = InitCrypto(mspMgrConfigDir, mspID, mspType)
if err != nil { // Handle errors reading the config file
mainLogger.Errorf("Cannot run peer because %s", err.Error())
os.Exit(1)
}
}
Node Start
var nodeStartCmd = &cobra.Command{
Use: "start",
Short: "Starts the node.",
Long: `Starts a node that interacts with the network.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("trailing args detected")
}
// Parsing of the command line is done so silence cmd usage
cmd.SilenceUsage = true
return serve(args)
},
}
func serve(args []string) error {
...
// start operation at a http server
opsSystem := newOperationsSystem(coreConfig)
err = opsSystem.Start()
if err != nil {
return errors.WithMessage(err, "failed to initialize operations subsystems")
}
defer opsSystem.Stop()
// Set log observer
metricsProvider := opsSystem.Provider
logObserver := floggingmetrics.NewObserver(metricsProvider)
flogging.SetObserver(logObserver)
...
go func() {
var grpcErr error
if grpcErr = peerServer.Start(); grpcErr != nil {
grpcErr = fmt.Errorf("grpc server exited with error: %s", grpcErr)
}
serve <- grpcErr
}()
// Block until grpc server exits
return <-serve
}
Node initialization
The relevent code is quite some…
Peer will register blow GRPC services:
- RegisterGossipServer, name=gossip.Gossip, method=Ping, Server=peerServer
- RegisterChainCodeSupportServer, name=protos.ChaincodeSupport, method=, Server=ChainCodeServer
- RegisterHealthServer, name=grpc.health.v1.Health, method=Check, Server=ChainCodeServer
- RegisterDeliverServer, name=protos.Deliver, method=, Server=peerServer
- RegisterDiscoveryServer, name=discovery.Discovery, method=Discover, Server=peerServer
- RegisterEndorserServer, name=protos.Endorser, method=ProcessProposal, Server=peerServer
Node Start Log
peer.exe -- node start #gosetup
API server listening at: 127.0.0.1:5056
2020-03-03 19:55:16.563 CST [nodeCmd] serve -> INFO 001 Starting peer:
Version: latest
Commit SHA: development build
Go version: go1.13.5
OS/Arch: windows/amd64
Chaincode:
Base Docker Label: org.hyperledger.fabric
Docker Namespace: hyperledger
2020-03-03 19:55:21.877 CST [peer] getLocalAddress -> INFO 002 Auto-detected peer address: 169.254.149.240:7051
2020-03-03 19:55:21.877 CST [peer] getLocalAddress -> INFO 003 Host is 0.0.0.0 , falling back to auto-detected address: 169.254.149.240:7051
2020-03-03 20:02:50.635 CST [nodeCmd] initGrpcSemaphores -> INFO 004 concurrency limit for endorser service is 2500
2020-03-03 20:02:50.635 CST [nodeCmd] initGrpcSemaphores -> INFO 005 concurrency limit for deliver service is 2500
2020-03-03 20:03:38.716 CST [ledgermgmt] NewLedgerMgr -> INFO 006 Initializing LedgerMgr
2020-03-03 20:03:39.327 CST [leveldbhelper] openDBAndCheckFormat -> INFO 007 DB is empty Setting db format as 2.0
2020-03-03 20:03:39.400 CST [fsblkstorage] NewProvider -> INFO 008 Creating new file ledger directory at D:\work\src\github.com\hyperledger\fabric\sampleconfig\var\hyperledger\production\ledgersData\chains\chains
2020-03-03 20:03:39.795 CST [leveldbhelper] openDBAndCheckFormat -> INFO 009 DB is empty Setting db format as 2.0
2020-03-03 20:03:40.477 CST [leveldbhelper] openDBAndCheckFormat -> INFO 00a DB is empty Setting db format as 2.0
2020-03-03 20:03:40.552 CST [ledgermgmt] NewLedgerMgr -> INFO 00b Initialized LedgerMgr
2020-03-03 20:03:40.565 CST [gossip.service] New -> INFO 00c Initialize gossip with endpoint 169.254.149.240:7051
2020-03-03 20:03:40.567 CST [gossip.gossip] New -> INFO 00d Creating gossip service with self membership of Endpoint: , InternalEndpoint: 169.254.149.240:7051, PKI-ID: 0d46737a45894d123895671221dbaddf8480fb0364f404be3aed491df442945f, Metadata:
2020-03-03 20:03:40.568 CST [gossip.gossip] New -> WARN 00e External endpoint is empty, peer will not be accessible outside of its organization
2020-03-03 20:03:40.568 CST [gossip.gossip] start -> INFO 00f Gossip instance 169.254.149.240:7051 started
2020-03-03 20:03:40.569 CST [lifecycle] InitializeLocalChaincodes -> INFO 010 Initialized lifecycle cache with 0 already installed chaincodes
2020-03-03 20:03:40.570 CST [nodeCmd] computeChaincodeEndpoint -> INFO 011 Entering computeChaincodeEndpoint with peerHostname: 169.254.149.240
2020-03-03 20:03:40.570 CST [nodeCmd] computeChaincodeEndpoint -> INFO 012 Exit with ccEndpoint: 169.254.149.240:7052
2020-03-03 20:03:40.570 CST [nodeCmd] createChaincodeServer -> WARN 013 peer.chaincodeListenAddress is not set, using 169.254.149.240:7052
2020-03-03 20:03:40.578 CST [sccapi] DeploySysCC -> INFO 014 deploying system chaincode 'lscc'
2020-03-03 20:03:40.579 CST [sccapi] DeploySysCC -> INFO 015 deploying system chaincode 'cscc'
2020-03-03 20:03:40.579 CST [sccapi] DeploySysCC -> INFO 016 deploying system chaincode 'qscc'
2020-03-03 20:03:40.579 CST [sccapi] DeploySysCC -> INFO 017 deploying system chaincode '_lifecycle'
2020-03-03 20:03:40.579 CST [nodeCmd] serve -> INFO 018 Deployed system chaincodes
2020-03-03 20:03:40.579 CST [discovery] NewService -> INFO 019 Created with config TLS: false, authCacheMaxSize: 1000, authCachePurgeRatio: 0.750000
2020-03-03 20:03:40.579 CST [nodeCmd] registerDiscoveryService -> INFO 01a Discovery service activated
2020-03-03 20:03:40.579 CST [nodeCmd] serve -> INFO 01b Starting peer with ID=[jdoe], network ID=[dev], address=[169.254.149.240:7051]
2020-03-03 20:03:40.579 CST [nodeCmd] serve -> INFO 01c Started peer with ID=[jdoe], network ID=[dev], address=[169.254.149.240:7051]
2020-03-03 20:03:40.580 CST [kvledger] LoadPreResetHeight -> INFO 01d Loading prereset height from path [D:\work\src\github.com\hyperledger\fabric\sampleconfig\var\hyperledger\production\ledgersData\chains]
2020-03-03 20:03:40.581 CST [fsblkstorage] preResetHtFiles -> INFO 01e No active channels passed
node operation http server
OperationSystem:
operations:
# host and port for the operations server
listenAddress: 127.0.0.1:9443
- system.initializeHealthCheckHandler() : “/healthz”
- system.initializeLoggingHandler() : “/logspec”
- system.initializeMetricsProvider() : “/metrics”
- system.initializeVersionInfoHandler() : “/version”