-
Notifications
You must be signed in to change notification settings - Fork 0
/
peers.go
180 lines (156 loc) · 4.18 KB
/
peers.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package discovery
import (
"encoding/json"
"fmt"
"io"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/msp"
"github.com/osdi23p228/fabric/cmd/common"
discovery "github.com/osdi23p228/fabric/discovery/client"
"github.com/pkg/errors"
)
// NewPeerCmd creates a new PeerCmd with the given Stub and ResponseParser
func NewPeerCmd(stub Stub, parser ResponseParser) *PeerCmd {
return &PeerCmd{
stub: stub,
parser: parser,
}
}
// PeerCmd executes channelPeer listing command
type PeerCmd struct {
stub Stub
server *string
channel *string
parser ResponseParser
}
// SetServer sets the server of the PeerCmd
func (pc *PeerCmd) SetServer(server *string) {
pc.server = server
}
// SetChannel sets the channel of the PeerCmd
func (pc *PeerCmd) SetChannel(channel *string) {
pc.channel = channel
}
// Execute executes the command
func (pc *PeerCmd) Execute(conf common.Config) error {
channel := ""
if pc.channel != nil {
channel = *pc.channel
}
if pc.server == nil || *pc.server == "" {
return errors.New("no server specified")
}
server := *pc.server
req := discovery.NewRequest()
if channel != "" {
req = req.OfChannel(channel)
req = req.AddPeersQuery()
} else {
req = req.AddLocalPeersQuery()
}
res, err := pc.stub.Send(server, conf, req)
if err != nil {
return err
}
return pc.parser.ParseResponse(channel, res)
}
// PeerResponseParser parses a channelPeer response
type PeerResponseParser struct {
io.Writer
}
// ParseResponse parses the given response about the given channel
func (parser *PeerResponseParser) ParseResponse(channel string, res ServiceResponse) error {
var listPeers peerLister
if channel == "" {
listPeers = res.ForLocal()
} else {
listPeers = &simpleChannelResponse{res.ForChannel(channel)}
}
peers, err := listPeers.Peers()
if err != nil {
return err
}
channelState := channel != ""
b, _ := json.MarshalIndent(assemblePeers(peers, channelState), "", "\t")
fmt.Fprintln(parser.Writer, string(b))
return nil
}
func assemblePeers(peers []*discovery.Peer, withChannelState bool) interface{} {
if withChannelState {
var peerSlices []channelPeer
for _, p := range peers {
peerSlices = append(peerSlices, rawPeerToChannelPeer(p))
}
return peerSlices
}
var peerSlices []localPeer
for _, p := range peers {
peerSlices = append(peerSlices, rawPeerToLocalPeer(p))
}
return peerSlices
}
type channelPeer struct {
MSPID string
LedgerHeight uint64
Endpoint string
Identity string
Chaincodes []string
}
type localPeer struct {
MSPID string
Endpoint string
Identity string
}
type peerLister interface {
Peers() ([]*discovery.Peer, error)
}
type simpleChannelResponse struct {
discovery.ChannelResponse
}
func (scr *simpleChannelResponse) Peers() ([]*discovery.Peer, error) {
return scr.ChannelResponse.Peers()
}
func rawPeerToChannelPeer(p *discovery.Peer) channelPeer {
var ledgerHeight uint64
var ccs []string
if p.StateInfoMessage != nil && p.StateInfoMessage.GetStateInfo() != nil && p.StateInfoMessage.GetStateInfo().Properties != nil {
properties := p.StateInfoMessage.GetStateInfo().Properties
ledgerHeight = properties.LedgerHeight
for _, cc := range properties.Chaincodes {
if cc == nil {
continue
}
ccs = append(ccs, cc.Name)
}
}
var endpoint string
if p.AliveMessage != nil && p.AliveMessage.GetAliveMsg() != nil && p.AliveMessage.GetAliveMsg().Membership != nil {
endpoint = p.AliveMessage.GetAliveMsg().Membership.Endpoint
}
sID := &msp.SerializedIdentity{}
proto.Unmarshal(p.Identity, sID)
return channelPeer{
MSPID: p.MSPID,
Endpoint: endpoint,
LedgerHeight: ledgerHeight,
Identity: string(sID.IdBytes),
Chaincodes: ccs,
}
}
func rawPeerToLocalPeer(p *discovery.Peer) localPeer {
var endpoint string
if p.AliveMessage != nil && p.AliveMessage.GetAliveMsg() != nil && p.AliveMessage.GetAliveMsg().Membership != nil {
endpoint = p.AliveMessage.GetAliveMsg().Membership.Endpoint
}
sID := &msp.SerializedIdentity{}
proto.Unmarshal(p.Identity, sID)
return localPeer{
MSPID: p.MSPID,
Endpoint: endpoint,
Identity: string(sID.IdBytes),
}
}