-
Notifications
You must be signed in to change notification settings - Fork 508
/
endpoint.go
81 lines (66 loc) · 1.99 KB
/
endpoint.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
// Package filter provides common filters (e.g. Endpoint)
package filter
import (
"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"
)
// EndpointType represents endpoint type
type EndpointType int32
// Endpoint types
const (
ChaincodeQuery EndpointType = iota
EndorsingPeer
LedgerQuery
EventSource
)
// NewEndpointFilter creates a new endpoint filter that is based on configuration.
// If channel peer is not configured it will be selected by default.
func NewEndpointFilter(ctx context.Channel, et EndpointType) *EndpointFilter {
// Retrieve channel peers
chPeers := ctx.EndpointConfig().ChannelPeers(ctx.ChannelID())
return &EndpointFilter{endpointType: et, ctx: ctx, chPeers: chPeers}
}
// EndpointFilter filters based on endpoint config options
type EndpointFilter struct {
endpointType EndpointType
ctx context.Channel
chPeers []fab.ChannelPeer // configured channel peers
}
// Accept returns false if this peer is to be excluded from the target list
func (f *EndpointFilter) Accept(peer fab.Peer) bool {
peerConfig, err := comm.SearchPeerConfigFromURL(f.ctx.EndpointConfig(), peer.URL())
if err != nil {
return true
}
chPeer := f.getChannelPeer(peerConfig)
if chPeer == nil {
return true
}
switch t := f.endpointType; t {
case ChaincodeQuery:
return chPeer.ChaincodeQuery
case EndorsingPeer:
return chPeer.EndorsingPeer
case LedgerQuery:
return chPeer.LedgerQuery
case EventSource:
return chPeer.EventSource
}
return true
}
func (f *EndpointFilter) getChannelPeer(peerConfig *fab.PeerConfig) *fab.ChannelPeer {
for _, chpeer := range f.chPeers {
if chpeer.URL == peerConfig.URL {
return &fab.ChannelPeer{
PeerChannelConfig: chpeer.PeerChannelConfig,
NetworkPeer: chpeer.NetworkPeer,
}
}
}
return nil
}