Skip to content

Commit

Permalink
Merge pull request #1529 from nervosnetwork/testnet
Browse files Browse the repository at this point in the history
Deploy to mainnet
  • Loading branch information
rabbitz authored Dec 26, 2023
2 parents cd8c2c5 + 78ffaa3 commit c311576
Show file tree
Hide file tree
Showing 34 changed files with 819 additions and 871 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
88 changes: 46 additions & 42 deletions app/controllers/api/v1/address_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,45 @@ class AddressTransactionsController < ApplicationController
before_action :set_address_transactions, only: [:show, :download_csv]

def show
@tx_ids = AccountBook.joins(:ckb_transaction).
where(account_books: { address_id: @address.id },
ckb_transactions: { tx_status: "committed" })

params[:sort] ||= "ckb_transaction_id.desc"
order_by, asc_or_desc = params[:sort].split(".", 2)
order_by =
case order_by
when "time" then "ckb_transactions.block_timestamp"
else order_by
end

head :not_found and return unless order_by.in? %w[
ckb_transaction_id block_timestamp
ckb_transactions.block_timestamp
]
expires_in 10.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds

@tx_ids = @tx_ids.
order_by, asc_or_desc = account_books_ordering
tx_ids = AccountBook.joins(:ckb_transaction).
where(account_books: { address_id: @address.id },
ckb_transactions: { tx_status: "committed" }).
order(order_by => asc_or_desc).
select("ckb_transaction_id").
page(@page).per(@page_size).fast_page

order_by = "id" if order_by == "ckb_transaction_id"
@ckb_transactions = CkbTransaction.where(id: @tx_ids.map(&:ckb_transaction_id)).
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :capacity_involved).
total_count = AccountBook.where(address_id: @address.id).count
total_count = tx_ids.total_count if total_count < 1_000

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, :created_at).
order(order_by => asc_or_desc)

options = FastJsonapi::PaginationMetaGenerator.new(
request: request,
records: ckb_transactions,
page: @page,
page_size: @page_size,
total_count: total_count
).call
ckb_transaction_serializer = CkbTransactionsSerializer.new(
ckb_transactions,
options.merge(params: { previews: true, address: @address })
)

json =
Rails.cache.realize("#{@ckb_transactions.cache_key}/#{@address.query_address}",
version: @ckb_transactions.cache_version) do
@options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: @ckb_transactions,
page: @page, page_size: @page_size, records_counter: @tx_ids).call
json_result
if QueryKeyUtils.valid_address?(params[:id])
if @address.address_hash == @address.query_address
ckb_transaction_serializer.serialized_json
else
ckb_transaction_serializer.serialized_json.gsub(@address.address_hash, @address.query_address)
end
else
ckb_transaction_serializer.serialized_json
end

render json: json
Expand Down Expand Up @@ -71,26 +77,24 @@ def pagination_params
@page_size = params[:page_size] || CkbTransaction.default_per_page
end

def json_result
ckb_transaction_serializer = CkbTransactionsSerializer.new(@ckb_transactions,
@options.merge(params: {
previews: true,
address: @address }))
def set_address_transactions
@address = Address.find_address!(params[:id])
raise Api::V1::Exceptions::AddressNotFoundError if @address.is_a?(NullAddress)
end

if QueryKeyUtils.valid_address?(params[:id])
if @address.address_hash == @address.query_address
ckb_transaction_serializer.serialized_json
else
ckb_transaction_serializer.serialized_json.gsub(@address.address_hash, @address.query_address)
def account_books_ordering
sort, order = params.fetch(:sort, "ckb_transaction_id.desc").split(".", 2)
sort =
case sort
when "time" then "ckb_transactions.block_timestamp"
else "ckb_transactions.id"
end
else
ckb_transaction_serializer.serialized_json

if order.nil? || !order.match?(/^(asc|desc)$/i)
order = "asc"
end
end

def set_address_transactions
@address = Address.find_address!(params[:id])
raise Api::V1::Exceptions::AddressNotFoundError if @address.is_a?(NullAddress)
[sort, order]
end
end
end
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
4 changes: 2 additions & 2 deletions app/lib/fast_jsonapi/pagination_meta_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ class PaginationMetaGenerator
DEFAULT_PAGE = 1
DEFAULT_PER_PAGE = 20

def initialize(request:, records:, page:, page_size:, records_counter: nil)
def initialize(request:, records:, page:, page_size:, records_counter: nil, total_count: nil)
@url = request.base_url + request.path + query_string(request.query_parameters)
@page = page.to_i
@page_size = limit_page_size(records, page_size.to_i)
@records = records
@records_counter = records_counter || records
@total_count = @records_counter.total_count.to_i
@total_count = total_count || @records_counter.total_count.to_i
@total_pages = total_pages
@hash = { links: {}, meta: { total: @total_count, page_size: @page_size } }
end
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_version}"
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_version}"
Rails.cache.fetch(cache_key, expires_in: 1.day) do
if params && params[:previews]
object.display_outputs(previews: true)
else
Expand Down
Loading

0 comments on commit c311576

Please sign in to comment.