Skip to content

Commit 70d8ad3

Browse files
committed
configuration with docs
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent eddc9e7 commit 70d8ad3

File tree

7 files changed

+354
-15
lines changed

7 files changed

+354
-15
lines changed

docs/core-token.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ token:
7373
workers: 10
7474
# queueSize is the size of the event buffer. Defaults to 1000.
7575
queueSize: 1000
76+
77+
# fabricx configuration for FabricX-specific settings
78+
fabricx:
79+
# lookup configuration for the lookup service
80+
lookup:
81+
# permanent lookup configuration
82+
permanent:
83+
# interval is the polling interval for permanent lookups. Defaults to 1m.
84+
interval: 1m
85+
# one-time lookup configuration
86+
once:
87+
# deadline is the maximum time to wait for a one-time lookup. Defaults to 5m.
88+
deadline: 5m
89+
# interval is the polling interval for one-time lookups. Defaults to 2s.
90+
interval: 2s
91+
7692
tms:
7793
mytms: # unique name of this token management system
7894
network: default # the name of the network this TMS refers to (Fabric, etc.)
@@ -217,4 +233,27 @@ Default values:
217233
- delivery.blockProcessParallelism: 10
218234
- delivery.lruSize: 30
219235
- delivery.lruBuffer: 15
220-
- delivery.listenerTimeout: 10s
236+
- delivery.listenerTimeout: 10s
237+
238+
---
239+
240+
## Optional: token.fabricx.lookup
241+
242+
If not specified, the default configuration is:
243+
244+
```yaml
245+
token:
246+
fabricx:
247+
lookup:
248+
permanent:
249+
interval: 1m
250+
once:
251+
deadline: 5m
252+
interval: 2s
253+
```
254+
255+
Default values:
256+
257+
- permanent.interval: 1m
258+
- once.deadline: 5m
259+
- once.interval: 2s

