github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/neatio/neatio.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/signal"
     7  	"strings"
     8  	"syscall"
     9  
    10  	"github.com/neatlab/neatio/chain/consensus/neatcon/consensus"
    11  	"github.com/neatlab/neatio/chain/log"
    12  	"github.com/neatlab/neatio/internal/debug"
    13  	"github.com/neatlab/neatio/utilities/utils"
    14  	"gopkg.in/urfave/cli.v1"
    15  )
    16  
    17  func neatchainCmd(ctx *cli.Context) error {
    18  
    19  	if ctx == nil {
    20  		log.Error("oh, ctx is null, how neatio works?")
    21  		return nil
    22  	}
    23  
    24  	log.Info("NEAT | Blazing FAST, ultra SECURE and ECO friendly payment solution.")
    25  
    26  	chainMgr := GetCMInstance(ctx)
    27  
    28  	requestSideChain := strings.Split(ctx.GlobalString(utils.SideChainFlag.Name), ",")
    29  
    30  	chainMgr.InitP2P()
    31  
    32  	err := chainMgr.LoadMainChain()
    33  	if err != nil {
    34  		log.Errorf("Load Main Chain failed. %v", err)
    35  		return nil
    36  	}
    37  
    38  	chainMgr.InitCrossChainHelper()
    39  
    40  	err = chainMgr.StartP2PServer()
    41  	if err != nil {
    42  		log.Errorf("Start P2P Server failed. %v", err)
    43  		return err
    44  	}
    45  	consensus.NodeID = chainMgr.GetNodeID()[0:16]
    46  
    47  	err = chainMgr.StartMainChain()
    48  
    49  	err = chainMgr.LoadChains(requestSideChain)
    50  	if err != nil {
    51  		log.Errorf("Load Side Chains failed. %v", err)
    52  		return err
    53  	}
    54  
    55  	err = chainMgr.StartChains()
    56  	if err != nil {
    57  		log.Error("start chains failed")
    58  		return err
    59  	}
    60  
    61  	err = chainMgr.StartRPC()
    62  	if err != nil {
    63  		log.Error("start NEAT RPC failed")
    64  		return err
    65  	}
    66  
    67  	chainMgr.StartInspectEvent()
    68  
    69  	go func() {
    70  		sigc := make(chan os.Signal, 1)
    71  		signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
    72  		defer signal.Stop(sigc)
    73  		<-sigc
    74  		log.Info("Got interrupt, shutting down...")
    75  
    76  		chainMgr.StopChain()
    77  		chainMgr.WaitChainsStop()
    78  		chainMgr.Stop()
    79  
    80  		for i := 10; i > 0; i-- {
    81  			<-sigc
    82  			if i > 1 {
    83  				log.Info(fmt.Sprintf("Already shutting down, interrupt %d more times for panic.", i-1))
    84  			}
    85  		}
    86  		debug.Exit()
    87  		debug.LoudPanic("boom")
    88  	}()
    89  
    90  	chainMgr.Wait()
    91  
    92  	return nil
    93  }