/
client_connections.go
203 lines (172 loc) · 6.03 KB
/
client_connections.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"crypto/tls"
pb "github.com/Yunpeng-J/fabric-protos-go/peer"
"github.com/Yunpeng-J/HLF-2.2/bccsp"
"github.com/Yunpeng-J/HLF-2.2/internal/peer/common"
"github.com/Yunpeng-J/HLF-2.2/internal/pkg/identity"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
// ClientConnections holds the clients for connecting to the various
// endpoints in a Fabric network.
type ClientConnections struct {
BroadcastClient common.BroadcastClient
DeliverClients []pb.DeliverClient
EndorserClients []pb.EndorserClient
Certificate tls.Certificate
Signer identity.SignerSerializer
CryptoProvider bccsp.BCCSP
}
// ClientConnectionsInput holds the input parameters for creating
// client connections.
type ClientConnectionsInput struct {
CommandName string
EndorserRequired bool
OrdererRequired bool
OrderingEndpoint string
ChannelID string
PeerAddresses []string
TLSRootCertFiles []string
ConnectionProfilePath string
TLSEnabled bool
}
// NewClientConnections creates a new set of client connections based on the
// input parameters.
func NewClientConnections(input *ClientConnectionsInput, cryptoProvider bccsp.BCCSP) (*ClientConnections, error) {
signer, err := common.GetDefaultSigner()
if err != nil {
return nil, errors.WithMessage(err, "failed to retrieve default signer")
}
c := &ClientConnections{
Signer: signer,
CryptoProvider: cryptoProvider,
}
if input.EndorserRequired {
err := c.setPeerClients(input)
if err != nil {
return nil, err
}
}
if input.OrdererRequired {
err := c.setOrdererClient()
if err != nil {
return nil, err
}
}
return c, nil
}
func (c *ClientConnections) setPeerClients(input *ClientConnectionsInput) error {
var endorserClients []pb.EndorserClient
var deliverClients []pb.DeliverClient
if err := c.validatePeerConnectionParameters(input); err != nil {
return errors.WithMessage(err, "failed to validate peer connection parameters")
}
for i, address := range input.PeerAddresses {
var tlsRootCertFile string
if input.TLSRootCertFiles != nil {
tlsRootCertFile = input.TLSRootCertFiles[i]
}
endorserClient, err := common.GetEndorserClient(address, tlsRootCertFile)
if err != nil {
return errors.WithMessagef(err, "failed to retrieve endorser client for %s", input.CommandName)
}
endorserClients = append(endorserClients, endorserClient)
deliverClient, err := common.GetPeerDeliverClient(address, tlsRootCertFile)
if err != nil {
return errors.WithMessagef(err, "failed to retrieve deliver client for %s", input.CommandName)
}
deliverClients = append(deliverClients, deliverClient)
}
if len(endorserClients) == 0 {
// this should only be empty due to a programming bug
return errors.New("no endorser clients retrieved")
}
err := c.setCertificate()
if err != nil {
return err
}
c.EndorserClients = endorserClients
c.DeliverClients = deliverClients
return nil
}
func (c *ClientConnections) validatePeerConnectionParameters(input *ClientConnectionsInput) error {
if input.ConnectionProfilePath != "" {
networkConfig, err := common.GetConfig(input.ConnectionProfilePath)
if err != nil {
return err
}
if len(networkConfig.Channels[input.ChannelID].Peers) != 0 {
input.PeerAddresses = []string{}
input.TLSRootCertFiles = []string{}
for peer, peerChannelConfig := range networkConfig.Channels[input.ChannelID].Peers {
if peerChannelConfig.EndorsingPeer {
peerConfig, ok := networkConfig.Peers[peer]
if !ok {
return errors.Errorf("peer '%s' is defined in the channel config but doesn't have associated peer config", peer)
}
input.PeerAddresses = append(input.PeerAddresses, peerConfig.URL)
input.TLSRootCertFiles = append(input.TLSRootCertFiles, peerConfig.TLSCACerts.Path)
}
}
}
}
// currently only support multiple peer addresses for _lifecycle
// for approveformyorg and commit
multiplePeersAllowed := map[string]bool{
"approveformyorg": true,
"commit": true,
}
if !multiplePeersAllowed[input.CommandName] && len(input.PeerAddresses) > 1 {
return errors.Errorf("'%s' command supports one peer. %d peers provided", input.CommandName, len(input.PeerAddresses))
}
if !input.TLSEnabled {
input.TLSRootCertFiles = nil
return nil
}
if len(input.TLSRootCertFiles) != len(input.PeerAddresses) {
return errors.Errorf("number of peer addresses (%d) does not match the number of TLS root cert files (%d)", len(input.PeerAddresses), len(input.TLSRootCertFiles))
}
return nil
}
func (c *ClientConnections) setCertificate() error {
certificate, err := common.GetCertificate()
if err != nil {
return errors.WithMessage(err, "failed to retrieve client cerificate")
}
c.Certificate = certificate
return nil
}
func (c *ClientConnections) setOrdererClient() error {
oe := viper.GetString("orderer.address")
if oe == "" {
// if we're here we didn't get an orderer endpoint from the command line
// so we'll attempt to get one from cscc - bless it
if c.Signer == nil {
return errors.New("cannot obtain orderer endpoint, no signer was configured")
}
if len(c.EndorserClients) == 0 {
return errors.New("cannot obtain orderer endpoint, empty endorser list")
}
orderingEndpoints, err := common.GetOrdererEndpointOfChainFnc(channelID, c.Signer, c.EndorserClients[0], c.CryptoProvider)
if err != nil {
return errors.WithMessagef(err, "error getting channel (%s) orderer endpoint", channelID)
}
if len(orderingEndpoints) == 0 {
return errors.Errorf("no orderer endpoints retrieved for channel %s, pass orderer endpoint with -o flag instead", channelID)
}
logger.Infof("Retrieved channel (%s) orderer endpoint: %s", channelID, orderingEndpoints[0])
// override viper env
viper.Set("orderer.address", orderingEndpoints[0])
}
broadcastClient, err := common.GetBroadcastClient()
if err != nil {
return errors.WithMessage(err, "failed to retrieve broadcast client")
}
c.BroadcastClient = broadcastClient
return nil
}