Skip to content

Commit 5b8f017

Browse files
Unit test for websocket comm service
Signed-off-by: Alexandros Filios <[email protected]>
1 parent 1d02aa3 commit 5b8f017

File tree

6 files changed

+241
-74
lines changed

6 files changed

+241
-74
lines changed

Diff for: platform/view/core/endpoint/endpoint.go

+8-63
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package endpoint
99
import (
1010
"bytes"
1111
"net"
12-
"reflect"
1312
"strings"
1413
"sync"
1514

@@ -19,7 +18,6 @@ import (
1918
"github.com/hyperledger-labs/fabric-smart-client/platform/view/driver"
2019
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
2120
"github.com/pkg/errors"
22-
"go.uber.org/zap/zapcore"
2321
"golang.org/x/exp/slices"
2422
)
2523

@@ -69,17 +67,14 @@ type Service struct {
6967
resolversMutex sync.RWMutex
7068
bindingKVS driver2.BindingStore
7169

72-
pkiExtractorsLock sync.RWMutex
73-
publicKeyExtractors []driver.PublicKeyExtractor
74-
publicKeyIDSynthesizer driver.PublicKeyIDSynthesizer
70+
pkiExtractor *PKIExtractor
7571
}
7672

7773
// NewService returns a new instance of the view-sdk endpoint service
7874
func NewService(bindingKVS driver2.BindingStore) (*Service, error) {
7975
er := &Service{
80-
bindingKVS: bindingKVS,
81-
publicKeyExtractors: []driver.PublicKeyExtractor{},
82-
publicKeyIDSynthesizer: DefaultPublicKeyIDSynthesizer{},
76+
bindingKVS: bindingKVS,
77+
pkiExtractor: NewPKIExtractor(),
8378
}
8479
return er, nil
8580
}
@@ -89,7 +84,7 @@ func (r *Service) Resolve(party view.Identity) (driver.Resolver, []byte, error)
8984
if err != nil {
9085
return nil, nil, err
9186
}
92-
return resolver, r.pkiResolve(resolver), nil
87+
return resolver, r.pkiExtractor.PkiResolve(resolver), nil
9388
}
9489

9590
func (r *Service) GetResolver(party view.Identity) (driver.Resolver, error) {
@@ -161,7 +156,7 @@ func (r *Service) matchesResolver(endpoint string, pkID []byte, resolver *Resolv
161156
}
162157

163158
return len(pkID) > 0 && (bytes.Equal(pkID, resolver.Id) ||
164-
bytes.Equal(pkID, r.pkiResolve(resolver)))
159+
bytes.Equal(pkID, r.pkiExtractor.PkiResolve(resolver)))
165160
}
166161

