Skip to content

Commit 8e3c65c

Browse files
authored
Merge pull request #2784 from nervosnetwork/testnet
Deploy to mainnet
2 parents 45a10db + 72ee610 commit 8e3c65c

15 files changed

+139
-36
lines changed

app/controllers/api/v1/address_dao_transactions_controller.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ def show
88
address = Address.find_address!(params[:id])
99
raise Api::V1::Exceptions::AddressNotFoundError if address.is_a?(NullAddress)
1010

11-
ckb_dao_transactions = address.ckb_dao_transactions.select(:id, :tx_hash, :block_id, :block_number, :tags, :block_timestamp, :is_cellbase, :updated_at, :created_at, :tx_index).
12-
recent.page(@page).per(@page_size)
11+
ckb_dao_transactions = address.ckb_dao_transactions
12+
.includes(:cell_inputs => [:previous_cell_output], :outputs => [], :bitcoin_annotation => [])
13+
# .joins("LEFT JOIN cell_inputs ON cell_inputs.ckb_transaction_id = ckb_transactions.id")
14+
# .joins("LEFT JOIN cell_outputs ON cell_outputs.ckb_transaction_id = ckb_transactions.id")
15+
# .group(select_fields.join(','))
16+
# .select(select_fields + ["COUNT(cell_inputs.id) AS cell_inputs_count", "COUNT(cell_outputs.id) AS cell_outputs_count"])
17+
.select(select_fields)
18+
.recent.page(@page).per(@page_size)
1319
json =
1420
Rails.cache.realize(ckb_dao_transactions.cache_key, version: ckb_dao_transactions.cache_version) do
1521
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: ckb_dao_transactions, page: @page, page_size: @page_size).call
@@ -21,6 +27,11 @@ def show
2127

2228
private
2329

30+
def select_fields
31+
%i[ckb_transactions.id ckb_transactions.tx_hash ckb_transactions.tx_index ckb_transactions.block_id ckb_transactions.block_number ckb_transactions.block_timestamp
32+
ckb_transactions.is_cellbase ckb_transactions.updated_at ckb_transactions.created_at ckb_transactions.tags]
33+
end
34+
2435
def validate_query_params
2536
validator = Validations::Address.new(params)
2637

app/controllers/api/v1/address_udt_transactions_controller.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ def show
1212
udt = Udt.find_by(type_hash: params[:type_hash], published: true)
1313
raise Api::V1::Exceptions::UdtNotFoundError if udt.blank?
1414

15-
ckb_udt_transactions = address.ckb_udt_transactions(udt.id).
16-
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at, :tags).
17-
recent.page(@page).per(@page_size).fast_page
15+
ckb_udt_transactions = address.ckb_udt_transactions(udt.id)
16+
.includes(:cell_inputs => [:previous_cell_output], :outputs => [], :bitcoin_annotation => [])
17+
# .joins("LEFT JOIN cell_inputs ON cell_inputs.ckb_transaction_id = ckb_transactions.id")
18+
# .joins("LEFT JOIN cell_outputs ON cell_outputs.ckb_transaction_id = ckb_transactions.id")
19+
# .group(select_fields.join(','))
20+
# .select(select_fields + ["COUNT(cell_inputs.id) AS cell_inputs_count", "COUNT(cell_outputs.id) AS cell_outputs_count"])
21+
.select(select_fields)
22+
.recent.page(@page).per(@page_size).fast_page
1823
json =
1924
Rails.cache.realize(ckb_udt_transactions.cache_key, version: ckb_udt_transactions.cache_version) do
2025
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: ckb_udt_transactions, page: @page, page_size: @page_size).call
@@ -26,6 +31,11 @@ def show
2631

2732
private
2833

34+
def select_fields
35+
%i[ckb_transactions.id ckb_transactions.tx_hash ckb_transactions.tx_index ckb_transactions.block_id ckb_transactions.block_number ckb_transactions.block_timestamp
36+
ckb_transactions.is_cellbase ckb_transactions.updated_at ckb_transactions.created_at ckb_transactions.tags]
37+
end
38+
2939
def validate_query_params
3040
validator = Validations::AddressUdtTransaction.new(params)
3141

app/controllers/api/v1/block_transactions_controller.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ class BlockTransactionsController < ApplicationController
55

