Пример #1
0
// GetBroadcastClient creates a simple instance of the BroadcastClient interface
func GetBroadcastClient() (BroadcastClient, error) {
	var orderer string
	if viper.GetBool("peer.committer.enabled") {
		orderer = viper.GetString("peer.committer.ledger.orderer")
	}

	if orderer == "" {
		return nil, fmt.Errorf("Can't get orderer address")
	}

	var opts []grpc.DialOption
	opts = append(opts, grpc.WithInsecure())
	opts = append(opts, grpc.WithTimeout(3*time.Second))
	opts = append(opts, grpc.WithBlock())

	conn, err := grpc.Dial(orderer, opts...)
	if err != nil {
		return nil, fmt.Errorf("Error connecting to %s due to %s", orderer, err)
	}
	client, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO())
	if err != nil {
		conn.Close()
		return nil, fmt.Errorf("Error connecting to %s due to %s", orderer, err)
	}

	return &broadcastClient{conn: conn, client: client}, nil
}
Пример #2
0
func main() {
	config := config.Load()

	var chainID string
	var serverAddr string
	var windowSize uint64

	flag.StringVar(&serverAddr, "server", fmt.Sprintf("%s:%d", config.General.ListenAddress, config.General.ListenPort), "The RPC server to connect to.")
	flag.StringVar(&chainID, "chainID", provisional.TestChainID, "The chain ID to deliver from.")
	flag.Uint64Var(&windowSize, "windowSize", 10, "The window size for the deliver.")
	flag.Parse()

	conn, err := grpc.Dial(serverAddr, grpc.WithInsecure())
	if err != nil {
		fmt.Println("Error connecting:", err)
		return
	}
	client, err := ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO())
	if err != nil {
		fmt.Println("Error connecting:", err)
		return
	}

	s := newDeliverClient(client, chainID, windowSize)
	s.seekOldest()
	s.readUntilClose()

}
Пример #3
0
func TestSbftPeer(t *testing.T) {
	tempDir, err := ioutil.TempDir("", "sbft_test")
	if err != nil {
		panic("Failed to create a temporary directory")
	}
	// We only need the path as the directory will be created
	// by the peer - TODO: modify sbft to tolerate an existing
	// directory
	os.RemoveAll(tempDir)
	defer func() {
		os.RemoveAll(tempDir)
	}()
	c := flags{init: "testdata/config.json",
		listenAddr: ":6101",
		grpcAddr:   ":7101",
		certFile:   "testdata/cert1.pem",
		keyFile:    "testdata/key.pem",
		dataDir:    tempDir}

	logger.Info("Initialization of instance.")
	err = initInstance(c)
	if err != nil {
		t.Errorf("Initialization failed: %s", err)
		return
	}
	logging.SetLevel(logging.DEBUG, "")

	logger.Info("Starting an instance in the background.")
	go serve(c)
	<-time.After(5 * time.Second)

	logger.Info("Creating an Atomic Broadcast GRPC connection.")
	timeout := 4 * time.Second
	clientconn, err := grpc.Dial(":7101", grpc.WithBlock(), grpc.WithTimeout(timeout), grpc.WithInsecure())
	if err != nil {
		t.Errorf("Failed to connect to GRPC: %s", err)
		return
	}
	client := ab.NewAtomicBroadcastClient(clientconn)

	resultch := make(chan byte)
	errorch := make(chan error)

	logger.Info("Starting a goroutine waiting for ledger updates.")
	go updateReceiver(t, resultch, errorch, client)

	logger.Info("Starting a single broadcast sender goroutine.")
	go broadcastSender(t, resultch, errorch, client)

	checkResults(t, resultch, errorch)
}
Пример #4
0
func main() {
	var loglevel string

	client := &clientImpl{doneChan: make(chan struct{})}

	backend := logging.NewLogBackend(os.Stderr, "", 0)
	logging.SetBackend(backend)
	formatter := logging.MustStringFormatter("[%{time:15:04:05}] %{shortfile:18s}: %{color}[%{level:-5s}]%{color:reset} %{message}")
	logging.SetFormatter(formatter)
	logger = logging.MustGetLogger(pkgName)

	flag.StringVar(&loglevel, "loglevel", "info",
		"The logging level. (Suggested values: info, debug)")
	flag.StringVar(&client.config.server, "server",
		"127.0.0.1:7050", "The RPC server to connect to.")
	flag.StringVar(&client.config.cmd.cmd, "cmd", "new-chain",
		"The action that this client is requesting via the config transaction.")
	flag.StringVar(&client.config.cmd.args.creationPolicy, "creationPolicy", "AcceptAllPolicy",
		"In case of a new-chain command, the chain createion policy this request should be validated against.")
	flag.StringVar(&client.config.cmd.args.chainID, "chainID", "NewChainID",
		"In case of a new-chain command, the chain ID to create.")
	flag.Parse()

	client.config.logLevel, _ = logging.LogLevel(strings.ToUpper(loglevel))
	logging.SetLevel(client.config.logLevel, logger.Module)

	conn, err := grpc.Dial(client.config.server, grpc.WithInsecure())
	if err != nil {
		logger.Fatalf("Client did not connect to %s: %v", client.config.server, err)
	}
	defer conn.Close()
	client.rpc = ab.NewAtomicBroadcastClient(conn)

	switch client.config.cmd.cmd {
	case "new-chain":
		envelope := newChainRequest(client.config.cmd.args.creationPolicy, client.config.cmd.args.chainID)
		logger.Infof("Requesting the creation of chain \"%s\"", client.config.cmd.args.chainID)
		client.broadcast(envelope)
	default:
		panic("Invalid cmd given")
	}
}
Пример #5
0
func main() {
	logger.Info("Creating an Atomic Broadcast GRPC connection.")
	timeout := 4 * time.Second
	clientconn, err := grpc.Dial(":7101", grpc.WithBlock(), grpc.WithTimeout(timeout), grpc.WithInsecure())
	if err != nil {
		logger.Errorf("Failed to connect to GRPC: %s", err)
		return
	}
	client := ab.NewAtomicBroadcastClient(clientconn)

	resultch := make(chan byte)
	errorch := make(chan error)

	logger.Info("Starting a goroutine waiting for ledger updates.")
	go updateReceiver(resultch, errorch, client)

	logger.Info("Starting a single broadcast sender goroutine.")
	go broadcastSender(resultch, errorch, client)

	checkResults(resultch, errorch)
}
Пример #6
0
func (d *DeliverService) initDeliver() error {
	opts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(3 * time.Second), grpc.WithBlock()}
	endpoint := viper.GetString("peer.committer.ledger.orderer")
	conn, err := grpc.Dial(endpoint, opts...)
	if err != nil {
		logger.Errorf("Cannot dial to %s, because of %s", endpoint, err)
		return err
	}
	var abc orderer.AtomicBroadcast_DeliverClient
	abc, err = orderer.NewAtomicBroadcastClient(conn).Deliver(context.TODO())
	if err != nil {
		logger.Errorf("Unable to initialize atomic broadcast, due to %s", err)
		return err
	}

	// Atomic Broadcast Deliver Client
	d.client = abc
	d.conn = conn
	return nil

}