Proficient in Filecoin: System Startup

When the user starts filecoin in daemon mode, the final command is located in the commands/daemon.go file, and its Run method directly calls the daemonRun function in the same file for processing. The processing of this function is as follows:

  1. Call getRepo to get the local repository repo.Repo 接口 object. This method internally calls the OpenFSRepo function of the repository to generate and initialize the repository object. The flow is as follows:
    • Generates a repository repo.FSRepo 对象 based on the repository path and version.
    • Lock the repo.lock file of the relevant repository and set the lockfile property of the repository object to ensure that only one full node can use the repository at the same time in order to maintain the integrity of the repository.
    • Call the loadFromDisk method of the repository object, load the various data of the repository from the disk, and continue to improve the repository object. The flow of this method is as follows:
      • Call the readVersion method of the repository object, read the version number of the repository from the repository file, and compare it with the version number of the repository object itself. If it does not match, an error is thrown.
      • Call the loadConfig method of the repository object and load the repository configuration file config.json .
      • Call the openDatastore method of the repository object to load the data storage file of the repository. By default, the data storage type is badgerds , so the loaded file directory is this directory under the repository. This method generates a repository storage object and saves the ds property of the repository object.
      • Call the openKeystore method of the repository object to open the keystore file of the repository. This method generates a private key storage object and saves it in the keystore property of the repository object.
      • Open the wallet data store by calling the openWalletDatastore method of the repository object. This method generates a repository storage object and saves the walletDs property of the repository object. The directory corresponding to this object is wallet .
      • Call the openChainDatastore method of the repository object to open the openChainDatastore data store. This method generates a repository storage object and saves the chainDs property of the repository object. The directory corresponding to this object is chain .
      • Call the openDealsDatastore method of the repository object to open the transaction data store.

      This method generates a repository storage object and saves the dealsDs property of the repository object. The directory corresponding to this object is deals .

  2. Get the environment variable.
  3. Get the command line arguments.
  4. Call the node's New method to create a node. First, generate a configuration object, use the option parameters and warehouse objects formed in the previous steps to set the configuration object; then call the Build method of the configuration object to build a Filecoin node. When we start without parameters, only the repository object in the configuration object will be set. Build function execution flow is as follows:
    • If there is no warehouse object in the configuration object, a memory version of the warehouse object is generated.
    • Generate a block storage object.
    • 'Invoke the buildHost method of the configuration object to generate the libp2p Host object'. buildHost method calls the New method of libp2p, which finally calls the NewNode method defined in config/config.go . This method:
      • First, create a swarm object by calling the NewSwarm 's NewSwarm method.
      • Then, call basic_host.go defined in NewHost to create the host object. In the process of creating the host object, save the swarm object as the network object of the host object, and set the connection handler and stream processor of the swarm to be the newStreamHandler method of the newConnHandler and newStreamHandler of the host object.
      • Then, call the AddTransport 's AddTransport method to add the specified transport association.
      • Finally, the host's Listen method is called and starts listening for the specified address.
    • Generate a pinger service object.
    • Generate a block validator consensus.DefaultBlockValidator object.
    • Generate a bitswap network object using the libp2p Host object and the route object.
    • Generates a bitswap object using the bitswap network object and the block storage object.
    • A block service object is generated using the block storage object and the bitswap object.
    • Generate a get block net.Fetcher object to get data from a remote node.
    • ''call the readGenesisCid function to get the CID' of the creation block.
    • Generate a chain.Store object.
    • Generate a chain.ChainStateProvider object.
    • Generate a powerTable object of type powerTable object.
    • Call different method node consensusors depending on whether the configuration object has a reward. If the reward object is empty, the Consensus consensus/processor.go NewDefaultProcessor function is called to generate the consensus; otherwise, the NewConfiguredProcessor function is called to generate the consensus. Both functions generate a consensus.DefaultProcessor object, and the blocks are in their properties.
    • Call the NewExpected function of consensus/NewExpected.go to generate the consensus.Protocol interface node consensus.Expected object. Depending on whether the configuration object has a proofs.Verifier interface object, different parameters are used during the build process.
    • Call the NewFloodSub function of the go-libp2p-pubsub class library to generate a publish/subscribe pubsub.PubSub object fsub that listens for all of its own messages.
    • Use the walletDs property of the repository repo.Repo interface object as the wallet backend to generate the wallet.Wallet object.
    • Call the chain/syncer.go NewSyncer function to generate the blockchain chain.Syncer synchronization object.
    • Generate a core.MessagePool object.
    • Generate a core.Inbox object.
    • Generate a core.MessageQueue object.
    • Generate a node.defaultMessagePublisher object. The subject of the message publishing object binding is /fil/msgs/devnet-3 , which posts a message on this topic. In the startup method of the whole node, the message notification of this topic is subscribed by calling the PubSubSubscribe method of the porcelain.API ​​object.
    • Generate a core.Outbox object. The message publishing object created by the object in the previous step, when the MessageSend method of the pornelain.API ​​object is called to send a message, internally calls the Send method of the object to send. The sending method finally calls the Publish message publishing object, and posts the signed message to the corresponding topic. After the message is sent, other full-node objects will be processed by calling the processMessage of the whole node because they subscribe to the message subject. See the full node startup process for details.
    • 'Generate a PorcelainAPI object.
    • 'Generate a Filecoin node.Node full node object.
    • 'Generate the net.Bootstrapper boot object.
    • Returns a Filecoin full node object.
  5. Call the internal function runAPIAndWait to start the full node.
    • ''Invoke the node's Start method, start the Filecon node''.
      • Call the Load method of the chain.Store object to load the local existing block.
      • Call the miningAddress method of the full-node object to obtain the miner address; if the miner address is configured, call the setupMining method of the node.Node full-node object to set the mining. This method calls the initSectorBuilderForNode function, initializes the initialized sector generator, and saves it on the sectorBuilder property of the full-node object.
      • Generates a syncCallBack synchronization callback function object for handling block synchronization.
      • Call the New function of the protocol/hello/hello.go file to perform Hello message processing. The internal processing of this method is as follows:
        • Generates a hello HelloSvc processor object and sets it as the HelloSvc property of the full node object.
        • Set the libp2p /fil/hello/1.0.0 protocol handler to the handleNewStream method of the protocol.hello.Handler object.
        • Set the connection notification for the libp2p network object to be the protocol.hello.helloNotify object.
      • Set the various definitions by calling the setupProtocols method of the Filecoin node. The internal processing of this method is as follows:
        • Generate a protocol.block.MiningAPI object and save it as the BlockMiningAPI property of the full-node object.
        • Call the NewAPI function of the protocol/retrieval/api.go file to generate the protocol.retrieval.API object and promote the RetrievalAPI property of the full-node object.
        • Call the NewAPI function of the protocol/storage/api.go file to generate the protocol.storage.API object and promote the StorageAPI property of the full-node object.
      • Using the node object as a parameter, call the NewMiner function of protocol/retrieval/NewMiner.go to generate a retrieve miner and set the RetrievalMiner property of the full node object. This function first generates a SetStreamHandler method that retrieves the miner, then calls the libp2p Host object of the full-node object, sets the handler for the /fil/retrieval/free/0.0.0 protocol to retrieve the miner's handleRetrievePieceForFree method, and finally returns to retrieve the miner.
      • Call the PubSubSubscribe method of the porcelain.API ​​object, subscribe to the block notification of the /fil/blocks/devnet-3 topic, and set it to the BlockSub property of the full node.
      • Call the PubSubSubscribe method of the porcelain.API ​​object, subscribe to the message notification of the /fil/msgs/devnet-3 topic, and set it to the MessageSub property of the full node.
      • Start a coroutine, call the handleSubscription method of the whole node in the handleSubscription , and process the block notification. The main body of this method is an infinite loop. It reads the topic from the topic specified by the parameter and calls the method specified by the parameter for processing. The block notification is subscribed to /fil/blocks/devnet-3 , and its processing method is the processBlock method of the whole node.
      • Start a coroutine, call the handleSubscription method of the whole node in the handleSubscription process the message notification. The message notification is subscribed to /fil/msgs/devnet-3 , and its processing method is the processMessage method of the whole node. This method deserializes the received message and then calls the Add method of the core.Inbox object to save the message object in the core.MessagePool object.
      • Call the HeadEvents method of the chain.Store object and subscribe to the new block header topic new-head using the Sub method of the publish/subscribe object it returns.
      • Call the ChainHead method of the porcelain.API ​​object, return the function that handles the block chain header, call the handleNewHeaviestTipSet method of the whole node in a handleNewHeaviestTipSet , and process the block chain header message.
      • In non-offline mode, start the net.Bootstrapper boot object. The boot object will connect to all boot nodes, and after the connection is successful, call the Connected method of the protocol.hello.helloNotify object to send its own top-level block information to the remote node, and the remote node will send us its top level. Block information, thus opening the block synchronization process.
      • Set up heartbeat services.
    • Generate a command line environment commands.Env object.
    • Generate server configuration variables
    • Generate the Go ServeMux processor object, set it to handle the /debug/pprof/ request object as Go's own DefaultServeMux ; handle the /api/ requested go-ipfs-cmds class library handler object. handler object holds the environment variables, server configuration variables, etc. generated above.
    • Create an Http server.
    • Start the Http server in a separate thread.