Initiate the Nodes

Each function module needs to be initialized in turn when the nodes are started. Ordinary nodes and DPoS nodes can be configured through configuration files. The initialization sequence of main function modules at startup is as follows:

  1. Initialize the trading pits and block pits.

  txMemPool := mempool.NewTxPool(st.Params())
  blockMemPool := mempool.NewBlockPool(st.Params())

2. Initialize the Committee, which is mainly used to maintain the related affairs of CR members.

  committee := crstate.NewCommittee(st.Params())

3. Initialize Arbitrators, which is mainly used to handle DPoS consensus-related affairs.

  arbiters, err := state.NewArbitrators(st.Params(), committee,
		func(programHash common.Uint168) (common.Fixed64,
			error) {
			amount := common.Fixed64(0)
			utxos, err := blockchain.DefaultLedger.Store.
				GetFFLDB().GetUTXO(&programHash)
			if err != nil {
				return amount, err
			}
			for _, utxo := range utxos {
				amount += utxo.Value
			}
			return amount, nil
		},
		committee.TryUpdateCRMemberInactivity,
		committee.TryRevertCRMemberInactivity,
		committee.TryUpdateCRMemberIllegal,
		committee.TryRevertCRMemberIllegal)
	if err != nil {
		printErrorAndExit(err)
	}

4. Initialize blockchain, which is mainly used to handle block-related affairs.

	chain, err := blockchain.New(chainStore, st.Params(), arbiters.State, committee)
	if err != nil {
		printErrorAndExit(err)
	}
	if err := chain.Init(interrupt.C); err != nil {
		printErrorAndExit(err)
	}
	if err := chain.MigrateOldDB(interrupt.C, pgBar.Start,
		pgBar.Increase, dataDir, st.Params()); err != nil {
		printErrorAndExit(err)
	}
	pgBar.Stop()

5. Initialize P2P network services.

	netServer, err := elanet.NewServer(dataDir, &elanet.Config{
		Chain:          chain,
		ChainParams:    st.Params(),
		PermanentPeers: st.Params().PermanentPeers,
		TxMemPool:      txMemPool,
		BlockMemPool:   blockMemPool,
		Routes:         route,
	}, nodePrefix+Version)
	if err != nil {
		printErrorAndExit(err)
	}

6. Initialize the PoW module, which is used to process the related affairs of PoW block.

	servers.Pow = pow.NewService(&pow.Config{
		PayToAddr:   st.Config().PowConfiguration.PayToAddr,
		MinerInfo:   st.Config().PowConfiguration.MinerInfo,
		Chain:       chain,
		ChainParams: st.Params(),
		TxMemPool:   txMemPool,
		BlkMemPool:  blockMemPool,
		BroadcastBlock: func(block *types.Block) {
			hash := block.Hash()
			netServer.RelayInventory(msg.NewInvVect(msg.InvTypeBlock, &hash), block)
		},
		Arbitrators: arbiters,
	})

7. Initialize CheckPoint, which is used for quickly initiating the nodes.

	// initialize producer state after arbiters has initialized.
	if err = chain.InitCheckpoint(interrupt.C, pgBar.Start,
		pgBar.Increase); err != nil {
		printErrorAndExit(err)
	}
	pgBar.Stop()

8. Open services such as RPC, Resutful, and Websocket.

	log.Info("Start services")
	if st.Config().EnableRPC {
		go httpjsonrpc.StartRPCServer()
	}
	if st.Config().HttpRestStart {
		go httprestful.StartServer()
	}
	if st.Config().HttpWsStart {
		go httpwebsocket.Start()
	}
	if st.Config().HttpInfoStart {
		go httpnodeinfo.StartServer()
	}

After each functional module is initiated, the ELA nodes can start to establish P2P connection with other network nodes and access to the ELA network.

Last updated