167162
func (r *Service) AddResolver(name string, domain string, addresses map[string]string, aliases []string, id []byte) (view.Identity, error) {
@@ -203,54 +198,15 @@ func (r *Service) AddResolver(name string, domain string, addresses map[string]s
203198
}
204199

205200
func (r *Service) AddPublicKeyExtractor(publicKeyExtractor driver.PublicKeyExtractor) error {
206-
r.pkiExtractorsLock.Lock()
207-
defer r.pkiExtractorsLock.Unlock()
208-
209-
if publicKeyExtractor == nil {
210-
return errors.New("pki resolver should not be nil")
211-
}
212-
r.publicKeyExtractors = append(r.publicKeyExtractors, publicKeyExtractor)
213-
return nil
201+
return r.pkiExtractor.AddPublicKeyExtractor(publicKeyExtractor)
214202
}
215203

216204
func (r *Service) SetPublicKeyIDSynthesizer(publicKeyIDSynthesizer driver.PublicKeyIDSynthesizer) {
217-
r.publicKeyIDSynthesizer = publicKeyIDSynthesizer
218-
}
219-
220-
func (r *Service) pkiResolve(resolver *Resolver) []byte {
221-
resolver.PKILock.RLock()
222-
if len(resolver.PKI) != 0 {
223-
resolver.PKILock.RUnlock()
224-
return resolver.PKI
225-
}
226-
resolver.PKILock.RUnlock()
227-
228-
resolver.PKILock.Lock()
229-
defer resolver.PKILock.Unlock()
230-
if len(resolver.PKI) == 0 {
231-
resolver.PKI = r.ExtractPKI(resolver.Id)
232-
}
233-
return resolver.PKI
205+
r.pkiExtractor.SetPublicKeyIDSynthesizer(publicKeyIDSynthesizer)
234206
}
235207

236208
func (r *Service) ExtractPKI(id []byte) []byte {
237-
r.pkiExtractorsLock.RLock()
238-
defer r.pkiExtractorsLock.RUnlock()
239-
240-
for _, extractor := range r.publicKeyExtractors {
241-
if pk, err := extractor.ExtractPublicKey(id); pk != nil {
242-
if logger.IsEnabledFor(zapcore.DebugLevel) {
243-
logger.Debugf("pki resolved for [%s]", id)
244-
}
245-
return r.publicKeyIDSynthesizer.PublicKeyID(pk)
246-
} else {
247-
if logger.IsEnabledFor(zapcore.DebugLevel) {
248-
logger.Debugf("pki not resolved by [%s] for [%s]: [%s]", getIdentifier(extractor), id, err)
249-
}
250-
}
251-
}
252-
logger.Warnf("cannot resolve pki for [%s]", id)
253-
return nil
209+
return r.pkiExtractor.ExtractPKI(id)
254210
}
255211

256212
func (r *Service) rootEndpoint(party view.Identity) (*Resolver, error) {
@@ -296,14 +252,3 @@ func LookupIPv4(endpoint string) string {
296252
port := s[1]
297253
return net.JoinHostPort(addrS, port)
298254
}
299-
300-
func getIdentifier(f any) string {
301-
if f == nil {
302-
return "<nil view>"
303-
}
304-
t := reflect.TypeOf(f)
305-
for t.Kind() == reflect.Ptr {
306-
t = t.Elem()
307-
}
308-
return t.PkgPath() + "/" + t.Name()
309-
}

Diff for: platform/view/core/endpoint/endpoint_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestPKIResolveConcurrency(t *testing.T) {
4141
for i := 0; i < 100; i++ {
4242
go func() {
4343
defer wg.Done()
44-
svc.pkiResolve(resolver)
44+
svc.pkiExtractor.PkiResolve(resolver)
4545
}()
4646
}
4747
wg.Wait()

Diff for: platform/view/core/endpoint/extractor.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package endpoint
2+
3+
/*
4+
Copyright IBM Corp. All Rights Reserved.
5+
6+
SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
import (
10+
"reflect"
11+
"sync"
12+
13+
"github.com/hyperledger-labs/fabric-smart-client/platform/view/driver"
14+
"github.com/pkg/errors"
15+
"go.uber.org/zap/zapcore"
16+
)
17+
18+
func NewPKIExtractor() *PKIExtractor {
19+
return &PKIExtractor{
20+
publicKeyExtractors: []driver.PublicKeyExtractor{},
21+
publicKeyIDSynthesizer: DefaultPublicKeyIDSynthesizer{},
22+
}
23+
}
24+
25+
type PKIExtractor struct {
26+
pkiExtractorsLock sync.RWMutex
27+
publicKeyExtractors []driver.PublicKeyExtractor
28+
publicKeyIDSynthesizer driver.PublicKeyIDSynthesizer
29+
}
30+
31+
func (r *PKIExtractor) AddPublicKeyExtractor(publicKeyExtractor driver.PublicKeyExtractor) error {
32+
r.pkiExtractorsLock.Lock()
33+
defer r.pkiExtractorsLock.Unlock()
34+
35+
if publicKeyExtractor == nil {
36+
return errors.New("pki resolver should not be nil")
37+
}
38+
r.publicKeyExtractors = append(r.publicKeyExtractors, publicKeyExtractor)
39+
return nil
40+
}
41+
42+
func (r *PKIExtractor) SetPublicKeyIDSynthesizer(publicKeyIDSynthesizer driver.PublicKeyIDSynthesizer) {
43+
r.publicKeyIDSynthesizer = publicKeyIDSynthesizer
44+
}
45+
46+
func (r *PKIExtractor) PkiResolve(resolver *Resolver) []byte {
47+
resolver.PKILock.RLock()
48+
if len(resolver.PKI) != 0 {
49+
resolver.PKILock.RUnlock()
50+
return resolver.PKI
51+
}
52+
resolver.PKILock.RUnlock()
53+
54+
resolver.PKILock.Lock()
55+
defer resolver.PKILock.Unlock()
56+
if len(resolver.PKI) == 0 {
57+
resolver.PKI = r.ExtractPKI(resolver.Id)
58+
}
59+
return resolver.PKI
60+
}
61+
62+
func (r *PKIExtractor) ExtractPKI(id []byte) []byte {
63+
r.pkiExtractorsLock.RLock()
64+
defer r.pkiExtractorsLock.RUnlock()
65+
66+
for _, extractor := range r.publicKeyExtractors {
67+
if pk, err := extractor.ExtractPublicKey(id); pk != nil {
68+
if logger.IsEnabledFor(zapcore.DebugLevel) {
69+
logger.Debugf("pki resolved for [%s]", id)
70+
}
71+
return r.publicKeyIDSynthesizer.PublicKeyID(pk)
72+
} else {
73+
if logger.IsEnabledFor(zapcore.DebugLevel) {
74+
logger.Debugf("pki not resolved by [%s] for [%s]: [%s]", getIdentifier(extractor), id, err)
75+
}
76+
}
77+
}
78+
logger.Warnf("cannot resolve pki for [%s]", id)
79+
return nil
80+
}
81+
82+
func getIdentifier(f any) string {
83+
if f == nil {
84+
return "<nil view>"
85+
}
86+
t := reflect.TypeOf(f)
87+
for t.Kind() == reflect.Ptr {
88+
t = t.Elem()
89+
}
90+
return t.PkgPath() + "/" + t.Name()
91+
}

Diff for: platform/view/services/comm/comm.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,39 @@ type ConfigService interface {
3333
type Service struct {
3434
HostProvider host.GeneratorProvider
3535
EndpointService EndpointService
36-
ConfigService ConfigService
36+
config *Config
3737

3838
Node *P2PNode
3939
NodeSync sync.RWMutex
4040
tracerProvider trace.TracerProvider
4141
metricsProvider metrics.Provider
4242
}
4343

44+
type Config struct {
45+
ListenAddress string
46+
BootstrapNode string
47+
KeyFile string
48+
CertFile string
49+
}
50+
4451
func NewService(hostProvider host.GeneratorProvider, endpointService EndpointService, configService ConfigService, tracerProvider trace.TracerProvider, metricsProvider metrics.Provider) (*Service, error) {
45-
s := &Service{
52+
config := &Config{
53+
ListenAddress: configService.GetString("fsc.p2p.listenAddress"),
54+
BootstrapNode: configService.GetString("fsc.p2p.opts.bootstrapNode"),
55+
KeyFile: configService.GetPath("fsc.identity.key.file"),
56+
CertFile: configService.GetPath("fsc.identity.cert.file"),
57+
}
58+
return newService(hostProvider, endpointService, config, tracerProvider, metricsProvider), nil
59+
}
60+
61+
func newService(hostProvider host.GeneratorProvider, endpointService EndpointService, config *Config, tracerProvider trace.TracerProvider, metricsProvider metrics.Provider) *Service {
62+
return &Service{
4663
HostProvider: hostProvider,
4764
EndpointService: endpointService,
48-
ConfigService: configService,
65+
config: config,
4966
tracerProvider: tracerProvider,
5067
metricsProvider: metricsProvider,
5168
}
52-
return s, nil
5369
}
5470

5571
func (s *Service) Start(ctx context.Context) {
@@ -123,10 +139,10 @@ func (s *Service) init() error {
123139
return nil
124140
}
125141

126-
p2pListenAddress := s.ConfigService.GetString("fsc.p2p.listenAddress")
127-
p2pBootstrapNode := s.ConfigService.GetString("fsc.p2p.opts.bootstrapNode")
128-
keyFile := s.ConfigService.GetPath("fsc.identity.key.file")
129-
certFile := s.ConfigService.GetPath("fsc.identity.cert.file")
142+
p2pListenAddress := s.config.ListenAddress
143+
p2pBootstrapNode := s.config.BootstrapNode
144+
keyFile := s.config.KeyFile
145+
certFile := s.config.CertFile
130146

131147
if len(p2pBootstrapNode) == 0 {
132148
// this is a bootstrap node

0 commit comments

Comments
 (0)