From 7d367dcfb67bf0dd6e2a45fe9343faaefda2ca2c Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Thu, 28 Nov 2024 18:32:25 +0900 Subject: [PATCH] fix: block rollback test Signed-off-by: Miles Zhang --- app/models/block.rb | 11 +++++++++-- app/models/ckb_transaction.rb | 16 ++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/models/block.rb b/app/models/block.rb index 2cfddbebc..7f22d905a 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -284,8 +284,15 @@ def cache_keys def invalid! uncle_blocks.delete_all # delete_address_txs_cache - ckb_transactions.delete_all - CellOutput.where(block_id: id).delete_all + ckb_transaction_ids = ckb_transactions.pluck(:id) + 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 + 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 + ckb_transactions.destroy_all ForkedBlock.create(attributes) destroy end diff --git a/app/models/ckb_transaction.rb b/app/models/ckb_transaction.rb index 5434b8bf9..aecd211ea 100644 --- a/app/models/ckb_transaction.rb +++ b/app/models/ckb_transaction.rb @@ -16,11 +16,11 @@ class CkbTransaction < ApplicationRecord has_many :included_blocks, class_name: "Block", through: :block_transactions, inverse_of: :contained_transactions - has_many :account_books, dependent: :delete_all + has_many :account_books has_many :addresses, through: :account_books - has_many :cell_inputs, dependent: :delete_all + has_many :cell_inputs has_many :input_cells, through: :cell_inputs, source: :previous_cell_output - has_many :cell_outputs, dependent: :delete_all + has_many :cell_outputs accepts_nested_attributes_for :cell_outputs has_many :inputs, class_name: "CellOutput", inverse_of: "consumed_by", foreign_key: "consumed_by_id" has_many :outputs, class_name: "CellOutput" @@ -28,11 +28,11 @@ class CkbTransaction < ApplicationRecord # has_many :script_transactions, dependent: :delete_all # has_many :scripts, through: :script_transactions - has_many :referring_cells, dependent: :delete_all - has_many :token_transfers, foreign_key: :transaction_id, dependent: :delete_all, inverse_of: :ckb_transaction - has_many :cell_dependencies, dependent: :delete_all - has_many :header_dependencies, dependent: :delete_all - has_many :witnesses, dependent: :delete_all + has_many :referring_cells + has_many :token_transfers, foreign_key: :transaction_id, inverse_of: :ckb_transaction + has_many :cell_dependencies + has_many :header_dependencies + has_many :witnesses has_one :reject_reason