Skip to content

Commit

Permalink
Merge pull request #1531 from nervosnetwork/develop
Browse files Browse the repository at this point in the history
Deploy to testnet
  • Loading branch information
rabbitz authored Dec 22, 2023
2 parents 4bbff70 + 8864b57 commit cf9ff37
Show file tree
Hide file tree
Showing 33 changed files with 772 additions and 828 deletions.
665 changes: 548 additions & 117 deletions .rubocop.yml
100644 → 100755

Large diffs are not rendered by default.

650 changes: 0 additions & 650 deletions .rubocop_thoughtbot.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def show
address = Address.find_address!(params[:id])
raise Api::V1::Exceptions::AddressNotFoundError if address.is_a?(NullAddress)

ckb_dao_transactions = address.ckb_dao_transactions.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).recent.page(@page).per(@page_size).fast_page
ckb_dao_transactions = address.ckb_dao_transactions.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at).recent.page(@page).per(@page_size).fast_page
json =
Rails.cache.realize(ckb_dao_transactions.cache_key, version: ckb_dao_transactions.cache_version) do
records_counter = RecordCounters::AddressDaoTransactions.new(address)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/address_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def show
ckb_transaction_ids = tx_ids.map(&:ckb_transaction_id)
ckb_transactions = CkbTransaction.where(id: ckb_transaction_ids).
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp,
:is_cellbase, :updated_at, :capacity_involved).
:is_cellbase, :updated_at, :capacity_involved, :created_at).
order(order_by => asc_or_desc)

options = FastJsonapi::PaginationMetaGenerator.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def show
udt = Udt.find_by(type_hash: params[:type_hash], published: true)
raise Api::V1::Exceptions::UdtNotFoundError if udt.blank?

ckb_dao_transactions = address.ckb_udt_transactions(udt.id).select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).recent.page(@page).per(@page_size).fast_page
ckb_dao_transactions = address.ckb_udt_transactions(udt.id).
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at).
recent.page(@page).per(@page_size).fast_page
json =
Rails.cache.realize(ckb_dao_transactions.cache_key, version: ckb_dao_transactions.cache_version) do
records_counter = RecordCounters::AddressUdtTransactions.new(address, udt.id)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/block_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BlockTransactionsController < ApplicationController
def show
block = Block.find_by!(block_hash: params[:id])
ckb_transactions = block.ckb_transactions.
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at).
order(is_cellbase: :desc, id: :asc)

if params[:tx_hash].present?
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/api/v1/ckb_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CkbTransactionsController < ApplicationController
def index
if from_home_page?
ckb_transactions = CkbTransaction.tx_committed.recent.normal.select(
:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved, :updated_at
:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved, :updated_at, :created_at
).limit((Settings.homepage_transactions_records_count || 15).to_i)
json =
Rails.cache.realize(ckb_transactions.cache_key,
Expand All @@ -18,7 +18,7 @@ def index
render json: json
else
ckb_transactions = CkbTransaction.tx_committed.normal.select(
:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved, :updated_at
:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved, :updated_at, :created_at
)

params[:sort] ||= "id.desc"
Expand Down Expand Up @@ -84,7 +84,7 @@ def query
CkbTransaction.recent.normal.page(@page).per(@page_size).fast_page
end
ckb_transactions = ckb_transactions.select(:id, :tx_hash, :block_id,
:block_number, :block_timestamp, :is_cellbase, :updated_at)
:block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at)
json =
Rails.cache.realize(ckb_transactions.cache_key,
version: ckb_transactions.cache_version, race_condition_ttl: 1.minute) do
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/contract_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def show
expires_in 10.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds

ckb_transactions = dao_contract.ckb_transactions.includes(:cell_inputs, :cell_outputs).tx_committed.select(
:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at
:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at
).order("ckb_transactions.block_timestamp desc nulls last, ckb_transactions.id desc")

if params[:tx_hash].present?
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/udt_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def show

