Skip to content

Commit cc22fbd

Browse files
committed
Improve import validation
1 parent 939c6e4 commit cc22fbd

5 files changed

Lines changed: 200 additions & 145 deletions

File tree

app/services/eth_block_importer.rb

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ def initialize
2020

2121
# Validation configuration
2222
@validation_enabled = ENV.fetch('VALIDATE_IMPORT').casecmp?('true')
23-
@validation_strict = ENV.fetch('VALIDATE_STRICT').casecmp?('true')
24-
@validator = BlockValidator.new if @validation_enabled
25-
@validation_stats = {passed: 0, failed: 0, skipped: 0}
23+
@validation_stats = {passed: 0, failed: 0}
2624

2725
# Create a shared thread pool for validation
2826
if @validation_enabled
@@ -36,7 +34,7 @@ def initialize
3634
logger.info "Created validation thread pool with #{@validation_threads} threads"
3735
end
3836

39-
logger.info "EthBlockImporter initialized - Validation: #{@validation_enabled ? 'ENABLED' : 'disabled'}, Strict: #{@validation_strict ? 'YES' : 'no'}"
37+
logger.info "EthBlockImporter initialized - Validation: #{@validation_enabled ? 'ENABLED' : 'disabled'}"
4038

4139
MemeryExtensions.clear_all_caches!
4240

@@ -443,22 +441,49 @@ def validate_imported_blocks(l1_block_numbers, l2_blocks)
443441

444442
Concurrent::Promise.execute(executor: @validation_executor) do
445443
if l2_hashes.empty?
446-
{ block: l1_block_num, status: :skipped, message: "No L2 blocks found" }
444+
{
445+
block: l1_block_num,
446+
status: :failed,
447+
message: "No L2 blocks found for L1 block #{l1_block_num}",
448+
errors: ["No L2 blocks found for L1 block #{l1_block_num}"]
449+
}
447450
else
448451
begin
449-
result = @validator.validate_l1_block(l1_block_num, l2_hashes)
450-
{
451-
block: l1_block_num,
452-
status: result.success ? :passed : :failed,
453-
result: result,
454-
errors: result.errors
455-
}
452+
validator = BlockValidator.new
453+
result = validator.validate_l1_block(l1_block_num, l2_hashes)
454+
455+
if result.stats[:api_unavailable]
456+
{
457+
block: l1_block_num,
458+
status: :failed,
459+
result: result,
460+
errors: result.errors.presence || ["API unavailable for block #{l1_block_num}"],
461+
message: "API unavailable for block #{l1_block_num}"
462+
}
463+
elsif result.stats[:incomplete_actual]
464+
{
465+
block: l1_block_num,
466+
status: :failed,
467+
result: result,
468+
errors: result.errors.presence || ["Incomplete L2 receipts for block #{l1_block_num}"],
469+
message: "Incomplete L2 receipts for block #{l1_block_num}"
470+
}
471+
else
472+
payload = {
473+
block: l1_block_num,
474+
status: result.success ? :passed : :failed,
475+
result: result,
476+
errors: result.errors
477+
}
478+
payload
479+
end
456480
rescue => e
457481
{
458482
block: l1_block_num,
459483
status: :error,
460484
message: e.message,
461-
backtrace: e.backtrace.first(5)
485+
backtrace: e.backtrace.first(5),
486+
errors: [e.message]
462487
}
463488
end
464489
end
@@ -475,23 +500,16 @@ def validate_imported_blocks(l1_block_numbers, l2_blocks)
475500
validation[:result].log_summary(logger)
476501
@validation_stats[:passed] += 1
477502
when :failed
478-
validation[:result].log_summary(logger)
503+
validation[:result]&.log_summary(logger)
479504
@validation_stats[:failed] += 1
480-
if @validation_strict
481-
binding.irb
482-
raise "Validation failed for L1 block #{validation[:block]}: #{validation[:errors].join('; ')}"
483-
end
484-
when :skipped
485-
logger.warn "Validation: #{validation[:message]} for L1 block #{validation[:block]}"
486-
@validation_stats[:skipped] += 1
505+
binding.irb if ENV['DEBUG'] == '1'
506+
raise "Validation failed for L1 block #{validation[:block]}: #{Array(validation[:errors]).join('; ')}"
487507
when :error
488508
logger.error "Validation error for block #{validation[:block]}: #{validation[:message]}"
489509
logger.error validation[:backtrace].join("\n") if ENV['DEBUG'] && validation[:backtrace]
490510
@validation_stats[:failed] += 1
491-
if @validation_strict
492-
binding.irb
493-
raise "Validation failed for L1 block #{validation[:block]}: #{validation[:errors].join('; ')}"
494-
end
511+
binding.irb if ENV['DEBUG'] == '1'
512+
raise "Validation failed for L1 block #{validation[:block]}: #{Array(validation[:errors]).join('; ')}"
495513
end
496514
end
497515