66
def show
77
block = Block.find_by!(block_hash: params[:id])
8-
ckb_transactions = block.ckb_transactions.
9-
select(:id, :tx_hash, :tx_index, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at, :tags).
10-
order(tx_index: :asc)
8+
ckb_transactions = block.ckb_transactions.select(select_fields).order(tx_index: :asc)
9+
# select(select_fields + ["COUNT(cell_inputs.id) AS cell_inputs_count", "COUNT(cell_outputs.id) AS cell_outputs_count"])
1110

1211
if params[:tx_hash].present?
1312
ckb_transactions = ckb_transactions.where(tx_hash: params[:tx_hash])
@@ -21,7 +20,13 @@ def show
2120

2221
if stale?(ckb_transactions)
2322
expires_in 10.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
24-
ckb_transactions = ckb_transactions.page(@page).per(@page_size).fast_page
23+
ckb_transactions = ckb_transactions
24+
.includes(:cell_inputs => [:previous_cell_output], :outputs => [], :bitcoin_annotation => [])
25+
# .joins("LEFT JOIN cell_inputs ON cell_inputs.ckb_transaction_id = ckb_transactions.id")
26+
# .joins("LEFT JOIN cell_outputs ON cell_outputs.ckb_transaction_id = ckb_transactions.id")
27+
# .group(select_fields.join(','))
28+
.page(@page).per(@page_size).fast_page
29+
2530
options = FastJsonapi::PaginationMetaGenerator.new(
2631
request:,
2732
records: ckb_transactions,
@@ -39,6 +44,11 @@ def show
3944

4045
private
4146

47+
def select_fields
48+
%i[ckb_transactions.id ckb_transactions.tx_hash ckb_transactions.tx_index ckb_transactions.block_id ckb_transactions.block_number ckb_transactions.block_timestamp
49+
ckb_transactions.is_cellbase ckb_transactions.updated_at ckb_transactions.created_at ckb_transactions.tags]
50+
end
51+
4252
def validate_query_params
4353
validator = Validations::BlockTransaction.new(params)
4454

app/controllers/api/v1/contract_transactions_controller.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ def show
1111
if stale?(dao_contract)
1212
expires_in 10.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
1313

14-
ckb_transactions = dao_contract.ckb_transactions.includes(:cell_inputs, :cell_outputs).tx_committed.select(
15-
:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at, :tags
16-
).order("ckb_transactions.block_timestamp desc nulls last, ckb_transactions.id desc")
14+
ckb_transactions = dao_contract.ckb_transactions.includes(:cell_inputs, :cell_outputs).tx_committed.order("ckb_transactions.block_timestamp desc nulls last, ckb_transactions.id desc")
1715

1816
if params[:tx_hash].present?
1917
ckb_transactions = ckb_transactions.where(tx_hash: params[:tx_hash])
@@ -27,7 +25,14 @@ def show
2725
where(account_books: { address_id: address.id })
2826
end
2927

30-
ckb_transactions = ckb_transactions.page(@page).per(@page_size).fast_page
28+
ckb_transactions = ckb_transactions
29+
.includes(:cell_inputs => [:previous_cell_output], :outputs => [], :bitcoin_annotation => [])
30+
# .select(select_fields + ["COUNT(cell_inputs.id) AS cell_inputs_count", "COUNT(cell_outputs.id) AS cell_outputs_count"])
31+
# .joins("LEFT JOIN cell_inputs ON cell_inputs.ckb_transaction_id = ckb_transactions.id")
32+
# .joins("LEFT JOIN cell_outputs ON cell_outputs.ckb_transaction_id = ckb_transactions.id")
33+
# .group(select_fields.join(','))
34+
.select(select_fields)
35+
.page(@page).per(@page_size).fast_page
3136
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: ckb_transactions,
3237
page: @page, page_size: @page_size).call
3338
json = CkbTransactionsSerializer.new(ckb_transactions,
@@ -37,6 +42,11 @@ def show
3742
end
3843
end
3944

45+
def select_fields
46+
%i[ckb_transactions.id ckb_transactions.tx_hash ckb_transactions.block_id ckb_transactions.block_number ckb_transactions.block_timestamp
47+
ckb_transactions.is_cellbase ckb_transactions.updated_at ckb_transactions.created_at ckb_transactions.tags]
48+
end
49+
4050
def download_csv
4151
args = params.permit(:start_date, :end_date, :start_number, :end_number)
4252
file = CsvExportable::ExportContractTransactionsJob.perform_now(args.to_h)

app/controllers/api/v2/dao_events_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ def index
1313
page_size = params[:page_size] || 10
1414

1515
total = ckb_transactions.count
16-
ckb_transactions = ckb_transactions.page(page).per(page_size).fast_page
16+
ckb_transactions = ckb_transactions
17+
.includes(:cell_inputs => [:previous_cell_output], :outputs => [], :bitcoin_annotation => [])
18+
.page(page).per(page_size).fast_page
1719

1820
activities = CkbTransactionsSerializer.new(ckb_transactions).serializable_hash
1921

app/interactions/addresses/ckb_transactions.rb

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,28 @@ class CkbTransactions < ActiveInteraction::Base
99
integer :page_size, default: CkbTransaction.default_per_page
1010

1111
def execute
12+
is_bitcoin = BitcoinUtils.valid_address?(key)
1213
address = Explore.run!(key:)
1314
raise AddressNotFoundError if address.is_a?(NullAddress)
1415

1516
address_id = address.map(&:id)
1617
account_books = AccountBook.tx_committed.where(address_id:).
17-
order("block_number desc, tx_index desc").
18+
order(account_books_ordering).
1819
select(:ckb_transaction_id, :block_number, :tx_index).
19-
distinct.
20-
limit(Settings.query_default_limit)
21-
records = CkbTransaction.includes(:account_books).where(id: account_books.map(&:ckb_transaction_id)).select(select_fields).order(transactions_ordering).page(page).per(page_size)
20+
distinct.page(page).per(page_size)
21+
22+
includes = { :cell_inputs => [:previous_cell_output], :outputs => {}, :bitcoin_annotation => [] }
23+
includes[:bitcoin_transfers] = {} if is_bitcoin
24+
25+
records = CkbTransaction.where(id: account_books.map(&:ckb_transaction_id))
26+
.includes(includes)
27+
# .select(select_fields + ["COUNT(cell_inputs.id) AS cell_inputs_count", "COUNT(cell_outputs.id) AS cell_outputs_count"])
28+
.select(select_fields)
29+
# .joins("LEFT JOIN cell_inputs ON cell_inputs.ckb_transaction_id = ckb_transactions.id")
30+
# .joins("LEFT JOIN cell_outputs ON cell_outputs.ckb_transaction_id = ckb_transactions.id")
31+
# .group(select_fields.join(','))
32+
.order(transactions_ordering)
33+
2234
options = paginate_options(records, address_id)
2335
options.merge!(params: { previews: true, address_id: })
2436

@@ -28,8 +40,15 @@ def execute
2840

2941
private
3042

43+
def account_books_ordering
44+
_, sort_order = sort.split(".", 2)
45+
sort_order = "asc" unless sort_order&.match?(/^(asc|desc)$/i)
46+
47+
"block_number #{sort_order}, tx_index desc"
48+
end
49+
3150
def transactions_ordering
32-
sort_by = "ckb_transactions.block_timestamp"
51+
sort_by = "ckb_transactions.block_number"
3352
_, sort_order = sort.split(".", 2)
3453
sort_order = "asc" unless sort_order&.match?(/^(asc|desc)$/i)
3554

@@ -38,14 +57,15 @@ def transactions_ordering
3857

3958
def paginate_options(records, address_id)
4059
total_count = Address.where(id: address_id).sum(:ckb_transactions_count)
60+
count = [total_count, Settings.query_default_limit].min
4161
FastJsonapi::PaginationMetaGenerator.new(
42-
request:, records:, page:, page_size:, total_pages: records.total_pages, total_count:,
62+
request:, records:, page:, page_size:, total_pages: (count.to_f / page_size).ceil, total_count:,
4363
).call
4464
end
4565

4666
def select_fields
47-
%i[id tx_hash tx_index block_id block_number block_timestamp
48-
is_cellbase updated_at capacity_involved created_at tx_status]
67+
%i[ckb_transactions.id ckb_transactions.tx_hash ckb_transactions.tx_index ckb_transactions.block_id ckb_transactions.block_number ckb_transactions.block_timestamp
68+
ckb_transactions.is_cellbase ckb_transactions.updated_at ckb_transactions.capacity_involved ckb_transactions.created_at ckb_transactions.tx_status]
4969
end
5070

5171
# A lock script can correspond to multiple CKB addresses

app/interactions/addresses/deployed_cells.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def execute
1414

1515
order_by, asc_or_desc = deployed_cells_ordering
1616
deployed_cell_output_ids = Contract.joins(:deployed_cell_output).where(cell_outputs: { address: address.map(&:id) }).pluck(:deployed_cell_output_id)
17-
records = CellOutput.where(id: deployed_cell_output_ids).order(order_by => asc_or_desc).
17+
records = CellOutput.includes(:type_script, :lock_script).where(id: deployed_cell_output_ids).order(order_by => asc_or_desc).
1818
page(page).per(page_size).fast_page
1919

2020
options = FastJsonapi::PaginationMetaGenerator.new(request:, records:, page:, page_size:).call

app/interactions/addresses/info.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class Info < ActiveInteraction::Base
88
integer :page_size, default: Address.default_per_page
99

1010
def execute
11-
address = Explore.run!(key:)
11+
result = find_address
12+
address = result ? wrap_result(result) : NullAddress.new(key)
13+
1214
raise AddressNotFoundError if address.is_a?(NullAddress)
1315

1416
return LockHashSerializer.new(address[0]) if QueryKeyUtils.valid_hex?(key)
@@ -18,5 +20,33 @@ def execute
1820
).call
1921
AddressSerializer.new(address, options)
2022
end
23+
24+
private
25+
26+
def find_address
27+
return Address.includes(udt_accounts: [:udt], lock_script: [], bitcoin_address: []).find_by(lock_hash: key) if QueryKeyUtils.valid_hex?(key)
28+
return find_by_address_hash(key) if QueryKeyUtils.valid_address?(key)
29+
30+
find_by_bitcoin_address_hash(key) if BitcoinUtils.valid_address?(key)
31+
end
32+
33+
def find_by_address_hash(key)
34+
lock_hash = CkbUtils.parse_address(key).script.compute_hash
35+
address = Address.includes(udt_accounts: [:udt], lock_script: [], bitcoin_address: []).find_by(lock_hash:)
36+
address.query_address = key if address
37+
address
38+
rescue StandardError
39+
nil
40+
end
41+
42+
def find_by_bitcoin_address_hash(key)
43+
address_ids = BitcoinAddressMapping.includes(:bitcoin_address).
44+
where(bitcoin_address: { address_hash: key }).pluck(:ckb_address_id)
45+
Address.includes(udt_accounts: [:udt], lock_script: [], bitcoin_address: []).where(id: address_ids)
46+
end
47+
48+
def wrap_result(result)
49+
result.is_a?(Address) ? [result] : result
50+
end
2151
end
2252
end