ckb_transactions = udt.ckb_transactions.tx_committed.
select(:id, :tx_hash, :block_id, :block_number,
:block_timestamp, :is_cellbase, :updated_at).
:block_timestamp, :is_cellbase, :updated_at, :created_at).
order("ckb_transactions.block_timestamp desc nulls last, ckb_transactions.id desc")

if params[:tx_hash].present?
Expand Down
21 changes: 18 additions & 3 deletions app/controllers/api/v2/scripts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,36 @@ 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 = @contract.ckb_transactions.order(id: :desc).page(@page).per(@page_size)
@ckb_transactions =
if @contract.ckb_transactions_count.zero?
CkbTransaction.none
else
@contract.ckb_transactions.order(id: :desc).page(@page).per(@page_size)
end
end

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_cell_outputs.live.page(@page).per(@page_size)
@deployed_cells =
if @contract.deployed_cells_count.zero?
CellOutput.none
else
@contract.deployed_cell_outputs.live.page(@page).per(@page_size)
end
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_cell_outputs.live.page(@page).per(@page_size)
@referring_cells =
if @contract.referring_cells_count.zero?
CellOutput.none
else
@contract.referring_cell_outputs.live.page(@page).per(@page_size)
end
end

private
Expand Down
6 changes: 5 additions & 1 deletion app/models/ckb_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ def cell_deps
end

def income(address)
outputs.where(address: address).sum(:capacity) - inputs.where(address: address).sum(:capacity)
if tx_pending?
cell_outputs.where(address: address).sum(:capacity) - input_cells.where(address: address).sum(:capacity)
else
outputs.where(address: address).sum(:capacity) - inputs.where(address: address).sum(:capacity)
end
end

def dao_transaction?
Expand Down
26 changes: 16 additions & 10 deletions app/models/statistic_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def epoch_info
{
epoch_number: tip_block.epoch.to_s,
epoch_length: tip_block.length.to_s,
index: (tip_block_number - tip_block.start_number).to_s
index: (tip_block_number - tip_block.start_number).to_s,
}
end

Expand All @@ -49,7 +49,8 @@ def current_epoch_difficulty
define_logic :transactions_count_per_minute do
interval = 100
start_block_number = [tip_block_number.to_i - interval + 1, 0].max
timestamps = Block.where(number: [start_block_number, tip_block_number]).recent.pluck(:timestamp)
timestamps = Block.where(number: [start_block_number,
tip_block_number]).recent.pluck(:timestamp)
next if timestamps.empty?

transactions_count = Block.where(number: start_block_number..tip_block_number).sum(:ckb_transactions_count)
Expand All @@ -60,7 +61,8 @@ def current_epoch_difficulty
define_logic :average_block_time do
interval = (Settings.average_block_time_interval || 100)
start_block_number = [tip_block_number.to_i - interval + 1, 0].max
timestamps = Block.where(number: [start_block_number, tip_block_number]).recent.pluck(:timestamp)
timestamps = Block.where(number: [start_block_number,
tip_block_number]).recent.pluck(:timestamp)
next if timestamps.empty?

total_block_time(timestamps) / blocks_count(interval)
Expand Down Expand Up @@ -90,14 +92,17 @@ def self.hash_rate(block_number)
define_logic :address_balance_ranking do
addresses = Address.visible.where("balance > 0").order(balance: :desc).limit(50)
addresses.each.with_index(1).map do |address, index|
{ address: address.address_hash, balance: address.balance.to_s, ranking: index.to_s }
{ address: address.address_hash, balance: address.balance.to_s,
ranking: index.to_s }
end
end

define_logic :blockchain_info do
message_need_to_be_filtered_out = "CKB v0.105.* have bugs. Please upgrade to the latest version."
result = CkbSync::Api.instance.get_blockchain_info
result.alerts.delete_if { |alert| alert.message == message_need_to_be_filtered_out }
result.alerts.delete_if do |alert|
alert.message == message_need_to_be_filtered_out
end
JSON.generate(result.as_json)
end

