Skip to content

Commit

Permalink
feat: add contract statistic (#1456)
Browse files Browse the repository at this point in the history
* feat: add contract statistic

* chore: adjust tests
  • Loading branch information
rabbitz authored Sep 18, 2023
1 parent fa5ef5a commit 11ad77c
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 75 deletions.
27 changes: 10 additions & 17 deletions app/controllers/api/v2/scripts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ class ScriptsController < BaseController
def general_info
head :not_found and return if @script.blank? || @contract.blank?

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 }
expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
render json: { data: get_script_content }
end

def ckb_transactions
Expand All @@ -31,23 +27,20 @@ def deployed_cells
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
@deployed_cells = @contract.deployed_cell_outputs.live.page(@page).per(@page_size).fast_page
end

def referring_cells
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
@referring_cells = @contract.referring_cell_outputs.live.page(@page).per(@page_size).fast_page
end

private

def get_script_content
referring_cells = @contract&.referring_cell_outputs
deployed_cells = @contract&.deployed_cell_outputs&.live
transactions = @contract&.cell_dependencies

deployed_cells = @contract.deployed_cell_outputs
if deployed_cells.present?
deployed_type_script = deployed_cells[0].type_script
if deployed_type_script.code_hash == Settings.type_id_code_hash
Expand All @@ -60,11 +53,11 @@ def get_script_content
code_hash: @script.code_hash,
hash_type: @script.hash_type,
script_type: @script.class.to_s,
capacity_of_deployed_cells: deployed_cells&.sum(:capacity),
capacity_of_referring_cells: referring_cells&.sum(:capacity),
count_of_transactions: transactions&.count.to_i,
count_of_deployed_cells: deployed_cells&.count.to_i,
count_of_referring_cells: referring_cells&.count.to_i
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
}
end

Expand Down
29 changes: 17 additions & 12 deletions app/models/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,23 @@ def self.create_initial_data
#
# Table name: contracts
#
# id :bigint not null, primary key
# code_hash :binary
# hash_type :string
# deployed_args :string
# role :string default("type_script")
# name :string
# symbol :string
# description :string
# verified :boolean default(FALSE)
# created_at :datetime not null
# updated_at :datetime not null
# deprecated :boolean
# id :bigint not null, primary key
# code_hash :binary
# hash_type :string
# deployed_args :string
# role :string default("type_script")
# name :string
# symbol :string
# description :string
# verified :boolean default(FALSE)
# created_at :datetime not null
# updated_at :datetime not null
# deprecated :boolean
# ckb_transactions_count :decimal(30, ) default(0)
# deployed_cells_count :decimal(30, ) default(0)
# referring_cells_count :decimal(30, ) default(0)
# total_deployed_cells_capacity :decimal(30, ) default(0)
# total_referring_cells_capacity :decimal(30, ) default(0)
#
# Indexes
#
Expand Down
43 changes: 21 additions & 22 deletions app/views/api/v2/scripts/deployed_cells.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
json.data do
json.deployed_cells @deployed_cells do |deployed_cell|
cell_output = deployed_cell.cell_output
json.id deployed_cell.cell_output.id
json.capacity deployed_cell.cell_output.capacity
json.ckb_transaction_id deployed_cell.cell_output.ckb_transaction_id
json.created_at deployed_cell.cell_output.created_at
json.updated_at deployed_cell.cell_output.updated_at
json.status cell_output.status
json.address_id cell_output.address_id
json.block_id cell_output.block_id
json.tx_hash cell_output.tx_hash
json.cell_index cell_output.cell_index
json.consumed_by_id cell_output.consumed_by_id
json.cell_type cell_output.cell_type
json.data_size cell_output.data_size
json.occupied_capacity cell_output.occupied_capacity
json.block_timestamp cell_output.block_timestamp
json.consumed_block_timestamp cell_output.consumed_block_timestamp
json.type_hash cell_output.type_hash
json.udt_amount cell_output.udt_amount
json.dao cell_output.dao
json.lock_script_id cell_output.lock_script_id
json.type_script_id cell_output.type_script_id
json.id deployed_cell.id
json.capacity deployed_cell.capacity
json.ckb_transaction_id deployed_cell.ckb_transaction_id
json.created_at deployed_cell.created_at
json.updated_at deployed_cell.updated_at
json.status deployed_cell.status
json.address_id deployed_cell.address_id
json.block_id deployed_cell.block_id
json.tx_hash deployed_cell.tx_hash
json.cell_index deployed_cell.cell_index
json.consumed_by_id deployed_cell.consumed_by_id
json.cell_type deployed_cell.cell_type
json.data_size deployed_cell.data_size
json.occupied_capacity deployed_cell.occupied_capacity
json.block_timestamp deployed_cell.block_timestamp
json.consumed_block_timestamp deployed_cell.consumed_block_timestamp
json.type_hash deployed_cell.type_hash
json.udt_amount deployed_cell.udt_amount
json.dao deployed_cell.dao
json.lock_script_id deployed_cell.lock_script_id
json.type_script_id deployed_cell.type_script_id
end
json.meta do
json.total @deployed_cells.total_count
Expand Down
43 changes: 21 additions & 22 deletions app/views/api/v2/scripts/referring_cells.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
json.data do
json.referring_cells @referring_cells do |referring_cell|
cell_output = referring_cell.cell_output
json.id cell_output.id
json.capacity cell_output.capacity
json.ckb_transaction_id cell_output.ckb_transaction_id
json.created_at cell_output.created_at
json.updated_at cell_output.updated_at
json.status cell_output.status
json.address_id cell_output.address_id
json.block_id cell_output.block_id
json.tx_hash cell_output.tx_hash
json.cell_index cell_output.cell_index
json.consumed_by_id cell_output.consumed_by_id
json.cell_type cell_output.cell_type
json.data_size cell_output.data_size
json.occupied_capacity cell_output.occupied_capacity
json.block_timestamp cell_output.block_timestamp
json.consumed_block_timestamp cell_output.consumed_block_timestamp
json.type_hash cell_output.type_hash
json.udt_amount cell_output.udt_amount
json.dao cell_output.dao
json.lock_script_id cell_output.lock_script_id
json.type_script_id cell_output.type_script_id
json.id referring_cell.id
json.capacity referring_cell.capacity
json.ckb_transaction_id referring_cell.ckb_transaction_id
json.created_at referring_cell.created_at
json.updated_at referring_cell.updated_at
json.status referring_cell.status
json.address_id referring_cell.address_id
json.block_id referring_cell.block_id
json.tx_hash referring_cell.tx_hash
json.cell_index referring_cell.cell_index
json.consumed_by_id referring_cell.consumed_by_id
json.cell_type referring_cell.cell_type
json.data_size referring_cell.data_size
json.occupied_capacity referring_cell.occupied_capacity
json.block_timestamp referring_cell.block_timestamp
json.consumed_block_timestamp referring_cell.consumed_block_timestamp
json.type_hash referring_cell.type_hash
json.udt_amount referring_cell.udt_amount
json.dao referring_cell.dao
json.lock_script_id referring_cell.lock_script_id
json.type_script_id referring_cell.type_script_id
end
json.meta do
json.total @referring_cells.total_count
Expand Down
20 changes: 20 additions & 0 deletions app/workers/contact_statistic_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class ContractStatisticWorker
include Sidekiq::Worker
sidekiq_options queue: "critical"

