@@ -39,132 +39,119 @@ type options struct {
39
39
40
40
func (o * options ) verify () error {
41
41
if o .clientURL != "" && o .ethClient != nil {
42
- // TODO: error message
43
- return errors .New ("can't use client and client url" )
42
+ return errors .New ("'WithClient' and 'WithClientURL' options are mutually exclusive" )
44
43
}
45
44
if o .clientURL == "" && o .ethClient == nil {
46
- // TODO: error message
47
- return errors .New ("have to provide either url or client" )
45
+ return errors .New ("either 'WithClient' or 'WithClientURL' options are expected" )
48
46
}
49
47
// TODO: check for the existence of the contract addresses depending on
50
48
// what handlers are not nil
51
49
return nil
52
50
}
53
51
54
- // initialize the shutter client and apply the options.
55
- // the context is only the initialisation context,
56
- // and should not be considered to handle the lifecycle
57
- // of shutter clients background workers.
58
- func (o * options ) apply (ctx context.Context , c * Client ) error {
59
- var (
60
- client syncclient.EthereumClient
61
- err error
62
- )
63
- if o .clientURL != "" {
64
- o .ethClient , err = ethclient .DialContext (ctx , o .clientURL )
65
- if err != nil {
66
- return err
67
- }
68
- }
69
- client = o .ethClient
70
-
71
- c .EthereumClient = client
72
-
73
- if o .logger != nil {
74
- c .log = o .logger
75
- // NOCHECKIN:
76
- c .log .Info ("got logger in options" )
77
- }
78
-
52
+ func (o * options ) applyHandler (c * Client ) error {
53
+ var err error
79
54
syncedServices := []syncer.ManualFilterHandler {}
80
- // the nil passthrough will use "latest" for each call,
81
- // but we want to harmonize and fix the sync start to a specific block.
82
- if o .syncStart .IsLatest () {
83
- latestBlock , err := c .EthereumClient .BlockNumber (ctx )
84
- if err != nil {
85
- return errors .Wrap (err , "polling latest block" )
86
- }
87
- o .syncStart = number .NewBlockNumber (& latestBlock )
88
- }
89
55
90
- c .KeyperSetManager , err = bindings .NewKeyperSetManager (* o .keyperSetManagerAddress , client )
56
+ c .KeyperSetManager , err = bindings .NewKeyperSetManager (* o .keyperSetManagerAddress , o . ethClient )
91
57
if err != nil {
92
58
return err
93
59
}
94
60
c .kssync = & syncer.KeyperSetSyncer {
95
- Client : client ,
96
- Contract : c .KeyperSetManager ,
97
- Log : c .log ,
98
- StartBlock : o .syncStart ,
99
- Handler : o .handlerKeyperSet ,
100
- FetchActiveAtStartBlock : o .fetchActivesAtSyncStart ,
101
- DisableEventWatcher : true ,
61
+ Client : o .ethClient ,
62
+ Contract : c .KeyperSetManager ,
63
+ Log : c .log ,
64
+ Handler : o .handlerKeyperSet ,
102
65
}
103
66
if o .handlerKeyperSet != nil {
104
- c .services = append (c .services , c .kssync )
105
67
syncedServices = append (syncedServices , c .kssync )
106
68
}
107
69
108
- c .KeyBroadcast , err = bindings .NewKeyBroadcastContract (* o .keyBroadcastContractAddress , client )
70
+ c .KeyBroadcast , err = bindings .NewKeyBroadcastContract (* o .keyBroadcastContractAddress , o . ethClient )
109
71
if err != nil {
110
72
return err
111
73
}
112
74
c .epksync = & syncer.EonPubKeySyncer {
113
- Client : client ,
114
- Log : c .log ,
115
- KeyBroadcast : c .KeyBroadcast ,
116
- KeyperSetManager : c .KeyperSetManager ,
117
- Handler : o .handlerEonPublicKey ,
118
- StartBlock : o .syncStart ,
119
- FetchActiveAtStartBlock : o .fetchActivesAtSyncStart ,
120
- DisableEventWatcher : true ,
75
+ Client : o .ethClient ,
76
+ Log : c .log ,
77
+ KeyBroadcast : c .KeyBroadcast ,
78
+ KeyperSetManager : c .KeyperSetManager ,
79
+ Handler : o .handlerEonPublicKey ,
121
80
}
122
81
if o .handlerEonPublicKey != nil {
123
- c .services = append (c .services , c .epksync )
124
82
syncedServices = append (syncedServices , c .epksync )
125
83
}
126
-
127
84
c .sssync = & syncer.ShutterStateSyncer {
128
- Client : client ,
129
- Contract : c .KeyperSetManager ,
130
- Log : c .log ,
131
- Handler : o .handlerShutterState ,
132
- StartBlock : o .syncStart ,
133
- FetchActiveAtStartBlock : o .fetchActivesAtSyncStart ,
134
- DisableEventWatcher : true ,
85
+ Client : o .ethClient ,
86
+ Contract : c .KeyperSetManager ,
87
+ Log : c .log ,
88
+ Handler : o .handlerShutterState ,
135
89
}
136
90
if o .handlerShutterState != nil {
137
- c .services = append (c .services , c .sssync )
138
91
syncedServices = append (syncedServices , c .sssync )
139
92
}
140
93
141
94
if o .handlerBlock == nil {
142
- // NOOP - but we need to run the UnsafeHeadSyncer.
143
- // This is to keep the inner workings consisten,
144
- // we use the DisableEventWatcher mechanism in combination
145
- // with the UnsafeHeadSyncer instead of the streaming
146
- // Watch... subscription on events.
147
- // TODO: think about allowing the streaming events,
148
- // when guaranteed event order (event1,event2,new-block-event)
149
- // is not required
95
+ // Even if the user is not interested in handling new block events,
96
+ // the streaming block handler must be running in order to
97
+ // synchronize polling of new contract events.
98
+ // Since the handler function is always called, we need to
99
+ // inject a noop-handler
150
100
o .handlerBlock = func (ctx context.Context , lb * event.LatestBlock ) error {
151
101
return nil
152
102
}
153
103
}
154
104
155
105
c .uhsync = & syncer.UnsafeHeadSyncer {
156
- Client : client ,
157
- Log : c .log ,
158
- Handler : o .handlerBlock ,
159
- SyncedHandler : syncedServices ,
106
+ Client : o .ethClient ,
107
+ Log : c .log ,
108
+ Handler : o .handlerBlock ,
109
+ SyncedHandler : syncedServices ,
110
+ FetchActiveAtStart : o .fetchActivesAtSyncStart ,
111
+ SyncStartBlock : o .syncStart ,
160
112
}
161
113
if o .handlerBlock != nil {
162
114
c .services = append (c .services , c .uhsync )
163
115
}
164
- c .privKey = o .privKey
165
116
return nil
166
117
}
167
118
119
+ // initialize the shutter client and apply the options.
120
+ // the context is only the initialisation context,
121
+ // and should not be considered to handle the lifecycle
122
+ // of shutter clients background workers.
123
+ func (o * options ) apply (ctx context.Context , c * Client ) error {
124
+ var (
125
+ client syncclient.EthereumClient
126
+ err error
127
+ )
128
+ if o .clientURL != "" {
129
+ o .ethClient , err = ethclient .DialContext (ctx , o .clientURL )
130
+ if err != nil {
131
+ return err
132
+ }
133
+ }
134
+ client = o .ethClient
135
+ c .EthereumClient = client
136
+
137
+ // the nil passthrough will use "latest" for each call,
138
+ // but we want to harmonize and fix the sync start to a specific block.
139
+ if o .syncStart .IsLatest () {
140
+ latestBlock , err := c .EthereumClient .BlockNumber (ctx )
141
+ if err != nil {
142
+ return errors .Wrap (err , "polling latest block" )
143
+ }
144
+ o .syncStart = number .NewBlockNumber (& latestBlock )
145
+ }
146
+
147
+ if o .logger != nil {
148
+ c .log = o .logger
149
+ }
150
+
151
+ c .privKey = o .privKey
152
+ return o .applyHandler (c )
153
+ }
154
+
168
155
func defaultOptions () * options {
169
156
return & options {
170
157
keyperSetManagerAddress : & predeploy .KeyperSetManagerAddr ,
0 commit comments