From 0506bb4c307d7abed090dc291c51261957a6bb70 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Tue, 6 Feb 2024 08:47:14 +0800 Subject: [PATCH 1/2] fix: check token exist first when update udt (#1618) Signed-off-by: Miles Zhang --- app/controllers/api/v1/udts_controller.rb | 2 ++ app/lib/api/v1/exceptions.rb | 10 ++++++++-- app/models/udt_verification.rb | 8 +++++--- test/controllers/api/v1/udts_controller_test.rb | 13 +++++++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/udts_controller.rb b/app/controllers/api/v1/udts_controller.rb index 12f028f73..e632f6f73 100644 --- a/app/controllers/api/v1/udts_controller.rb +++ b/app/controllers/api/v1/udts_controller.rb @@ -55,6 +55,8 @@ def update raise Api::V1::Exceptions::TokenExpiredError rescue UdtVerification::TokenNotMatchError raise Api::V1::Exceptions::TokenNotMatchError + rescue UdtVerification::TokenNotExistError + raise Api::V1::Exceptions::TokenNotExistError end def show diff --git a/app/lib/api/v1/exceptions.rb b/app/lib/api/v1/exceptions.rb index c011015fb..44d6721d1 100644 --- a/app/lib/api/v1/exceptions.rb +++ b/app/lib/api/v1/exceptions.rb @@ -189,13 +189,13 @@ def initialize class UdtInfoInvalidError < Error def initialize(detail) - super code: 1030, status: 400, title: "UDT info parameters invalid", detail: detail, href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html" + super code: 1030, status: 400, title: "UDT info parameters invalid", detail:, href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html" end end class UdtVerificationInvalidError < Error def initialize(detail) - super code: 1031, status: 400, title: "UDT verification invalid", detail: detail, href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html" + super code: 1031, status: 400, title: "UDT verification invalid", detail:, href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html" end end @@ -228,6 +228,12 @@ def initialize super code: 1036, status: 400, title: "Token sent too frequently", detail: "", href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html" end end + + class TokenNotExistError < Error + def initialize + super code: 1037, status: 400, title: "Token is required when you update udt info", detail: "", href: "https://nervosnetwork.github.io/ckb-explorer/public/api_doc.html" + end + end end end end diff --git a/app/models/udt_verification.rb b/app/models/udt_verification.rb index f92c4276b..817b35a25 100644 --- a/app/models/udt_verification.rb +++ b/app/models/udt_verification.rb @@ -5,20 +5,22 @@ class UdtVerification < ApplicationRecord class TokenExpiredError < StandardError; end class TokenNotMatchError < StandardError; end class TokenSentTooFrequentlyError < StandardError; end + class TokenNotExistError < StandardError; end belongs_to :udt def refresh_token!(ip) - raise TokenSentTooFrequentlyError if sent_at.present? && self.sent_at + SENT_FREQUENCY_MINUTES.minutes > Time.now + raise TokenSentTooFrequentlyError if sent_at.present? && sent_at + SENT_FREQUENCY_MINUTES.minutes > Time.now self.token = rand(999999).to_s.rjust(6, "0") self.sent_at = Time.now self.last_ip = ip - self.save! + save! end def validate_token!(token_params) - raise TokenExpiredError if self.sent_at + KEEP_ALIVE_MINUTES.minutes < Time.now + raise TokenNotExistError if token_params.blank? + raise TokenExpiredError if sent_at + KEEP_ALIVE_MINUTES.minutes < Time.now raise TokenNotMatchError if token != token_params.to_i end end diff --git a/test/controllers/api/v1/udts_controller_test.rb b/test/controllers/api/v1/udts_controller_test.rb index ea1cd2b99..3470cc547 100644 --- a/test/controllers/api/v1/udts_controller_test.rb +++ b/test/controllers/api/v1/udts_controller_test.rb @@ -587,6 +587,19 @@ class UdtsControllerTest < ActionDispatch::IntegrationTest assert_equal true, udt.reload.published assert_equal "abc@sudt.com", udt.reload.email end + + test "should raise token not exist error when update udt but token not passed" do + udt = create(:udt, email: "abc@sudt.com") + create(:udt_verification, udt:) + valid_put api_v1_udt_url("#{udt.type_hash}"), params: { + symbol: "GWK", + full_name: "GodwokenToken on testnet_v1", + } + + assert_equal 400, response.status + assert_equal [{ "title" => "Token is required when you update udt info", "detail" => "", "code" => 1037, "status" => 400 }], + JSON.parse(response.body) + end end end end From 7d65d7a636c83810e21b07f7a1f3214d623d0eac Mon Sep 17 00:00:00 2001 From: Rabbit Date: Tue, 6 Feb 2024 15:57:36 +0800 Subject: [PATCH 2/2] fix: transaction display cells count (#1621) --- app/serializers/ckb_transactions_serializer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/serializers/ckb_transactions_serializer.rb b/app/serializers/ckb_transactions_serializer.rb index f36570812..2e2ff1495 100644 --- a/app/serializers/ckb_transactions_serializer.rb +++ b/app/serializers/ckb_transactions_serializer.rb @@ -16,11 +16,11 @@ class CkbTransactionsSerializer end attribute :display_inputs_count do |object| - object.display_inputs.count + object.is_cellbase ? 1 : object.cell_inputs.count end attribute :display_outputs_count do |object| - object.display_outputs.count + object.outputs.count end attribute :display_inputs do |object, params|