From 86b4440602c4fa5f016dc7a2a5b58a32f3c7bd96 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Wed, 13 Mar 2024 20:46:24 +0800 Subject: [PATCH] feat: handle is_repeated_symbol logic in node processor Signed-off-by: Miles Zhang --- .../v1/address_udt_transactions_controller.rb | 4 ++-- .../api/v1/udt_transactions_controller.rb | 4 ++-- .../ckb_sync/new_node_data_processor.rb | 24 +++++++++---------- app/serializers/udt_serializer.rb | 6 +++++ ...ddress_udt_transactions_controller_test.rb | 2 +- .../ckb_sync/node_data_processor_test.rb | 4 ++-- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/app/controllers/api/v1/address_udt_transactions_controller.rb b/app/controllers/api/v1/address_udt_transactions_controller.rb index 6136fc5898..c0d81f5458 100644 --- a/app/controllers/api/v1/address_udt_transactions_controller.rb +++ b/app/controllers/api/v1/address_udt_transactions_controller.rb @@ -9,8 +9,8 @@ def show raise Api::V1::Exceptions::AddressNotFoundError if address.is_a?(NullAddress) raise Api::V1::Exceptions::TypeHashInvalidError if params[:type_hash].blank? - udt = Udt.find_by(type_hash: params[:type_hash]) - raise Api::V1::Exceptions::UdtNotFoundError if udt.blank? || (udt.udt_type != "omiga_inscription" && !udt.published) + udt = Udt.find_by(type_hash: params[:type_hash], published: true) + raise Api::V1::Exceptions::UdtNotFoundError if udt.blank? ckb_dao_transactions = address.ckb_udt_transactions(udt.id). select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at). diff --git a/app/controllers/api/v1/udt_transactions_controller.rb b/app/controllers/api/v1/udt_transactions_controller.rb index 0924e9d5f4..517609ff3f 100644 --- a/app/controllers/api/v1/udt_transactions_controller.rb +++ b/app/controllers/api/v1/udt_transactions_controller.rb @@ -4,8 +4,8 @@ class UdtTransactionsController < ApplicationController before_action :validate_query_params, :validate_pagination_params, :pagination_params def show - udt = Udt.find_by(type_hash: params[:id]) - raise Api::V1::Exceptions::UdtNotFoundError if udt.blank? || (udt.udt_type != "omiga_inscription" && !udt.published) + udt = Udt.find_by(type_hash: params[:id], published: true) + raise Api::V1::Exceptions::UdtNotFoundError if udt.blank? ckb_transactions = udt.ckb_transactions.tx_committed. select(:id, :tx_hash, :block_id, :block_number, diff --git a/app/models/ckb_sync/new_node_data_processor.rb b/app/models/ckb_sync/new_node_data_processor.rb index 98f05899ff..882395247b 100644 --- a/app/models/ckb_sync/new_node_data_processor.rb +++ b/app/models/ckb_sync/new_node_data_processor.rb @@ -633,24 +633,24 @@ def build_udts!(local_block, outputs, outputs_data) next unless cell_type.in?(%w(udt m_nft_token nrc_721_token spore_cell omiga_inscription_info omiga_inscription)) - type_hash, parsed_udt_type, published = + type_hash, parsed_udt_type = if cell_type == "omiga_inscription_info" info = CkbUtils.parse_omiga_inscription_info(outputs_data[tx_index][index]) info_type_hash = output.type.compute_hash - attrs = info.merge(output.type.to_h, type_hash: info_type_hash) pre_closed_info = OmigaInscriptionInfo.includes(:udt).find_by( type_hash: info_type_hash, mint_status: :closed, ) - attrs, published = - if pre_closed_info - [attrs.merge(pre_udt_hash: pre_closed_info.udt_hash), pre_closed_info.udt&.published == true] - else - [attrs, false] - end + attrs = info.merge(output.type.to_h, type_hash: info_type_hash) + if pre_closed_info + attrs[:pre_udt_hash] = pre_closed_info.udt_hash + attrs[:is_repeated_symbol] = pre_closed_info.is_repeated_symbol + else + attrs[:is_repeated_symbol] = OmigaInscriptionInfo.where(symbol: info[:symbol].strip).exists? + end OmigaInscriptionInfo.upsert(attrs, unique_by: :udt_hash) - [info[:udt_hash], "omiga_inscription", published] + [info[:udt_hash], "omiga_inscription"] else - [output.type.compute_hash, udt_type(cell_type), false] + [output.type.compute_hash, udt_type(cell_type)] end if cell_type == "omiga_inscription" @@ -714,9 +714,7 @@ def build_udts!(local_block, outputs, outputs_data) nft_token_attr[:full_name] = info[:name] nft_token_attr[:symbol] = info[:symbol] nft_token_attr[:decimal] = info[:decimal] - if published || !Udt.where(symbol: info[:symbol].strip, udt_type: :omiga_inscription).exists? - nft_token_attr[:published] = true - end + nft_token_attr[:published] = true end # fill issuer_address after publish the token udts_attributes << { diff --git a/app/serializers/udt_serializer.rb b/app/serializers/udt_serializer.rb index de8e55a5c4..ad752bbe37 100644 --- a/app/serializers/udt_serializer.rb +++ b/app/serializers/udt_serializer.rb @@ -61,4 +61,10 @@ class UdtSerializer } do |object| object.omiga_inscription_info.pre_udt_hash end + + attribute :is_repeated_symbol, if: Proc.new { |record, _params| + record.udt_type == "omiga_inscription" + } do |object| + object.is_repeated_symbol + end end diff --git a/test/controllers/api/v1/address_udt_transactions_controller_test.rb b/test/controllers/api/v1/address_udt_transactions_controller_test.rb index dfee0eae51..a7de8f36fb 100644 --- a/test/controllers/api/v1/address_udt_transactions_controller_test.rb +++ b/test/controllers/api/v1/address_udt_transactions_controller_test.rb @@ -238,7 +238,7 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest end test "should return meta if udt is omiga_inscription" do - udt = create(:udt, udt_type: :omiga_inscription, published: false) + udt = create(:udt, udt_type: :omiga_inscription, published: true) address = create(:address, :with_udt_transactions, transactions_count: 3, udt:) valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash) diff --git a/test/models/ckb_sync/node_data_processor_test.rb b/test/models/ckb_sync/node_data_processor_test.rb index 44d97fef13..c0fb9c1b07 100644 --- a/test/models/ckb_sync/node_data_processor_test.rb +++ b/test/models/ckb_sync/node_data_processor_test.rb @@ -4047,7 +4047,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase udt = Udt.first assert_equal "0x5fa66c8d5f43914f85d3083e0529931883a5b0a14282f891201069f1b5067908", udt.type_hash - assert_equal true, udt.published + assert_equal false, info.is_repeated_symbol end end @@ -4148,7 +4148,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase node_data_processor.process_block(node_block) assert_equal 2, Udt.count assert_equal info.udt_hash, OmigaInscriptionInfo.last.pre_udt_hash - assert_equal true, Udt.last.published + assert_equal false, OmigaInscriptionInfo.last.is_repeated_symbol end end