|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/optimism-java/dispute-explorer/internal/schema" |
| 7 | + "github.com/optimism-java/dispute-explorer/internal/svc" |
| 8 | + "github.com/optimism-java/dispute-explorer/pkg/log" |
| 9 | +) |
| 10 | + |
| 11 | +func SyncFrontendMoveTransactions(ctx *svc.ServiceContext) { |
| 12 | + log.Infof("[Handler.SyncFrontendMoveTransactions] Starting sync frontend move transactions...") |
| 13 | + |
| 14 | + for { |
| 15 | + select { |
| 16 | + case <-ctx.Context.Done(): |
| 17 | + log.Infof("[Handler.SyncFrontendMoveTransactions] Context cancelled, stopping...") |
| 18 | + return |
| 19 | + default: |
| 20 | + err := processFrontendMoveTransactions(ctx) |
| 21 | + if err != nil { |
| 22 | + log.Errorf("[Handler.SyncFrontendMoveTransactions] Process error: %s", err) |
| 23 | + } |
| 24 | + time.Sleep(30 * time.Second) |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// processFrontendMoveTransactions |
| 30 | +func processFrontendMoveTransactions(ctx *svc.ServiceContext) error { |
| 31 | + var unsyncedTransactions []schema.FrontendMoveTransaction |
| 32 | + err := ctx.DB.Where("is_synced = ?", false).Order("id").Limit(10).Find(&unsyncedTransactions).Error |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + if len(unsyncedTransactions) == 0 { |
| 38 | + log.Debugf("[Handler.SyncFrontendMoveTransactions] No unsynced transactions found") |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + log.Infof("[Handler.SyncFrontendMoveTransactions] Found %d unsynced transactions", len(unsyncedTransactions)) |
| 43 | + |
| 44 | + for _, transaction := range unsyncedTransactions { |
| 45 | + err := syncSingleTransaction(ctx, &transaction) |
| 46 | + if err != nil { |
| 47 | + log.Errorf("[Handler.SyncFrontendMoveTransactions] Failed to sync transaction %s: %s", transaction.TxHash, err) |
| 48 | + continue |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// syncSingleTransaction |
| 56 | +func syncSingleTransaction(ctx *svc.ServiceContext, transaction *schema.FrontendMoveTransaction) error { |
| 57 | + tx := ctx.DB.Begin() |
| 58 | + defer func() { |
| 59 | + if r := recover(); r != nil { |
| 60 | + tx.Rollback() |
| 61 | + } |
| 62 | + }() |
| 63 | + |
| 64 | + // 1. update dispute_game has_frontend_move column to true |
| 65 | + err := tx.Model(&schema.DisputeGame{}). |
| 66 | + Where("game_contract = ?", transaction.GameContract). |
| 67 | + Update("has_frontend_move", true).Error |
| 68 | + if err != nil { |
| 69 | + tx.Rollback() |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + // 2. update game_claim_data is_from_frontend column to true |
| 74 | + err = tx.Model(&schema.GameClaimData{}). |
| 75 | + Where("game_contract = ? AND parent_index = ?", transaction.GameContract, transaction.ParentIndex). |
| 76 | + Update("is_from_frontend", true).Error |
| 77 | + if err != nil { |
| 78 | + tx.Rollback() |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + // 3. update frontend_move_transactions is_synced column to true |
| 83 | + err = tx.Model(transaction).Update("is_synced", true).Error |
| 84 | + if err != nil { |
| 85 | + tx.Rollback() |
| 86 | + return err |
| 87 | + } |
| 88 | + |
| 89 | + // tx commit |
| 90 | + err = tx.Commit().Error |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + log.Infof("[Handler.SyncFrontendMoveTransactions] Successfully synced transaction %s (game: %s, parent_index: %s)", |
| 96 | + transaction.TxHash, transaction.GameContract, transaction.ParentIndex) |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
0 commit comments