示例#1
0
func serve(c flags) {
	if c.dataDir == "" {
		fmt.Fprintln(os.Stderr, "need data directory")
		os.Exit(1)
	}

	persist := persist.New(c.dataDir)
	config, err := RestoreConfig(persist)
	if err != nil {
		panic(err)
	}

	conn, err := connection.New(c.listenAddr, c.certFile, c.keyFile)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Connection error.")
		panic(err)
	}
	s := &consensusStack{
		persist: nil,
	}

	localConf := localconfig.Load()
	localConf.General.OrdererType = provisional.ConsensusTypeSbft
	genesisBlock := provisional.New(localConf).GenesisBlock()

	_, ledger := fileledger.New(c.dataDir, genesisBlock)
	s.backend, err = backend.NewBackend(config.Peers, conn, ledger, persist)
	if err != nil {
		panic(err)
	}

	sbft, _ := pb.New(s.backend.GetMyId(), config.Consensus, s.backend)
	s.backend.SetReceiver(sbft)

	grpcServer := grpc.NewServer()
	lis, err := net.Listen("tcp", c.grpcAddr)
	if err != nil {
		panic(fmt.Sprintf("Failed to listen: %s", err))
	}
	broadcastab := backend.NewBackendAB(s.backend)
	ab.RegisterAtomicBroadcastServer(grpcServer, broadcastab)
	grpcServer.Serve(lis)

	// block forever
	select {}
}
示例#2
0
func initInstance(c flags) error {
	config, err := ReadJsonConfig(c.init)
	if err != nil {
		return err
	}

	err = os.Mkdir(c.dataDir, 0755)
	if err != nil {
		return err
	}

	p := persist.New(c.dataDir)
	err = SaveConfig(p, config)
	if err != nil {
		return err
	}

	fmt.Println("initialized new peer")
	return nil
}