|
| 1 | +package ingester |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/duneanalytics/blockchain-ingester/models" |
| 7 | +) |
| 8 | + |
| 9 | +type Info struct { |
| 10 | + BlockchainName string |
| 11 | + Stack string |
| 12 | + LatestBlockNumber int64 |
| 13 | + IngestedBlockNumber int64 |
| 14 | + ConsumedBlockNumber int64 |
| 15 | + Errors ErrorState |
| 16 | + Since time.Time |
| 17 | +} |
| 18 | + |
| 19 | +func NewInfo(blockchain string, stack string) Info { |
| 20 | + return Info{ |
| 21 | + BlockchainName: blockchain, |
| 22 | + Stack: stack, |
| 23 | + Errors: ErrorState{ |
| 24 | + RPCErrors: make([]ErrorInfo, 0, 100), |
| 25 | + DuneErrors: make([]ErrorInfo, 0, 100), |
| 26 | + RPCErrorCount: 0, |
| 27 | + DuneErrorCount: 0, |
| 28 | + }, |
| 29 | + Since: time.Now(), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (info *Info) ToProgressReport() models.BlockchainIndexProgress { |
| 34 | + return models.BlockchainIndexProgress{ |
| 35 | + BlockchainName: info.BlockchainName, |
| 36 | + EVMStack: info.Stack, |
| 37 | + LastIngestedBlockNumber: info.IngestedBlockNumber, |
| 38 | + LatestBlockNumber: info.LatestBlockNumber, |
| 39 | + Errors: info.ProgressReportErrors(), |
| 40 | + DuneErrorCounts: info.Errors.DuneErrorCount, |
| 41 | + RPCErrorCounts: info.Errors.RPCErrorCount, |
| 42 | + Since: info.Since, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (info *Info) ResetErrors() { |
| 47 | + info.Since = time.Now() |
| 48 | + info.Errors.Reset() |
| 49 | +} |
| 50 | + |
| 51 | +type ErrorState struct { |
| 52 | + RPCErrors []ErrorInfo |
| 53 | + DuneErrors []ErrorInfo |
| 54 | + RPCErrorCount int |
| 55 | + DuneErrorCount int |
| 56 | +} |
| 57 | + |
| 58 | +// ProgressReportErrors returns a combined list of errors from RPC requests and Dune requests |
| 59 | +func (info Info) ProgressReportErrors() []models.BlockchainIndexError { |
| 60 | + errors := make([]models.BlockchainIndexError, 0, len(info.Errors.RPCErrors)+len(info.Errors.DuneErrors)) |
| 61 | + for _, e := range info.Errors.RPCErrors { |
| 62 | + errors = append(errors, models.BlockchainIndexError{ |
| 63 | + Timestamp: e.Timestamp, |
| 64 | + BlockNumbers: e.BlockNumbers, |
| 65 | + Error: e.Error.Error(), |
| 66 | + Source: "rpc", |
| 67 | + }) |
| 68 | + } |
| 69 | + for _, e := range info.Errors.DuneErrors { |
| 70 | + errors = append(errors, models.BlockchainIndexError{ |
| 71 | + Timestamp: e.Timestamp, |
| 72 | + BlockNumbers: e.BlockNumbers, |
| 73 | + Error: e.Error.Error(), |
| 74 | + Source: "dune", |
| 75 | + }) |
| 76 | + } |
| 77 | + return errors |
| 78 | +} |
| 79 | + |
| 80 | +func (es *ErrorState) Reset() { |
| 81 | + es.RPCErrors = es.RPCErrors[:0] |
| 82 | + es.DuneErrors = es.DuneErrors[:0] |
| 83 | + es.RPCErrorCount = 0 |
| 84 | + es.DuneErrorCount = 0 |
| 85 | +} |
| 86 | + |
| 87 | +func (es *ErrorState) ObserveRPCError(err ErrorInfo) { |
| 88 | + es.RPCErrorCount++ |
| 89 | + err.Timestamp = time.Now() |
| 90 | + |
| 91 | + // If we have filled the slice, remove the oldest error |
| 92 | + if len(es.RPCErrors) == cap(es.RPCErrors) { |
| 93 | + tmp := make([]ErrorInfo, len(es.RPCErrors)-1, cap(es.RPCErrors)) |
| 94 | + copy(tmp, es.RPCErrors[1:]) |
| 95 | + es.RPCErrors = tmp |
| 96 | + } |
| 97 | + es.RPCErrors = append(es.RPCErrors, err) |
| 98 | +} |
| 99 | + |
| 100 | +func (es *ErrorState) ObserveDuneError(err ErrorInfo) { |
| 101 | + es.DuneErrorCount++ |
| 102 | + err.Timestamp = time.Now() |
| 103 | + |
| 104 | + // If we have filled the slice, remove the oldest error |
| 105 | + if len(es.DuneErrors) == cap(es.DuneErrors) { |
| 106 | + tmp := make([]ErrorInfo, len(es.DuneErrors)-1, cap(es.DuneErrors)) |
| 107 | + copy(tmp, es.DuneErrors[1:]) |
| 108 | + es.DuneErrors = tmp |
| 109 | + } |
| 110 | + es.DuneErrors = append(es.DuneErrors, err) |
| 111 | +} |
| 112 | + |
| 113 | +type ErrorInfo struct { |
| 114 | + Timestamp time.Time |
| 115 | + BlockNumbers string |
| 116 | + Error error |
| 117 | +} |
0 commit comments