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 #2307

Merged
merged 1 commit into from
Dec 4, 2024
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
126 changes: 62 additions & 64 deletions app/controllers/api/v2/scripts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,104 +3,102 @@
module Api
module V2
class ScriptsController < BaseController
before_action :set_page_and_page_size, except: :referring_capacities
before_action :find_script, except: :referring_capacities
before_action :set_page_and_page_size
before_action :set_contracts

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

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
render json: { data: get_script_content }
end

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

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
@ckb_transactions =
if @contract.ckb_transactions_count.zero?
CkbTransaction.none
else
@contract.ckb_transactions.order(block_number: :desc).page(@page).per(@page_size).fast_page
end
head :not_found and return if @contracts.blank?

contract_ids = @contracts.map { |contract| contract.id }
contract_cell_ids = CellDepsOutPoint.list_contract_cell_ids_by_contract(contract_ids)
base_query = CkbTransaction.joins(:cell_dependencies).
where(cell_dependencies: { contract_cell_id: contract_cell_ids }).
order("cell_dependencies.block_number DESC, cell_dependencies.tx_index DESC").
limit(10000)
@ckb_transactions = CkbTransaction.from("(#{base_query.to_sql}) AS ckb_transactions").
order("block_number DESC, tx_index DESC").
page(@page).
per(@page_size)
end

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

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
@deployed_cells =
if @contract.deployed_cells_count.zero?
CellOutput.none
else
@contract.deployed_cell_outputs.live.page(@page).per(@page_size)
end

@deployed_cells = CellOutput.live.where(id: @contracts.map(&:deployed_cell_output_id)).page(@page).per(@page_size)
end

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

expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds

if @contract.referring_cells_count.zero?
@referring_cells = CellOutput.none
else
scope = @contract.referring_cell_outputs.live.where.not(block_id: nil)
if params[:args].present?
type_script = TypeScript.find_by(args: params[:args])
scope = scope.where(cell_outputs: { type_script_id: type_script.id })
end
if params[:address_hash].present?
address = Addresses::Explore.run!(key: params[:address_hash])
scope = if address.is_a?(NullAddress)
CellOutput.none
else
scope.where(cell_outputs: { address_id: address.map(&:id) })
end
end

@referring_cells = sort_referring_cells(scope).page(@page).per(@page_size)
scope = Contract.referring_cells_query(@contracts).
order("block_timestamp DESC, cell_index DESC").
limit(10000)
if params[:args].present?
type_script = TypeScript.find_by(args: params[:args])
scope = scope.or(CellOutput.where(type_script_id: type_script.id))
end
if params[:address_hash].present?
address = Addresses::Explore.run!(key: params[:address_hash])
scope = scope.where(address_id: address.map(&:id))
end

@referring_cells =
CellOutput.from("(#{scope.to_sql}) AS cell_outputs").
order("block_timestamp DESC, cell_index DESC").
page(@page).
per(@page_size)
end

private

def get_script_content
deployed_cells = @contract.deployed_cell_outputs
if deployed_cells.present?
deployed_type_script = deployed_cells[0].type_script
if deployed_type_script && deployed_type_script.code_hash == Settings.type_id_code_hash
type_id = deployed_type_script.script_hash
sum_hash =
@contracts.inject({
capacity_of_deployed_cells: 0,
capacity_of_referring_cells: 0,
count_of_transactions: 0,
count_of_deployed_cells: 0,
count_of_referring_cells: 0,
}) do |sum, contract|
sum[:capacity_of_deployed_cells] += contract.total_deployed_cells_capacity
sum[:capacity_of_referring_cells] += contract.total_referring_cells_capacity
sum[:count_of_transactions] += contract.ckb_transactions_count
sum[:count_of_deployed_cells] += 1
sum[:count_of_referring_cells] += contract.referring_cells_count
sum
end
end

{
id: type_id,
code_hash: @script.code_hash,
hash_type: @script.hash_type,
script_type: @script.class.to_s,
capacity_of_deployed_cells: @contract.total_deployed_cells_capacity,
capacity_of_referring_cells: @contract.total_referring_cells_capacity,
count_of_transactions: @contract.ckb_transactions_count,
count_of_deployed_cells: @contract.deployed_cells_count,
count_of_referring_cells: @contract.referring_cells_count,
}
id: @contracts.first.type_hash,
code_hash: params[:code_hash],
hash_type: params[:hash_type],
script_type: @contracts.first.is_lock_script ? "LockScript" : "TypeScript",
}.merge(sum_hash)
end

def set_page_and_page_size
@page = params[:page] || 1
@page_size = params[:page_size] || 10
end

def find_script
@script = TypeScript.find_by(code_hash: params[:code_hash],
hash_type: params[:hash_type])
if @script.blank?
@script = LockScript.find_by(code_hash: params[:code_hash],
hash_type: params[:hash_type])
end
@contract = Contract.find_by(code_hash: params[:code_hash],
hash_type: params[:hash_type])
def set_contracts
@contracts =
case params[:hash_type]
when "data", "data1", "data2"
Contract.where(data_hash: params[:code_hash])
when "type"
Contract.where(type_hash: params[:code_hash])
end
end