token/services/network/fabricx/driver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func NewDriver(
9090
queryExecutorProvider: queryExecutorProvider,
9191
keyTranslator: kt,
9292
flmProvider: finality2.NewNotificationServiceBased(queryServiceProvider, finalityProvider, q),
93-
llmProvider: lookup2.NewQueryServiceBased(queryServiceProvider, q),
93+
llmProvider: lookup2.NewQueryServiceBased(queryServiceProvider, q, lookup2.NewConfig(configService)),
9494
EndorsementServiceProvider: endorsement.NewServiceProvider(
9595
configs,
9696
viewManager,
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package lookup
8+
9+
import "time"
10+
11+
const (
12+
// PermanentInterval is the configuration key for the polling interval of permanent lookups
13+
PermanentInterval = "token.fabricx.lookup.permanent.interval"
14+
// OnceDeadline is the configuration key for the deadline of one-time lookups
15+
OnceDeadline = "token.fabricx.lookup.once.deadline"
16+
// OnceInterval is the configuration key for the polling interval of one-time lookups
17+
OnceInterval = "token.fabricx.lookup.once.interval"
18+
19+
// DefaultPermanentInterval is the default polling interval for permanent lookups
20+
DefaultPermanentInterval = 1 * time.Minute
21+
// DefaultOnceDeadline is the default deadline for one-time lookups
22+
DefaultOnceDeadline = 5 * time.Minute
23+
// DefaultOnceInterval is the default polling interval for one-time lookups
24+
DefaultOnceInterval = 2 * time.Second
25+
)
26+
27+
// ConfigGetter models the configuration getter for the lookup service
28+
type ConfigGetter interface {
29+
// PermanentInterval returns the polling interval for permanent lookups
30+
PermanentInterval() time.Duration
31+
// OnceDeadline returns the deadline for one-time lookups
32+
OnceDeadline() time.Duration
33+
// OnceInterval returns the polling interval for one-time lookups
34+
OnceInterval() time.Duration
35+
}
36+
37+
//go:generate counterfeiter -o mock/configuration.go -fake-name Configuration . Configuration
38+
type Configuration interface {
39+
GetDuration(key string) time.Duration
40+
}
41+
42+
// NewConfig creates a new ConfigGetter
43+
func NewConfig(configuration Configuration) *serviceConfig {
44+
return &serviceConfig{configuration: configuration}
45+
}
46+
47+
type serviceConfig struct {
48+
configuration Configuration
49+
}
50+
51+
// PermanentInterval returns the polling interval for permanent lookups
52+
func (c *serviceConfig) PermanentInterval() time.Duration {
53+
if v := c.configuration.GetDuration(PermanentInterval); v > 0 {
54+
return v
55+
}
56+
57+
return DefaultPermanentInterval
58+
}
59+
60+
// OnceDeadline returns the deadline for one-time lookups
61+
func (c *serviceConfig) OnceDeadline() time.Duration {
62+
if v := c.configuration.GetDuration(OnceDeadline); v > 0 {
63+
return v
64+
}
65+
66+
return DefaultOnceDeadline
67+
}
68+
69+
// OnceInterval returns the polling interval for one-time lookups
70+
func (c *serviceConfig) OnceInterval() time.Duration {
71+
if v := c.configuration.GetDuration(OnceInterval); v > 0 {
72+
return v
73+
}
74+
75+
return DefaultOnceInterval
76+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package lookup_test
8+
9+
import (
10+
"testing"
11+
"time"
12+
13+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/fabricx/lookup"
14+
"github.com/hyperledger-labs/fabric-token-sdk/token/services/network/fabricx/lookup/mock"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func TestNewConfig(t *testing.T) {
19+
m := &mock.Configuration{}
20+
cfg := lookup.NewConfig(m)
21+
assert.NotNil(t, cfg)
22+
}
23+
24+
func TestServiceConfig_PermanentInterval(t *testing.T) {
25+
t.Run("returns value from configuration when > 0", func(t *testing.T) {
26+
m := &mock.Configuration{}
27+
expected := 2 * time.Minute
28+
m.GetDurationReturns(expected)
29+
cfg := lookup.NewConfig(m)
30+
31+
assert.Equal(t, expected, cfg.PermanentInterval())
32+
assert.Equal(t, 1, m.GetDurationCallCount())
33+
assert.Equal(t, lookup.PermanentInterval, m.GetDurationArgsForCall(0))
34+
})
35+
36+
t.Run("returns default when configuration returns 0", func(t *testing.T) {
37+
m := &mock.Configuration{}
38+
m.GetDurationReturns(0)
39+
cfg := lookup.NewConfig(m)
40+
41+
assert.Equal(t, lookup.DefaultPermanentInterval, cfg.PermanentInterval())
42+
})
43+
44+
t.Run("returns default when configuration returns negative", func(t *testing.T) {
45+
m := &mock.Configuration{}
46+
m.GetDurationReturns(-1 * time.Second)
47+
cfg := lookup.NewConfig(m)
48+
49+
assert.Equal(t, lookup.DefaultPermanentInterval, cfg.PermanentInterval())
50+
})
51+
}
52+
53+
func TestServiceConfig_OnceDeadline(t *testing.T) {
54+
t.Run("returns value from configuration when > 0", func(t *testing.T) {
55+
m := &mock.Configuration{}
56+
expected := 10 * time.Minute
57+
m.GetDurationReturns(expected)
58+
cfg := lookup.NewConfig(m)
59+
60+
assert.Equal(t, expected, cfg.OnceDeadline())
61+
assert.Equal(t, 1, m.GetDurationCallCount())
62+
assert.Equal(t, lookup.OnceDeadline, m.GetDurationArgsForCall(0))
63+
})
64+
65+
t.Run("returns default when configuration returns 0", func(t *testing.T) {
66+
m := &mock.Configuration{}
67+
m.GetDurationReturns(0)
68+
cfg := lookup.NewConfig(m)
69+
70+
assert.Equal(t, lookup.DefaultOnceDeadline, cfg.OnceDeadline())
71+
})
72+
73+
t.Run("returns default when configuration returns negative", func(t *testing.T) {
74+
m := &mock.Configuration{}
75+
m.GetDurationReturns(-1 * time.Second)
76+
cfg := lookup.NewConfig(m)
77+
78+
assert.Equal(t, lookup.DefaultOnceDeadline, cfg.OnceDeadline())
79+
})
80+
}
81+
82+
func TestServiceConfig_OnceInterval(t *testing.T) {
83+
t.Run("returns value from configuration when > 0", func(t *testing.T) {
84+
m := &mock.Configuration{}
85+
expected := 5 * time.Second
86+
m.GetDurationReturns(expected)
87+
cfg := lookup.NewConfig(m)
88+
89+
assert.Equal(t, expected, cfg.OnceInterval())
90+
assert.Equal(t, 1, m.GetDurationCallCount())
91+
assert.Equal(t, lookup.OnceInterval, m.GetDurationArgsForCall(0))
92+
})
93+
94+
t.Run("returns default when configuration returns 0", func(t *testing.T) {
95+
m := &mock.Configuration{}
96+
m.GetDurationReturns(0)
97+
cfg := lookup.NewConfig(m)
98+
99+
assert.Equal(t, lookup.DefaultOnceInterval, cfg.OnceInterval())
100+
})
101+
102+
t.Run("returns default when configuration returns negative", func(t *testing.T) {
103+
m := &mock.Configuration{}
104+
m.GetDurationReturns(-1 * time.Second)
105+
cfg := lookup.NewConfig(m)
106+
107+
assert.Equal(t, lookup.DefaultOnceInterval, cfg.OnceInterval())
108+
})
109+
}

token/services/network/fabricx/lookup/lookup.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Listener = lookup.Listener
4949
type NSListenerManager struct {
5050
queue Queue
5151
queryService QueryService
52+
config ConfigGetter
5253

5354
mu sync.RWMutex
5455
listeners map[string][]Listener
@@ -58,10 +59,12 @@ type NSListenerManager struct {
5859
func NewNSListenerManager(
5960
queue Queue,
6061
qs QueryService,
62+
config ConfigGetter,
6163
) *NSListenerManager {
6264
return &NSListenerManager{
6365
queue: queue,
6466
queryService: qs,
67+
config: config,
6568
listeners: make(map[string][]Listener),
6669
}
6770
}
@@ -85,7 +88,7 @@ func (n *NSListenerManager) AddPermanentLookupListener(namespace string, key str
8588
Namespace: namespace,
8689
Key: key,
8790
Listener: listener,
88-
Interval: 1 * time.Minute,
91+
Interval: n.config.PermanentInterval(),
8992
})
9093
}
9194

@@ -106,8 +109,8 @@ func (n *NSListenerManager) AddLookupListener(namespace string, key string, list
106109
Key: key,
107110
Listener: l,
108111
Original: listener,
109-
Deadline: time.Now().Add(5 * time.Minute),
110-
Interval: 2 * time.Second,
112+
Deadline: time.Now().Add(n.config.OnceDeadline()),
113+
Interval: n.config.OnceInterval(),
111114
})
112115
}
113116

@@ -159,13 +162,15 @@ func (n *NSListenerManager) isRegistered(key string, listener Listener) bool {
159162
type NSListenerManagerProvider struct {
160163
QueryServiceProvider QueryServiceProvider
161164
queue Queue
165+
config ConfigGetter
162166
}
163167

164168
// NewQueryServiceBased creates a new NSListenerManagerProvider
165-
func NewQueryServiceBased(queryServiceProvider QueryServiceProvider, queue Queue) lookup.ListenerManagerProvider {
169+
func NewQueryServiceBased(queryServiceProvider QueryServiceProvider, queue Queue, config ConfigGetter) lookup.ListenerManagerProvider {
166170
return &NSListenerManagerProvider{
167171
QueryServiceProvider: queryServiceProvider,
168172
queue: queue,
173+
config: config,
169174
}
170175
}
171176

@@ -175,7 +180,7 @@ func (n *NSListenerManagerProvider) NewManager(network, channel string) (lookup.
175180
return nil, errors.Wrapf(err, "failed getting query service")
176181
}
177182

178-
return NewNSListenerManager(n.queue, qs), nil
183+
return NewNSListenerManager(n.queue, qs, n.config), nil
179184
}
180185

181186
// KeyCheck represents a key check event

token/services/network/fabricx/lookup/lookup_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
func TestNSListenerManager(t *testing.T) {
2424
q := &mock.Queue{}
2525
qs := &mock.QueryService{}
26-
mgr := lookup.NewNSListenerManager(q, qs)
26+
mgr := lookup.NewNSListenerManager(q, qs, lookup.NewConfig(&mock.Configuration{}))
2727

2828
assert.True(t, mgr.PermanentLookupListenerSupported())
2929

@@ -62,7 +62,7 @@ func TestKeyCheck_Process(t *testing.T) {
6262
q := &mock.Queue{}
6363
qs := &mock.QueryService{}
6464
qs.GetStateReturns(&driver2.VaultValue{Raw: []byte("value")}, nil)
65-
mgr := lookup.NewNSListenerManager(q, qs)
65+
mgr := lookup.NewNSListenerManager(q, qs, lookup.NewConfig(&mock.Configuration{}))
6666
l := &mock.Listener{}
6767
require.NoError(t, mgr.AddLookupListener("ns", "key", l))
6868

@@ -77,7 +77,7 @@ func TestKeyCheck_Process(t *testing.T) {
7777
t.Run("Removed", func(t *testing.T) {
7878
q := &mock.Queue{}
7979
qs := &mock.QueryService{}
80-
mgr := lookup.NewNSListenerManager(q, qs)
80+
mgr := lookup.NewNSListenerManager(q, qs, lookup.NewConfig(&mock.Configuration{}))
8181
l := &mock.Listener{}
8282
require.NoError(t, mgr.AddLookupListener("ns", "key", l))
8383

@@ -97,7 +97,7 @@ func TestKeyCheck_Process(t *testing.T) {
9797
q := &mock.Queue{}
9898
qs := &mock.QueryService{}
9999
qs.GetStateReturns(nil, nil)
100-
mgr := lookup.NewNSListenerManager(q, qs)
100+
mgr := lookup.NewNSListenerManager(q, qs, lookup.NewConfig(&mock.Configuration{}))
101101
l := &mock.Listener{}
102102
require.NoError(t, mgr.AddLookupListener("ns", "key", l))
103103

@@ -119,7 +119,7 @@ func TestKeyCheck_Process(t *testing.T) {
119119
q := &mock.Queue{}
120120
qs := &mock.QueryService{}
121121
qs.GetStateReturns(nil, nil)
122-
mgr := lookup.NewNSListenerManager(q, qs)
122+
mgr := lookup.NewNSListenerManager(q, qs, lookup.NewConfig(&mock.Configuration{}))
123123
l := &mock.Listener{}
124124
require.NoError(t, mgr.AddLookupListener("ns", "key", l))
125125

@@ -148,7 +148,7 @@ func TestPermanentKeyCheck_Process(t *testing.T) {
148148
// 3rd call: changed value
149149
qs.GetStateReturnsOnCall(2, &driver2.VaultValue{Raw: []byte("v2")}, nil)
150150

151-
mgr := lookup.NewNSListenerManager(q, qs)
151+
mgr := lookup.NewNSListenerManager(q, qs, lookup.NewConfig(&mock.Configuration{}))
152152
l := &mock.Listener{}
153153
require.NoError(t, mgr.AddPermanentLookupListener("ns", "key", l))
154154

@@ -179,7 +179,7 @@ func TestPermanentKeyCheck_Process(t *testing.T) {
179179
t.Run("Removed", func(t *testing.T) {
180180
q := &mock.Queue{}
181181
qs := &mock.QueryService{}
182-
mgr := lookup.NewNSListenerManager(q, qs)
182+
mgr := lookup.NewNSListenerManager(q, qs, lookup.NewConfig(&mock.Configuration{}))
183183
l := &mock.Listener{}
184184
require.NoError(t, mgr.AddPermanentLookupListener("ns", "key", l))
185185

@@ -220,7 +220,7 @@ func TestNSListenerManagerProvider(t *testing.T) {
220220
qsp := &mock.QueryServiceProvider{}
221221
qsp.GetReturns(qs, nil)
222222

223-
p := lookup.NewQueryServiceBased(qsp, q)
223+
p := lookup.NewQueryServiceBased(qsp, q, lookup.NewConfig(&mock.Configuration{}))
224224

225225
// Successful creation
226226
mgr, err := p.NewManager("network", "channel")

0 commit comments

Comments
 (0)