@@ -510,7 +528,6 @@ def log_validation_summary
510528
logger.info "Validation Summary: #{@validation_stats[:passed]}/#{total} passed (#{pass_rate}%)"
511529
logger.info " Passed: #{@validation_stats[:passed]}"
512530
logger.info " Failed: #{@validation_stats[:failed]}"
513-
logger.info " Skipped: #{@validation_stats[:skipped]}"
514531
logger.info "=" * 60
515532
end
516533

@@ -523,7 +540,7 @@ def validation_summary
523540
pass_rate = total > 0 ? (@validation_stats[:passed].to_f / total * 100).round(2) : 0
524541

525542
"Validation: #{@validation_stats[:passed]}/#{total} passed (#{pass_rate}%), " \
526-
"#{@validation_stats[:failed]} failed, #{@validation_stats[:skipped]} skipped"
543+
"#{@validation_stats[:failed]} failed"
527544
end
528545

529546
def shutdown

config/derive_ethscriptions_blocks.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
'L1_GENESIS_BLOCK' => { description: 'L1 Genesis Block number', required: true },
1515
'BLOCK_IMPORT_BATCH_SIZE' => { description: 'Block import batch size', default: '2' },
1616
'VALIDATE_IMPORT' => { description: 'Enable validation (true/false)', default: 'false' },
17-
'VALIDATE_STRICT' => { description: 'Halt on validation failure (true/false)', default: 'false' },
1817
'IMPORT_INTERVAL' => { description: 'Seconds between import attempts', default: '6' }
1918
}
2019

@@ -84,7 +83,6 @@
8483
puts " Geth RPC: #{ENV['NON_AUTH_GETH_RPC_URL']}"
8584
puts " Batch Size: #{ENV['BLOCK_IMPORT_BATCH_SIZE']}"
8685
puts " Validation: #{ENV['VALIDATE_IMPORT'] == 'true' ? 'ENABLED' : 'disabled'}"
87-
puts " Strict Mode: #{ENV['VALIDATE_STRICT'] == 'true' ? 'ENABLED' : 'disabled'}"
8886
puts " Import Interval: #{ENV['IMPORT_INTERVAL']}s"
8987
puts "="*80
9088

@@ -174,17 +172,11 @@ module Clockwork
174172

175173
puts "[#{Time.now}] ❌ Error: #{e.message}"
176174

177-
# For validation failures in strict mode, exit
178-
if ENV['VALIDATE_STRICT'] == 'true' && e.message.include?('Validation failed')
179-
puts "[#{Time.now}] Exiting due to validation failure in strict mode"
180-
exit 1
181-
end
182-
183175
# For other errors, wait and retry
184176
puts "[#{Time.now}] Retrying in #{import_interval}s..."
185177
end
186178

187179
sleep import_interval
188180
end
189181
end
190-
end
182+
end

0 commit comments

Comments
 (0)