From 33ccdac1823b9d9164e8d9db5455c309e9fa329c Mon Sep 17 00:00:00 2001 From: Rabbit Date: Thu, 4 Jan 2024 16:00:54 +0800 Subject: [PATCH 1/2] refactor: move display_inputs and display_outputs to api/v2 (#1553) * refactor: move display_inputs and display_outputs to api/v2 * chore: add display_cells option --- .../api/v1/ckb_transactions_controller.rb | 49 ++-------------- .../api/v2/ckb_transactions_controller.rb | 57 ++++++++++++++++++- app/serializers/ckb_transaction_serializer.rb | 6 ++ config/initializers/rack_attack.rb | 4 +- config/routes.rb | 2 - config/routes/v2.rb | 2 + 6 files changed, 72 insertions(+), 48 deletions(-) diff --git a/app/controllers/api/v1/ckb_transactions_controller.rb b/app/controllers/api/v1/ckb_transactions_controller.rb index 9688d68c8..82e64b4b8 100644 --- a/app/controllers/api/v1/ckb_transactions_controller.rb +++ b/app/controllers/api/v1/ckb_transactions_controller.rb @@ -1,12 +1,9 @@ module Api module V1 class CkbTransactionsController < ApplicationController - before_action :validate_query_params, - only: %i[show display_inputs display_outputs] - before_action :find_transaction, - only: %i[show display_inputs display_outputs] - before_action :validate_pagination_params, :pagination_params, - only: %i[index display_inputs display_outputs] + before_action :validate_query_params, only: %i[show] + before_action :find_transaction,only: %i[show] + before_action :validate_pagination_params, :pagination_params, only: %i[index] def index if from_home_page? @@ -109,43 +106,9 @@ def query def show expires_in 10.seconds, public: true, must_revalidate: true - render json: CkbTransactionSerializer.new(@ckb_transaction) - end - - def display_inputs - expires_in 1.hour, public: true, must_revalidate: true - - if @ckb_transaction.is_cellbase - cell_inputs = @ckb_transaction.cellbase_display_inputs - total_count = cell_inputs.count - else - cell_inputs = @ckb_transaction.cell_inputs.order(id: :asc). - page(@page).per(@page_size).fast_page - total_count = cell_inputs.total_count - cell_inputs = @ckb_transaction.normal_tx_display_inputs(cell_inputs) - end - - render json: { data: cell_inputs, - meta: { total: total_count, - page_size: @page_size.to_i } } - end - - def display_outputs - expires_in 1.hour, public: true, must_revalidate: true - - if @ckb_transaction.is_cellbase - cell_outputs = @ckb_transaction.cellbase_display_outputs - total_count = cell_outputs.count - else - cell_outputs = @ckb_transaction.outputs.order(id: :asc). - page(@page).per(@page_size).fast_page - total_count = cell_outputs.total_count - cell_outputs = @ckb_transaction.normal_tx_display_outputs(cell_outputs) - end - - render json: { data: cell_outputs, - meta: { total: total_count, - page_size: @page_size.to_i } } + render json: CkbTransactionSerializer.new(@ckb_transaction, { + params: { display_cells: params.fetch(:display_cells, true) + }}) end private diff --git a/app/controllers/api/v2/ckb_transactions_controller.rb b/app/controllers/api/v2/ckb_transactions_controller.rb index 5e392e7fc..382b40b28 100644 --- a/app/controllers/api/v2/ckb_transactions_controller.rb +++ b/app/controllers/api/v2/ckb_transactions_controller.rb @@ -1,7 +1,8 @@ module Api module V2 class CkbTransactionsController < BaseController - # transaction lite info + before_action :set_page_and_page_size, only: %i[display_inputs display_outputs] + def details ckb_transaction = CkbTransaction.where(tx_hash: params[:id]).order(tx_status: :desc).first head :not_found and return if ckb_transaction.blank? @@ -15,6 +16,55 @@ def details render json: { data: transfers } end + def display_inputs + expires_in 1.hour, public: true, must_revalidate: true + + ckb_transaction = CkbTransaction.where(tx_hash: params[:id]).order(tx_status: :desc).first + head :not_found and return if ckb_transaction.blank? + + if ckb_transaction.is_cellbase + cell_inputs = ckb_transaction.cellbase_display_inputs + total_count = cell_inputs.count + else + cell_inputs = ckb_transaction.cell_inputs.order(id: :asc). + page(@page).per(@page_size).fast_page + total_count = cell_inputs.total_count + cell_inputs = ckb_transaction.normal_tx_display_inputs(cell_inputs) + end + + render json: { + data: cell_inputs, + meta: { + total: total_count, + page_size: @page_size.to_i, + }, + } + end + + def display_outputs + expires_in 1.hour, public: true, must_revalidate: true + + ckb_transaction = CkbTransaction.where(tx_hash: params[:id]).order(tx_status: :desc).first + head :not_found and return if ckb_transaction.blank? + + if ckb_transaction.is_cellbase + cell_outputs = ckb_transaction.cellbase_display_outputs + total_count = cell_outputs.count + else + cell_outputs = ckb_transaction.outputs.order(id: :asc). + page(@page).per(@page_size).fast_page + total_count = cell_outputs.total_count + cell_outputs = ckb_transaction.normal_tx_display_outputs(cell_outputs) + end + + render json: { + data: cell_outputs, + meta: { + total: total_count, page_size: @page_size.to_i + }, + } + end + private def build_cell_capacities(outputs) @@ -92,6 +142,11 @@ def token_unit(cell) "CKB" end end + + def set_page_and_page_size + @page = params.fetch(:page, 1) + @page_size = params.fetch(:page_size, CkbTransaction.default_per_page) + end end end end diff --git a/app/serializers/ckb_transaction_serializer.rb b/app/serializers/ckb_transaction_serializer.rb index 1e221fff8..462f1f13a 100644 --- a/app/serializers/ckb_transaction_serializer.rb +++ b/app/serializers/ckb_transaction_serializer.rb @@ -45,6 +45,9 @@ class CkbTransactionSerializer end attribute :display_inputs do |object, params| + display_cells = ActiveModel::Type::Boolean.new.cast(params[:display_cells]) + next [] unless display_cells + cache_key = "display_inputs_previews_#{params[:previews].present?}_#{object.id}_#{object.inputs.cache_version}" Rails.cache.fetch(cache_key, expires_in: 1.day) do if params && params[:previews] @@ -56,6 +59,9 @@ class CkbTransactionSerializer end attribute :display_outputs do |object, params| + display_cells = ActiveModel::Type::Boolean.new.cast(params[:display_cells]) + next [] unless display_cells + cache_key = "display_outputs_previews_#{params[:previews].present?}_#{object.id}_#{object.outputs.cache_version}" Rails.cache.fetch(cache_key, expires_in: 1.day) do if params && params[:previews] diff --git a/config/initializers/rack_attack.rb b/config/initializers/rack_attack.rb index 17eb08af0..5a60ad13d 100644 --- a/config/initializers/rack_attack.rb +++ b/config/initializers/rack_attack.rb @@ -35,13 +35,13 @@ class Rack::Attack # If you want to return 503 so that the attacker might be fooled into # believing that they've successfully broken your app (or you just want to # customize the response), then uncomment these lines. - # self.throttled_response = lambda do |env| + # self.throttled_responder = lambda do |env| # [ 503, # status # {}, # headers # ['']] # body # end # - self.throttled_response = + self.throttled_responder = lambda do |env| match_data = env["rack.attack.match_data"] now = match_data[:epoch_time] diff --git a/config/routes.rb b/config/routes.rb index 4a91a6226..e32ad9356 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,8 +25,6 @@ resources :block_transactions, only: :show resources :addresses, only: :show get "/transactions/:id", to: "ckb_transactions#show", as: "ckb_transaction" - get "/transactions/:id/display_inputs", to: "ckb_transactions#display_inputs" - get "/transactions/:id/display_outputs", to: "ckb_transactions#display_outputs" get "/transactions", to: "ckb_transactions#index", as: "ckb_transactions" post "/transactions/query", to: "ckb_transactions#query", as: "query_ckb_transactions" resources :cell_input_lock_scripts, only: :show diff --git a/config/routes/v2.rb b/config/routes/v2.rb index 0d1dfe8cb..cdb46cf20 100644 --- a/config/routes/v2.rb +++ b/config/routes/v2.rb @@ -4,6 +4,8 @@ resources :ckb_transactions, only: [:index, :show] do member do get :details + get :display_inputs + get :display_outputs end end resources :transactions do From 789be08e588899abeb6ddaf2d287b400f7d4c91a Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 8 Jan 2024 11:42:50 +0800 Subject: [PATCH 2/2] feat: remove published: true limit when udpate api token info (#1555) Signed-off-by: Miles Zhang --- app/controllers/api/v1/udts_controller.rb | 5 +++-- test/controllers/api/v1/udts_controller_test.rb | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/udts_controller.rb b/app/controllers/api/v1/udts_controller.rb index 3ad82eae0..08ac99d61 100644 --- a/app/controllers/api/v1/udts_controller.rb +++ b/app/controllers/api/v1/udts_controller.rb @@ -23,7 +23,7 @@ def index end def update - udt = Udt.find_by!(type_hash: params[:id], published: true) + udt = Udt.find_by!(type_hash: params[:id]) attrs = { symbol: params[:symbol], full_name: params[:full_name], @@ -34,7 +34,8 @@ def update icon_file: params[:icon_file], uan: params[:uan], display_name: params[:display_name], - email: params[:email] + email: params[:email], + published: true } if udt.email.blank? raise Api::V1::Exceptions::UdtInfoInvalidError.new("Email can't be blank") if params[:email].blank? diff --git a/test/controllers/api/v1/udts_controller_test.rb b/test/controllers/api/v1/udts_controller_test.rb index f833acfb4..c55583487 100644 --- a/test/controllers/api/v1/udts_controller_test.rb +++ b/test/controllers/api/v1/udts_controller_test.rb @@ -549,7 +549,7 @@ class UdtsControllerTest < ActionDispatch::IntegrationTest end test "should update successfully when update udt" do - udt = create(:udt, email: "abc@sudt.com", published: true) + udt = create(:udt, email: "abc@sudt.com") create(:udt_verification, udt: udt) valid_put api_v1_udt_url("#{udt.type_hash}"), params: { symbol: "GWK", @@ -561,6 +561,7 @@ class UdtsControllerTest < ActionDispatch::IntegrationTest assert_equal 200, response.status assert_equal "ok", JSON.parse(response.body) assert_equal "GWK", udt.reload.symbol + assert_equal true, udt.reload.published end end end