Skip to content

Commit

Permalink
feat: ckb_transactions order by tx_index desc default (#2160)
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid authored Aug 27, 2024
1 parent bbe9887 commit 476fd28
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v1/block_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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, :created_at, :tags).
order(is_cellbase: :desc, id: :asc)
select(:id, :tx_hash, :tx_index, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at, :tags).
order(tx_index: :asc)

if params[:tx_hash].present?
ckb_transactions = ckb_transactions.where(tx_hash: params[:tx_hash])
Expand Down
4 changes: 2 additions & 2 deletions app/interactions/addresses/ckb_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def execute
order_by, asc_or_desc = account_books_ordering
records = CkbTransaction.tx_committed.joins(:account_books).where(
account_books: { address_id: },
).order(order_by => asc_or_desc).distinct.page(page).per(page_size)
).order(order_by => asc_or_desc, "ckb_transactions.tx_index" => "desc").distinct.page(page).per(page_size)

options = paginate_options(records, address_id)
options.merge!(params: { previews: true, address: })
Expand Down Expand Up @@ -45,7 +45,7 @@ def paginate_options(records, address_id)
end

def select_fields
%i[id tx_hash block_id block_number block_timestamp
%i[id tx_hash tx_index block_id block_number block_timestamp
is_cellbase updated_at capacity_involved created_at]
end

Expand Down
4 changes: 2 additions & 2 deletions app/interactions/udts/ckb_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def execute
udt = Udt.find_by(type_hash:, published: true)
raise UdtNotFoundError if udt.blank?

order_by = "ckb_transactions.block_timestamp desc nulls last, ckb_transactions.id desc"
order_by = "ckb_transactions.block_timestamp desc nulls last, ckb_transactions.tx_index desc"
ckb_transactions = udt.ckb_transactions.tx_committed.select(select_fields).order(order_by)
ckb_transactions = ckb_transactions.where(tx_hash:) if tx_hash.present?

Expand Down Expand Up @@ -53,7 +53,7 @@ def validate_tx_hash!
end

def select_fields
%i[id tx_hash block_id block_number block_timestamp
%i[id tx_hash tx_index block_id block_number block_timestamp
is_cellbase updated_at created_at tags]
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/ckb_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CkbTransaction < ApplicationRecord

attribute :tx_hash, :ckb_hash

scope :recent, -> { order("block_timestamp desc nulls last, id desc") }
scope :recent, -> { order("block_timestamp desc nulls last, tx_index desc") }
scope :cellbase, -> { where(is_cellbase: true) }
scope :normal, -> { where(is_cellbase: false) }
scope :created_after, ->(block_timestamp) { where("block_timestamp >= ?", block_timestamp) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class AddressTransactionsControllerTest < ActionDispatch::IntegrationTest
page = 1
page_size = 10
address = create(:address, :with_transactions)
ckb_transactions = address.ckb_transactions.order(block_timestamp: :desc).page(page).per(page_size)
ckb_transactions = address.ckb_transactions.order(block_timestamp: :desc, tx_index: :desc).page(page).per(page_size)

valid_get api_v1_address_transaction_url(address.address_hash)

Expand Down
4 changes: 2 additions & 2 deletions test/controllers/api/v1/block_transactions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ class BlockTransactionsControllerTest < ActionDispatch::IntegrationTest
page = 1
page_size = 10
block = create(:block, :with_block_hash)
_ckb_transaction = create(:ckb_transaction, :with_multiple_inputs_and_outputs, block:)
_cellbase_ckb_transaction = create(:ckb_transaction, block:, is_cellbase: true)
_ckb_transaction = create(:ckb_transaction, :with_multiple_inputs_and_outputs, block:, tx_index: 1)
_cellbase_ckb_transaction = create(:ckb_transaction, block:, is_cellbase: true, tx_index: 0)

valid_get api_v1_block_transaction_url(block.block_hash), params: {
page:, page_size:
Expand Down
18 changes: 9 additions & 9 deletions test/factories/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

after(:create) do |address|
lock_hash = CkbUtils.parse_address(address.address_hash).script.compute_hash
address.update(lock_hash: lock_hash)
address.update(lock_hash:)
end

trait :with_lock_script do
after(:create) do |address, _evaluator|
block = create(:block, :with_block_hash)
cell_output = create(:cell_output, :with_full_transaction, block: block)
cell_output.lock_script.update(address: address)
cell_output = create(:cell_output, :with_full_transaction, block:)
cell_output.lock_script.update(address:)
end
end

Expand All @@ -38,7 +38,7 @@
block = create(:block, :with_block_hash)
ckb_transactions = []
evaluator.transactions_count.times do |i|
ckb_transactions << create(:ckb_transaction, block: block, block_timestamp: Time.current.to_i + i)
ckb_transactions << create(:ckb_transaction, block:, block_timestamp: Time.current.to_i + i, tx_index: i)
end

AccountBook.upsert_all ckb_transactions.map { |t| { address_id: address.id, ckb_transaction_id: t.id } }
Expand All @@ -52,7 +52,7 @@
block = create(:block, :with_block_hash)
ckb_transactions = []
evaluator.transactions_count.times do |i|
ckb_transactions << create(:pending_transaction, :with_multiple_inputs_and_outputs, block: block,
ckb_transactions << create(:pending_transaction, :with_multiple_inputs_and_outputs, block:,
block_timestamp: Time.current.to_i + i)
end

Expand All @@ -66,14 +66,14 @@
after(:create) do |address, evaluator|
evaluator.transactions_count.times do
block = create(:block, :with_block_hash)
transaction = create(:ckb_transaction, block: block, udt_address_ids: [address.id], tags: ["udt"])
transaction = create(:ckb_transaction, block:, udt_address_ids: [address.id], tags: ["udt"])
transaction.contained_address_ids = [address.id]
transaction.contained_udt_ids = [evaluator.udt.id]
transaction1 = create(:ckb_transaction, block: block, udt_address_ids: [address.id], tags: ["udt"])
transaction1 = create(:ckb_transaction, block:, udt_address_ids: [address.id], tags: ["udt"])
transaction1.contained_address_ids = [address.id]
transaction1.contained_udt_ids = [evaluator.udt.id]
create(:cell_output, address: address,
block: block,
create(:cell_output, address:,
block:,
ckb_transaction: transaction,
consumed_by: transaction1,
status: "dead",
Expand Down
1 change: 1 addition & 0 deletions test/factories/ckb_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
factory :ckb_transaction do
block
tx_hash { "0x#{SecureRandom.hex(32)}" }
sequence(:tx_index) { |n| n }
tx_status { "committed" }
block_number { block.number }
block_timestamp { block.timestamp }
Expand Down
6 changes: 3 additions & 3 deletions test/factories/udt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
trait :with_transactions do
after(:create) do |udt, _evaluator|
full_udt_address_ids = []
20.times do
20.times do |i|
block = create(:block, :with_block_hash)
transaction = create(:ckb_transaction, block:,
contained_udt_ids: [udt.id], tags: ["udt"])
contained_udt_ids: [udt.id], tags: ["udt"], tx_index: i + 2)
transaction1 = create(:ckb_transaction, block:,
contained_udt_ids: [udt.id], tags: ["udt"])
contained_udt_ids: [udt.id], tags: ["udt"], tx_index: i + 1 == 1 ? 0 : i + 1)
cell_output = create(:cell_output, block:,
ckb_transaction: transaction,
consumed_by: transaction1,
Expand Down

0 comments on commit 476fd28

Please sign in to comment.