Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 6880d84

Browse files
committed
[FAB-9170] Define one WithBlockEvents option
The WithBlockEvents options in deliverclient and eventhubclient were deleted and a new one was added to the abstract client, which both deliver and eventhub use. Change-Id: I18e85a90e07e7ae102ff7a14b58cbeb5735dc558 Signed-off-by: Bob Stasyszyn <Bob.Stasyszyn@securekey.com>
1 parent 84bff1a commit 6880d84

File tree

10 files changed

+93
-85
lines changed

10 files changed

+93
-85
lines changed

pkg/fab/events/client/client.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,25 @@ type Client struct {
4141
eventservice.Service
4242
params
4343
sync.RWMutex
44-
connEvent chan *dispatcher.ConnectionEvent
45-
connectionState int32
46-
stopped int32
47-
registerOnce sync.Once
48-
permitBlockEvents bool
49-
afterConnect handler
50-
beforeReconnect handler
44+
connEvent chan *dispatcher.ConnectionEvent
45+
connectionState int32
46+
stopped int32
47+
registerOnce sync.Once
48+
afterConnect handler
49+
beforeReconnect handler
5150
}
5251

5352
type handler func() error
5453

5554
// New returns a new event client
56-
func New(permitBlockEvents bool, dispatcher eventservice.Dispatcher, opts ...options.Opt) *Client {
55+
func New(dispatcher eventservice.Dispatcher, opts ...options.Opt) *Client {
5756
params := defaultParams()
5857
options.Apply(params, opts)
5958

6059
return &Client{
61-
Service: *eventservice.New(dispatcher, opts...),
62-
params: *params,
63-
connectionState: int32(Disconnected),
64-
permitBlockEvents: permitBlockEvents,
60+
Service: *eventservice.New(dispatcher, opts...),
61+
params: *params,
62+
connectionState: int32(Disconnected),
6563
}
6664
}
6765

pkg/fab/events/client/client_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,8 @@ func listenEvents(blockch <-chan *fab.BlockEvent, ccch <-chan *fab.CCEvent, wait
12261226
type ClientProvider func(context context.Client, chConfig fab.ChannelCfg, connectionProvider api.ConnectionProvider, opts []options.Opt) (*Client, error)
12271227

12281228
var clientProvider = func(context context.Client, chConfig fab.ChannelCfg, connectionProvider api.ConnectionProvider, opts []options.Opt) (*Client, error) {
1229-
return newClient(context, chConfig, connectionProvider, opts, true,
1229+
opts = append(opts, WithBlockEvents())
1230+
return newClient(context, chConfig, connectionProvider, opts,
12301231
func() error {
12311232
fmt.Printf("AfterConnect called")
12321233
return nil
@@ -1238,15 +1239,16 @@ var clientProvider = func(context context.Client, chConfig fab.ChannelCfg, conne
12381239
}
12391240

12401241
var failAfterConnectClientProvider = func(context context.Client, chConfig fab.ChannelCfg, connectionProvider api.ConnectionProvider, opts []options.Opt) (*Client, error) {
1241-
return newClient(context, chConfig, connectionProvider, opts, true,
1242+
opts = append(opts, WithBlockEvents())
1243+
return newClient(context, chConfig, connectionProvider, opts,
12421244
func() error {
12431245
return errors.New("simulated failure after connect")
12441246
},
12451247
nil)
12461248
}
12471249

12481250
var filteredClientProvider = func(context context.Client, chConfig fab.ChannelCfg, connectionProvider api.ConnectionProvider, opts []options.Opt) (*Client, error) {
1249-
return newClient(context, chConfig, connectionProvider, opts, false,
1251+
return newClient(context, chConfig, connectionProvider, opts,
12501252
func() error {
12511253
fmt.Printf("AfterConnect called")
12521254
return nil
@@ -1257,9 +1259,8 @@ var filteredClientProvider = func(context context.Client, chConfig fab.ChannelCf
12571259
})
12581260
}
12591261

1260-
func newClient(context context.Client, chConfig fab.ChannelCfg, connectionProvider api.ConnectionProvider, opts []options.Opt, permitBlockEvents bool, afterConnect handler, beforeReconnect handler) (*Client, error) {
1262+
func newClient(context context.Client, chConfig fab.ChannelCfg, connectionProvider api.ConnectionProvider, opts []options.Opt, afterConnect handler, beforeReconnect handler) (*Client, error) {
12611263
client := New(
1262-
permitBlockEvents,
12631264
dispatcher.New(
12641265
context, chConfig,
12651266
connectionProvider,

pkg/fab/events/client/opts.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type params struct {
2222
timeBetweenConnAttempts time.Duration
2323
connEventCh chan *dispatcher.ConnectionEvent
2424
respTimeout time.Duration
25+
permitBlockEvents bool
2526
}
2627

2728
func defaultParams() *params {
@@ -36,6 +37,16 @@ func defaultParams() *params {
3637
}
3738
}
3839

40+
// WithBlockEvents indicates that block events are to be received.
41+
// Note that the caller must have sufficient privileges for this option.
42+
func WithBlockEvents() options.Opt {
43+
return func(p options.Params) {
44+
if setter, ok := p.(permitBlockEventsSetter); ok {
45+
setter.PermitBlockEvents()
46+
}
47+
}
48+
}
49+
3950
// WithReconnect indicates whether the client should automatically attempt to reconnect
4051
// to the server after a connection has been lost
4152
func WithReconnect(value bool) options.Opt {
@@ -143,6 +154,11 @@ func (p *params) SetResponseTimeout(value time.Duration) {
143154
p.respTimeout = value
144155
}
145156

157+
func (p *params) PermitBlockEvents() {
158+
logger.Debugf("PermitBlockEvents")
159+
p.permitBlockEvents = true
160+
}
161+
146162
type reconnectSetter interface {
147163
SetReconnect(value bool)
148164
}
@@ -170,3 +186,7 @@ type timeBetweenConnectAttemptsSetter interface {
170186
type responseTimeoutSetter interface {
171187
SetResponseTimeout(value time.Duration)
172188
}
189+
190+
type permitBlockEventsSetter interface {
191+
PermitBlockEvents()
192+
}

pkg/fab/events/deliverclient/deliverclient.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ func New(context fabcontext.Client, chConfig fab.ChannelCfg, opts ...options.Opt
6161

6262
client := &Client{
6363
Client: *client.New(
64-
params.permitBlockEvents,
6564
dispatcher.New(deliverCtx, chConfig, params.connProvider, opts...),
6665
opts...,
6766
),

pkg/fab/events/deliverclient/deliverclient_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestOptionsInNewClient(t *testing.T) {
4646
client, err := New(
4747
newMockContext(),
4848
fabmocks.NewMockChannelCfg(channelID),
49-
WithBlockEvents(),
49+
client.WithBlockEvents(),
5050
)
5151
if err != nil {
5252
t.Fatalf("error creating deliver client: %s", err)
@@ -59,13 +59,13 @@ func TestClientConnect(t *testing.T) {
5959
eventClient, err := New(
6060
newMockContext(),
6161
fabmocks.NewMockChannelCfg(channelID),
62+
client.WithBlockEvents(),
6263
withConnectionProvider(
6364
clientmocks.NewProviderFactory().Provider(
6465
delivermocks.NewConnection(
6566
clientmocks.WithLedger(servicemocks.NewMockLedger(delivermocks.BlockEventFactory, sourceURL)),
6667
),
6768
),
68-
true,
6969
),
7070
WithSeekType(seek.FromBlock),
7171
WithBlockNum(0),
@@ -187,6 +187,7 @@ func testConnect(t *testing.T, maxConnectAttempts uint, expectedOutcome clientmo
187187
eventClient, err := New(
188188
newMockContext(),
189189
fabmocks.NewMockChannelCfg(channelID),
190+
client.WithBlockEvents(),
190191
withConnectionProvider(
191192
cp.FlakeyProvider(
192193
connAttemptResult,
@@ -195,7 +196,6 @@ func testConnect(t *testing.T, maxConnectAttempts uint, expectedOutcome clientmo
195196
return delivermocks.NewConnection(opts...)
196197
}),
197198
),
198-
true,
199199
),
200200
esdispatcher.WithEventConsumerTimeout(time.Second),
201201
client.WithMaxConnectAttempts(maxConnectAttempts),
@@ -227,6 +227,7 @@ func testReconnect(t *testing.T, reconnect bool, maxReconnectAttempts uint, expe
227227
eventClient, err := New(
228228
newMockContext(),
229229
fabmocks.NewMockChannelCfg(channelID),
230+
client.WithBlockEvents(),
230231
withConnectionProvider(
231232
cp.FlakeyProvider(
232233
connAttemptResult,
@@ -235,7 +236,6 @@ func testReconnect(t *testing.T, reconnect bool, maxReconnectAttempts uint, expe
235236
return delivermocks.NewConnection(opts...)
236237
}),
237238
),
238-
true,
239239
),
240240
esdispatcher.WithEventConsumerTimeout(3*time.Second),
241241
client.WithReconnect(reconnect),
@@ -297,6 +297,7 @@ func testReconnectRegistration(t *testing.T, connectResults clientmocks.ConnectA
297297
eventClient, err := New(
298298
newMockContext(),
299299
fabmocks.NewMockChannelCfg(channelID),
300+
client.WithBlockEvents(),
300301
withConnectionProvider(
301302
cp.FlakeyProvider(
302303
connectResults,
@@ -305,7 +306,6 @@ func testReconnectRegistration(t *testing.T, connectResults clientmocks.ConnectA
305306
return delivermocks.NewConnection(opts...)
306307
}),
307308
),
308-
true,
309309
),
310310
esdispatcher.WithEventConsumerTimeout(3*time.Second),
311311
client.WithReconnect(true),

pkg/fab/events/deliverclient/opts.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import (
1515
)
1616

1717
type params struct {
18-
connProvider api.ConnectionProvider
19-
permitBlockEvents bool
20-
seekType seek.Type
21-
fromBlock uint64
22-
respTimeout time.Duration
18+
connProvider api.ConnectionProvider
19+
seekType seek.Type
20+
fromBlock uint64
21+
respTimeout time.Duration
2322
}
2423

2524
func defaultParams() *params {
@@ -30,16 +29,6 @@ func defaultParams() *params {
3029
}
3130
}
3231

33-
// WithBlockEvents indicates that block events are to be received.
34-
// Note that the caller must have sufficient privileges for this option.
35-
func WithBlockEvents() options.Opt {
36-
return func(p options.Params) {
37-
if setter, ok := p.(connectionProviderSetter); ok {
38-
setter.SetConnectionProvider(deliverProvider, true)
39-
}
40-
}
41-
}
42-
4332
// WithSeekType specifies the point from which block events are to be received.
4433
func WithSeekType(value seek.Type) options.Opt {
4534
return func(p options.Params) {
@@ -60,16 +49,17 @@ func WithBlockNum(value uint64) options.Opt {
6049
}
6150

6251
// withConnectionProvider is used only for testing
63-
func withConnectionProvider(connProvider api.ConnectionProvider, permitBlockEvents bool) options.Opt {
52+
func withConnectionProvider(connProvider api.ConnectionProvider) options.Opt {
6453
return func(p options.Params) {
6554
if setter, ok := p.(connectionProviderSetter); ok {
66-
setter.SetConnectionProvider(connProvider, permitBlockEvents)
55+
setter.SetConnectionProvider(connProvider)
6756
}
6857
}
6958
}
7059

60+
// connectionProviderSetter is only used in unit tests
7161
type connectionProviderSetter interface {
72-
SetConnectionProvider(value api.ConnectionProvider, permitBlockEvents bool)
62+
SetConnectionProvider(value api.ConnectionProvider)
7363
}
7464

7565
type seekTypeSetter interface {
@@ -80,10 +70,15 @@ type fromBlockSetter interface {
8070
SetFromBlock(value uint64)
8171
}
8272

83-
func (p *params) SetConnectionProvider(connProvider api.ConnectionProvider, permitBlockEvents bool) {
84-
logger.Debugf("ConnectionProvider: %#v, PermitBlockEvents: %t", connProvider, permitBlockEvents)
73+
func (p *params) PermitBlockEvents() {
74+
logger.Debugf("PermitBlockEvents")
75+
p.connProvider = deliverProvider
76+
}
77+
78+
// SetConnectionProvider is only used in unit tests
79+
func (p *params) SetConnectionProvider(connProvider api.ConnectionProvider) {
80+
logger.Debugf("ConnectionProvider: %#v", connProvider)
8581
p.connProvider = connProvider
86-
p.permitBlockEvents = permitBlockEvents
8782
}
8883

8984
func (p *params) SetFromBlock(value uint64) {

pkg/fab/events/eventhubclient/eventhubclient.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ type Client struct {
4242
// New returns a new event hub client
4343
func New(context context.Client, chConfig fab.ChannelCfg, opts ...options.Opt) (*Client, error) {
4444
params := defaultParams()
45+
46+
// FIXME: Temporarily set the default to block events since Fabric 1.0 does
47+
// not support filtered block events
48+
opts = append(opts, client.WithBlockEvents())
49+
4550
options.Apply(params, opts)
4651

4752
// Use a context that returns a custom Discovery Provider which
@@ -51,7 +56,6 @@ func New(context context.Client, chConfig fab.ChannelCfg, opts ...options.Opt) (
5156

5257
client := &Client{
5358
Client: *client.New(
54-
params.permitBlockEvents,
5559
dispatcher.New(ehCtx, chConfig, params.connProvider, opts...),
5660
opts...,
5761
),

pkg/fab/events/eventhubclient/eventhubclient_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestOptionsInNewClient(t *testing.T) {
4949
client, err := New(
5050
newMockContext(),
5151
fabmocks.NewMockChannelCfg(channelID),
52-
WithBlockEvents(),
52+
client.WithBlockEvents(),
5353
)
5454
if err != nil {
5555
t.Fatalf("error creating new event hub client: %s", err)
@@ -62,12 +62,11 @@ func TestClientConnect(t *testing.T) {
6262
eventClient, err := New(
6363
newMockContext(),
6464
fabmocks.NewMockChannelCfg(channelID),
65-
withConnectionProviderAndInterests(
65+
withConnectionProvider(
6666
clientmocks.NewProviderFactory().Provider(
6767
ehclientmocks.NewConnection(
6868
clientmocks.WithLedger(servicemocks.NewMockLedger(ehmocks.BlockEventFactory, sourceURL)),
6969
)),
70-
filteredBlockInterests, false,
7170
),
7271
)
7372
if err != nil {
@@ -95,15 +94,14 @@ func TestTimeoutClientConnect(t *testing.T) {
9594
eventClient, err := New(
9695
newMockContext(),
9796
fabmocks.NewMockChannelCfg(channelID),
98-
withConnectionProviderAndInterests(
97+
withConnectionProvider(
9998
clientmocks.NewProviderFactory().Provider(
10099
ehclientmocks.NewConnection(
101100
clientmocks.WithLedger(servicemocks.NewMockLedger(ehmocks.BlockEventFactory, sourceURL)),
102101
clientmocks.WithResults(
103102
clientmocks.NewResult(ehmocks.RegInterests, clientmocks.NoOpResult),
104103
),
105104
)),
106-
filteredBlockInterests, false,
107105
),
108106
)
109107
if err != nil {
@@ -209,15 +207,15 @@ func testConnect(t *testing.T, maxConnectAttempts uint, expectedOutcome clientmo
209207
eventClient, err := New(
210208
newMockContext(),
211209
fabmocks.NewMockChannelCfg(channelID),
212-
withConnectionProviderAndInterests(
210+
client.WithBlockEvents(),
211+
withConnectionProvider(
213212
clientmocks.NewProviderFactory().FlakeyProvider(
214213
connAttemptResult,
215214
clientmocks.WithLedger(servicemocks.NewMockLedger(ehmocks.BlockEventFactory, sourceURL)),
216215
clientmocks.WithFactory(func(opts ...clientmocks.Opt) clientmocks.Connection {
217216
return ehclientmocks.NewConnection(opts...)
218217
}),
219218
),
220-
blockInterests, true,
221219
),
222220
esdispatcher.WithEventConsumerTimeout(time.Second),
223221
client.WithMaxConnectAttempts(maxConnectAttempts),
@@ -249,15 +247,15 @@ func testReconnect(t *testing.T, reconnect bool, maxReconnectAttempts uint, expe
249247
eventClient, err := New(
250248
newMockContext(),
251249
fabmocks.NewMockChannelCfg(channelID),
252-
withConnectionProviderAndInterests(
250+
client.WithBlockEvents(),
251+
withConnectionProvider(
253252
cp.FlakeyProvider(
254253
connAttemptResult,
255254
clientmocks.WithLedger(ledger),
256255
clientmocks.WithFactory(func(opts ...clientmocks.Opt) clientmocks.Connection {
257256
return ehclientmocks.NewConnection(opts...)
258257
}),
259258
),
260-
blockInterests, true,
261259
),
262260
esdispatcher.WithEventConsumerTimeout(3*time.Second),
263261
client.WithReconnect(reconnect),
@@ -308,15 +306,15 @@ func testReconnectRegistration(t *testing.T, expectedBlockEvents clientmocks.Num
308306
eventClient, err := New(
309307
newMockContext(),
310308
fabmocks.NewMockChannelCfg(channelID),
311-
withConnectionProviderAndInterests(
309+
client.WithBlockEvents(),
310+
withConnectionProvider(
312311
cp.FlakeyProvider(
313312
connectResults,
314313
clientmocks.WithLedger(ledger),
315314
clientmocks.WithFactory(func(opts ...clientmocks.Opt) clientmocks.Connection {
316315
return ehclientmocks.NewConnection(opts...)
317316
}),
318317
),
319-
blockInterests, true,
320318
),
321319
esdispatcher.WithEventConsumerTimeout(3*time.Second),
322320
client.WithReconnectInitialDelay(0),

0 commit comments

Comments
 (0)