From 244cacfd8855541354f4d2fcb600d85f9ee64924 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Sun, 5 Jan 2025 18:22:52 +0900 Subject: [PATCH 1/3] fix: update address not nil attributes (#2390) Signed-off-by: Miles Zhang --- app/models/ckb_sync/new_node_data_processor.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/models/ckb_sync/new_node_data_processor.rb b/app/models/ckb_sync/new_node_data_processor.rb index ae2134539..d27834518 100644 --- a/app/models/ckb_sync/new_node_data_processor.rb +++ b/app/models/ckb_sync/new_node_data_processor.rb @@ -355,7 +355,17 @@ def update_addresses_dao_info(addrs_deposit_info) is_depositor: address_info[:is_depositor], } end - Address.upsert_all(addresses_deposit_attributes, record_timestamps: true) if addresses_deposit_attributes.present? + if addresses_deposit_attributes.present? + Address.upsert_all( + addresses_deposit_attributes, + record_timestamps: true, + on_duplicate: Arel.sql( + "dao_deposit = COALESCE(EXCLUDED.dao_deposit, addresses.dao_deposit), " \ + "interest = COALESCE(EXCLUDED.interest, addresses.interest), " \ + "is_depositor = COALESCE(EXCLUDED.is_depositor, addresses.is_depositor)", + ), + ) + end end def update_or_create_udt_accounts!(local_block) From 992b4f35e192425926e0d7bcb89695040a3cd36d Mon Sep 17 00:00:00 2001 From: Rabbit Date: Tue, 7 Jan 2025 17:18:41 +0800 Subject: [PATCH 2/3] refactor: address live cells (#2392) --- app/interactions/addresses/live_cells.rb | 19 +++++++++---------- app/models/bitcoin_vout.rb | 4 ++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/interactions/addresses/live_cells.rb b/app/interactions/addresses/live_cells.rb index 0fc2ca757..0e3014163 100644 --- a/app/interactions/addresses/live_cells.rb +++ b/app/interactions/addresses/live_cells.rb @@ -14,17 +14,16 @@ def execute raise AddressNotFoundError if address.is_a?(NullAddress) order_by, asc_or_desc = live_cells_ordering - cell_outputs = CellOutput.includes(:bitcoin_vout).where( - cell_outputs: { status: "live", address_id: address.map(&:id) }, - ) - cell_outputs = cell_outputs.where(bitcoin_vouts: { status: bound_status }) if bound_status - cell_output_ids = cell_outputs.pluck("cell_outputs.id") - records = CellOutput.where(id: cell_output_ids).order(order_by => asc_or_desc). - page(page).per(page_size).fast_page - options = FastJsonapi::PaginationMetaGenerator.new( - request:, records:, page:, page_size:, - ).call + if bound_status + bitcoin_vouts = BitcoinVout.where(address_id: address.map(&:id), status: bound_status) + records = CellOutput.live.where(id: bitcoin_vouts.map(&:cell_output_id)).order(order_by => asc_or_desc). + page(page).per(page_size).fast_page + else + records = CellOutput.live.where(address_id: address.map(&:id)).order(order_by => asc_or_desc). + page(page).per(page_size).fast_page + end + options = FastJsonapi::PaginationMetaGenerator.new(request:, records:, page:, page_size:).call CellOutputSerializer.new(records, options).serialized_json end diff --git a/app/models/bitcoin_vout.rb b/app/models/bitcoin_vout.rb index 3b4a7a6da..9191268ba 100644 --- a/app/models/bitcoin_vout.rb +++ b/app/models/bitcoin_vout.rb @@ -13,8 +13,8 @@ class BitcoinVout < ApplicationRecord def commitment return unless op_return? - script_pubkey = Bitcoin::Script.parse_from_payload(data.htb) - script_pubkey.op_return_data.bth + script_pub_key = Bitcoin::Script.parse_from_payload(data.htb) + script_pub_key.op_return_data.bth end end From d8c18aafe75e6d13790acf1bb08aa7f07d956d65 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Tue, 7 Jan 2025 20:12:23 +0900 Subject: [PATCH 3/3] fix: clear cell_outputs cache when rollback Signed-off-by: Miles Zhang --- app/models/block.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/models/block.rb b/app/models/block.rb index 7f22d905a..bc0afc959 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -285,10 +285,15 @@ def invalid! uncle_blocks.delete_all # delete_address_txs_cache ckb_transaction_ids = ckb_transactions.pluck(:id) + tx_hashes = ckb_transactions.pluck(:tx_hash) CellOutput.where(ckb_transaction_id: ckb_transaction_ids).delete_all CellInput.where(ckb_transaction_id: ckb_transaction_ids).delete_all AccountBook.where(ckb_transaction_id: ckb_transaction_ids).delete_all CellDependency.where(ckb_transaction_id: ckb_transaction_ids).delete_all + cell_deps = CellDepsOutPoint.where(tx_hash: tx_hashes).select(:tx_hash, :cell_index) + cell_deps.each do |cell_dep| + Rails.cache.delete(["cell_output", cell_dep.tx_hash, cell_dep.cell_index]) + end HeaderDependency.where(ckb_transaction_id: ckb_transaction_ids).delete_all TokenTransfer.where(transaction_id: ckb_transaction_ids).delete_all Witness.where(ckb_transaction_id: ckb_transaction_ids).delete_all