Skip to content

Commit

Permalink
feat: use task to fill is_used to cell_dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid committed Feb 20, 2025
1 parent 24158c0 commit 05d5bdc
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions lib/tasks/migration/fill_is_used_to_cell_dependency.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace :migration do
desc "Usage: RAILS_ENV=production bundle exec rake migration:fill_is_used_to_cell_dependency[0,10000]"
task :fill_is_used_to_cell_dependency, %i[start_block end_block] => :environment do |_, args|
$error_ids = Set.new
ActiveRecord::Base.connection.execute("SET statement_timeout = 0")
(args[:start_block].to_i..args[:end_block].to_i).to_a.each_slice(100).to_a.each do |range|
fill_is_used(range)

Check warning on line 7 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L4-L7

Added lines #L4 - L7 were not covered by tests
end; nil
puts "error IDS:"
puts $error_ids.join(",")
puts "done"

Check warning on line 11 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L9-L11

Added lines #L9 - L11 were not covered by tests
end

private

def fill_is_used(range)
puts range.first
response = CkbTransaction.includes(:cell_dependencies, :cell_outputs, :cell_inputs).where(block_number: range, is_cellbase: false)
cell_deps_attrs = Set.new
response.each do |ckb_transaction|
type_scripts = Hash.new
lock_scripts = Hash.new

Check warning on line 22 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L17-L22

Added lines #L17 - L22 were not covered by tests

cell_outputs = ckb_transaction.cell_outputs.includes(:type_script).to_a
cell_inputs = ckb_transaction.cell_inputs.includes(:previous_cell_output).map(&:previous_cell_output)
(cell_inputs + cell_outputs).each do |cell|
lock_scripts[cell.lock_script.code_hash] = cell.lock_script.hash_type
if cell.type_script
type_scripts[cell.type_script.code_hash] = cell.type_script.hash_type

Check warning on line 29 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L24-L29

Added lines #L24 - L29 were not covered by tests
end
end

ckb_transaction.cell_dependencies.each do |cell_dep|
case cell_dep.dep_type

Check warning on line 34 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L33-L34

Added lines #L33 - L34 were not covered by tests
when "code"
process_code_dep(cell_dep, lock_scripts, type_scripts, cell_deps_attrs)

Check warning on line 36 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L36

Added line #L36 was not covered by tests
when "dep_group"
process_dep_group(cell_dep, lock_scripts, type_scripts, cell_deps_attrs)

Check warning on line 38 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L38

Added line #L38 was not covered by tests
end
end
end
CellDependency.upsert_all(cell_deps_attrs.to_a, unique_by: %i[ckb_transaction_id contract_cell_id dep_type], update_only: :is_used) if cell_deps_attrs.any?

Check warning on line 42 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L42

Added line #L42 was not covered by tests
end

def process_code_dep(cell_dep, lock_scripts, type_scripts, cell_deps_attrs)
cell_output = cell_dep.cell_output
is_lock_script = (lock_scripts[cell_output.data_hash] || lock_scripts[cell_output.type_script&.script_hash]).present?
is_type_script = (type_scripts[cell_output.data_hash] || type_scripts[cell_output.type_script&.script_hash]).present?
unless is_lock_script || is_type_script
cell_deps_attrs << { ckb_transaction_id: cell_dep.ckb_transaction_id,

Check warning on line 50 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L46-L50

Added lines #L46 - L50 were not covered by tests
contract_cell_id: cell_dep.contract_cell_id,
dep_type: cell_dep.dep_type, is_used: false }
end
end

def process_dep_group(cell_dep, lock_scripts, type_scripts, cell_deps_attrs)
mid_cell = cell_dep.cell_output
binary_data = mid_cell.binary_data
out_points_count = binary_data[0, 4].unpack1("L<")
is_used = false

Check warning on line 60 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L57-L60

Added lines #L57 - L60 were not covered by tests

0.upto(out_points_count - 1) do |i|
part_tx_hash, cell_index = binary_data[4 + i * 36, 36].unpack("H64L<")
tx_hash = "0x#{part_tx_hash}"
cell_output = CellOutput.find_by_pointer(tx_hash, cell_index)

Check warning on line 65 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L62-L65

Added lines #L62 - L65 were not covered by tests

is_lock_script = (lock_scripts[cell_output.data_hash] || lock_scripts[cell_output.type_script&.script_hash]).present?
is_type_script = (type_scripts[cell_output.data_hash] || type_scripts[cell_output.type_script&.script_hash]).present?
if is_lock_script || is_type_script
is_used = is_used || true

Check warning on line 70 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L67-L70

Added lines #L67 - L70 were not covered by tests
end
end
unless is_used
cell_deps_attrs << { ckb_transaction_id: cell_dep.ckb_transaction_id,

Check warning on line 74 in lib/tasks/migration/fill_is_used_to_cell_dependency.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/migration/fill_is_used_to_cell_dependency.rake#L73-L74

Added lines #L73 - L74 were not covered by tests
contract_cell_id: cell_dep.contract_cell_id,
dep_type: cell_dep.dep_type, is_used: false }

end
end
end

0 comments on commit 05d5bdc

Please sign in to comment.