|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/ethereum/go-ethereum" |
| 5 | + "github.com/ethereum/go-ethereum/accounts/abi" |
| 6 | + "github.com/ethereum/go-ethereum/common" |
| 7 | + Types "github.com/ethereum/go-ethereum/core/types" |
| 8 | + "github.com/ethereum/go-ethereum/ethclient" |
| 9 | + "math/big" |
| 10 | + "razor/cache" |
| 11 | + "razor/core" |
| 12 | + "razor/core/types" |
| 13 | + "razor/pkg/bindings" |
| 14 | + "razor/utils" |
| 15 | + "strings" |
| 16 | +) |
| 17 | + |
| 18 | +func (*UtilsStruct) InitJobAndCollectionCache(client *ethclient.Client) (*cache.JobsCache, *cache.CollectionsCache, *big.Int, error) { |
| 19 | + initAssetCacheBlock, err := clientUtils.GetLatestBlockWithRetry(client) |
| 20 | + if err != nil { |
| 21 | + log.Error("Error in fetching block: ", err) |
| 22 | + return nil, nil, nil, err |
| 23 | + } |
| 24 | + log.Debugf("InitJobAndCollectionCache: Latest header value when initializing jobs and collections cache: %d", initAssetCacheBlock.Number) |
| 25 | + |
| 26 | + log.Info("INITIALIZING JOBS AND COLLECTIONS CACHE...") |
| 27 | + |
| 28 | + // Create instances of cache |
| 29 | + jobsCache := cache.NewJobsCache() |
| 30 | + collectionsCache := cache.NewCollectionsCache() |
| 31 | + |
| 32 | + // Initialize caches |
| 33 | + if err := utils.InitJobsCache(client, jobsCache); err != nil { |
| 34 | + log.Error("Error in initializing jobs cache: ", err) |
| 35 | + return nil, nil, nil, err |
| 36 | + } |
| 37 | + if err := utils.InitCollectionsCache(client, collectionsCache); err != nil { |
| 38 | + log.Error("Error in initializing collections cache: ", err) |
| 39 | + return nil, nil, nil, err |
| 40 | + } |
| 41 | + |
| 42 | + return jobsCache, collectionsCache, initAssetCacheBlock.Number, nil |
| 43 | +} |
| 44 | + |
| 45 | +// CheckForJobAndCollectionEvents checks for specific job and collections event that were emitted. |
| 46 | +func CheckForJobAndCollectionEvents(client *ethclient.Client, commitParams *types.CommitParams) error { |
| 47 | + collectionManagerContractABI, err := abi.JSON(strings.NewReader(bindings.CollectionManagerMetaData.ABI)) |
| 48 | + if err != nil { |
| 49 | + log.Errorf("Error in parsing collection manager contract ABI: %v", err) |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + eventNames := []string{core.JobUpdatedEvent, core.CollectionUpdatedEvent, core.CollectionActivityStatusEvent, core.JobCreatedEvent, core.CollectionCreatedEvent} |
| 54 | + |
| 55 | + log.Debug("Checking for Job/Collection update events...") |
| 56 | + toBlock, err := clientUtils.GetLatestBlockWithRetry(client) |
| 57 | + if err != nil { |
| 58 | + log.Error("Error in getting latest block to start event listener: ", err) |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + // Process events and update the fromBlock for the next iteration |
| 63 | + newFromBlock, err := processEvents(client, collectionManagerContractABI, commitParams.FromBlockToCheckForEvents, toBlock.Number, eventNames, commitParams.JobsCache, commitParams.CollectionsCache) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + // Update the commitParams with the new fromBlock |
| 69 | + commitParams.FromBlockToCheckForEvents = new(big.Int).Add(newFromBlock, big.NewInt(1)) |
| 70 | + |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +// processEvents fetches and processes logs for multiple event types. |
| 75 | +func processEvents(client *ethclient.Client, contractABI abi.ABI, fromBlock, toBlock *big.Int, eventNames []string, jobsCache *cache.JobsCache, collectionsCache *cache.CollectionsCache) (*big.Int, error) { |
| 76 | + logs, err := getEventLogs(client, fromBlock, toBlock) |
| 77 | + if err != nil { |
| 78 | + log.Errorf("Failed to fetch logs: %v", err) |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + for _, eventName := range eventNames { |
| 83 | + eventID := contractABI.Events[eventName].ID.Hex() |
| 84 | + for _, vLog := range logs { |
| 85 | + if len(vLog.Topics) > 0 && vLog.Topics[0].Hex() == eventID { |
| 86 | + switch eventName { |
| 87 | + case core.JobUpdatedEvent, core.JobCreatedEvent: |
| 88 | + jobId := utils.ConvertHashToUint16(vLog.Topics[1]) |
| 89 | + updatedJob, err := utils.UtilsInterface.GetActiveJob(client, jobId) |
| 90 | + if err != nil { |
| 91 | + log.Errorf("Error in getting job with job Id %v: %v", jobId, err) |
| 92 | + continue |
| 93 | + } |
| 94 | + log.Debugf("RECEIVED JOB EVENT: Updating the job with Id %v with details %+v...", jobId, updatedJob) |
| 95 | + jobsCache.UpdateJob(jobId, updatedJob) |
| 96 | + case core.CollectionUpdatedEvent, core.CollectionCreatedEvent, core.CollectionActivityStatusEvent: |
| 97 | + collectionId := utils.ConvertHashToUint16(vLog.Topics[1]) |
| 98 | + newCollection, err := utils.UtilsInterface.GetCollection(client, collectionId) |
| 99 | + if err != nil { |
| 100 | + log.Errorf("Error in getting collection with collection Id %v: %v", collectionId, err) |
| 101 | + continue |
| 102 | + } |
| 103 | + log.Debugf("RECEIVED COLLECTION EVENT: Updating the collection with ID %v with details %+v", collectionId, newCollection) |
| 104 | + collectionsCache.UpdateCollection(collectionId, newCollection) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Return the new toBlock for the next iteration |
| 111 | + return toBlock, nil |
| 112 | +} |
| 113 | + |
| 114 | +// getEventLogs is a utility function to fetch the event logs |
| 115 | +func getEventLogs(client *ethclient.Client, fromBlock *big.Int, toBlock *big.Int) ([]Types.Log, error) { |
| 116 | + log.Debugf("Checking for events from block %v to block %v...", fromBlock, toBlock) |
| 117 | + |
| 118 | + // Set up the query for filtering logs |
| 119 | + query := ethereum.FilterQuery{ |
| 120 | + FromBlock: fromBlock, |
| 121 | + ToBlock: toBlock, |
| 122 | + Addresses: []common.Address{ |
| 123 | + common.HexToAddress(core.CollectionManagerAddress), |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + // Retrieve the logs |
| 128 | + logs, err := clientUtils.FilterLogsWithRetry(client, query) |
| 129 | + if err != nil { |
| 130 | + log.Errorf("Error in filter logs: %v", err) |
| 131 | + return []Types.Log{}, err |
| 132 | + } |
| 133 | + |
| 134 | + return logs, nil |
| 135 | +} |
0 commit comments