示例#1
0
func TestLedgerReadWrite(t *testing.T) {
	localConf := localconfig.Load()
	localConf.General.OrdererType = provisional.ConsensusTypeSbft
	genesis := provisional.New(localConf).GenesisBlock()
	_, rl := ramledger.New(10, genesis)
	b := Backend{ledger: rl}

	header := []byte("header")
	e1 := &cb.Envelope{Payload: []byte("data1")}
	e2 := &cb.Envelope{Payload: []byte("data2")}
	ebytes1, _ := proto.Marshal(e1)
	ebytes2, _ := proto.Marshal(e2)
	data := [][]byte{ebytes1, ebytes2}
	sgns := make(map[uint64][]byte)
	sgns[uint64(1)] = []byte("sgn1")
	sgns[uint64(22)] = []byte("sgn22")
	batch := simplebft.Batch{Header: header, Payloads: data, Signatures: sgns}

	b.Deliver(&batch)
	batch2 := b.LastBatch()

	if !reflect.DeepEqual(batch, *batch2) {
		t.Errorf("The wrong batch was returned by LastBatch after Deliver: %v (original was: %v)", batch2, &batch)
	}
}
示例#2
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 {}
}