From 4c18b9cdf1c205e4d332da0a3010c7f567b27594 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 4 Dec 2023 10:30:20 +0800 Subject: [PATCH] fix: keep block's first tx is cellbase (#1522) Signed-off-by: Miles Zhang --- .../api/v1/block_transactions_controller.rb | 2 +- .../api/v1/block_transactions_controller_test.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/block_transactions_controller.rb b/app/controllers/api/v1/block_transactions_controller.rb index 45ceb64e6..27272b167 100644 --- a/app/controllers/api/v1/block_transactions_controller.rb +++ b/app/controllers/api/v1/block_transactions_controller.rb @@ -7,7 +7,7 @@ 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). - order(:id) + order(is_cellbase: :desc, id: :asc) if params[:tx_hash].present? ckb_transactions = ckb_transactions.where(tx_hash: params[:tx_hash]) diff --git a/test/controllers/api/v1/block_transactions_controller_test.rb b/test/controllers/api/v1/block_transactions_controller_test.rb index 52d590888..0a074f0fa 100644 --- a/test/controllers/api/v1/block_transactions_controller_test.rb +++ b/test/controllers/api/v1/block_transactions_controller_test.rb @@ -339,6 +339,19 @@ class BlockTransactionsControllerTest < ActionDispatch::IntegrationTest assert_equal response_transaction, response.body assert_equal 1, json.dig("meta", "total") end + + test "first tx should be cellbase" do + page = 1 + page_size = 10 + block = create(:block, :with_block_hash) + _ckb_transaction = create(:ckb_transaction, :with_multiple_inputs_and_outputs, block: block) + _cellbase_ckb_transaction = create(:ckb_transaction, block: block, is_cellbase: true) + + valid_get api_v1_block_transaction_url(block.block_hash), params: { + page: page, page_size: page_size } + + assert_equal true, json.dig("data").first["attributes"]["is_cellbase"] + end end end end