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 383df40
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 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,82 @@
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
cell_deps_attrs = Set.new
CellDependency.where(block_number: range).group_by { |cell_dep| cell_dep.ckb_transaction_id }.each do |tx_id, cell_deps|
ckb_transaction = CkbTransaction.find_by(id: tx_id)
if ckb_transaction
type_scripts = Hash.new
lock_scripts = Hash.new

Check warning on line 23 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-L23

Added lines #L17 - L23 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 30 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#L25-L30

Added lines #L25 - L30 were not covered by tests
end
end

cell_deps.each do |cell_dep|
case cell_dep.dep_type

Check warning on line 35 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#L34-L35

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

Check warning on line 37 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#L37

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

Check warning on line 39 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#L39

Added line #L39 was not covered by tests
end
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 44 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#L44

Added line #L44 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 52 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#L48-L52

Added lines #L48 - L52 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 62 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#L59-L62

Added lines #L59 - L62 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 67 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#L64-L67

Added lines #L64 - L67 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 72 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#L69-L72

Added lines #L69 - L72 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 76 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#L75-L76

Added lines #L75 - L76 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 383df40

Please sign in to comment.