forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deliverclient.go
221 lines (190 loc) · 5.67 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package common
import (
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/localmsp"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/peer/common/api"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
"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 {
Service api.DeliverService
ChannelID string
TLSCertHash []byte
}
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)
return d.Service.Send(env)
}
func (d *DeliverClient) seekOldest() error {
env := seekHelper(d.ChannelID, seekOldest, d.TLSCertHash)
return d.Service.Send(env)
}
func (d *DeliverClient) seekNewest() error {
env := seekHelper(d.ChannelID, seekNewest, d.TLSCertHash)
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("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)
d.Service.Recv() // Flush the success message
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) *cb.Envelope {
seekInfo := &ab.SeekInfo{
Start: position,
Stop: position,
Behavior: ab.SeekInfo_BLOCK_UNTIL_READY,
}
env, err := utils.CreateSignedEnvelopeWithTLSBinding(
cb.HeaderType_DELIVER_SEEK_INFO,
channelID,
localmsp.NewSigner(),
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) (*DeliverClient, error) {
var tlsCertHash []byte
oc, err := NewOrdererClientFromEnv()
if err != nil {
return nil, errors.WithMessage(err, "failed to create deliver client")
}
dc, err := oc.Deliver()
if err != nil {
return nil, errors.WithMessage(err, "failed to create deliver client")
}
// 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{
Service: ds,
ChannelID: channelID,
TLSCertHash: tlsCertHash,
}
return o, nil
}
type peerDeliverService struct {
pb.Deliver_DeliverClient
}
// NewDeliverClientForPeer creates a new DeliverClient from a PeerClient
func NewDeliverClientForPeer(channelID string) (*DeliverClient, error) {
var tlsCertHash []byte
pc, err := NewPeerClientFromEnv()
if err != nil {
return nil, errors.WithMessage(err, "failed to create deliver client")
}
d, err := pc.Deliver()
if err != nil {
return nil, errors.WithMessage(err, "failed to create deliver client")
}
// 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{
Service: ds,
ChannelID: channelID,
TLSCertHash: tlsCertHash,
}
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
}