@@ -5,36 +5,47 @@ 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 "
14+ eth "github.com/ethereum/go-ethereum/common "
1515 "go.uber.org/zap"
1616)
1717
1818type operationKey [32 ]byte
1919
20+ func operationKeyFromHex (hex string ) operationKey {
21+ return operationKey (eth .HexToHash (hex ))
22+ }
23+
24+ type TimelockCallScheduled interface {
25+ Id () operationKey
26+ Index () int
27+ BlockNumber () * big.Int
28+ TxHash () string
29+ }
30+
2031type Scheduler interface {
2132 runScheduler (ctx context.Context ) <- chan struct {}
22- addToScheduler (op * contracts. RBACTimelockCallScheduled )
33+ addToScheduler (op TimelockCallScheduled )
2334 delFromScheduler (op operationKey )
2435 dumpOperationStore (now func () time.Time )
2536}
2637
27- type executeFn func (context.Context , []* contracts. RBACTimelockCallScheduled )
38+ type executeFn func (context.Context , []TimelockCallScheduled )
2839
2940// Scheduler represents a scheduler with an in memory store.
3041// Whenever accesing the map the mutex should be Locked, to prevent
3142// any race condition.
3243type scheduler struct {
3344 mu sync.Mutex
3445 ticker * time.Ticker
35- add chan * contracts. RBACTimelockCallScheduled
46+ add chan TimelockCallScheduled
3647 del chan operationKey
37- store map [operationKey ][]* contracts. RBACTimelockCallScheduled
48+ store map [operationKey ][]TimelockCallScheduled
3849 busy bool
3950 logger * zap.SugaredLogger
4051 executeFn executeFn
@@ -44,9 +55,9 @@ type scheduler struct {
4455func newScheduler (tick time.Duration , logger * zap.SugaredLogger , executeFn executeFn ) * scheduler {
4556 s := & scheduler {
4657 ticker : time .NewTicker (tick ),
47- add : make (chan * contracts. RBACTimelockCallScheduled ),
58+ add : make (chan TimelockCallScheduled ),
4859 del : make (chan operationKey ),
49- store : make (map [operationKey ][]* contracts. RBACTimelockCallScheduled ),
60+ store : make (map [operationKey ][]TimelockCallScheduled ),
5061 busy : false ,
5162 logger : logger ,
5263 executeFn : executeFn ,
@@ -88,12 +99,12 @@ func (tw *scheduler) runScheduler(ctx context.Context) <-chan struct{} {
8899
89100 case op := <- tw .add :
90101 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 )
102+ for len (tw .store [op .Id () ]) <= op .Index ( ) {
103+ tw .store [op .Id () ] = append (tw .store [op .Id () ], op )
93104 }
94- tw .store [op .Id ][op .Index . Int64 ()] = op
105+ tw .store [op .Id () ][op .Index ()] = op
95106 tw .mu .Unlock ()
96- tw .logger .Debugf ("scheduled operation: %x" , op .Id )
107+ tw .logger .Debugf ("scheduled operation: %x" , op .Id () )
97108
98109 case op := <- tw .del :
99110 if _ , ok := tw .store [op ]; ok {
@@ -125,8 +136,8 @@ func (tw *scheduler) updateSchedulerDelay(t time.Duration) {
125136}
126137
127138// 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 )
139+ func (tw * scheduler ) addToScheduler (op TimelockCallScheduled ) {
140+ tw .logger .Debugf ("scheduling operation: %x" , op .Id () )
130141 tw .add <- op
131142}
132143
@@ -171,11 +182,11 @@ func (tw *scheduler) dumpOperationStore(now func() time.Time) {
171182 tw .logger .Infof ("generating logs with pending operations in %s" , logPath + logFile )
172183
173184 // Get the earliest block from all the operations stored by sorting them.
174- blocks := make ([]uint64 , 0 )
185+ blocks := make ([]* big. Int , 0 )
175186 for _ , op := range tw .store {
176- blocks = append (blocks , op [0 ].Raw . BlockNumber )
187+ blocks = append (blocks , op [0 ].BlockNumber () )
177188 }
178- slices .Sort (blocks )
189+ slices .SortFunc (blocks , func ( a , b * big. Int ) int { return a . Cmp ( b ) } )
179190
180191 w := bufio .NewWriter (f )
181192
@@ -185,22 +196,22 @@ func (tw *scheduler) dumpOperationStore(now func() time.Time) {
185196}
186197
187198type storeRecord struct {
188- Block uint64
199+ Block * big. Int
189200 OpKey operationKey
190- Ops []* contracts. RBACTimelockCallScheduled
201+ Ops []TimelockCallScheduled
191202}
192203
193204// writeOperationStore writes the operations to the writer.
194205func writeOperationStore (
195206 w io.Writer ,
196207 logger * zap.SugaredLogger ,
197- store map [operationKey ][]* contracts. RBACTimelockCallScheduled ,
198- earliest uint64 ,
208+ store map [operationKey ][]TimelockCallScheduled ,
209+ earliest * big. Int ,
199210 now func () time.Time ,
200211) {
201212 var (
202213 err error
203- op * contracts. RBACTimelockCallScheduled
214+ op TimelockCallScheduled
204215 msg string
205216 )
206217
@@ -216,28 +227,24 @@ func writeOperationStore(
216227 continue
217228 }
218229 storeRecords = append (storeRecords , storeRecord {
219- Block : ops [0 ].Raw . BlockNumber ,
230+ Block : ops [0 ].BlockNumber () ,
220231 OpKey : opID ,
221232 Ops : ops ,
222233 })
223234 }
224- sort .Slice (storeRecords , func (i , j int ) bool {
225- return storeRecords [i ].Block < storeRecords [j ].Block
226- })
235+ slices .SortFunc (storeRecords , func (a , b storeRecord ) int { return a .Block .Cmp (b .Block ) })
227236
228237 for _ , record := range storeRecords {
229238 op = record .Ops [0 ]
230239
231- if op .Raw . BlockNumber == earliest {
240+ if op .BlockNumber (). Cmp ( earliest ) == 0 {
232241 logLine := fmt .Sprintf ("earliest unexecuted CallSchedule. Use this block number when " +
233242 "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 )
243+ "or using the flag --from-block=%v" , op .BlockNumber (), op .BlockNumber ())
244+ logger .With (fieldTXHash , op .TxHash ()).With (fieldBlockNumber , op .BlockNumber ()).Info (logLine )
237245 msg = toEarliestRecord (op )
238246 } else {
239- logger .With (fieldTXHash , fmt .Sprintf ("%x" , op .Raw .TxHash [:])).
240- With (fieldBlockNumber , op .Raw .BlockNumber ).Info ("CallSchedule pending" )
247+ logger .With (fieldTXHash , op .TxHash ()).With (fieldBlockNumber , op .BlockNumber ()).Info ("CallSchedule pending" )
241248 msg = toSubsequentRecord (op )
242249 }
243250
@@ -249,17 +256,17 @@ func writeOperationStore(
249256}
250257
251258// toEarliestRecord returns a string with the earliest record.
252- func toEarliestRecord (op * contracts. RBACTimelockCallScheduled ) string {
259+ func toEarliestRecord (op TimelockCallScheduled ) string {
253260 tmpl := "Earliest CallSchedule pending ID: %x\t Block Number: %v\n " +
254261 "\t Use this block number to ensure all pending operations are properly executed. " +
255262 "\t Set it as environment variable or in timelock.env with FROM_BLOCK=%v, or as a flag with --from-block=%v\n "
256263
257- return fmt .Sprintf (tmpl , op .Id , op .Raw . BlockNumber , op .Raw . BlockNumber , op .Raw . BlockNumber )
264+ return fmt .Sprintf (tmpl , op .Id () , op .BlockNumber () , op .BlockNumber () , op .BlockNumber () )
258265}
259266
260267// 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 )
268+ func toSubsequentRecord (op TimelockCallScheduled ) string {
269+ return fmt .Sprintf ("CallSchedule pending ID: %x\t Block Number: %v\n " , op .Id () , op .BlockNumber () )
263270}
264271
265272// ----- nop scheduler -----
@@ -284,7 +291,7 @@ func (s *nopScheduler) runScheduler(ctx context.Context) <-chan struct{} {
284291 return ch
285292}
286293
287- func (s * nopScheduler ) addToScheduler (op * contracts. RBACTimelockCallScheduled ) {
294+ func (s * nopScheduler ) addToScheduler (op TimelockCallScheduled ) {
288295 s .logger .With ("op" , op ).Info ("nop.addToScheduler" )
289296}
290297
0 commit comments