Skip to content

Commit

Permalink
Merge pull request #2380 from nervosnetwork/testnet
Browse files Browse the repository at this point in the history
Deploy to mainnet
  • Loading branch information
zmcNotafraid authored Jan 2, 2025
2 parents 19e6bd9 + a96540f commit 0169d80
Show file tree
Hide file tree
Showing 26 changed files with 271 additions and 567 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v1/address_dao_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def show
address = Address.find_address!(params[:id])
raise Api::V1::Exceptions::AddressNotFoundError if address.is_a?(NullAddress)

ckb_dao_transactions = address.ckb_dao_transactions.select(:id, :tx_hash, :block_id, :block_number, :tags, :block_timestamp, :is_cellbase, :updated_at, :created_at).
recent.page(@page).per(@page_size).fast_page
ckb_dao_transactions = address.ckb_dao_transactions.select(:id, :tx_hash, :block_id, :block_number, :tags, :block_timestamp, :is_cellbase, :updated_at, :created_at, :tx_index).
recent.page(@page).per(@page_size)
json =
Rails.cache.realize(ckb_dao_transactions.cache_key, version: ckb_dao_transactions.cache_version) do
records_counter = RecordCounters::AddressDaoTransactions.new(address)
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/api/v1/address_udt_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def show
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).
ckb_udt_transactions = address.ckb_udt_transactions(udt.id).
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at, :tags).
recent.page(@page).per(@page_size).fast_page
json =
Rails.cache.realize(ckb_dao_transactions.cache_key, version: ckb_dao_transactions.cache_version) do
Rails.cache.realize(ckb_udt_transactions.cache_key, version: ckb_udt_transactions.cache_version) do
records_counter = RecordCounters::AddressUdtTransactions.new(address, udt.id)
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: ckb_dao_transactions, page: @page, page_size: @page_size, records_counter:).call
CkbTransactionsSerializer.new(ckb_dao_transactions, options.merge(params: { previews: true })).serialized_json
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: ckb_udt_transactions, page: @page, page_size: @page_size, records_counter:).call
CkbTransactionsSerializer.new(ckb_udt_transactions, options.merge(params: { previews: true })).serialized_json
end

render json:
Expand Down
124 changes: 67 additions & 57 deletions app/jobs/revert_block_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def perform(local_tip_block = nil)
uniq.concat(local_tip_block.cell_outputs.m_nft_token.pluck(:type_hash).uniq)
end
benchmark :recalculate_udt_transactions_count, local_tip_block
benchmark :recalculate_dao_contract_transactions_count, local_tip_block
benchmark :decrease_records_count, local_tip_block

ApplicationRecord.benchmark "invalid! block" do
Expand Down Expand Up @@ -51,7 +50,7 @@ def update_address_balance_and_ckb_transactions_count(local_tip_block)
address.live_cells_count = address.cell_outputs.live.count
# address.ckb_transactions_count = address.custom_ckb_transactions.count
address.ckb_transactions_count = AccountBook.where(address_id: address.id).count
address.dao_transactions_count = AddressDaoTransaction.where(address_id: address.id).count
address.dao_transactions_count = DaoEvent.processed.where(address_id: address.id).distinct(:ckb_transaction_id).count
address.cal_balance!
address.save!
end
Expand All @@ -60,14 +59,6 @@ def update_address_balance_and_ckb_transactions_count(local_tip_block)
AddressBlockSnapshot.where(block_id: local_tip_block.id).delete_all
end

def recalculate_dao_contract_transactions_count(local_tip_block)
dao_transactions_count = local_tip_block.ckb_transactions.where("tags @> array[?]::varchar[]", ["dao"]).count
if dao_transactions_count > 0
DaoContract.default_contract.decrement!(:ckb_transactions_count,
dao_transactions_count)
end
end

def recalculate_udt_transactions_count(local_tip_block)
udt_ids = local_tip_block.ckb_transactions.map(&:contained_udt_ids).flatten
udt_counts = udt_ids.each_with_object(Hash.new(0)) { |udt_id, counts| counts[udt_id] += 1 }
Expand All @@ -85,16 +76,6 @@ def recalculate_udt_transactions_count(local_tip_block)
Udt.upsert_all(udt_counts_value) if udt_counts_value.present?
end

def revert_dao_contract_related_operations(local_tip_block)
dao_events = DaoEvent.where(block: local_tip_block).processed
dao_contract = DaoContract.default_contract
revert_withdraw_from_dao(dao_contract, dao_events)
revert_issue_interest(dao_contract, dao_events)
revert_deposit_to_dao(dao_contract, dao_events)
revert_new_dao_depositor(dao_contract, dao_events)
revert_take_away_all_deposit(dao_contract, dao_events)
end

def recalculate_udt_accounts(udt_type_hashes, local_tip_block)
return if udt_type_hashes.blank?

Expand Down Expand Up @@ -125,61 +106,90 @@ def revert_mining_info(local_tip_block)

def revert_dao_contract_related_operations(local_tip_block)
dao_events = DaoEvent.where(block: local_tip_block).processed
dao_transactions_count = local_tip_block.ckb_transactions.where("tags @> array[?]::varchar[]", ["dao"]).count
dao_contract = DaoContract.default_contract
revert_withdraw_from_dao(dao_contract, dao_events)
revert_issue_interest(dao_contract, dao_events)
revert_deposit_to_dao(dao_contract, dao_events)
revert_new_dao_depositor(dao_contract, dao_events)
revert_take_away_all_deposit(dao_contract, dao_events)
end

