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

Deploy to testnet #1439

Merged
merged 3 commits into from
Sep 12, 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
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
36 changes: 19 additions & 17 deletions app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def call
raise e
end

def process_block(node_block)
def process_block(node_block, refresh_balance: true)
local_block = nil

ApplicationRecord.transaction do
Expand All @@ -77,7 +77,7 @@ def process_block(node_block)
@contained_udt_ids = contained_udt_ids = []
@contained_address_ids = contained_address_ids = []

process_ckb_txs(ckb_txs, contained_address_ids, contained_udt_ids, dao_address_ids, tags, udt_address_ids)
process_ckb_txs(node_block, ckb_txs, contained_address_ids, contained_udt_ids, dao_address_ids, tags, udt_address_ids)
addrs_changes = Hash.new { |hash, key| hash[key] = {} }
input_capacities, output_capacities = build_cells_and_locks!(local_block, node_block, ckb_txs, inputs, outputs,
tags, udt_address_ids, dao_address_ids, contained_udt_ids, contained_address_ids, addrs_changes)
Expand All @@ -93,7 +93,7 @@ def process_block(node_block)
# maybe can be changed to asynchronous update
update_udt_info(local_block)
process_dao_events!(local_block)
update_addresses_info(addrs_changes, local_block)
update_addresses_info(addrs_changes, local_block) if refresh_balance
end

flush_inputs_outputs_caches(local_block)
Expand Down Expand Up @@ -136,7 +136,7 @@ def generate_statistics_data(local_block)
GenerateStatisticsDataWorker.perform_async(local_block.id)
end

