Skip to content

Commit

Permalink
Merge pull request #642 from nervosnetwork/rc/v0.9.7
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda authored Jun 5, 2020
2 parents 8d6d735 + b1eb350 commit 0a8f9cf
Show file tree
Hide file tree
Showing 34 changed files with 405 additions and 205 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# [v0.9.7](https://github.com/shaojunda/ckb-explorer/compare/v0.9.6...v0.9.7) (2020-06-05)


### Features

* #[635](https://github.com/nervosnetwork/ckb-explorer/pull/635): add udts api


### Performance Improvements

* #[625](https://github.com/nervosnetwork/ckb-explorer/pull/625): address dao and udt transactions api


# [v0.9.6](https://github.com/shaojunda/ckb-explorer/compare/v0.9.5...v0.9.6) (2020-06-01)


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

presented_address = AddressPresenter.new(address)
ckb_dao_transactions = presented_address.ckb_dao_transactions.recent.distinct.page(@page).per(@page_size)
ckb_dao_transactions = address.ckb_dao_transactions.recent.page(@page).per(@page_size)
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_dao_transactions, page: @page, page_size: @page_size).call

render json: CkbTransactionSerializer.new(ckb_dao_transactions, options.merge({ params: { previews: true } }))
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/api/v1/address_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ def show
@address = Address.find_address!(params[:id])
raise Api::V1::Exceptions::AddressNotFoundError if @address.is_a?(NullAddress)

presented_address = AddressPresenter.new(@address)
@ckb_transactions = presented_address.ckb_transactions.recent.distinct.page(@page).per(@page_size)
@ckb_transactions = @address.custom_ckb_transactions.recent.page(@page).per(@page_size)
@options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: @ckb_transactions, page: @page, page_size: @page_size).call

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

presented_address = AddressPresenter.new(address)
ckb_dao_transactions = presented_address.ckb_udt_transactions(params[:type_hash]).recent.distinct.page(@page).per(@page_size)
ckb_dao_transactions = address.ckb_udt_transactions(params[:type_hash]).recent.page(@page).per(@page_size)
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_dao_transactions, page: @page, page_size: @page_size).call

render json: CkbTransactionSerializer.new(ckb_dao_transactions, options.merge({ params: { previews: true } }))
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/addresses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def json_response(address)
if QueryKeyUtils.valid_hex?(params[:id])
LockHashSerializer.new(address)
else
presented_address = address.is_a?(NullAddress) ? NullAddress.new(params[:id]) : AddressPresenter.new(address)
presented_address = address.is_a?(NullAddress) ? NullAddress.new(params[:id]) : address
AddressSerializer.new(presented_address)
end
end
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 @@ -4,7 +4,7 @@ class Api::V1::UdtTransactionsController < ApplicationController

def show
udt = Udt.find_by!(type_hash: params[:id], published: true)
ckb_transactions = udt.ckb_transactions.recent.distinct.page(@page).per(@page_size)
ckb_transactions = udt.ckb_transactions.recent.page(@page).per(@page_size)
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: @page, page_size: @page_size).call

render json: CkbTransactionSerializer.new(ckb_transactions, options.merge({ params: { previews: true } }))
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/api/v1/udts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class Api::V1::UdtsController < ApplicationController
before_action :validate_query_params, only: :show

def index
udts = Udt.order(addresses_count: :desc).limit(1000)
render json: UdtSerializer.new(udts)
end

def show
udt = Udt.find_by!(type_hash: params[:id], published: true)

Expand Down
8 changes: 7 additions & 1 deletion app/controllers/validations/distribution_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ def error_object
attr_accessor :query_key

def query_key_format_must_be_correct
if query_key.blank? || !(query_key.split("-") - ::DistributionData::VALID_INDICATORS).empty?
if query_key.blank? || !query_key_valid?
errors.add(:query_key, "indicator name is invalid")
end
end

def query_key_valid?
query_keys = query_key.split("-")
extra_keys = query_keys - ::DistributionData::VALID_INDICATORS
extra_keys.blank? || extra_keys.size == 1 && /^miner_address_distribution(\d+)$/ =~ extra_keys.first
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/validations/monetary_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def query_key_format_must_be_correct
def query_key_valid?
query_keys = query_key.split("-")
extra_keys = query_keys - ::MonetaryData::VALID_INDICATORS
extra_keys.blank? || extra_keys.size == 1 && extra_keys.first =~ /^nominal_apc(\d+)$/
extra_keys.blank? || extra_keys.size == 1 && /^nominal_apc(\d+)$/ =~ extra_keys.first
end
end
end
47 changes: 47 additions & 0 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,53 @@ class Address < ApplicationRecord

attr_accessor :query_address

def custom_ckb_transactions
ckb_transaction_ids = account_books.select(:ckb_transaction_id).distinct
CkbTransaction.where(id: ckb_transaction_ids)
end

