From 476fd28e30952ec32cb67bdd514e5ec6f716bb98 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Tue, 27 Aug 2024 16:54:39 +0800 Subject: [PATCH] feat: ckb_transactions order by tx_index desc default (#2160) Signed-off-by: Miles Zhang --- .../api/v1/block_transactions_controller.rb | 4 ++-- app/interactions/addresses/ckb_transactions.rb | 4 ++-- app/interactions/udts/ckb_transactions.rb | 4 ++-- app/models/ckb_transaction.rb | 2 +- .../v1/address_transactions_controller_test.rb | 2 +- .../v1/block_transactions_controller_test.rb | 4 ++-- test/factories/address.rb | 18 +++++++++--------- test/factories/ckb_transaction.rb | 1 + test/factories/udt.rb | 6 +++--- 9 files changed, 23 insertions(+), 22 deletions(-) diff --git a/app/controllers/api/v1/block_transactions_controller.rb b/app/controllers/api/v1/block_transactions_controller.rb index bd71f0888..83a42f7cc 100644 --- a/app/controllers/api/v1/block_transactions_controller.rb +++ b/app/controllers/api/v1/block_transactions_controller.rb @@ -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]) diff --git a/app/interactions/addresses/ckb_transactions.rb b/app/interactions/addresses/ckb_transactions.rb index dcba4cfe6..9748499cb 100644 --- a/app/interactions/addresses/ckb_transactions.rb +++ b/app/interactions/addresses/ckb_transactions.rb @@ -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: }) @@ -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 diff --git a/app/interactions/udts/ckb_transactions.rb b/app/interactions/udts/ckb_transactions.rb index 09186717e..84982f168 100644 --- a/app/interactions/udts/ckb_transactions.rb +++ b/app/interactions/udts/ckb_transactions.rb @@ -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? @@ -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 diff --git a/app/models/ckb_transaction.rb b/app/models/ckb_transaction.rb index 55b84e40d..e9435a936 100644 --- a/app/models/ckb_transaction.rb +++ b/app/models/ckb_transaction.rb @@ -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) } diff --git a/test/controllers/api/v1/address_transactions_controller_test.rb b/test/controllers/api/v1/address_transactions_controller_test.rb index e7a9c91c9..46acca2cf 100644 --- a/test/controllers/api/v1/address_transactions_controller_test.rb +++ b/test/controllers/api/v1/address_transactions_controller_test.rb @@ -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) diff --git a/test/controllers/api/v1/block_transactions_controller_test.rb b/test/controllers/api/v1/block_transactions_controller_test.rb index 3f0899ce7..10a635bf0 100644 --- a/test/controllers/api/v1/block_transactions_controller_test.rb +++ b/test/controllers/api/v1/block_transactions_controller_test.rb @@ -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: diff --git a/test/factories/address.rb b/test/factories/address.rb index 6a0f2d8c0..924942bbe 100644 --- a/test/factories/address.rb +++ b/test/factories/address.rb @@ -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 @@ -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 } } @@ -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 @@ -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", diff --git a/test/factories/ckb_transaction.rb b/test/factories/ckb_transaction.rb index a11f1014c..9bb5d473f 100644 --- a/test/factories/ckb_transaction.rb +++ b/test/factories/ckb_transaction.rb @@ -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 } diff --git a/test/factories/udt.rb b/test/factories/udt.rb index 210ed5861..e660c95a6 100644 --- a/test/factories/udt.rb +++ b/test/factories/udt.rb @@ -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,