|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/optimism-java/dispute-explorer/internal/svc" |
| 9 | + "github.com/optimism-java/dispute-explorer/pkg/log" |
| 10 | + "github.com/optimism-java/dispute-explorer/pkg/rpc" |
| 11 | + "github.com/pkg/errors" |
| 12 | + "github.com/spf13/cast" |
| 13 | +) |
| 14 | + |
| 15 | +// LatestBlockNumberWithRateLimit uses unified RPC manager for latest block number retrieval |
| 16 | +func LatestBlockNumberWithRateLimit(ctx *svc.ServiceContext) { |
| 17 | + for { |
| 18 | + // use unified RPC manager to get L1 latest block number (with rate limiting) |
| 19 | + latest, err := ctx.RPCManager.GetLatestBlockNumber(context.Background(), true) |
| 20 | + if err != nil { |
| 21 | + log.Errorf("[Handler.LatestBlockNumberWithRateLimit] Get latest block number error: %s\n", errors.WithStack(err)) |
| 22 | + time.Sleep(12 * time.Second) |
| 23 | + continue |
| 24 | + } |
| 25 | + |
| 26 | + ctx.LatestBlockNumber = cast.ToInt64(latest) |
| 27 | + log.Infof("[Handler.LatestBlockNumberWithRateLimit] Latest block number: %d (using RPC Manager)\n", latest) |
| 28 | + time.Sleep(12 * time.Second) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// SyncBlockWithRateLimit uses unified RPC manager for block synchronization |
| 33 | +func SyncBlockWithRateLimit(ctx *svc.ServiceContext) { |
| 34 | + for { |
| 35 | + // Check pending block count |
| 36 | + // ... existing check logic ... |
| 37 | + |
| 38 | + syncingBlockNumber := ctx.SyncedBlockNumber + 1 |
| 39 | + log.Infof("[Handler.SyncBlockWithRateLimit] Try to sync block number: %d using RPC Manager\n", syncingBlockNumber) |
| 40 | + |
| 41 | + if syncingBlockNumber > ctx.LatestBlockNumber { |
| 42 | + time.Sleep(3 * time.Second) |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + // Use unified RPC manager to get block (automatically handles rate limiting) |
| 47 | + block, err := ctx.RPCManager.GetBlockByNumber(context.Background(), big.NewInt(syncingBlockNumber), true) |
| 48 | + if err != nil { |
| 49 | + log.Errorf("[Handler.SyncBlockWithRateLimit] Get block by number error: %s\n", errors.WithStack(err)) |
| 50 | + time.Sleep(3 * time.Second) |
| 51 | + continue |
| 52 | + } |
| 53 | + |
| 54 | + log.Infof("[Handler.SyncBlockWithRateLimit] Got block number: %d, hash: %v, parent hash: %v\n", |
| 55 | + block.Number().Int64(), block.Hash().Hex(), block.ParentHash().Hex()) |
| 56 | + |
| 57 | + // Verify parent hash |
| 58 | + if block.ParentHash() != ctx.SyncedBlockHash { |
| 59 | + log.Errorf("[Handler.SyncBlockWithRateLimit] ParentHash mismatch: expected %s, got %s\n", |
| 60 | + ctx.SyncedBlockHash.Hex(), block.ParentHash().Hex()) |
| 61 | + // Can call rollback logic here |
| 62 | + time.Sleep(3 * time.Second) |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + // Save block to database |
| 67 | + // ... existing database save logic ... |
| 68 | + |
| 69 | + // Update sync status |
| 70 | + ctx.SyncedBlockNumber = block.Number().Int64() |
| 71 | + ctx.SyncedBlockHash = block.Hash() |
| 72 | + |
| 73 | + log.Infof("[Handler.SyncBlockWithRateLimit] Successfully synced block %d\n", block.Number().Int64()) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// GetBlockByNumberHTTP gets block using HTTP method (with rate limiting) |
| 78 | +func GetBlockByNumberHTTP(ctx *svc.ServiceContext, blockNumber int64) ([]byte, error) { |
| 79 | + requestBody := "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBlockByNumber\",\"params\":[\"" + |
| 80 | + cast.ToString(blockNumber) + "\", true],\"id\":1}" |
| 81 | + |
| 82 | + // Use unified RPC manager for HTTP calls (automatically handles rate limiting) |
| 83 | + return ctx.RPCManager.HTTPPostJSON(context.Background(), requestBody, true) |
| 84 | +} |
| 85 | + |
| 86 | +// MigrateExistingHandlers example of migrating existing handlers |
| 87 | +func MigrateExistingHandlers(ctx *svc.ServiceContext) { |
| 88 | + log.Infof("[Handler.Migration] Starting migration to RPC Manager") |
| 89 | + |
| 90 | + // Start handlers with rate limiting |
| 91 | + go LatestBlockNumberWithRateLimit(ctx) |
| 92 | + go SyncBlockWithRateLimit(ctx) |
| 93 | + |
| 94 | + // Start RPC monitoring |
| 95 | + go StartRPCMonitoring(ctx) |
| 96 | + |
| 97 | + log.Infof("[Handler.Migration] All handlers migrated to use RPC Manager") |
| 98 | +} |
| 99 | + |
| 100 | +// StartRPCMonitoring starts RPC monitoring |
| 101 | +func StartRPCMonitoring(ctx *svc.ServiceContext) { |
| 102 | + // Create monitor |
| 103 | + monitor := rpc.NewMonitor(ctx.RPCManager, 30*time.Second) |
| 104 | + |
| 105 | + // Start monitoring |
| 106 | + monitor.Start(ctx.Context) |
| 107 | +} |
| 108 | + |
| 109 | +// Compatibility functions: provide smooth migration for existing code |
| 110 | +func GetL1Client(ctx *svc.ServiceContext) interface{} { |
| 111 | + // Return rate-limited client wrapper |
| 112 | + return ctx.RPCManager.GetRawClient(true) |
| 113 | +} |
| 114 | + |
| 115 | +func GetL2Client(ctx *svc.ServiceContext) interface{} { |
| 116 | + // Return rate-limited client wrapper |
| 117 | + return ctx.RPCManager.GetRawClient(false) |
| 118 | +} |
0 commit comments