def ckb_dao_transactions
ckb_transaction_ids = cell_outputs.where(cell_type: %w(nervos_dao_deposit nervos_dao_withdrawing)).select("ckb_transaction_id").distinct
CkbTransaction.where(id: ckb_transaction_ids)
end

def ckb_udt_transactions(type_hash)
sql =
<<-SQL
SELECT
generated_by_id ckb_transaction_id
FROM
cell_outputs
WHERE
address_id = #{id}
AND
cell_type = #{CellOutput::cell_types['udt']}
AND
type_hash = '#{type_hash}'
UNION
SELECT
consumed_by_id ckb_transaction_id
FROM
cell_outputs
WHERE
address_id = #{id}
AND
cell_type = #{CellOutput::cell_types['udt']}
AND
type_hash = '#{type_hash}'
AND
consumed_by_id is not null
SQL
ckb_transaction_ids = CellOutput.select("ckb_transaction_id").from("(#{sql}) as cell_outputs")
CkbTransaction.where(id: ckb_transaction_ids.distinct)
end

def lock_info
lock_script.lock_info
end

def lock_script
LockScript.where(address: self).first
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/ckb_sync/node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def process_block(node_block)
update_current_block_mining_info(local_block)
update_block_contained_address_info(local_block)
update_block_reward_info(local_block)
update_udt_accounts(udt_infos)
update_udt_accounts(udt_infos, local_block.timestamp)
update_udt_info(udt_infos)
dao_events = build_new_dao_depositor_events(new_dao_depositor_events)
DaoEvent.import!(dao_events, validate: false)
Expand Down Expand Up @@ -67,7 +67,7 @@ def update_udt_info(udt_infos)
Udt.import columns, import_values, validate: false, on_duplicate_key_update: { conflict_target: [:type_hash], columns: [:total_amount, :addresses_count] }
end

def update_udt_accounts(udt_infos)
def update_udt_accounts(udt_infos, block_timestamp)
return if udt_infos.blank?

udt_infos.each do |udt_output|
Expand All @@ -78,7 +78,7 @@ def update_udt_accounts(udt_infos)
if udt_account.present?
udt_account.update!(amount: amount)
else
udt = Udt.find_or_create_by!(type_hash: udt_output[:type_hash], code_hash: ENV["SUDT_CELL_TYPE_HASH"], udt_type: "sudt")
udt = Udt.find_or_create_by!(type_hash: udt_output[:type_hash], code_hash: ENV["SUDT_CELL_TYPE_HASH"], udt_type: "sudt", block_timestamp: block_timestamp)
address.udt_accounts.create!(udt_type: udt.udt_type, full_name: udt.full_name, symbol: udt.symbol, decimal: udt.decimal, published: udt.published, code_hash: udt.code_hash, type_hash: udt.type_hash, amount: amount, udt: udt)
end
end
Expand Down
12 changes: 8 additions & 4 deletions app/models/distribution_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ def transaction_propagation_delay_history
TransactionPropagationDelay.connection.select_all(sql)
end

def miner_address_distribution
Rails.cache.realize("miner_address_distribution", expires_in: 1.day) do
result = Block.where("timestamp >= ?", CkbUtils.time_in_milliseconds(7.days.ago.to_i)).group(:miner_hash).order("count(miner_hash) desc").count.to_a
def miner_address_distribution(checkpoint = 7)
supported_checkpoints = [7, 90]
return unless checkpoint.in?(supported_checkpoints)

