Skip to content

Commit

Permalink
feat: async missing transactions (#1440)
Browse files Browse the repository at this point in the history
* feat: async missing transactions

* chore: cache process error

* chore: cache contract general info
  • Loading branch information
rabbitz committed Sep 7, 2023
1 parent 0ebba5a commit 9c54fed
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
16 changes: 10 additions & 6 deletions app/controllers/api/v2/scripts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ class ScriptsController < BaseController
before_action :find_script

def general_info
head :not_found and return if @script.blank?
head :not_found and return if @script.blank? || @contract.blank?

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
render json: { data: get_script_content }
key = ["contract_info", @contract.code_hash, @contract.hash_type]
result =
Rails.cache.fetch(key, expires_in: 10.minutes) do
get_script_content
end
render json: { data: result }
end

def ckb_transactions
head :not_found and return if @script.blank?
head :not_found and return if @contract.blank?

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
scope = CellDependency.where(contract_id: @contract.id).order(ckb_transaction_id: :desc)
Expand All @@ -24,14 +28,14 @@ def ckb_transactions
end

def deployed_cells
head :not_found and return if @script.blank? || @script.contract.blank?
head :not_found and return if @contract.blank?

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
@deployed_cells = @contract.deployed_cells.page(@page).per(@page_size).fast_page
end

def referring_cells
head :not_found and return if @script.blank?
head :not_found and return if @contract.blank?

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
@referring_cells = @contract.referring_cells.page(@page).per(@page_size).fast_page
Expand Down
61 changes: 61 additions & 0 deletions lib/tasks/migration/async_missing_transactions.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class AsyncMissingTransactions
include Rake::DSL

def initialize
namespace :migration do
desc "Usage: RAILS_ENV=production bundle exec rake migration:generate_referring_cells['2023-5-1', '2023-5-31']"
task :async_missing_transactions, [:star_date, :end_date] => :environment do |_, args|
start_at = DateTime.parse(args[:star_date]).beginning_of_day
end_at = DateTime.parse(args[:end_date]).end_of_day
count = 0

blocks = Block.where(created_at: start_at..end_at)
blocks.find_each do |local_block|
ApplicationRecord.transaction do
txs_count1 = local_block.ckb_transactions_count
txs_count2 = local_block.ckb_transactions.count
next if txs_count1 == txs_count2

puts "async missing block number: #{local_block.number} transactions count: #{txs_count1}"

node_block = CkbSync::Api.instance.get_block_by_number(local_block.number)
CkbSync::NewNodeDataProcessor.new.process_block(node_block)

process_duplicate_blocks(local_block.number)
count += 1
end
rescue StandardError => e
puts "async missing block number: #{local_block.number} failed, error:#{e}"
end

puts "done, the number of blocks: #{count}"
end
end
end

private

def process_duplicate_blocks(block_number)
blocks = Block.where(number: block_number).order(id: :asc)
return if blocks.count == 1

puts "block number: #{block_number} has two records: #{blocks.map(&:id)}"
old_block = blocks.first
puts "delete block #{old_block.id}"
old_block.mining_infos.first.destroy
old_block.destroy

current_block = Block.find_by_number(block_number)
CkbUtils.update_block_reward!(current_block)
CkbUtils.calculate_received_tx_fee!(current_block)

current_block.contained_addresses.each do |address|
address.cal_balance!
address.save!
end

UpdateAddressInfoWorker.new.perform(block_number)
end
end

AsyncMissingTransactions.new

0 comments on commit 9c54fed

Please sign in to comment.