func NewBeaconApp(options config.Options) (*BeaconApp, error) {
	initialPeers, err := p2p.ParseInitialConnections(options.InitialConnections)
	if err != nil {
		logger.Fatal(err)
	}

	f, err := os.Open(options.ChainCFG)
	if err != nil {
		logger.Fatal(err)
	}

	beaconConfig, err := ReadChainFileToConfig(f)
	if err != nil {
		logger.Fatal(err)
	}

	err = f.Close()
	if err != nil {
		panic(err)
	}

	beaconConfig.ListeningAddress = options.P2PListen
	beaconConfig.RPCAddress = options.RPCListen
	beaconConfig.DiscoveryOptions.BootstrapAddresses = append(beaconConfig.DiscoveryOptions.BootstrapAddresses, initialPeers...)
	beaconConfig.DataDirectory = options.DataDir

	beaconConfig.Resync = options.Resync
	if beaconConfig.GenesisTime == 0 {
		if options.GenesisTime == "" {
			beaconConfig.GenesisTime = uint64(utils.Now().Unix())
		} else {
			genesisTimeString := options.GenesisTime
			if strings.HasPrefix(genesisTimeString, "+") {
				offsetString := genesisTimeString[1:]

				offset, err := strconv.Atoi(offsetString)
				if err != nil {
					panic(fmt.Errorf("invalid offset (should be number): %s", offsetString))
				}

				beaconConfig.GenesisTime = uint64(utils.Now().Add(time.Duration(offset) * time.Second).Unix())
			} else if strings.HasPrefix(genesisTimeString, "-") {
				offsetString := genesisTimeString[1:]

				offset, err := strconv.Atoi(offsetString)
				if err != nil {
					panic(fmt.Errorf("invalid offset (should be number): %s", offsetString))
				}

				beaconConfig.GenesisTime = uint64(utils.Now().Add(time.Duration(-offset) * time.Second).Unix())
			} else {
				offset, err := strconv.Atoi(genesisTimeString)
				if err != nil {
					panic(fmt.Errorf("invalid genesis time (should be number): %s", genesisTimeString))
				}

				beaconConfig.GenesisTime = uint64(offset)
			}
		}
	}

	app := &BeaconApp{
		config:   *beaconConfig,
		exitChan: make(chan struct{}),
		exited:   new(sync.Mutex),
	}

	err = app.loadConfig()
	if err != nil {
		return nil, err
	}
	err = app.loadDatabase()
	if err != nil {
		return nil, err
	}

	signalHandler := make(chan os.Signal, 1)
	signal.Notify(signalHandler, os.Interrupt, syscall.SIGTERM)

	if !app.config.IsIntegrationTest {
		go app.listenForInterrupt(signalHandler)
	}

	err = app.loadBlockchain()
	if err != nil {
		return nil, err
	}

	err = app.loadP2P()
	if err != nil {
		return nil, err
	}
	err = app.createRPCServer()
	if err != nil {
		return nil, err
	}
	sm, err := beacon.NewSyncManager(app.hostNode, app.blockchain, app.mempool)
	if err != nil {
		return nil, err
	}

	app.syncManager = sm

	// locked while running
	app.exited.Lock()
	return app, nil
}
// NewBeaconApp creates a new instance of BeaconApp
NewBeaconApp creates a new instance of BeaconApp
// NewBeaconApp creates a new instance of BeaconApp
func NewBeaconApp(options config.Options) (*BeaconApp, error) {
	initialPeers, err := p2p.ParseInitialConnections(options.InitialConnections)
	if err != nil {
		logger.Fatal(err)
	}
	f, err := os.Open(options.ChainCFG)
	if err != nil {
		logger.Fatal(err)
	}
	beaconConfig, err := ReadChainFileToConfig(f)
	if err != nil {
		logger.Fatal(err)
	}
	err = f.Close()
	if err != nil {
		panic(err)
	}
	beaconConfig.ListeningAddress = options.P2PListen
	beaconConfig.RPCAddress = options.RPCListen
	beaconConfig.DiscoveryOptions.BootstrapAddresses = append(beaconConfig.DiscoveryOptions.BootstrapAddresses, initialPeers...)
	beaconConfig.DataDirectory = options.DataDir
	beaconConfig.Resync = options.Resync
	if beaconConfig.GenesisTime == 0 {
		if options.GenesisTime == "" {
			beaconConfig.GenesisTime = uint64(utils.Now().Unix())
		} else {
			genesisTimeString := options.GenesisTime
			if strings.HasPrefix(genesisTimeString, "+") {
				offsetString := genesisTimeString[1:]
				offset, err := strconv.Atoi(offsetString)
				if err != nil {
					panic(fmt.Errorf("invalid offset (should be number): %s", offsetString))
				}
				beaconConfig.GenesisTime = uint64(utils.Now().Add(time.Duration(offset) * time.Second).Unix())
			} else if strings.HasPrefix(genesisTimeString, "-") {
				offsetString := genesisTimeString[1:]
				offset, err := strconv.Atoi(offsetString)
				if err != nil {
					panic(fmt.Errorf("invalid offset (should be number): %s", offsetString))
				}
				beaconConfig.GenesisTime = uint64(utils.Now().Add(time.Duration(-offset) * time.Second).Unix())
			} else {
				offset, err := strconv.Atoi(genesisTimeString)
				if err != nil {
					panic(fmt.Errorf("invalid genesis time (should be number): %s", genesisTimeString))
				}
				beaconConfig.GenesisTime = uint64(offset)
			}
		}
	}
	app := &BeaconApp{
		config:   *beaconConfig,
		exitChan: make(chan struct{}),
		exited:   new(sync.Mutex),
	}
	err = app.loadConfig()
	if err != nil {
		return nil, err
	}
	err = app.loadDatabase()
	if err != nil {
		return nil, err
	}
	signalHandler := make(chan os.Signal, 1)
	signal.Notify(signalHandler, os.Interrupt, syscall.SIGTERM)
	if !app.config.IsIntegrationTest {
		go app.listenForInterrupt(signalHandler)
	}
	err = app.loadBlockchain()
	if err != nil {
		return nil, err
	}
	err = app.loadP2P()
	if err != nil {
		return nil, err
	}
	err = app.createRPCServer()
	if err != nil {
		return nil, err
	}
	sm, err := beacon.NewSyncManager(app.hostNode, app.blockchain, app.mempool)
	if err != nil {
		return nil, err
	}
	app.syncManager = sm
	app.exited.Lock()
	return app, nil
}
func NewBeaconApp ( options config . Options ) ( * BeaconApp , error ) { initialPeers , err := p2p . ParseInitialConnections ( options . InitialConnections ) if err != nil { logger . Fatal ( err ) } f , err := os . Open ( options . ChainCFG ) if err != nil { logger . Fatal ( err ) } beaconConfig , err := ReadChainFileToConfig ( f ) if err != nil { logger . Fatal ( err ) } err = f . Close ( ) if err != nil { panic ( err ) } beaconConfig . ListeningAddress = options . P2PListen beaconConfig . RPCAddress = options . RPCListen beaconConfig . DiscoveryOptions . BootstrapAddresses = append ( beaconConfig . DiscoveryOptions . BootstrapAddresses , initialPeers ... ) beaconConfig . DataDirectory = options . DataDir beaconConfig . Resync = options . Resync if beaconConfig . GenesisTime == 0 { if options . GenesisTime == " " { beaconConfig . GenesisTime = uint64 ( utils . Now ( ) . Unix ( ) ) } else { genesisTimeString := options . GenesisTime if strings . HasPrefix ( genesisTimeString , " " ) { offsetString := genesisTimeString [ 1 : ] offset , err := strconv . Atoi ( offsetString ) if err != nil { panic ( fmt . Errorf ( " " , offsetString ) ) } beaconConfig . GenesisTime = uint64 ( utils . Now ( ) . Add ( time . Duration ( offset ) * time . Second ) . Unix ( ) ) } else if strings . HasPrefix ( genesisTimeString , " " ) { offsetString := genesisTimeString [ 1 : ] offset , err := strconv . Atoi ( offsetString ) if err != nil { panic ( fmt . Errorf ( " " , offsetString ) ) } beaconConfig . GenesisTime = uint64 ( utils . Now ( ) . Add ( time . Duration ( - offset ) * time . Second ) . Unix ( ) ) } else { offset , err := strconv . Atoi ( genesisTimeString ) if err != nil { panic ( fmt . Errorf ( " " , genesisTimeString ) ) } beaconConfig . GenesisTime = uint64 ( offset ) } } } app := & BeaconApp { config : * beaconConfig , exitChan : make ( chan struct { } ) , exited : new ( sync . Mutex ) , } err = app . loadConfig ( ) if err != nil { return nil , err } err = app . loadDatabase ( ) if err != nil { return nil , err } signalHandler := make ( chan os . Signal , 1 ) signal . Notify ( signalHandler , os . Interrupt , syscall . SIGTERM ) if ! app . config . IsIntegrationTest { go app . listenForInterrupt ( signalHandler ) } err = app . loadBlockchain ( ) if err != nil { return nil , err } err = app . loadP2P ( ) if err != nil { return nil , err } err = app . createRPCServer ( ) if err != nil { return nil , err } sm , err := beacon . NewSyncManager ( app . hostNode , app . blockchain , app . mempool ) if err != nil { return nil , err } app . syncManager = sm app . exited . Lock ( ) return app , nil }
NewBeaconApp creates a new instance of BeaconApp
NewBeaconApp creates a new instance of BeaconApp
// locked while running