This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
forked from s7techlab/hlf-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gossip.go
102 lines (83 loc) · 2.7 KB
/
gossip.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
package discovery
import (
"context"
"fmt"
discClient "github.com/hyperledger/fabric/discovery/client"
"go.uber.org/zap"
"google.golang.org/grpc"
"github.com/atomyze-ru/hlf-sdk-go/api"
"github.com/atomyze-ru/hlf-sdk-go/api/config"
grpcclient "github.com/atomyze-ru/hlf-sdk-go/client/grpc"
)
// implementation of api.DiscoveryProvider interface
var _ api.DiscoveryProvider = (*GossipDiscoveryProvider)(nil)
type GossipDiscoveryProvider struct {
sd *gossipServiceDiscovery
tlsMapper tlsConfigMapper
}
// return tls config for peers found via gossip
type tlsConfigMapper interface {
TlsConfigForAddress(address string) *config.TlsConfig
}
func NewGossipDiscoveryProvider(
ctx context.Context,
connCfg config.ConnectionConfig,
log *zap.Logger,
identitySigner discClient.Signer,
clientIdentity []byte,
tlsMapper tlsConfigMapper,
) (*GossipDiscoveryProvider, error) {
discClient, err := newFabricDiscoveryClient(ctx, connCfg, log, identitySigner)
if err != nil {
return nil, err
}
// TODO probably we need to make a test call(ping) here to make sure user provided valid identity
sd := newGossipServiceDiscovery(discClient, clientIdentity)
return &GossipDiscoveryProvider{sd: sd, tlsMapper: tlsMapper}, nil
}
// newFabricDiscoveryClient - initializes grpc fabric discovery client
// necessary for GossipDiscoveryProvider
func newFabricDiscoveryClient(
ctx context.Context,
c config.ConnectionConfig,
log *zap.Logger,
identitySigner discClient.Signer,
) (*discClient.Client, error) {
opts, err := grpcclient.OptionsFromConfig(c, log)
if err != nil {
return nil, err
}
conn, err := grpc.DialContext(ctx, c.Host, opts.Dial...)
if err != nil {
return nil, fmt.Errorf(`grpc dial to host=%s: %w`, c.Host, err)
}
discClient := discClient.NewClient(
func() (*grpc.ClientConn, error) {
return conn, nil
},
identitySigner,
10,
)
return discClient, nil
}
func (d *GossipDiscoveryProvider) Chaincode(ctx context.Context, channelName string, ccName string) (api.ChaincodeDiscoverer, error) {
ccDTO, err := d.sd.DiscoverChaincode(ctx, ccName, channelName)
if err != nil {
return nil, err
}
return newChaincodeDiscovererTLSDecorator(ccDTO, d.tlsMapper), nil
}
func (d *GossipDiscoveryProvider) Channel(ctx context.Context, channelName string) (api.ChannelDiscoverer, error) {
chanDTO, err := d.sd.DiscoverChannel(ctx, channelName)
if err != nil {
return nil, err
}
return newChannelDiscovererTLSDecorator(chanDTO, d.tlsMapper), nil
}
func (d *GossipDiscoveryProvider) LocalPeers(ctx context.Context) (api.LocalPeersDiscoverer, error) {
localPeers, err := d.sd.LocalDiscovery(ctx)
if err != nil {
return nil, err
}
return newLocalPeersDiscovererTLSDecorator(localPeers, d.tlsMapper), nil
}