forked from CognitionFoundry/gohfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
66 lines (61 loc) · 1.71 KB
/
peer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Copyright: Cognition Foundry. All Rights Reserved.
License: Apache License Version 2.0
*/
package gohfc
import (
"google.golang.org/grpc"
"github.com/hyperledger/fabric/protos/peer"
"context"
"fmt"
"google.golang.org/grpc/credentials"
"time"
)
// Peer expose API's to communicate with peer
type Peer struct {
Name string
Uri string
Opts []grpc.DialOption
caPath string
Conn *grpc.ClientConn
}
// PeerResponse is response from peer transaction request
type PeerResponse struct {
Response *peer.ProposalResponse
Err error
Name string
}
// Endorse sends single transaction to single peer.
func (p *Peer) Endorse(resp chan *PeerResponse, prop *peer.SignedProposal) {
if p.Conn==nil{
conn, err := grpc.Dial(p.Uri, p.Opts...)
if err != nil {
resp <- &PeerResponse{Response: nil, Err: err,Name:p.Name}
return
}
p.Conn=conn
}
proposalResp, err := peer.NewEndorserClient(p.Conn).ProcessProposal(context.Background(), prop)
if err != nil {
resp <- &PeerResponse{Response: nil, Name: p.Name, Err: err}
return
}
resp <- &PeerResponse{Response: proposalResp, Name: p.Name, Err: nil}
return
}
// NewPeerFromConfig creates new peer from provided config
func NewPeerFromConfig(conf PeerConfig) (*Peer,error) {
p := Peer{Uri: conf.Host,caPath:conf.TlsPath}
if conf.Insecure {
p.Opts = []grpc.DialOption{grpc.WithInsecure()}
} else if p.caPath != "" {
creds, err := credentials.NewClientTLSFromFile(p.caPath, "")
if err != nil {
return nil, fmt.Errorf("cannot read peer %s credentials err is: %v", p.Name, err)
}
p.Opts = append(p.Opts, grpc.WithTransportCredentials(creds))
}
p.Opts = append(p.Opts, grpc.WithBlock())
p.Opts = append(p.Opts, grpc.WithTimeout(3*time.Second))
return &p,nil
}