|
| 1 | +namespace :migration do |
| 2 | + desc "Usage: RAILS_ENV=production bundle exec rake migration:check_cell_output_data[0,1000000]" |
| 3 | + task :check_cell_output_data, %i[start_block end_block] => :environment do |_, args| |
| 4 | + $retry_ids = Set.new |
| 5 | + @api = CKB::API.new(host: ENV.fetch("CKB_NODE_URL", nil), |
| 6 | + timeout_config: { |
| 7 | + open_timeout: 1, read_timeout: 3, |
| 8 | + write_timeout: 1 |
| 9 | + }) |
| 10 | + (args[:start_block].to_i..args[:end_block].to_i).to_a.each_slice(100).to_a.each do |range| |
| 11 | + compare_output(range, 0) |
| 12 | + end |
| 13 | + nil |
| 14 | + |
| 15 | + puts "==============" |
| 16 | + puts "retry IDS:" |
| 17 | + puts $retry_ids.join(",") |
| 18 | + puts "done" |
| 19 | + end |
| 20 | + |
| 21 | + def compare_output(range, retry_count) |
| 22 | + request_body = |
| 23 | + range.map do |number| |
| 24 | + ["get_block_by_number", number] |
| 25 | + end |
| 26 | + response = @api.batch_request(*request_body) |
| 27 | + response.each do |r| |
| 28 | + r[:transactions].each do |tx| |
| 29 | + tx[:outputs].each_with_index do |_output, index| |
| 30 | + output_data = tx[:outputs_data][index] |
| 31 | + binary_data = CKB::Utils.hex_to_bin(output_data) |
| 32 | + data_hash = nil |
| 33 | + data_size = 0 |
| 34 | + if binary_data&.bytesize&.positive? |
| 35 | + data_size = binary_data.bytesize |
| 36 | + data_hash = CKB::Utils.bin_to_hex(CKB::Blake2b.digest(binary_data)) |
| 37 | + end |
| 38 | + co = CellOutput.find_by(tx_hash: tx[:hash], cell_index: index) |
| 39 | + |
| 40 | + if co.data_size != data_size || co.data_hash != data_hash |
| 41 | + puts co.id |
| 42 | + co.update!(data_hash:, data_size:) |
| 43 | + if data_size.zero? |
| 44 | + co.cell_datum.destroy if co.cell_datum.present? |
| 45 | + elsif co.cell_datum.present? |
| 46 | + co.cell_datum.update!(data: binary_data) |
| 47 | + else |
| 48 | + co.cell_datum.create!(data: binary_data) |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + rescue StandardError => _e |
| 55 | + retry_count += 1 |
| 56 | + if retry_count > 2 |
| 57 | + $retry_ids << range.first |
| 58 | + else |
| 59 | + compare_output(range, retry_count) |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments