@@ -5,36 +5,42 @@ import (
55 "context"
66 "fmt"
77 "io"
8+ "math/big"
89 "os"
910 "slices"
10- "sort"
1111 "sync"
1212 "time"
1313
14- contracts "github.com/smartcontractkit/ccip-owner-contracts/gethwrappers"
1514 "go.uber.org/zap"
1615)
1716
1817type operationKey [32 ]byte
1918
19+ type TimelockCallScheduled interface {
20+ Id () operationKey
21+ Index () int
22+ BlockNumber () * big.Int
23+ TxHash () string
24+ }
25+
2026type Scheduler interface {
2127 runScheduler (ctx context.Context ) <- chan struct {}
22- addToScheduler (op * contracts. RBACTimelockCallScheduled )
28+ addToScheduler (op TimelockCallScheduled )
2329 delFromScheduler (op operationKey )
2430 dumpOperationStore (now func () time.Time )
2531}
2632
27- type executeFn func (context.Context , []* contracts. RBACTimelockCallScheduled )
33+ type executeFn func (context.Context , []TimelockCallScheduled )
2834
2935// Scheduler represents a scheduler with an in memory store.
3036// Whenever accesing the map the mutex should be Locked, to prevent
3137// any race condition.
3238type scheduler struct {
3339 mu sync.Mutex
3440 ticker * time.Ticker
35- add chan * contracts. RBACTimelockCallScheduled
41+ add chan TimelockCallScheduled
3642 del chan operationKey
37- store map [operationKey ][]* contracts. RBACTimelockCallScheduled
43+ store map [operationKey ][]TimelockCallScheduled
3844 busy bool
3945 logger * zap.SugaredLogger
4046 executeFn executeFn
@@ -44,9 +50,9 @@ type scheduler struct {
4450func newScheduler (tick time.Duration , logger * zap.SugaredLogger , executeFn executeFn ) * scheduler {
4551 s := & scheduler {
4652 ticker : time .NewTicker (tick ),
47- add : make (chan * contracts. RBACTimelockCallScheduled ),
53+ add : make (chan TimelockCallScheduled ),
4854 del : make (chan operationKey ),
49- store : make (map [operationKey ][]* contracts. RBACTimelockCallScheduled ),
55+ store : make (map [operationKey ][]TimelockCallScheduled ),
5056 busy : false ,
5157 logger : logger ,
5258 executeFn : executeFn ,
@@ -88,12 +94,12 @@ func (tw *scheduler) runScheduler(ctx context.Context) <-chan struct{} {
8894
8995 case op := <- tw .add :
9096 tw .mu .Lock ()
91- for len (tw .store [op .Id ]) <= int ( op .Index . Int64 () ) {
92- tw .store [op .Id ] = append (tw .store [op .Id ], op )
97+ for len (tw .store [op .Id () ]) <= op .Index ( ) {
98+ tw .store [op .Id () ] = append (tw .store [op .Id () ], op )
9399 }
94- tw .store [op .Id ][op .Index . Int64 ()] = op
100+ tw .store [op .Id () ][op .Index ()] = op
95101 tw .mu .Unlock ()
96- tw .logger .Debugf ("scheduled operation: %x" , op .Id )
102+ tw .logger .Debugf ("scheduled operation: %x" , op .Id () )
97103
98104 case op := <- tw .del :
99105 if _ , ok := tw .store [op ]; ok {
@@ -125,8 +131,8 @@ func (tw *scheduler) updateSchedulerDelay(t time.Duration) {
125131}
126132
127133// addToScheduler adds a new CallSchedule operation safely to the store.
128- func (tw * scheduler ) addToScheduler (op * contracts. RBACTimelockCallScheduled ) {
129- tw .logger .Debugf ("scheduling operation: %x" , op .Id )
134+ func (tw * scheduler ) addToScheduler (op TimelockCallScheduled ) {
135+ tw .logger .Debugf ("scheduling operation: %x" , op .Id () )
130136 tw .add <- op
131137}
132138
@@ -171,11 +177,11 @@ func (tw *scheduler) dumpOperationStore(now func() time.Time) {
171177 tw .logger .Infof ("generating logs with pending operations in %s" , logPath + logFile )
172178
173179 // Get the earliest block from all the operations stored by sorting them.
174- blocks := make ([]uint64 , 0 )
180+ blocks := make ([]* big. Int , 0 )
175181 for _ , op := range tw .store {
176- blocks = append (blocks , op [0 ].Raw . BlockNumber )
182+ blocks = append (blocks , op [0 ].BlockNumber () )
177183 }
178- slices .Sort (blocks )
184+ slices .SortFunc (blocks , func ( a , b * big. Int ) int { return a . Cmp ( b ) } )
179185
180186 w := bufio .NewWriter (f )
181187
@@ -185,22 +191,22 @@ func (tw *scheduler) dumpOperationStore(now func() time.Time) {
185191}
186192
187193type storeRecord struct {
188- Block uint64
194+ Block * big. Int
189195 OpKey operationKey
190- Ops []* contracts. RBACTimelockCallScheduled
196+ Ops []TimelockCallScheduled
191197}
192198
193199// writeOperationStore writes the operations to the writer.
194200func writeOperationStore (
195201 w io.Writer ,
196202 logger * zap.SugaredLogger ,
197- store map [operationKey ][]* contracts. RBACTimelockCallScheduled ,
198- earliest uint64 ,
203+ store map [operationKey ][]TimelockCallScheduled ,
204+ earliest * big. Int ,
199205 now func () time.Time ,
200206) {
201207 var (
202208 err error
203- op * contracts. RBACTimelockCallScheduled
209+ op TimelockCallScheduled
204210 msg string
205211 )
206212
@@ -216,28 +222,24 @@ func writeOperationStore(
216222 continue
217223 }
218224 storeRecords = append (storeRecords , storeRecord {
219- Block : ops [0 ].Raw . BlockNumber ,
225+ Block : ops [0 ].BlockNumber () ,
220226 OpKey : opID ,
221227 Ops : ops ,
222228 })
223229 }
224- sort .Slice (storeRecords , func (i , j int ) bool {
225- return storeRecords [i ].Block < storeRecords [j ].Block
226- })
230+ slices .SortFunc (storeRecords , func (a , b storeRecord ) int { return a .Block .Cmp (b .Block ) })
227231
228232 for _ , record := range storeRecords {
229233 op = record .Ops [0 ]
230234
231- if op .Raw . BlockNumber == earliest {
235+ if op .BlockNumber (). Cmp ( earliest ) == 0 {
232236 logLine := fmt .Sprintf ("earliest unexecuted CallSchedule. Use this block number when " +
233237 "spinning up the service again, with the environment variable or in timelock.env as FROM_BLOCK=%v, " +
234- "or using the flag --from-block=%v" , op .Raw .BlockNumber , op .Raw .BlockNumber )
235- logger .With (fieldTXHash , fmt .Sprintf ("%x" , op .Raw .TxHash [:])).
236- With (fieldBlockNumber , op .Raw .BlockNumber ).Info (logLine )
238+ "or using the flag --from-block=%v" , op .BlockNumber (), op .BlockNumber ())
239+ logger .With (fieldTXHash , op .TxHash ()).With (fieldBlockNumber , op .BlockNumber ()).Info (logLine )
237240 msg = toEarliestRecord (op )
238241 } else {
239- logger .With (fieldTXHash , fmt .Sprintf ("%x" , op .Raw .TxHash [:])).
240- With (fieldBlockNumber , op .Raw .BlockNumber ).Info ("CallSchedule pending" )
242+ logger .With (fieldTXHash , op .TxHash ()).With (fieldBlockNumber , op .BlockNumber ()).Info ("CallSchedule pending" )
241243 msg = toSubsequentRecord (op )
242244 }
243245
@@ -249,17 +251,17 @@ func writeOperationStore(
249251}
250252
251253// toEarliestRecord returns a string with the earliest record.
252- func toEarliestRecord (op * contracts. RBACTimelockCallScheduled ) string {
254+ func toEarliestRecord (op TimelockCallScheduled ) string {
253255 tmpl := "Earliest CallSchedule pending ID: %x\t Block Number: %v\n " +
254256 "\t Use this block number to ensure all pending operations are properly executed. " +
255257 "\t Set it as environment variable or in timelock.env with FROM_BLOCK=%v, or as a flag with --from-block=%v\n "
256258
257- return fmt .Sprintf (tmpl , op .Id , op .Raw . BlockNumber , op .Raw . BlockNumber , op .Raw . BlockNumber )
259+ return fmt .Sprintf (tmpl , op .Id () , op .BlockNumber () , op .BlockNumber () , op .BlockNumber () )
258260}
259261
260262// toSubsequentRecord returns a string for use with each subsequent record sent to a writer.
261- func toSubsequentRecord (op * contracts. RBACTimelockCallScheduled ) string {
262- return fmt .Sprintf ("CallSchedule pending ID: %x\t Block Number: %v\n " , op .Id , op .Raw . BlockNumber )
263+ func toSubsequentRecord (op TimelockCallScheduled ) string {
264+ return fmt .Sprintf ("CallSchedule pending ID: %x\t Block Number: %v\n " , op .Id () , op .BlockNumber () )
263265}
264266
265267// ----- nop scheduler -----
@@ -284,7 +286,7 @@ func (s *nopScheduler) runScheduler(ctx context.Context) <-chan struct{} {
284286 return ch
285287}
286288
287- func (s * nopScheduler ) addToScheduler (op * contracts. RBACTimelockCallScheduled ) {
289+ func (s * nopScheduler ) addToScheduler (op TimelockCallScheduled ) {
288290 s .logger .With ("op" , op ).Info ("nop.addToScheduler" )
289291}
290292
0 commit comments