forked from tw-bc-group/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deliverclient.go
241 lines (208 loc) · 6.37 KB
/
deliverclient.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package common
import (
cb "github.com/hyperledger/fabric-protos-go/common"
ab "github.com/hyperledger/fabric-protos-go/orderer"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/internal/pkg/identity"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
)
var (
logger = flogging.MustGetLogger("cli.common")
seekNewest = &ab.SeekPosition{
Type: &ab.SeekPosition_Newest{
Newest: &ab.SeekNewest{},
},
}
seekOldest = &ab.SeekPosition{
Type: &ab.SeekPosition_Oldest{
Oldest: &ab.SeekOldest{},
},
}
)
// DeliverClient holds the necessary information to connect a client
// to an orderer/peer deliver service
type DeliverClient struct {
Signer identity.SignerSerializer
Service ab.AtomicBroadcast_DeliverClient
ChannelID string
TLSCertHash []byte
BestEffort bool
}
func (d *DeliverClient) seekSpecified(blockNumber uint64) error {
seekPosition := &ab.SeekPosition{
Type: &ab.SeekPosition_Specified{
Specified: &ab.SeekSpecified{
Number: blockNumber,
},
},
}
env := seekHelper(d.ChannelID, seekPosition, d.TLSCertHash, d.Signer, d.BestEffort)
return d.Service.Send(env)
}
func (d *DeliverClient) seekOldest() error {
env := seekHelper(d.ChannelID, seekOldest, d.TLSCertHash, d.Signer, d.BestEffort)
return d.Service.Send(env)
}
func (d *DeliverClient) seekNewest() error {
env := seekHelper(d.ChannelID, seekNewest, d.TLSCertHash, d.Signer, d.BestEffort)
return d.Service.Send(env)
}
func (d *DeliverClient) readBlock() (*cb.Block, error) {
msg, err := d.Service.Recv()
if err != nil {
return nil, errors.Wrap(err, "error receiving")
}
switch t := msg.Type.(type) {
case *ab.DeliverResponse_Status:
logger.Infof("Expect block, but got status: %v", t)
return nil, errors.Errorf("can't read the block: %v", t)
case *ab.DeliverResponse_Block:
logger.Infof("Received block: %v", t.Block.Header.Number)
if resp, err := d.Service.Recv(); err != nil { // Flush the success message
logger.Errorf("Failed to flush success message: %s", err)
} else if status := resp.GetStatus(); status != cb.Status_SUCCESS {
logger.Errorf("Expect status to be SUCCESS, got: %s", status)
}
return t.Block, nil
default:
return nil, errors.Errorf("response error: unknown type %T", t)
}
}
// GetSpecifiedBlock gets the specified block from a peer/orderer's deliver
// service
func (d *DeliverClient) GetSpecifiedBlock(num uint64) (*cb.Block, error) {
err := d.seekSpecified(num)
if err != nil {
return nil, errors.WithMessage(err, "error getting specified block")
}
return d.readBlock()
}
// GetOldestBlock gets the oldest block from a peer/orderer's deliver service
func (d *DeliverClient) GetOldestBlock() (*cb.Block, error) {
err := d.seekOldest()
if err != nil {
return nil, errors.WithMessage(err, "error getting oldest block")
}
return d.readBlock()
}
// GetNewestBlock gets the newest block from a peer/orderer's deliver service
func (d *DeliverClient) GetNewestBlock() (*cb.Block, error) {
err := d.seekNewest()
if err != nil {
return nil, errors.WithMessage(err, "error getting newest block")
}
return d.readBlock()
}
// Close closes a deliver client's connection
func (d *DeliverClient) Close() error {
return d.Service.CloseSend()
}
func seekHelper(
channelID string,
position *ab.SeekPosition,
tlsCertHash []byte,
signer identity.SignerSerializer,
bestEffort bool,
) *cb.Envelope {
seekInfo := &ab.SeekInfo{
Start: position,
Stop: position,
Behavior: ab.SeekInfo_BLOCK_UNTIL_READY,
}
if bestEffort {
seekInfo.ErrorResponse = ab.SeekInfo_BEST_EFFORT
}
env, err := protoutil.CreateSignedEnvelopeWithTLSBinding(
cb.HeaderType_DELIVER_SEEK_INFO,
channelID,
signer,
seekInfo,
int32(0),
uint64(0),
tlsCertHash,
)
if err != nil {
logger.Errorf("Error signing envelope: %s", err)
return nil
}
return env
}
type ordererDeliverService struct {
ab.AtomicBroadcast_DeliverClient
}
// NewDeliverClientForOrderer creates a new DeliverClient from an OrdererClient
func NewDeliverClientForOrderer(channelID string, signer identity.SignerSerializer, bestEffort bool) (*DeliverClient, error) {
var tlsCertHash []byte
oc, err := NewOrdererClientFromEnv()
if err != nil {
return nil, errors.WithMessage(err, "failed to create deliver client for orderer")
}
dc, err := oc.Deliver()
if err != nil {
return nil, errors.WithMessage(err, "failed to create deliver client for orderer")
}
// check for client certificate and create hash if present
if len(oc.Certificate().Certificate) > 0 {
tlsCertHash = util.ComputeSHA256(oc.Certificate().Certificate[0])
}
ds := &ordererDeliverService{dc}
o := &DeliverClient{
Signer: signer,
Service: ds,
ChannelID: channelID,
TLSCertHash: tlsCertHash,
BestEffort: bestEffort,
}
return o, nil
}
type peerDeliverService struct {
pb.Deliver_DeliverClient
}
// NewDeliverClientForPeer creates a new DeliverClient from a PeerClient
func NewDeliverClientForPeer(channelID string, signer identity.SignerSerializer, bestEffort bool) (*DeliverClient, error) {
var tlsCertHash []byte
pc, err := NewPeerClientFromEnv()
if err != nil {
return nil, errors.WithMessage(err, "failed to create deliver client for peer")
}
d, err := pc.Deliver()
if err != nil {
return nil, errors.WithMessage(err, "failed to create deliver client for peer")
}
// check for client certificate and create hash if present
if len(pc.Certificate().Certificate) > 0 {
tlsCertHash = util.ComputeSHA256(pc.Certificate().Certificate[0])
}
ds := &peerDeliverService{d}
p := &DeliverClient{
Signer: signer,
Service: ds,
ChannelID: channelID,
TLSCertHash: tlsCertHash,
BestEffort: bestEffort,
}
return p, nil
}
func (p *peerDeliverService) Recv() (*ab.DeliverResponse, error) {
pbResp, err := p.Deliver_DeliverClient.Recv()
if err != nil {
return nil, errors.Wrap(err, "error receiving from peer deliver service")
}
abResp := &ab.DeliverResponse{}
switch t := pbResp.Type.(type) {
case *pb.DeliverResponse_Status:
abResp.Type = &ab.DeliverResponse_Status{Status: t.Status}
case *pb.DeliverResponse_Block:
abResp.Type = &ab.DeliverResponse_Block{Block: t.Block}
default:
return nil, errors.Errorf("response error: unknown type %T", t)
}
return abResp, nil
}