forked from CognitionFoundry/gohfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
242 lines (221 loc) · 7.11 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Copyright Cognition Foundry / Conquex 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gohfc
import (
"fmt"
"io"
"google.golang.org/grpc"
"github.com/hyperledger/fabric/protos/peer"
"context"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/orderer"
)
// Peer represents a single peer
type Peer struct {
// Name is name of the peer. It must be unique in all peers
Name string
// Url is access point for this peer
Url string
// Opts are grpc.DialOption that manage TlS verification, certificates and communication rules
Opts []grpc.DialOption
}
type Orderer struct {
// Name is name of the orderer. It must be unique in all orderers
Name string
// Url is access point for this orderer
Url string
// Opts are grpc.DialOption that manage TlS verification, certificates and communication rules
Opts []grpc.DialOption
}
// Deliver delivers envelope to orderer for execution.
// Note that this method return only result of the connection and sending the envelope. Actual result depends by
// actual envelope and most of the times result will be send back as event.
func (o *Orderer) Deliver(envelope *common.Envelope) (*orderer.BroadcastResponse,error){
conn, err := grpc.Dial(o.Url, o.Opts...)
if err != nil {
Logger.Errorf("Error connecting to orderer %s: %s", o.Name, err)
return nil, err
}
defer conn.Close()
client := orderer.NewAtomicBroadcastClient(conn)
bk, err := client.Broadcast(context.Background())
if err != nil {
Logger.Errorf("Error sendig transaction to orderer %s: %s", o.Name, err)
return nil, err
}
bk.Send(envelope)
reply, err := bk.Recv()
if err != nil {
Logger.Errorf("Error recv Response from orderer %s: %s", o.Name, err)
return nil, err
}
return reply,nil
}
// GetBlock gets block data for particular block. Actual block number and channel are defined in envelope message.
// Note that this method blocks until all block data is received or error occurs.
func (o *Orderer) GetBlock(envelope *common.Envelope) (*orderer.DeliverResponse_Block,error){
conn, err := grpc.Dial(o.Url, o.Opts...)
if err != nil {
Logger.Errorf("Error connecting to orderer %s: %s", o.Name, err)
return nil, err
}
defer conn.Close()
client, err := orderer.NewAtomicBroadcastClient(conn).Deliver(context.TODO())
if err != nil {
Logger.Errorf("Error connecting orderer %s error: %s", o.Name, err)
return nil, err
}
client.Send(envelope)
var block *orderer.DeliverResponse_Block
L:
for {
msg, err := client.Recv()
if err != nil {
Logger.Errorf("Error recv data from orderer %s error: %s", o.Name, err)
return nil, err
}
switch t := msg.Type.(type) {
case *orderer.DeliverResponse_Status:
if t.Status != common.Status_SUCCESS {
Logger.Errorf("Delivery status from orderer %s is not 200: %s", o.Name, t.Status)
return nil, ErrBadTransactionStatus
}
continue
case *orderer.DeliverResponse_Block:
block = t
break L
}
}
return block,nil
}
// EventResponse is response from Event
//TODO Only one event that is working for now is Block. When fabric fix this revisit this structure.
type EventResponse struct {
// Error is error message.
Error error
// TxId is transaction id that generates this event
TxID string
}
// NewPeerFromConfig creates new Peer from PeerConf
func NewPeerFromConfig(conf *PeerConf) *Peer {
if conf.Insecure {
return &Peer{Name: conf.Name, Url: conf.URL, Opts: []grpc.DialOption{grpc.WithInsecure()}}
}
return &Peer{Name: conf.Name, Url: conf.URL}
}
// NewOrdererFromConfig creates new orderer from OrdererConf
func NewOrdererFromConfig(conf *OrdererConf) *Orderer {
if conf.Insecure {
return &Orderer{Name: conf.Name, Url: conf.URL, Opts: []grpc.DialOption{grpc.WithInsecure()}}
}
return &Orderer{Name: conf.Name, Url: conf.URL}
}
// Event connects to event peer and listens for events sent from this peer.
// Note that all peers will send same events so it makes no sense to listen for events in more than one peer.
// If value is sent through doneChan channel, connection to peer will be closed and eventChan channel will be closed.
//TODO fabric send events only for Block.
func (p *Peer) Event(eventChan chan *EventResponse, doneChan chan bool) error {
conn, err := grpc.Dial(p.Url, p.Opts...)
if err != nil {
return err
}
event := peer.NewEventsClient(conn)
cl, err := event.Chat(context.Background())
if err != nil {
return err
}
interest := &peer.Event{Event: &peer.Event_Register{Register: &peer.Register{
Events: []*peer.Interest{
{EventType: peer.EventType_REJECTION},
{EventType: peer.EventType_REGISTER},
{EventType: peer.EventType_CHAINCODE},
{EventType: peer.EventType_BLOCK},
}}}}
if err = cl.Send(interest); err != nil {
return err
}
//will close connection so blocking Recv will unblock with error and return
go func() {
select {
case <-doneChan:
conn.Close()
return
}
}()
go func() {
for {
in, err := cl.Recv()
if err == io.EOF {
close(eventChan)
conn.Close()
return
}
if err != nil {
close(eventChan)
conn.Close()
return
}
switch in.Event.(type) {
case *peer.Event_Block:
eventChan <- func() *EventResponse {
//TODO multiple Data?
envelope := new(common.Envelope)
payload := new(common.Payload)
response := new(EventResponse)
header := new(common.ChannelHeader)
if err := proto.Unmarshal(in.GetBlock().Data.Data[0], envelope); err != nil {
response.Error = err
return response
}
if err := proto.Unmarshal(envelope.Payload, payload); err != nil {
response.Error = err
return response
}
if err := proto.Unmarshal(payload.Header.ChannelHeader, header); err != nil {
response.Error = err
return response
}
response.TxID = header.TxId
return response
}()
case *peer.Event_Rejection:
fmt.Println("Rejection")
case *peer.Event_Register:
fmt.Println("Register")
case *peer.Event_ChaincodeEvent:
fmt.Println("Chaincode")
}
}
}()
return nil
}
// sendToEndorser sends single transaction to single peer.
func (p *Peer) Endorse(resp chan *PeerResponse, prop *peer.SignedProposal) {
conn, err := grpc.Dial(p.Url, p.Opts...)
if err != nil {
Logger.Errorf("Error connecting to peer %s: %s", p.Name, err)
resp <- &PeerResponse{Response: nil, Err: err}
return
}
defer conn.Close()
client := peer.NewEndorserClient(conn)
proposalResp, err := client.ProcessProposal(context.Background(), prop)
if err != nil {
Logger.Errorf("Error getting response from peer %s: %s", p.Name, err)
resp <- &PeerResponse{Response: nil, Err: err}
return
}
resp <- &PeerResponse{Response: proposalResp, Err: nil}
return
}