Rails.cache.realize("miner_address_distribution_#{checkpoint}", expires_in: 1.day) do
result = Block.where("timestamp >= ?", CkbUtils.time_in_milliseconds(checkpoint.days.ago.to_i)).group(:miner_hash).order("count(miner_hash) desc").count.to_a
cut_off_point = (result.count * 0.7).floor
if result.present?
(result[0..9].map{ |item| [item[0], item[1].to_s] } + [["other", result[10..-1].map { |item| item[1] }.reduce(:+).to_s]]).to_h
(result[0..cut_off_point].map{ |item| [item[0], item[1].to_s] } + [["other", result[(cut_off_point + 1)..-1].map { |item| item[1] }.reduce(:+).to_s]]).to_h
else
result
end
Expand Down
12 changes: 6 additions & 6 deletions app/models/market_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def first_released_timestamp_may
@first_released_timestamp_may ||=
begin
lock_address = Address.find_by(address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn323t90gna20lusyshreg32qee4fhkt9jj2t6qrqzzqxzq8yqt8kmd9")
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : Time.find_zone("UTC").parse("2020-05-01").to_i
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : CkbUtils.time_in_milliseconds(Time.find_zone("UTC").parse("2020-05-01"))
end
end

Expand All @@ -111,7 +111,7 @@ def second_released_timestamp_may
@second_released_timestamp_may ||=
begin
lock_address = Address.find_by(address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn323crn7nscet5sfwxjkzhexymfa4zntzt8vasvqzzqxzq8yq92pgkg")
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : Time.find_zone("UTC").parse("2021-05-01").to_i
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : CkbUtils.time_in_milliseconds(Time.find_zone("UTC").parse("2021-05-01"))
end
end

Expand All @@ -120,7 +120,7 @@ def third_released_timestamp_may
@third_released_timestamp_may ||=
begin
lock_address = Address.find_by(address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sl0qgva2l78fcnjt6x8kr8sln4lqs4twcpq4qzzqxzq8yq7hpadu")
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : Time.find_zone("UTC").parse("2022-05-01").to_i
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : CkbUtils.time_in_milliseconds(Time.find_zone("UTC").parse("2022-05-01"))
end
end

Expand All @@ -129,7 +129,7 @@ def first_released_timestamp_other
@first_released_timestamp_other ||=
begin
lock_address = Address.find_by(address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32s3y29vjv73cfm8qax220dwwmpdccl4upy4s9qzzqxzq8yqyd09am")
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : Time.find_zone("UTC").parse("2020-07-01").to_i
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : CkbUtils.time_in_milliseconds(Time.find_zone("UTC").parse("2020-07-01"))
end
end

Expand All @@ -138,7 +138,7 @@ def second_released_timestamp_other
@second_released_timestamp_other ||=
begin
lock_address = Address.find_by(address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sn23uga5m8u5v87q98vr29qa8tl0ruu84gqfqzzqxzq8yqc2dxk6")
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : Time.find_zone("UTC").parse("2020-12-31").to_i
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : CkbUtils.time_in_milliseconds(Time.find_zone("UTC").parse("2020-12-31"))
end
end

Expand All @@ -147,7 +147,7 @@ def third_released_timestamp_other
@third_released_timestamp_other ||=
begin
lock_address = Address.find_by(address_hash: "ckb1q3w9q60tppt7l3j7r09qcp7lxnp3vcanvgha8pmvsa3jplykxn32sdufwedw7a0w9dkvhpsah4mdk2gkfq63e0q6qzzqxzq8yqnqq85p")
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : Time.find_zone("UTC").parse("2022-12-31").to_i
lock_address.present? ? lock_address.lock_script.lock_info[:estimated_unlock_time].to_i : CkbUtils.time_in_milliseconds(Time.find_zone("UTC").parse("2022-12-31"))
end
end

Expand Down
5 changes: 1 addition & 4 deletions app/models/suggest_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ def find_cached_address
address = Address.cached_find(query_key)
raise Api::V1::Exceptions::AddressNotFoundError if address.blank?

return AddressSerializer.new(address) if address.is_a?(NullAddress)

presented_address = AddressPresenter.new(address)
AddressSerializer.new(presented_address)
AddressSerializer.new(address)
end

def find_udt_by_type_hash
Expand Down
35 changes: 33 additions & 2 deletions app/models/udt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,38 @@ class Udt < ApplicationRecord
attribute :code_hash, :ckb_hash

def ckb_transactions
ckb_transaction_ids = CellOutput.udt.where(type_hash: type_hash).pluck("generated_by_id") + CellOutput.udt.where(type_hash: type_hash).pluck("consumed_by_id").compact
CkbTransaction.where(id: ckb_transaction_ids.uniq)
sql =
<<-SQL
SELECT
generated_by_id ckb_transaction_id
FROM
cell_outputs
WHERE
cell_type = #{CellOutput::cell_types['udt']}
AND
type_hash = '#{type_hash}'
UNION
SELECT
consumed_by_id ckb_transaction_id
FROM
cell_outputs
WHERE
cell_type = #{CellOutput::cell_types['udt']}
AND
type_hash = '#{type_hash}'
AND
consumed_by_id is not null
SQL
ckb_transaction_ids = CellOutput.select("ckb_transaction_id").from("(#{sql}) as cell_outputs")
CkbTransaction.where(id: ckb_transaction_ids.distinct)
end

def h24_ckb_transactions_count
Rails.cache.realize("udt_h24_ckb_transactions_count_#{id}", expires_in: 1.hour) do
ckb_transactions.where("block_timestamp >= ?", CkbUtils.time_in_milliseconds(24.hours.ago)).count
end
end
end

Expand All @@ -36,6 +66,7 @@ def ckb_transactions
# published :boolean default(FALSE)
# created_at :datetime not null
# updated_at :datetime not null
# block_timestamp :decimal(30, )
#
# Indexes
#
Expand Down
78 changes: 0 additions & 78 deletions app/presenters/address_presenter.rb

This file was deleted.

Loading

0 comments on commit 0a8f9cf

Please sign in to comment.