Skip to content

Commit 00b6948

Browse files
committed
refactor removeID to handle leftover IDs with wrong timestamp
1 parent a86879f commit 00b6948

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ coverage.lcov
1919
lcov.info
2020
*.pkey
2121
imports*
22+
.cursorignore
2223

2324
imports*
2425

2526
# Private flow.jsons
2627

2728
private.flow.json
29+
30+
my-test-account.pkey

contracts/FlowTransactionSchedulerUtils.cdc

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -288,24 +288,24 @@ access(all) contract FlowTransactionSchedulerUtils {
288288
access(self) fun removeID(id: UInt64, timestamp: UFix64, handlerTypeIdentifier: String) {
289289
pre {
290290
self.handlerInfos.containsKey(handlerTypeIdentifier): "Invalid handler type identifier: Handler with type identifier \(handlerTypeIdentifier) not found in manager"
291-
self.idsByTimestamp.containsKey(timestamp): "Invalid timestamp: Timestamp \(timestamp) not found in manager for transaction with ID \(id)"
292291
}
293292

294-
let ids = &self.idsByTimestamp[timestamp]! as auth(Mutate) &[UInt64]
295-
let index = ids.firstIndex(of: id)
296-
ids.remove(at: index!)
297-
if ids.length == 0 {
298-
self.idsByTimestamp.remove(key: timestamp)
299-
self.sortedTimestamps.remove(timestamp: timestamp)
293+
if self.idsByTimestamp.containsKey(timestamp) {
294+
let ids = &self.idsByTimestamp[timestamp]! as auth(Mutate) &[UInt64]
295+
let index = ids.firstIndex(of: id)
296+
ids.remove(at: index!)
297+
if ids.length == 0 {
298+
self.idsByTimestamp.remove(key: timestamp)
299+
self.sortedTimestamps.remove(timestamp: timestamp)
300+
}
300301
}
301302

302-
let handlerUUID = self.handlerUUIDsByTransactionID.remove(key: id)
303-
?? panic("Invalid ID: Transaction with ID \(id) not found in manager")
304-
305-
// Remove the transaction ID from the handler info array
306-
let handlers = &self.handlerInfos[handlerTypeIdentifier]! as auth(Mutate) &{UInt64: HandlerInfo}
307-
if let handlerInfo = handlers[handlerUUID] {
308-
handlerInfo.removeTransactionID(id: id)
303+
if let handlerUUID = self.handlerUUIDsByTransactionID.remove(key: id) {
304+
// Remove the transaction ID from the handler info array
305+
let handlers = &self.handlerInfos[handlerTypeIdentifier]! as auth(Mutate) &{UInt64: HandlerInfo}
306+
if let handlerInfo = handlers[handlerUUID] {
307+
handlerInfo.removeTransactionID(id: id)
308+
}
309309
}
310310
}
311311

@@ -314,7 +314,7 @@ access(all) contract FlowTransactionSchedulerUtils {
314314
/// @return: The transactions that were cleaned up (removed from the manager)
315315
access(Owner) fun cleanup(): [UInt64] {
316316
let currentTimestamp = getCurrentBlock().timestamp
317-
var transactionsToRemove: [UInt64] = []
317+
var transactionsToRemove: {UInt64: UFix64} = {}
318318

319319
let pastTimestamps = self.sortedTimestamps.getBefore(current: currentTimestamp)
320320
for timestamp in pastTimestamps {
@@ -326,7 +326,7 @@ access(all) contract FlowTransactionSchedulerUtils {
326326
for id in ids {
327327
let status = FlowTransactionScheduler.getStatus(id: id)
328328
if status == nil || status! != FlowTransactionScheduler.Status.Scheduled {
329-
transactionsToRemove.append(id)
329+
transactionsToRemove[id] = timestamp
330330
// Need to temporarily limit the number of transactions to remove
331331
// because some managers on mainnet have already hit the limit and we need to batch them
332332
// to make sure they get cleaned up properly
@@ -339,14 +339,14 @@ access(all) contract FlowTransactionSchedulerUtils {
339339
}
340340

341341
// Then remove and destroy the identified transactions
342-
for id in transactionsToRemove {
342+
for id in transactionsToRemove.keys {
343343
if let tx <- self.scheduledTransactions.remove(key: id) {
344-
self.removeID(id: id, timestamp: tx.timestamp, handlerTypeIdentifier: tx.handlerTypeIdentifier)
344+
self.removeID(id: id, timestamp: transactionsToRemove[id]!, handlerTypeIdentifier: tx.handlerTypeIdentifier)
345345
destroy tx
346346
}
347347
}
348348

349-
return transactionsToRemove
349+
return transactionsToRemove.keys
350350
}
351351

352352
/// Remove a handler capability from the manager

0 commit comments

Comments
 (0)