def sort_referring_cells(records)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v2/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def transaction_fees
end

def contract_resource_distributed
contracts = Contract.filter_nil_hash_type
contracts = Contract.active
if params[:code_hashes].present?
hashes = params[:code_hashes].split(",")
contracts = contracts.where(code_hash: hashes)
contracts = contracts.where(type_hash: hashes)
end
if stale?(contracts)
expires_in 30.minutes, public: true
Expand Down
27 changes: 14 additions & 13 deletions app/models/cell_dependency.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# this is the ReferringCell model, parse from `cell_deps` of transaction raw hash
class CellDependency < ApplicationRecord
belongs_to :contract, optional: true
belongs_to :ckb_transaction
belongs_to :script
belongs_to :cell_output, foreign_key: "contract_cell_id", class_name: "CellOutput"
enum :dep_type, [:code, :dep_group]
scope :implicit, -> { where(implicit: true) }
scope :explicit, -> { where(implicit: false) }
belongs_to :cell_deps_out_point, foreign_key: :contract_cell_id, primary_key: :contract_cell_id, optional: true

enum :dep_type, %i[code dep_group]

def self.refresh_implicit
connection.execute "SELECT update_cell_dependencies_implicit();"
Expand All @@ -16,9 +14,9 @@ def to_raw
{
out_point: {
tx_hash: cell_output.tx_hash,
index: cell_output.cell_index
index: cell_output.cell_index,
},
dep_type: dep_type
dep_type:,
}
end
end
Expand All @@ -28,17 +26,20 @@ def to_raw
# Table name: cell_dependencies
#
# id :bigint not null, primary key
# contract_id :bigint
# ckb_transaction_id :bigint not null
# dep_type :integer
# contract_cell_id :bigint not null
# script_id :bigint
# implicit :boolean default(TRUE), not null
# contract_id :bigint
# implicit :boolean
# block_number :bigint
# tx_index :integer
# contract_analyzed :boolean default(FALSE)
#
# Indexes
#
# cell_deps_tx_cell_idx (ckb_transaction_id,contract_cell_id) UNIQUE
# index_cell_dependencies_on_contract_cell_id (contract_cell_id)
# index_cell_dependencies_on_contract_id (contract_id)
# index_cell_dependencies_on_script_id (script_id)
# index_cell_dependencies_on_block_number_and_tx_index (block_number,tx_index)
# index_cell_dependencies_on_contract_analyzed (contract_analyzed)
# index_cell_dependencies_on_tx_id_and_cell_id_and_dep_type (ckb_transaction_id,contract_cell_id,dep_type) UNIQUE
# index_on_cell_dependencies_contract_cell_block_tx (contract_cell_id,block_number DESC,tx_index DESC)
#
23 changes: 23 additions & 0 deletions app/models/cell_deps_out_point.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class CellDepsOutPoint < ApplicationRecord
belongs_to :contract, foreign_key: :deployed_cell_output_id, primary_key: :deployed_cell_output_id, optional: true
has_many :cell_dependencies, foreign_key: :contract_cell_id, primary_key: :contract_cell_id

scope :list_contract_cell_ids_by_contract, ->(contract_ids) { joins(:contract).where(contracts: { id: contract_ids }).pluck(:contract_cell_id) }
end

# == Schema Information
#
# Table name: cell_deps_out_points
#
# id :bigint not null, primary key
# tx_hash :binary
# cell_index :integer
# deployed_cell_output_id :bigint
# contract_cell_id :bigint
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_cell_deps_out_points_on_contract_cell_id_deployed_cell_id (contract_cell_id,deployed_cell_output_id) UNIQUE
#
5 changes: 3 additions & 2 deletions app/models/cell_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class CellOutput < ApplicationRecord
# but one cell may be included by many pending transactions,
# 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_many :referring_cells
# belongs_to :deployed_cell, optional: true
# 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 Expand Up @@ -108,6 +108,7 @@ class CellOutput < ApplicationRecord
scope :occupied, -> {
where.not(type_hash: nil).or(where.not(data_hash: nil))
}
scope :by_scripts, ->(lock_script_ids, type_script_ids) { where("lock_script_id IN (?) OR type_script_id IN (?)", lock_script_ids, type_script_ids) }

before_create :setup_address

Expand Down
4 changes: 2 additions & 2 deletions app/models/ckb_sync/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ def spore_cluster_code_hashes
if mode == CKB::MODE::MAINNET
[Settings.spore_cluster1_code_hash]
else
[Settings.spore_cluster1_code_hash, Settings.spore_cluster2_code_hash, Settings.spore_cluster3_code_hash]
[Settings.spore_cluster1_code_hash, Settings.spore_cluster2_code_hash]
end
end

def spore_cell_code_hashes
if mode == CKB::MODE::MAINNET
[Settings.spore_cell1_code_hash]
else
[Settings.spore_cell1_code_hash, Settings.spore_cell2_code_hash, Settings.spore_cell3_code_hash]
[Settings.spore_cell1_code_hash, Settings.spore_cell2_code_hash]
end
end

Expand Down
Loading
Loading