diff --git a/app/serializers/ckb_transaction_serializer.rb b/app/serializers/ckb_transaction_serializer.rb index 462f1f13a..b7ce575dc 100644 --- a/app/serializers/ckb_transaction_serializer.rb +++ b/app/serializers/ckb_transaction_serializer.rb @@ -7,7 +7,7 @@ class CkbTransactionSerializer attributes :is_cellbase, :tx_status attribute :witnesses do |o| - o.witnesses&.map(&:data) || [] + o.witnesses.order("index ASC")&.map(&:data) || [] end attribute :cell_deps do |o| diff --git a/test/controllers/api/v1/ckb_transactions_controller_test.rb b/test/controllers/api/v1/ckb_transactions_controller_test.rb index 20984d54c..ff9586c86 100644 --- a/test/controllers/api/v1/ckb_transactions_controller_test.rb +++ b/test/controllers/api/v1/ckb_transactions_controller_test.rb @@ -30,7 +30,8 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should respond with 415 Unsupported Media Type when Content-Type is wrong" do ckb_transaction = create(:ckb_transaction) - get api_v1_ckb_transaction_url(ckb_transaction.tx_hash), headers: { "Content-Type": "text/plain" } + get api_v1_ckb_transaction_url(ckb_transaction.tx_hash), + headers: { "Content-Type": "text/plain" } assert_equal 415, response.status end @@ -38,9 +39,11 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should respond with error object when Content-Type is wrong" do ckb_transaction = create(:ckb_transaction) error_object = Api::V1::Exceptions::InvalidContentTypeError.new - response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + response_json = RequestErrorSerializer.new([error_object], + message: error_object.title).serialized_json - get api_v1_ckb_transaction_url(ckb_transaction.tx_hash), headers: { "Content-Type": "text/plain" } + get api_v1_ckb_transaction_url(ckb_transaction.tx_hash), + headers: { "Content-Type": "text/plain" } assert_equal response_json, response.body end @@ -49,7 +52,8 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest ckb_transaction = create(:ckb_transaction) get api_v1_ckb_transaction_url(ckb_transaction.tx_hash), - headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" } + headers: { "Content-Type": "application/vnd.api+json", + "Accept": "application/json" } assert_equal 406, response.status end @@ -57,17 +61,20 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should respond with error object when Accept is wrong" do ckb_transaction = create(:ckb_transaction) error_object = Api::V1::Exceptions::InvalidAcceptError.new - response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + response_json = RequestErrorSerializer.new([error_object], + message: error_object.title).serialized_json get api_v1_ckb_transaction_url(ckb_transaction.tx_hash), - headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" } + headers: { "Content-Type": "application/vnd.api+json", + "Accept": "application/json" } assert_equal response_json, response.body end test "should return error object when id is not a hex start with 0x" do error_object = Api::V1::Exceptions::CkbTransactionTxHashInvalidError.new - response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + response_json = RequestErrorSerializer.new([error_object], + message: error_object.title).serialized_json valid_get api_v1_ckb_transaction_url("9034fwefwef") @@ -76,7 +83,8 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return error object when id is a hex start with 0x but it's length is wrong" do error_object = Api::V1::Exceptions::CkbTransactionTxHashInvalidError.new - response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + response_json = RequestErrorSerializer.new([error_object], + message: error_object.title).serialized_json valid_get api_v1_ckb_transaction_url("0x9034fwefwef") @@ -85,7 +93,8 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return error object when no records found by id" do error_object = Api::V1::Exceptions::CkbTransactionNotFoundError.new - response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + response_json = RequestErrorSerializer.new([error_object], + message: error_object.title).serialized_json valid_get api_v1_ckb_transaction_url("0x3b138b3126d10ec000417b68bc715f17e86293d6cdbcb3fd8a628ad4a0b756f6") @@ -94,10 +103,16 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return corresponding ckb transaction with given transaction hash" do ckb_transaction = create(:ckb_transaction) + create(:witness, index: 0, data: "0x", ckb_transaction:) + create(:witness, index: 1, data: "0x0102", + ckb_transaction:) valid_get api_v1_ckb_transaction_url(ckb_transaction.tx_hash) - assert_equal CkbTransactionSerializer.new(ckb_transaction).serialized_json, response.body + assert_equal CkbTransactionSerializer.new(ckb_transaction).serialized_json, + response.body + assert_equal "0x", + JSON.parse(CkbTransactionSerializer.new(ckb_transaction).serialized_json)["data"]["attributes"]["witnesses"].first end test "should return pool tx when tx is in the pool" do @@ -118,7 +133,8 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest valid_get api_v1_ckb_transaction_url(ckb_transaction.tx_hash) response_tx_transaction = json["data"] - assert_equal TransactionKeys, response_tx_transaction["attributes"].keys.sort + assert_equal TransactionKeys, + response_tx_transaction["attributes"].keys.sort end test "returned income should be null" do @@ -134,7 +150,8 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return all display_inputs" do block = create(:block, :with_block_hash) - ckb_transaction = create(:ckb_transaction, :with_multiple_inputs_and_outputs, block: block) + ckb_transaction = create(:ckb_transaction, + :with_multiple_inputs_and_outputs, block:) valid_get api_v1_ckb_transaction_url(ckb_transaction.tx_hash) @@ -146,7 +163,8 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return all display_outputs" do block = create(:block, :with_block_hash) - ckb_transaction = create(:ckb_transaction, :with_multiple_inputs_and_outputs, block: block) + ckb_transaction = create(:ckb_transaction, + :with_multiple_inputs_and_outputs, block:) valid_get api_v1_ckb_transaction_url(ckb_transaction.tx_hash) @@ -169,51 +187,58 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest end test "should respond with 415 Unsupported Media Type when call index and Content-Type is wrong" do - get api_v1_ckb_transactions_url, headers: { "Content-Type": "text/plain" } + get api_v1_ckb_transactions_url, + headers: { "Content-Type": "text/plain" } assert_equal 415, response.status end test "should respond with error object when call index and Content-Type is wrong" do error_object = Api::V1::Exceptions::InvalidContentTypeError.new - response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + response_json = RequestErrorSerializer.new([error_object], + message: error_object.title).serialized_json - get api_v1_ckb_transactions_url, headers: { "Content-Type": "text/plain" } + get api_v1_ckb_transactions_url, + headers: { "Content-Type": "text/plain" } assert_equal response_json, response.body end test "should respond with 406 Not Acceptable when call index and Accept is wrong" do get api_v1_ckb_transactions_url, - headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" } + headers: { "Content-Type": "application/vnd.api+json", + "Accept": "application/json" } assert_equal 406, response.status end test "should respond with error object when call index and Accept is wrong" do error_object = Api::V1::Exceptions::InvalidAcceptError.new - response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + response_json = RequestErrorSerializer.new([error_object], + message: error_object.title).serialized_json get api_v1_ckb_transactions_url, - headers: { "Content-Type": "application/vnd.api+json", "Accept": "application/json" } + headers: { "Content-Type": "application/vnd.api+json", + "Accept": "application/json" } assert_equal response_json, response.body end test "should get serialized objects" do block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 15, block: block) + create_list(:ckb_transaction, 15, block:) ckb_transactions = CkbTransaction.recent.limit(ENV["HOMEPAGE_TRANSACTIONS_RECORDS_COUNT"].to_i) valid_get api_v1_ckb_transactions_url - assert_equal CkbTransactionListSerializer.new(ckb_transactions).serialized_json, response.body + assert_equal CkbTransactionListSerializer.new(ckb_transactions).serialized_json, + response.body end test "serialized objects should in reverse order of timestamp" do block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 15, block: block) + create_list(:ckb_transaction, 15, block:) valid_get api_v1_ckb_transactions_url @@ -226,7 +251,7 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should contain right keys in the serialized object" do block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 15, block: block) + create_list(:ckb_transaction, 15, block:) valid_get api_v1_ckb_transactions_url @@ -237,7 +262,7 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return the corresponding number of ckb transactions " do block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 30, block: block) + create_list(:ckb_transaction, 30, block:) valid_get api_v1_ckb_transactions_url @@ -261,9 +286,10 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return error object when page param is invalid" do block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 15, block: block) + create_list(:ckb_transaction, 15, block:) error_object = Api::V1::Exceptions::PageParamError.new - response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + response_json = RequestErrorSerializer.new([error_object], + message: error_object.title).serialized_json valid_get api_v1_ckb_transactions_url, params: { page: "aaa" } @@ -272,9 +298,10 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return error object when page size param is invalid" do block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 15, block: block) + create_list(:ckb_transaction, 15, block:) error_object = Api::V1::Exceptions::PageSizeParamError.new - response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json + response_json = RequestErrorSerializer.new([error_object], + message: error_object.title).serialized_json valid_get api_v1_ckb_transactions_url, params: { page_size: "aaa" } @@ -284,19 +311,21 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return error object when page and page size param are invalid" do errors = [] block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 15, block: block) + create_list(:ckb_transaction, 15, block:) errors << Api::V1::Exceptions::PageParamError.new errors << Api::V1::Exceptions::PageSizeParamError.new - response_json = RequestErrorSerializer.new(errors, message: errors.first.title).serialized_json + response_json = RequestErrorSerializer.new(errors, + message: errors.first.title).serialized_json - valid_get api_v1_ckb_transactions_url, params: { page: "bbb", page_size: "aaa" } + valid_get api_v1_ckb_transactions_url, + params: { page: "bbb", page_size: "aaa" } assert_equal response_json, response.body end test "should return 15 records when page and page_size are not set" do block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 20, block: block) + create_list(:ckb_transaction, 20, block:) valid_get api_v1_ckb_transactions_url @@ -306,12 +335,14 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return corresponding page's records when page is set and page_size is not set" do page = 2 block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 20, block: block) + create_list(:ckb_transaction, 20, block:) ckb_transactions = CkbTransaction.recent.limit(ENV["HOMEPAGE_TRANSACTIONS_RECORDS_COUNT"].to_i) - valid_get api_v1_ckb_transactions_url, params: { page: page } + valid_get api_v1_ckb_transactions_url, params: { page: } - response_ckb_transactions = CkbTransactionListSerializer.new(ckb_transactions, {}).serialized_json + response_ckb_transactions = CkbTransactionListSerializer.new( + ckb_transactions, {} + ).serialized_json assert_equal response_ckb_transactions, response.body assert_equal 15, json["data"].size @@ -319,33 +350,41 @@ class CkbTransactionsControllerTest < ActionDispatch::IntegrationTest test "should return the corresponding number of ckb_transactions when page is not set and page_size is set" do block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 20, block: block) + create_list(:ckb_transaction, 20, block:) valid_get api_v1_ckb_transactions_url, params: { page_size: 12 } ckb_transactions = CkbTransaction.recent.limit(ENV["HOMEPAGE_TRANSACTIONS_RECORDS_COUNT"].to_i) - response_ckb_transactions = CkbTransactionListSerializer.new(ckb_transactions, {}).serialized_json + response_ckb_transactions = CkbTransactionListSerializer.new( + ckb_transactions, {} + ).serialized_json assert_equal response_ckb_transactions, response.body - assert_equal [false], CkbTransaction.where(id: json["data"].map { |tx| tx.dig("id") }).pluck(:is_cellbase).uniq + assert_equal [false], CkbTransaction.where(id: json["data"].map do |tx| + tx.dig("id") + end).pluck(:is_cellbase).uniq assert_equal 15, json["data"].size end test "should return the corresponding transactions when page and page_size are set" do block = create(:block, :with_block_hash) - create_list(:ckb_transaction, 15, block: block) + create_list(:ckb_transaction, 15, block:) create(:table_record_count, :block_counter, count: Block.count) - create(:table_record_count, :ckb_transactions_counter, count: CkbTransaction.count) + create(:table_record_count, :ckb_transactions_counter, + count: CkbTransaction.count) page = 2 page_size = 5 ckb_transactions = CkbTransaction.recent.page(page).per(page_size) - valid_get api_v1_ckb_transactions_url, params: { page: page, page_size: page_size } + valid_get api_v1_ckb_transactions_url, + params: { page:, page_size: } records_counter = RecordCounters::Transactions.new - options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: page, - page_size: page_size, records_counter: records_counter).call - response_ckb_transactions = CkbTransactionListSerializer.new(ckb_transactions, options).serialized_json + options = FastJsonapi::PaginationMetaGenerator.new(request:, records: ckb_transactions, page:, + page_size:, records_counter:).call + response_ckb_transactions = CkbTransactionListSerializer.new( + ckb_transactions, options + ).serialized_json assert_equal response_ckb_transactions, response.body end @@ -353,15 +392,17 @@ class CkbTransactionsControllerTest < 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) - valid_post api_v1_query_ckb_transactions_url, params: { address: address.address_hash } + valid_post api_v1_query_ckb_transactions_url, + params: { address: address.address_hash } records_counter = RecordCounters::AddressTransactions.new(address) - options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_transactions, page: page, - page_size: page_size, records_counter: records_counter).call + options = FastJsonapi::PaginationMetaGenerator.new(request:, records: ckb_transactions, page:, + page_size:, records_counter:).call - assert_equal CkbTransactionsSerializer.new(ckb_transactions, options.merge(params: { previews: true, address: address })).serialized_json, + assert_equal CkbTransactionsSerializer.new(ckb_transactions, options.merge(params: { previews: true, address: })).serialized_json, response.body end end