From 57e925cf3e7eccbbf5a8640acc9fb0507cf98eb5 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 27 May 2024 14:08:52 +0800 Subject: [PATCH] feat: cellbase outputs paginate (#1886) Signed-off-by: Miles Zhang --- .../api/v2/ckb_transactions_controller.rb | 5 ++- .../v2/ckb_transactions_controller_test.rb | 9 ++++ test/factories/ckb_transaction.rb | 43 ++++++++++++------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/app/controllers/api/v2/ckb_transactions_controller.rb b/app/controllers/api/v2/ckb_transactions_controller.rb index bec966869..dcf11c27f 100644 --- a/app/controllers/api/v2/ckb_transactions_controller.rb +++ b/app/controllers/api/v2/ckb_transactions_controller.rb @@ -36,8 +36,9 @@ def display_outputs expires_in 15.seconds, public: true, must_revalidate: true if @ckb_transaction.is_cellbase - cell_outputs = @ckb_transaction.cellbase_display_outputs - total_count = cell_outputs.count + cell_outputs = @ckb_transaction.cellbase_display_outputs.sort_by { |output| output[:id] } + cell_outputs = Kaminari.paginate_array(cell_outputs).page(@page).per(@page_size) + total_count = cell_outputs.total_count else cell_outputs = @ckb_transaction.outputs.order(id: :asc). page(@page).per(@page_size).fast_page diff --git a/test/controllers/api/v2/ckb_transactions_controller_test.rb b/test/controllers/api/v2/ckb_transactions_controller_test.rb index efda76515..8631b1179 100644 --- a/test/controllers/api/v2/ckb_transactions_controller_test.rb +++ b/test/controllers/api/v2/ckb_transactions_controller_test.rb @@ -144,6 +144,15 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest assert_equal "-2000000000000.000000", transfers1[0]["udt_info"]["amount"] assert_equal "2000000000000.000000", transfers2[0]["udt_info"]["amount"] end + + test "cellbase should can paginate either" do + block = create(:block, number: 0) + tx = create(:ckb_transaction, :with_cell_base, is_cellbase: true, block:) + get display_outputs_api_v2_ckb_transaction_url(tx.tx_hash), params: { page: 1, page_size: 5 } + assert_equal 5, json["data"].size + assert_equal 15, json["meta"]["total"] + assert_equal "5", json["meta"]["page_size"] + end end end end diff --git a/test/factories/ckb_transaction.rb b/test/factories/ckb_transaction.rb index c7f1a2627..a11f1014c 100644 --- a/test/factories/ckb_transaction.rb +++ b/test/factories/ckb_transaction.rb @@ -59,15 +59,15 @@ trait :with_cell_output_and_lock_script do after(:create) do |ckb_transaction, _evaluator| - output1 = create(:cell_output, ckb_transaction: ckb_transaction, + output1 = create(:cell_output, ckb_transaction:, block: ckb_transaction.block, tx_hash: ckb_transaction.tx_hash, cell_index: 0) - output2 = create(:cell_output, ckb_transaction: ckb_transaction, + output2 = create(:cell_output, ckb_transaction:, block: ckb_transaction.block, tx_hash: ckb_transaction.tx_hash, cell_index: 1) - output3 = create(:cell_output, ckb_transaction: ckb_transaction, + output3 = create(:cell_output, ckb_transaction:, block: ckb_transaction.block, tx_hash: ckb_transaction.tx_hash, cell_index: 2) @@ -84,17 +84,17 @@ trait :with_cell_output_and_lock_and_type_script do after(:create) do |ckb_transaction, _evaluator| output1 = create(:cell_output, capacity: 10**8 * 8, - ckb_transaction: ckb_transaction, + ckb_transaction:, block: ckb_transaction.block, tx_hash: ckb_transaction.tx_hash, cell_index: 0) output2 = create(:cell_output, capacity: 10**8 * 8, - ckb_transaction: ckb_transaction, + ckb_transaction:, block: ckb_transaction.block, tx_hash: ckb_transaction.tx_hash, cell_index: 1) output3 = create(:cell_output, capacity: 10**8 * 8, - ckb_transaction: ckb_transaction, + ckb_transaction:, block: ckb_transaction.block, tx_hash: ckb_transaction.tx_hash, cell_index: 2) @@ -115,14 +115,14 @@ 15.times do |index| block = create(:block, :with_block_hash, number: 12) tx = create(:ckb_transaction, :with_cell_output_and_lock_script, - block: block) + block:) create(:cell_output, capacity: 10**8 * 8, - ckb_transaction: ckb_transaction, block: ckb_transaction.block, tx_hash: ckb_transaction.tx_hash, cell_index: index) + ckb_transaction:, block: ckb_transaction.block, tx_hash: ckb_transaction.tx_hash, cell_index: index) previous_output = { tx_hash: tx.tx_hash, index: 0 } - create(:cell_input, previous_output: previous_output, - ckb_transaction: ckb_transaction, block: ckb_transaction.block) + create(:cell_input, previous_output:, + ckb_transaction:, block: ckb_transaction.block) - ckb_transaction.witnesses.create index: index, + ckb_transaction.witnesses.create index:, data: "0x4e52933358ae2f26863b8c1c71bf20f17489328820f8f2cd84a070069f10ceef784bc3693c3c51b93475a7b5dbf652ba6532d0580ecc1faf909f9fd53c5f6405000000000000000000" end end @@ -131,7 +131,7 @@ trait :with_single_output do after(:create) do |ckb_transaction| create(:cell_output, capacity: 10**8 * 8, - ckb_transaction: ckb_transaction, + ckb_transaction:, block: ckb_transaction.block, tx_hash: ckb_transaction.tx_hash, cell_index: 0) @@ -142,18 +142,29 @@ after(:create) do |ckb_transaction| 15.times do |index| create(:cell_output, capacity: 10**8 * 8, - ckb_transaction: ckb_transaction, + ckb_transaction:, block: ckb_transaction.block, tx_hash: ckb_transaction.tx_hash, cell_index: index) previous_output = { tx_hash: ckb_transaction.tx_hash, index: 1 } - create(:cell_input, previous_output: previous_output, - ckb_transaction: ckb_transaction, + create(:cell_input, previous_output:, + ckb_transaction:, block: ckb_transaction.block) end end end - factory :cell_base_transaction do + + trait :with_cell_base do + after(:create) do |ckb_transaction| + 15.times do |index| + create(:cell_output, capacity: 10**8 * 8, + ckb_transaction:, + block: ckb_transaction.block, + tx_hash: ckb_transaction.tx_hash, + cell_index: index, + status: :live) + end + end end end end