-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpeer.go
79 lines (72 loc) · 2.08 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
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
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"
"google.golang.org/grpc/keepalive"
)
// Peer expose API's to communicate with peer
type Peer struct {
Name string
Uri string
MspId string
Opts []grpc.DialOption
caPath string
conn *grpc.ClientConn
client peer.EndorserClient
}
// 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
p.client = peer.NewEndorserClient(p.conn)
}
proposalResp, err := p.client.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}
}
// NewPeerFromConfig creates new peer from provided config
func NewPeerFromConfig(conf PeerConfig) (*Peer, error) {
p := Peer{Uri: conf.Host, caPath: conf.TlsPath}
if !conf.UseTLS {
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.WithKeepaliveParams(keepalive.ClientParameters{
Time: time.Duration(1) * time.Minute,
Timeout: time.Duration(20) * time.Second,
PermitWithoutStream: true,
}),
grpc.WithBlock(),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(maxRecvMsgSize),
grpc.MaxCallSendMsgSize(maxSendMsgSize)))
return &p, nil
}