forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
executable file
·194 lines (159 loc) · 5.11 KB
/
connection.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package connection
import (
"context"
"fmt"
"io"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/crypto"
ab "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
fabcontext "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/comm"
clientdisp "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/client/dispatcher"
cb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/utils"
"github.com/pkg/errors"
"google.golang.org/grpc"
)
var logger = logging.NewLogger("fabsdk/fab")
type deliverStream interface {
grpc.ClientStream
Send(*cb.Envelope) error
Recv() (*pb.DeliverResponse, error)
}
// DeliverConnection manages the connection to the deliver server
type DeliverConnection struct {
*comm.StreamConnection
url string
}
// StreamProvider creates a deliver stream
type StreamProvider func(pb.DeliverClient) (deliverStream, error)
var (
// Deliver creates a Deliver stream
Deliver = func(client pb.DeliverClient) (deliverStream, error) {
return client.Deliver(context.Background())
}
// DeliverFiltered creates a DeliverFiltered stream
DeliverFiltered = func(client pb.DeliverClient) (deliverStream, error) {
return client.DeliverFiltered(context.Background())
}
)
// New returns a new Deliver Server connection
func New(ctx fabcontext.Client, chConfig fab.ChannelCfg, streamProvider StreamProvider, url string, opts ...options.Opt) (*DeliverConnection, error) {
logger.Debugf("Connecting to %s...", url)
connect, err := comm.NewStreamConnection(
ctx, chConfig,
func(grpcconn *grpc.ClientConn) (grpc.ClientStream, error) {
return streamProvider(pb.NewDeliverClient(grpcconn))
},
url, opts...,
)
if err != nil {
return nil, err
}
return &DeliverConnection{
StreamConnection: connect,
url: url,
}, nil
}
func (c *DeliverConnection) deliverStream() deliverStream {
if c.Stream() == nil {
return nil
}
stream, ok := c.Stream().(deliverStream)
if !ok {
panic(fmt.Sprintf("invalid DeliverStream type %T", c.Stream()))
}
return stream
}
// Send sends a seek request to the deliver server
func (c *DeliverConnection) Send(seekInfo *ab.SeekInfo) error {
if c.Closed() {
return errors.New("connection is closed")
}
logger.Debugf("Sending %#v", seekInfo)
env, err := c.createSignedEnvelope(seekInfo)
if err != nil {
return err
}
return c.deliverStream().Send(env)
}
// Receive receives events from the deliver server
func (c *DeliverConnection) Receive(eventch chan<- interface{}) {
for {
stream := c.deliverStream()
if stream == nil {
logger.Warn("The stream has closed. Terminating loop.")
break
}
in, err := stream.Recv()
logger.Debugf("Got deliver response: %#v", in)
if c.Closed() {
logger.Debugf("The connection has closed with error [%s]. Terminating loop.", err)
break
}
if err == io.EOF {
// This signifies that the stream has been terminated at the client-side. No need to send an event.
logger.Debug("Received EOF from stream.")
break
}
if err != nil {
logger.Warnf("Received error from stream: [%s]. Sending disconnected event.", err)
eventch <- clientdisp.NewDisconnectedEvent(err)
break
}
eventch <- NewEvent(in, c.url)
}
logger.Debug("Exiting stream listener")
}
func (c *DeliverConnection) createSignedEnvelope(msg proto.Message) (*cb.Envelope, error) {
// TODO: Do we need to make these configurable?
var msgVersion int32
var epoch uint64
payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_DELIVER_SEEK_INFO, msgVersion, c.ChannelConfig().ID(), epoch)
payloadChannelHeader.TlsCertHash = c.TLSCertHash()
data, err := proto.Marshal(msg)
if err != nil {
return nil, err
}
identity, err := c.Context().Serialize()
if err != nil {
return nil, err
}
nonce, err := crypto.GetRandomNonce()
if err != nil {
return nil, err
}
payloadSignatureHeader := &cb.SignatureHeader{
Creator: identity,
Nonce: nonce,
}
paylBytes := utils.MarshalOrPanic(&cb.Payload{
Header: utils.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader),
Data: data,
})
signature, err := c.Context().SigningManager().Sign(paylBytes, c.Context().PrivateKey())
if err != nil {
return nil, err
}
return &cb.Envelope{Payload: paylBytes, Signature: signature}, nil
}
// Event contains the deliver event as well as the event source
type Event struct {
SourceURL string
Event interface{}
}
// NewEvent returns a deliver event
func NewEvent(event interface{}, sourceURL string) *Event {
return &Event{
SourceURL: sourceURL,
Event: event,
}
}