Expand All @@ -108,10 +113,10 @@ def self.hash_rate(block_number)
pluck(:id, :created_at, :transaction_fee, :bytes, :confirmation_time)
txs.map do |id, created_at, transaction_fee, bytes, confirmation_time|
{
id: id,
id:,
timestamp: created_at.to_i,
fee_rate: (transaction_fee.to_f / bytes),
confirmation_time: confirmation_time
confirmation_time:,
}
end
end
Expand All @@ -123,7 +128,7 @@ def self.hash_rate(block_number)
order("id desc").limit(100)

# This is a patch for those pending tx which has no `bytes`
fee_rates = fee_rates.map { |tx|
fee_rates = fee_rates.map do |tx|
tx_bytes = 0
if tx.bytes.blank? || tx.bytes == 0
Rails.logger.info "== checking tx bytes: #{tx.tx_hash}, #{tx.id}"
Expand All @@ -137,12 +142,12 @@ def self.hash_rate(block_number)
end

tx
}.select { |e| e.bytes > 0 }
end.select { |e| e.bytes > 0 }

fee_rates.map do |tx|
{
id: tx.id,
fee_rate: (tx.transaction_fee.to_f / tx.bytes)
fee_rate: (tx.transaction_fee.to_f / tx.bytes),
}
end
end
Expand Down Expand Up @@ -210,4 +215,5 @@ def tip_block
# updated_at :datetime not null
# pending_transaction_fee_rates :jsonb
# transaction_fee_rates :jsonb
# ckb_hodl_waves :jsonb
#
11 changes: 10 additions & 1 deletion app/models/suggest_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ def find_udt_by_type_hash
UdtSerializer.new(udt) if udt.present?
end

def find_type_script_by_type_id
type_script = TypeScript.find_by(args: query_key, code_hash: Settings.type_id_code_hash)
TypeScriptSerializer.new(type_script) if type_script.present?
end

def find_by_hex
Block.cached_find(query_key) || find_ckb_transaction_by_hash || find_address_by_lock_hash || find_udt_by_type_hash
Block.cached_find(query_key) ||
find_ckb_transaction_by_hash ||
find_address_by_lock_hash ||
find_udt_by_type_hash ||
find_type_script_by_type_id
end
end
6 changes: 4 additions & 2 deletions app/serializers/ckb_transaction_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class CkbTransactionSerializer
end

attribute :display_inputs do |object, params|
Rails.cache.fetch("display_inputs_previews_#{params[:previews].present?}_#{object.id}", expires_in: 1.day) do
cache_key = "display_inputs_previews_#{params[:previews].present?}_#{object.id}_#{object.inputs.cache_key}"
Rails.cache.fetch(cache_key, expires_in: 1.day) do
if params && params[:previews]
object.display_inputs(previews: true)
else
Expand All @@ -55,7 +56,8 @@ class CkbTransactionSerializer
end

attribute :display_outputs do |object, params|
Rails.cache.fetch("display_outputs_previews_#{params[:previews].present?}_#{object.id}", expires_in: 1.day) do
cache_key = "display_outputs_previews_#{params[:previews].present?}_#{object.id}_#{object.outputs.cache_key}"
Rails.cache.fetch(cache_key, expires_in: 1.day) do
if params && params[:previews]
object.display_outputs(previews: true)
else
Expand Down
14 changes: 12 additions & 2 deletions app/serializers/ckb_transactions_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class CkbTransactionsSerializer
end

attribute :display_inputs do |object, params|
Rails.cache.fetch("display_inputs_previews_#{params[:previews].present?}_#{object.id}", expires_in: 1.day) do
cache_key = "display_inputs_previews_#{params[:previews].present?}_#{object.id}_#{object.inputs.cache_key}"
Rails.cache.fetch(cache_key, expires_in: 1.day) do
if params && params[:previews]
object.display_inputs(previews: true)
else
Expand All @@ -34,7 +35,8 @@ class CkbTransactionsSerializer
end