def process_ckb_txs(ckb_txs, contained_address_ids, contained_udt_ids, dao_address_ids, tags, udt_address_ids)
def process_ckb_txs(node_block, ckb_txs, contained_address_ids, contained_udt_ids, dao_address_ids, tags, udt_address_ids)
tx_index = 0
ckb_txs.each do |cbk_tx|
cbk_tx["tx_hash"][0] = "0"
Expand All @@ -147,7 +147,8 @@ def process_ckb_txs(ckb_txs, contained_address_ids, contained_udt_ids, dao_addre
contained_address_ids[tx_index] = Set.new
tx_index += 1
end
ckb_txs.sort! { |tx1, tx2| tx1["id"] <=> tx2["id"] }
tx_hashes = node_block.transactions.map(&:hash)
ckb_txs.sort_by! { |tx| tx_hashes.index(tx["tx_hash"]) }
end

attr_accessor :local_cache
Expand Down Expand Up @@ -201,16 +202,16 @@ def build_new_dao_depositor_events!(local_block, new_dao_depositors, dao_contrac
end

# Process DAO withdraw
# Warning:because DAO withdraw is also a cell, to the destination address of withdrawl is the address of the withdraw cell output.
# So it's possible that the deposit address is different with the withrawal address.
# Warning:because DAO withdraw is also a cell, to the destination address of withdraw is the address of the withdraw cell output.
# So it's possible that the deposit address is different with the withdraw address.
def process_withdraw_dao_events!(local_block, new_dao_depositors, dao_contract)
dao_contract = DaoContract.default_contract
withdraw_amount = 0
withdraw_transaction_ids = Set.new
addrs_withdraw_info = {}
claimed_compensation = 0
take_away_all_deposit_count = 0
# When DAO Deposit Cell appears in cell inputs, the transcation is DAO withdrawal
# When DAO Deposit Cell appears in cell inputs, the transaction is DAO withdrawal
local_block.cell_inputs.nervos_dao_deposit.select(:id, :ckb_transaction_id,
:previous_cell_output_id).find_in_batches do |dao_inputs|
dao_events_attributes = []
Expand Down Expand Up @@ -590,9 +591,7 @@ def update_addresses_info(addrs_change, local_block)
end

def save_address_block_snapshot!(addr, local_block)
AddressBlockSnapshot.create!(
address_id: addr.id,
block_id: local_block.id,
AddressBlockSnapshot.create_with(
block_number: local_block.number,
final_state: {
balance: addr.balance,
Expand All @@ -601,6 +600,9 @@ def save_address_block_snapshot!(addr, local_block)
live_cells_count: addr.live_cells_count,
dao_transactions_count: addr.dao_transactions_count
}
).find_or_create_by!(
address_id: addr.id,
block_id: local_block.id
)
end

Expand Down Expand Up @@ -822,7 +824,7 @@ def build_cells_and_locks!(
end

if cell_data_attrs.present?
CellDatum.upsert_all(cell_data_attrs)
CellDatum.upsert_all(cell_data_attrs, unique_by: [:cell_output_id])
end
end

Expand Down Expand Up @@ -1101,7 +1103,7 @@ def cell_output_attributes(output, address, ckb_transaction, local_block, cell_i
created_at: Time.current,
updated_at: Time.current
}
# binding.pry

if binary_data && binary_data.bytesize > 0
attrs[:data_size] = binary_data.bytesize
data_hash = CKB::Utils.bin_to_hex(CKB::Blake2b.digest(binary_data))
Expand Down Expand Up @@ -1329,10 +1331,8 @@ def build_block!(node_block)
miner_hash = CkbUtils.miner_hash(cellbase)
miner_lock_hash = CkbUtils.miner_lock_hash(cellbase)
base_reward = CkbUtils.base_reward(header.number, epoch_info.number)
block = Block.find_or_create_by!(
block = Block.create_with(
compact_target: header.compact_target,
block_hash: header.hash,
number: header.number,
parent_hash: header.parent_hash,
nonce: header.nonce,
timestamp: header.timestamp,
Expand Down Expand Up @@ -1362,6 +1362,9 @@ def build_block!(node_block)
miner_message: CkbUtils.miner_message(cellbase),
extension: node_block.extension,
median_timestamp: get_median_timestamp(header.hash)
).find_or_create_by!(
block_hash: header.hash,
number: header.number
)
end
block
Expand Down Expand Up @@ -1438,7 +1441,6 @@ def update_nrc_factory_cell_info(type_script, output_data)
hash_type: type_script.hash_type,
args: type_script.args
)
# if factory_cell&.verified
parsed_factory_data = CkbUtils.parse_nrc_721_factory_data(output_data)
factory_cell.update(
name: parsed_factory_data.name,
Expand Down
2 changes: 1 addition & 1 deletion app/models/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Contract < ApplicationRecord
has_many :deployed_cells
has_many :deployed_cell_outputs, through: :deployed_cells, source: :cell_output
has_many :referring_cells
has_many :referring_cell_outputs, through: :deployed_cells, source: :cell_output
has_many :referring_cell_outputs, through: :referring_cells, source: :cell_output
has_many :cell_dependencies
has_many :ckb_transactions, through: :cell_dependencies

Expand Down
2 changes: 0 additions & 2 deletions app/models/dao_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ def get_froms
CkbUtils.generate_address(CkbUtils.parse_address(display_input[:address_hash]).script)
end
when "withdraw_from_dao"

ckb_transaction.display_inputs.select { |display_input|
display_input[:cell_type] == "nervos_dao_deposit"
}.map do |display_input|
CkbUtils.generate_address(CkbUtils.parse_address(display_input[:address_hash]).script)
end
when "issue_interest"

ckb_transaction.display_inputs.select { |display_input|
display_input[:cell_type] == "nervos_dao_withdrawing"
}.map do |display_input|
Expand Down
10 changes: 4 additions & 6 deletions app/services/das_indexer_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ def reverse_record(ckb_address)
}
})
data = JSON.parse(res.to_s)

case data["err_no"]
when 0
if data["err_no"] == 0
return data["data"]["account"]
when 20007
return ""
else
raise data["err_msg"] || "Unknown Error"
err_msg = data["err_msg"]
Rails.logger.info "das service response error: #{err_msg}, address: #{ckb_address}"
return ""
end
end
end
6 changes: 4 additions & 2 deletions app/utils/ckb_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,10 @@ def self.update_current_block_mining_info(block)
return if block.blank?

miner_address = block.miner_address
MiningInfo.create!(block: block, block_number: block.number, address: miner_address, status: "mined")
miner_address.increment!(:mined_blocks_count)
unless block.mining_infos.exists?(block_number: block.number, address: miner_address)
block.mining_infos.create!(block_number: block.number, address: miner_address, status: "mined")
miner_address.increment!(:mined_blocks_count)
end
end

def self.normal_tx_fee(input_capacities, output_capacities)
Expand Down
11 changes: 7 additions & 4 deletions app/workers/update_address_info_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ def perform(block_number)
return if block.blank?

block.contained_addresses.select(:id, :mined_blocks_count, :created_at).each do |addr|
next if addr.mined_blocks_count > 0

balance, balance_occupied = addr.cal_balance
address_attributes << {
id: addr.id, balance: addr.cell_outputs.live.sum(:capacity),
id: addr.id,
balance: balance,
balance_occupied: balance_occupied,
ckb_transactions_count: AccountBook.where(address_id: addr.id).count,
live_cells_count: addr.cell_outputs.live.count,
dao_transactions_count: addr.ckb_dao_transactions.count,
created_at: addr.created_at, updated_at: Time.current
created_at: addr.created_at,
updated_at: Time.current
}
end

if address_attributes.present?
Address.upsert_all(address_attributes)
block.contained_addresses.each(&:touch)
Expand Down
28 changes: 28 additions & 0 deletions lib/tasks/migration/async_missing_transactions.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace :migration do
desc "Usage: RAILS_ENV=production bundle exec rake 'migration:async_missing_transactions[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, refresh_balance: false)
UpdateAddressInfoWorker.new.perform(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
Loading