Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 445 #1472

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/cell_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CellOutput < ApplicationRecord
# the cell_inputs won't always be the same as `consumed_by`.`cell_inputs`
has_many :cell_inputs, foreign_key: :previous_cell_output_id
belongs_to :deployed_cell, optional: true
has_one :referring_cell
has_many :referring_cells
# the block_id is actually the same as ckb_transaction.block_id, must be on chain
# but one cell may be included by pending transactions, so block_id may be null
belongs_to :block, optional: true
Expand Down
34 changes: 17 additions & 17 deletions app/models/referring_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,26 @@ def self.create_initial_data_for_ckb_transaction(ckb_transaction)
outputs = ckb_transaction.outputs

(inputs + outputs).each do |cell|
contract = cell.lock_script.contract
contract ||= cell.type_script&.contract
contracts = [cell.lock_script.contract, cell.type_script&.contract].compact

next unless contract
next if contracts.empty?

if cell.live?
ReferringCell.create_or_find_by(
cell_output_id: cell.id,
ckb_transaction_id: ckb_transaction.id,
contract_id: contract.id
)
elsif cell.dead?
referring_cell = ReferringCell.find_by(
cell_output_id: cell.id,
ckb_transaction_id: ckb_transaction.id,
contract_id: contract.id
)
contracts.each do |contract|
if cell.live?
ReferringCell.create_or_find_by(
cell_output_id: cell.id,
ckb_transaction_id: ckb_transaction.id,
contract_id: contract.id
)
elsif cell.dead?
referring_cell = ReferringCell.find_by(
cell_output_id: cell.id,
ckb_transaction_id: ckb_transaction.id,
contract_id: contract.id
)

referring_cell.destroy if referring_cell
referring_cell.destroy if referring_cell
end
end
end
end
Expand All @@ -55,6 +56,5 @@ def self.create_initial_data_for_ckb_transaction(ckb_transaction)
#
# Indexes
#
# index_referring_cells_on_cell_output_id (cell_output_id) UNIQUE
# index_referring_cells_on_contract_id_and_cell_output_id (contract_id,cell_output_id) UNIQUE
#
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveReferringCellCellOutputIdIndex < ActiveRecord::Migration[7.0]
def change
remove_index :referring_cells, name: :index_referring_cells_on_cell_output_id, column: :cell_output_id
end
end
10 changes: 2 additions & 8 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3965,13 +3965,6 @@ CREATE INDEX index_pool_transaction_entries_on_tx_hash ON public.pool_transactio
CREATE INDEX index_pool_transaction_entries_on_tx_status ON public.pool_transaction_entries USING btree (tx_status);


--
-- Name: index_referring_cells_on_cell_output_id; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX index_referring_cells_on_cell_output_id ON public.referring_cells USING btree (cell_output_id);


--
-- Name: index_referring_cells_on_contract_id_and_cell_output_id; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -4756,6 +4749,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20230829061910'),
('20230913091025'),
('20230914120928'),
('20230918033957');
('20230918033957'),
('20231017074221');


44 changes: 37 additions & 7 deletions lib/tasks/migration/generate_referring_cells.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,46 @@ namespace :migration do
outputs.each do |output|
progress_bar.increment

contract = output.lock_script&.contract
contract ||= output.type_script&.contract
contracts = [cell.lock_script.contract, cell.type_script&.contract].compact

next if contracts.empty?

contracts.each do |contract|
ReferringCell.create_or_find_by(
cell_output_id: output.id,
ckb_transaction_id: output.ckb_transaction_id,
contract_id: contract.id
)
end
end
end

puts "done"
end

desc "Usage: RAILS_ENV=production bundle exec rake migration:generate_missed_type_script_contract_referring_cells"
task generate_missed_type_script_contract_referring_cells: :environment do
contract_hashes = Contract.pluck(:code_hash)
binary_hashes = CkbUtils.hexes_to_bins(contract_hashes)
contract_type_ids = TypeScript.where(code_hash: binary_hashes).pluck(:id)
live_cells = CellOutput.live.where(type_script_id: contract_type_ids)
progress_bar = ProgressBar.create({ total: live_cells.count, format: "%e %B %p%% %c/%C" })

live_cells.find_in_batches do |outputs|
outputs.each do |output|
progress_bar.increment

contract = cell.type_script&.contract

next unless contract

ReferringCell.create_or_find_by(
cell_output_id: output.id,
ckb_transaction_id: output.ckb_transaction_id,
contract_id: contract.id
)
contracts.each do |contract|
ReferringCell.create_or_find_by(
cell_output_id: output.id,
ckb_transaction_id: output.ckb_transaction_id,
contract_id: contract.id
)
end
end
end

Expand Down