@@ -39,132 +39,119 @@ type options struct {
3939
4040func (o * options ) verify () error {
4141 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" )
4443 }
4544 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" )
4846 }
4947 // TODO: check for the existence of the contract addresses depending on
5048 // what handlers are not nil
5149 return nil
5250}
5351
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
7954 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- }
8955
90- c .KeyperSetManager , err = bindings .NewKeyperSetManager (* o .keyperSetManagerAddress , client )
56+ c .KeyperSetManager , err = bindings .NewKeyperSetManager (* o .keyperSetManagerAddress , o . ethClient )
9157 if err != nil {
9258 return err
9359 }
9460 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 ,
10265 }
10366 if o .handlerKeyperSet != nil {
104- c .services = append (c .services , c .kssync )
10567 syncedServices = append (syncedServices , c .kssync )
10668 }
10769
108- c .KeyBroadcast , err = bindings .NewKeyBroadcastContract (* o .keyBroadcastContractAddress , client )
70+ c .KeyBroadcast , err = bindings .NewKeyBroadcastContract (* o .keyBroadcastContractAddress , o . ethClient )
10971 if err != nil {
11072 return err
11173 }
11274 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 ,
12180 }
12281 if o .handlerEonPublicKey != nil {
123- c .services = append (c .services , c .epksync )
12482 syncedServices = append (syncedServices , c .epksync )
12583 }
126-
12784 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 ,
13589 }
13690 if o .handlerShutterState != nil {
137- c .services = append (c .services , c .sssync )
13891 syncedServices = append (syncedServices , c .sssync )
13992 }
14093
14194 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
150100 o .handlerBlock = func (ctx context.Context , lb * event.LatestBlock ) error {
151101 return nil
152102 }
153103 }
154104
155105 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 ,
160112 }
161113 if o .handlerBlock != nil {
162114 c .services = append (c .services , c .uhsync )
163115 }
164- c .privKey = o .privKey
165116 return nil
166117}
167118
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+
168155func defaultOptions () * options {
169156 return & options {
170157 keyperSetManagerAddress : & predeploy .KeyperSetManagerAddr ,
0 commit comments