attribute :display_outputs do |object, params|
Rails.cache.fetch("display_outputs_previews_#{params[:previews].present?}_#{object.id}", expires_in: 1.day) do
cache_key = "display_outputs_previews_#{params[:previews].present?}_#{object.id}_#{object.outputs.cache_key}"
Rails.cache.fetch(cache_key, expires_in: 1.day) do
if params && params[:previews]
object.display_outputs(previews: true)
else
Expand All @@ -48,4 +50,12 @@ class CkbTransactionsSerializer
object.income(params[:address])
end
end

attribute :created_at do |object|
object.created_at.to_s
end

attribute :create_timestamp do |object|
(object.created_at.to_f * 1000).to_i.to_s
end
end
4 changes: 4 additions & 0 deletions app/serializers/statistic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ class StatisticSerializer
attribute :maintenance_info, if: Proc.new { |_record, params|
params && params[:info_name] == "maintenance_info"
}

attribute :ckb_hodl_waves, if: Proc.new { |_record, params|
params && params[:info_name] == "ckb_hodl_waves"
}
end
2 changes: 1 addition & 1 deletion app/serializers/type_script_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class TypeScriptSerializer
include FastJsonapi::ObjectSerializer

attributes :args, :code_hash, :hash_type
attributes :args, :code_hash, :hash_type, :script_hash
end
49 changes: 49 additions & 0 deletions app/workers/charts/ckb_hodl_waves_statistic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Charts
class CkbHodlWavesStatistic
include Sidekiq::Worker
sidekiq_options queue: "critical"

def perform
over_three_years = CellOutput.live.generated_before(3.years.ago.to_i * 1000).sum(:capacity)
one_year_to_three_years = CellOutput.live.generated_between(
3.years.ago.to_i * 1000, 1.year.ago.to_i * 1000
).sum(:capacity)
six_months_to_one_year = CellOutput.live.generated_between(
1.year.ago.to_i * 1000, 6.months.ago.to_i * 1000
).sum(:capacity)
three_months_to_six_months = CellOutput.live.generated_between(
6.months.ago.to_i * 1000, 3.months.ago.to_i * 1000
).sum(:capacity)
one_month_to_three_months = CellOutput.live.generated_between(
3.months.ago.to_i * 1000, 1.month.ago.to_i * 1000
).sum(:capacity)
one_week_to_one_month = CellOutput.live.generated_between(
1.month.ago.to_i * 1000, 1.week.ago.to_i * 1000
).sum(:capacity)
day_to_one_week = CellOutput.live.generated_between(
1.week.ago.to_i * 1000, 1.day.ago.to_i * 1000
).sum(:capacity)
latest_day = CellOutput.live.generated_between(
1.day.ago.beginning_of_day.to_i * 1000, 1.day.ago.end_of_day.to_i * 1000
).sum(:capacity)

info = {
total_supply: MarketData.new.indicators_json["total_supply"],
updated_at: Time.current.to_i,
}

ckb = {
over_three_years:,
one_year_to_three_years:,
six_months_to_one_year:,
three_months_to_six_months:,
one_month_to_three_months:,
one_week_to_one_month:,
day_to_one_week:,
latest_day:,
}.transform_values { |value| (value / 10**8).truncate(8) }

StatisticInfo.first.update(ckb_hodl_waves: ckb.merge!(info))
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCkbHodlWavesToStatisticInfo < ActiveRecord::Migration[7.0]
def change
add_column :statistic_infos, :ckb_hodl_waves, :jsonb
end
end
6 changes: 4 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,8 @@ CREATE TABLE public.statistic_infos (
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL,
pending_transaction_fee_rates jsonb,
transaction_fee_rates jsonb
transaction_fee_rates jsonb,
ckb_hodl_waves jsonb
);


Expand Down Expand Up @@ -4750,6 +4751,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20230913091025'),
('20230914120928'),
('20230918033957'),
('20231017074221');
('20231017074221'),
('20231218082938');


Loading

0 comments on commit cf9ff37

Please sign in to comment.