def revert_take_away_all_deposit(dao_contract, dao_events)
take_away_all_deposit_dao_events = dao_events.where(event_type: "take_away_all_deposit")
take_away_all_deposit_dao_events.each do |event|
dao_contract.increment!(:depositors_count)
event.reverted!
end
withdraw_transactions_count, withdraw_total_deposit = revert_withdraw_from_dao(dao_events)
claimed_compensation = revert_issue_interest(dao_events)
deposit_transactions_count, deposit_total_deposit = revert_deposit_to_dao(dao_events)

dao_events.update_all(status: "reverted")
dao_contract.update!(deposit_transactions_count: dao_contract.deposit_transactions_count - deposit_transactions_count,
withdraw_transactions_count: dao_contract.withdraw_transactions_count - withdraw_transactions_count,
total_deposit: dao_contract.total_deposit + withdraw_total_deposit - deposit_total_deposit,
claimed_compensation: dao_contract.claimed_compensation - claimed_compensation,
ckb_transactions_count: dao_contract.ckb_transactions_count - dao_transactions_count,
depositors_count: DaoEvent.depositor.count)
end

def revert_issue_interest(dao_contract, dao_events)
issue_interest_dao_events = dao_events.where(event_type: "issue_interest")
def revert_issue_interest(dao_events)
issue_interest_dao_events = dao_events.issue_interest
claimed_compensation = 0
address_attrs = {}
issue_interest_dao_events.each do |event|
dao_contract.decrement!(:claimed_compensation, event.value)
claimed_compensation += event.value

address = event.address
address.decrement!(:interest, event.value)
event.reverted!
address_attrs[address.id] ||= {
id: address.id,
interest: address.interest,
}
address_attrs[address.id][:interest] -= event.value
end
upsert_data = address_attrs.values
Address.upsert_all(upsert_data, unique_by: :id) if upsert_data.present?
claimed_compensation
end

def revert_withdraw_from_dao(dao_contract, dao_events)
withdraw_from_dao_events = dao_events.where(event_type: "withdraw_from_dao")
def revert_withdraw_from_dao(dao_events)
withdraw_from_dao_events = dao_events.includes(:address).withdraw_from_dao

ids = withdraw_from_dao_events.pluck(:ckb_transaction_id)
DaoEvent.processed.where(withdrawn_transaction_id: ids).update_all(withdrawn_transaction_id: nil)

redundant_total_deposit = 0
address_attrs = {}
withdraw_from_dao_events.each do |event|
dao_contract.decrement!(:withdraw_transactions_count)
dao_contract.increment!(:total_deposit, event.value)
redundant_total_deposit += event.value

address = event.address
address.increment!(:dao_deposit, event.value)
event.reverted!
address_attrs[address.id] ||= {
id: address.id,
dao_deposit: address.dao_deposit,
is_depositor: address.is_depositor,
}
address_attrs[address.id][:dao_deposit] += event.value
address_attrs[address.id][:is_depositor] = true
end
end

def revert_new_dao_depositor(dao_contract, dao_events)
new_dao_depositor_events = dao_events.where(event_type: "new_dao_depositor")
new_dao_depositor_events.each do |event|
dao_contract.decrement!(:depositors_count)
dao_contract.decrement!(:total_depositors_count)
event.reverted!
end
upsert_data = address_attrs.values
Address.upsert_all(upsert_data, unique_by: :id) if upsert_data.present?

[withdraw_from_dao_events.size, redundant_total_deposit]
end

def revert_deposit_to_dao(dao_contract, dao_events)
deposit_to_dao_events = dao_events.where(event_type: "deposit_to_dao")
def revert_deposit_to_dao(dao_events)
deposit_to_dao_events = dao_events.deposit_to_dao
redundant_total_deposit = 0
address_attrs = {}

deposit_to_dao_events.each do |event|
redundant_total_deposit += event.value

address = event.address
address.decrement!(:dao_deposit, event.value)
dao_contract.decrement!(:total_deposit, event.value)
dao_contract.decrement!(:deposit_transactions_count)
event.reverted!
address_attrs[address.id] ||= {
id: address.id,
dao_deposit: address.dao_deposit,
}
address_attrs[address.id][:dao_deposit] -= event.value
end

upsert_data = address_attrs.values
address_ids = address_attrs.values.map { |hash| hash[:id] }
Address.upsert_all(upsert_data, unique_by: :id) if upsert_data.present?
Address.where(id: address_ids, dao_deposit: 0).update_all(is_depositor: false)

[deposit_to_dao_events.size, redundant_total_deposit]
end

def revert_block_rewards(local_tip_block)
Expand Down
3 changes: 1 addition & 2 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Address < ApplicationRecord
has_many :udt_accounts
has_many :dao_events
belongs_to :lock_script, optional: true
has_many :ckb_dao_transactions, -> { distinct }, through: :dao_events, source: :ckb_transaction

has_one :bitcoin_address_mapping, foreign_key: "ckb_address_id"
has_one :bitcoin_address, through: :bitcoin_address_mapping
Expand All @@ -33,8 +34,6 @@ def custom_ckb_transactions
ckb_transactions
end

has_and_belongs_to_many :ckb_dao_transactions, class_name: "CkbTransaction", join_table: "address_dao_transactions"

def ckb_udt_transactions(udt)
udt = Udt.find_by_id(udt) unless udt.is_a?(Udt)
udt&.ckb_transactions || []
Expand Down
16 changes: 0 additions & 16 deletions app/models/address_dao_transaction.rb

This file was deleted.

Loading

0 comments on commit 0169d80

Please sign in to comment.