app/interactions/addresses/live_cells.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def fetch_cell_output_scope(address)
3939
scope =
4040
if bound_status
4141
vout_ids = BitcoinVout.where(address_id: address_ids, status: bound_status).pluck(:cell_output_id)
42-
CellOutput.live.where(id: vout_ids)
42+
CellOutput.live.includes(:type_script, :lock_script).where(id: vout_ids)
4343
else
44-
CellOutput.live.where(address_id: address_ids)
44+
CellOutput.live.includes(:type_script, :lock_script).where(address_id: address_ids)
4545
end
4646

4747
tag.present? ? filter_by_tag(scope) : scope

app/interactions/udts/ckb_transactions.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ def execute
3030
where(account_books: { address_id: address.map(&:id) }).distinct
3131
end
3232

33-
records = ckb_transactions.page(page).per(page_size)
33+
records = ckb_transactions
34+
.includes(:cell_inputs, :outputs, :bitcoin_annotation => [])
35+
.select(select_fields)
36+
.page(page).per(page_size)
3437
options = FastJsonapi::PaginationMetaGenerator.new(
3538
request:, records:, page:, page_size:,
3639
).call
@@ -53,8 +56,8 @@ def validate_tx_hash!
5356
end
5457

5558
def select_fields
56-
%i[id tx_hash tx_index block_id block_number block_timestamp
57-
is_cellbase updated_at created_at tags]
59+
%i[ckb_transactions.id ckb_transactions.tx_hash ckb_transactions.tx_index ckb_transactions.block_id ckb_transactions.block_number ckb_transactions.block_timestamp
60+
ckb_transactions.is_cellbase ckb_transactions.updated_at ckb_transactions.created_at ckb_transactions.tags]
5861
end
5962
end
6063
end

0 commit comments

Comments
 (0)