|
| 1 | +class ValidationResult < ApplicationRecord |
| 2 | + self.primary_key = 'l1_block' |
| 3 | + |
| 4 | + scope :successful, -> { where(success: true) } |
| 5 | + scope :failed, -> { where(success: false) } |
| 6 | + scope :recent, -> { order(validated_at: :desc) } |
| 7 | + scope :in_range, ->(start_block, end_block) { where(l1_block: start_block..end_block) } |
| 8 | + |
| 9 | + # Class methods for validation management |
| 10 | + def self.last_validated_block |
| 11 | + maximum(:l1_block) |
| 12 | + end |
| 13 | + |
| 14 | + def self.validation_gaps(start_block, end_block) |
| 15 | + # Use SQL recursive CTE to find gaps efficiently |
| 16 | + sql = <<~SQL |
| 17 | + WITH RECURSIVE expected(n) AS ( |
| 18 | + SELECT ? AS n |
| 19 | + UNION ALL |
| 20 | + SELECT n + 1 FROM expected WHERE n < ? |
| 21 | + ) |
| 22 | + SELECT n AS missing_block |
| 23 | + FROM expected |
| 24 | + LEFT JOIN validation_results vr ON vr.l1_block = expected.n |
| 25 | + WHERE vr.l1_block IS NULL |
| 26 | + ORDER BY n |
| 27 | + SQL |
| 28 | + |
| 29 | + connection.execute(sql, [start_block, end_block]).map { |row| row['missing_block'] } |
| 30 | + end |
| 31 | + |
| 32 | + def self.validation_stats(since: 1.hour.ago) |
| 33 | + results = where('validated_at >= ?', since) |
| 34 | + total = results.count |
| 35 | + passed = results.successful.count |
| 36 | + failed = results.failed.count |
| 37 | + |
| 38 | + { |
| 39 | + total: total, |
| 40 | + passed: passed, |
| 41 | + failed: failed, |
| 42 | + pass_rate: total > 0 ? (passed.to_f / total * 100).round(2) : 0 |
| 43 | + } |
| 44 | + end |
| 45 | + |
| 46 | + def self.recent_failures(limit: 10) |
| 47 | + failed.recent.limit(limit) |
| 48 | + end |
| 49 | + |
| 50 | + # Class method to perform validation and save result |
| 51 | + def self.validate_and_save(l1_block_number, l2_block_hashes) |
| 52 | + Rails.logger.info "ValidationResult: Validating L1 block #{l1_block_number}" |
| 53 | + |
| 54 | + begin |
| 55 | + # Create validator and validate (validator fetches its own API data) |
| 56 | + validator = BlockValidator.new |
| 57 | + block_result = validator.validate_l1_block(l1_block_number, l2_block_hashes) |
| 58 | + |
| 59 | + # Store validation result using create_or_find_by for concurrency safety |
| 60 | + validation_result = create_or_find_by(l1_block: l1_block_number) do |vr| |
| 61 | + vr.success = block_result.success |
| 62 | + vr.error_details = block_result.errors.to_json |
| 63 | + vr.validation_stats = block_result.stats.to_json |
| 64 | + vr.validated_at = Time.current |
| 65 | + end |
| 66 | + |
| 67 | + # Log the result |
| 68 | + validation_result.log_summary |
| 69 | + |
| 70 | + validation_result |
| 71 | + rescue => e |
| 72 | + Rails.logger.error "ValidationResult: Exception validating block #{l1_block_number}: #{e.message}" |
| 73 | + |
| 74 | + # Record the validation error |
| 75 | + error_result = create_or_find_by(l1_block: l1_block_number) do |vr| |
| 76 | + vr.success = false |
| 77 | + vr.error_details = [e.message].to_json |
| 78 | + vr.validation_stats = { exception: true, exception_class: e.class.name }.to_json |
| 79 | + vr.validated_at = Time.current |
| 80 | + end |
| 81 | + |
| 82 | + error_result.log_summary |
| 83 | + raise e |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + # Instance methods |
| 88 | + def parsed_errors |
| 89 | + return [] unless error_details.present? |
| 90 | + JSON.parse(error_details) rescue [] |
| 91 | + end |
| 92 | + |
| 93 | + def parsed_stats |
| 94 | + return {} unless validation_stats.present? |
| 95 | + JSON.parse(validation_stats) rescue {} |
| 96 | + end |
| 97 | + |
| 98 | + def failure_summary |
| 99 | + return nil if success? |
| 100 | + |
| 101 | + error_list = parsed_errors |
| 102 | + return "No error details" if error_list.empty? |
| 103 | + |
| 104 | + # Return first few errors for summary |
| 105 | + error_list.first(3).join('; ') |
| 106 | + end |
| 107 | + |
| 108 | + def log_summary(logger = Rails.logger) |
| 109 | + if success? |
| 110 | + stats_data = parsed_stats |
| 111 | + if stats_data['actual_creations'].to_i > 0 || stats_data['actual_transfers'].to_i > 0 || stats_data['storage_checks'].to_i > 0 |
| 112 | + logger.info "✅ Block #{l1_block} validated successfully: " \ |
| 113 | + "#{stats_data['actual_creations']} creations, " \ |
| 114 | + "#{stats_data['actual_transfers']} transfers, " \ |
| 115 | + "#{stats_data['storage_checks']} storage checks" |
| 116 | + end |
| 117 | + else |
| 118 | + logger.error "❌ Block #{l1_block} validation failed with #{parsed_errors.size} errors:" |
| 119 | + parsed_errors.first(5).each { |e| logger.error " - #{e}" } |
| 120 | + logger.error " ... and #{parsed_errors.size - 5} more errors" if parsed_errors.size > 5 |
| 121 | + end |
| 122 | + end |
| 123 | +end |
0 commit comments