def perform
Contract.find_each do |contract|
referring_cells = contract.referring_cell_outputs&.live
deployed_cells = contract.deployed_cell_outputs&.live
transactions = contract.cell_dependencies

contract.update(
ckb_transactions_count: transactions.count,
deployed_cells_count: deployed_cells&.count.to_i,
referring_cells_count: referring_cells&.count.to_i,
total_deployed_cells_capacity: deployed_cells&.sum(:capacity),
total_referring_cells_capacity: referring_cells&.sum(:capacity)
)
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20230918033957_add_statistics_to_contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddStatisticsToContract < ActiveRecord::Migration[7.0]
def change
add_column :contracts, :ckb_transactions_count, :decimal, precision: 30, default: 0
add_column :contracts, :deployed_cells_count, :decimal, precision: 30, default: 0
add_column :contracts, :referring_cells_count, :decimal, precision: 30, default: 0
add_column :contracts, :total_deployed_cells_capacity, :decimal, precision: 30, default: 0
add_column :contracts, :total_referring_cells_capacity, :decimal, precision: 30, default: 0
end
end
10 changes: 8 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,12 @@ CREATE TABLE public.contracts (
verified boolean DEFAULT false,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL,
deprecated boolean
deprecated boolean,
ckb_transactions_count numeric(30,0) DEFAULT 0.0,
deployed_cells_count numeric(30,0) DEFAULT 0.0,
referring_cells_count numeric(30,0) DEFAULT 0.0,
total_deployed_cells_capacity numeric(30,0) DEFAULT 0.0,
total_referring_cells_capacity numeric(30,0) DEFAULT 0.0
);


Expand Down Expand Up @@ -4750,6 +4755,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20230808020637'),
('20230829061910'),
('20230913091025'),
('20230914120928');
('20230914120928'),
('20230918033957');


4 changes: 4 additions & 0 deletions lib/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,8 @@ def call_worker(clz)
call_worker CleanAddressBlockSnapshotWorker
end

s.every "1h", overlap: false do
call_worker ContractStatisticWorker
end

s.join

0 comments on commit 11ad77c

Please sign in to comment.