Skip to content

Commit f09020c

Browse files
authored
Merge pull request #2887 from nervosnetwork/testnet
Deploy to mainnet
2 parents 273495b + 79b31a2 commit f09020c

22 files changed

+79
-382
lines changed

app/jobs/revert_block_job.rb

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def perform(local_tip_block = nil)
2222
end
2323
benchmark :recalculate_udt_transactions_count, local_tip_block
2424
benchmark :decrease_records_count, local_tip_block
25-
benchmark :update_address_balance_and_ckb_transactions_count, local_tip_block
2625

2726
ApplicationRecord.benchmark "invalid! block" do
2827
local_tip_block.invalid!
@@ -39,79 +38,6 @@ def perform(local_tip_block = nil)
3938
end
4039
end
4140

42-
def update_address_balance_and_ckb_transactions_count(local_tip_block)
43-
select_fields = [
44-
"address_id",
45-
"COUNT(*) AS cells_count",
46-
"SUM(capacity) AS total_capacity",
47-
"SUM(CASE WHEN type_hash IS NOT NULL OR data_hash IS NOT NULL THEN capacity ELSE 0 END) AS balance_occupied",
48-
]
49-
output_addrs =
50-
local_tip_block.cell_outputs.live.select(select_fields.join(", ")).group(:address_id)
51-
input_addrs =
52-
CellOutput.where(consumed_block_timestamp: local_tip_block.timestamp).select(select_fields.join(", ")).group(:address_id)
53-
out_hash = output_addrs.each_with_object({}) do |row, h|
54-
h[row.address_id] = {
55-
cells_count: row.cells_count.to_i,
56-
total_capacity: row.total_capacity.to_i,
57-
balance_occupied: row.balance_occupied.to_i,
58-
}
59-
end
60-
61-
in_hash = input_addrs.each_with_object({}) do |row, h|
62-
h[row.address_id] = {
63-
cells_count: row.cells_count.to_i,
64-
total_capacity: row.total_capacity.to_i,
65-
balance_occupied: row.balance_occupied.to_i,
66-
}
67-
end
68-
69-
# Merge keys
70-
all_ids = in_hash.keys | out_hash.keys
71-
72-
# 计算差值
73-
address_balance_diff = all_ids.each_with_object({}) do |addr_id, h|
74-
input = in_hash[addr_id] || { cells_count: 0, total_capacity: 0, balance_occupied: 0 }
75-
output = out_hash[addr_id] || { cells_count: 0, total_capacity: 0, balance_occupied: 0 }
76-
77-
h[addr_id] = {
78-
cells_count: input[:cells_count] - output[:cells_count],
79-
total_capacity: input[:total_capacity] - output[:total_capacity],
80-
balance_occupied: input[:balance_occupied] - output[:balance_occupied],
81-
}
82-
end
83-
# Preload dao transaction counts for all addresses in one query
84-
dao_tx_counts = DaoEvent.processed.
85-
where(block_id: local_tip_block.id, address_id: all_ids).
86-
group(:address_id).
87-
distinct.
88-
count(:ckb_transaction_id)
89-
tx_count_diffs = AccountBook.tx_committed.
90-
where(block_number: local_tip_block.number, address_id: all_ids).
91-
group(:address_id).
92-
count
93-
94-
changes =
95-
address_balance_diff.map do |address_id, changes|
96-
tx_count_diff = tx_count_diffs[address_id] || 0
97-
dao_tx_count_diff = dao_tx_counts[address_id] || 0
98-
{ address_id: address_id }.merge(changes).merge({ ckb_transactions_count: tx_count_diff, dao_transactions_count: dao_tx_count_diff })
99-
end
100-
101-
changes.each do |change|
102-
addr = Address.find_by_id(change[:address_id])
103-
if addr
104-
addr.update!(
105-
live_cells_count: addr.live_cells_count + change[:cells_count],
106-
ckb_transactions_count: addr.ckb_transactions_count - change[:ckb_transactions_count],
107-
balance: addr.balance + change[:total_capacity],
108-
balance_occupied: addr.balance_occupied + change[:balance_occupied],
109-
dao_transactions_count: addr.dao_transactions_count - change[:dao_transactions_count]
110-
)
111-
end
112-
end
113-
end
114-
11541
def recalculate_udt_transactions_count(local_tip_block)
11642
udt_ids = local_tip_block.ckb_transactions.map(&:contained_udt_ids).flatten
11743
udt_counts = udt_ids.each_with_object(Hash.new(0)) { |udt_id, counts| counts[udt_id] += 1 }
@@ -159,7 +85,6 @@ def revert_mining_info(local_tip_block)
15985

16086
def revert_dao_contract_related_operations(local_tip_block)
16187
dao_events = DaoEvent.where(block: local_tip_block).processed
162-
dao_transactions_count = local_tip_block.ckb_transactions.where("tags @> array[?]::varchar[]", ["dao"]).count
16388
dao_contract = DaoContract.default_contract
16489

16590
withdraw_total_deposit = revert_withdraw_from_dao(dao_events)

app/models/address.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ def unmade_dao_interests
222222
# average_deposit_time :bigint
223223
# unclaimed_compensation :decimal(30, )
224224
# is_depositor :boolean default(FALSE)
225-
# dao_transactions_count :bigint default(0)
226225
# lock_script_id :bigint
227226
# balance_occupied :decimal(30, ) default(0)
228227
# last_updated_block_number :bigint

app/models/address_block_snapshot.rb

Lines changed: 0 additions & 22 deletions
This file was deleted.

app/models/ckb_sync/new_node_data_processor.rb

Lines changed: 8 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class NewNodeDataProcessor
88
include Redis::Objects
99

1010
value :reorg_started_at, global: true
11-
attr_accessor :local_tip_block, :pending_raw_block, :ckb_txs, :target_block, :target_block_number, :addrs_changes,
11+
attr_accessor :local_tip_block, :pending_raw_block, :ckb_txs, :target_block, :target_block_number,
1212
:outputs, :inputs, :outputs_data, :udt_address_ids, :contained_address_ids,
1313
:contained_udt_ids, :cell_datas, :enable_cota, :token_transfer_ckb_tx_ids, :addr_tx_changes, :redis_keys, :tx_previous_outputs
1414

@@ -75,11 +75,10 @@ def process_block(node_block, refresh_balance: true)
7575

7676
benchmark :process_ckb_txs, node_block, ckb_txs, contained_address_ids,
7777
contained_udt_ids, tags, udt_address_ids
78-
@addrs_changes = Hash.new { |hash, key| hash[key] = {} }
7978
@addr_tx_changes = Hash.new { |h, k| h[k] = Hash.new(0) }
8079

8180
input_capacities, output_capacities = benchmark :build_cells_and_locks!, local_block, node_block, ckb_txs, inputs, outputs,
82-
tags, udt_address_ids, contained_udt_ids, contained_address_ids, addrs_changes, token_transfer_ckb_tx_ids, cell_deps
81+
tags, udt_address_ids, contained_udt_ids, contained_address_ids, token_transfer_ckb_tx_ids, cell_deps
8382

8483
# update explorer data
8584
benchmark :update_ckb_txs_rel_and_fee, ckb_txs, tags, input_capacities, output_capacities, udt_address_ids,
@@ -91,7 +90,6 @@ def process_block(node_block, refresh_balance: true)
9190
benchmark :update_or_create_udt_accounts!, local_block
9291
# maybe can be changed to asynchronous update
9392
benchmark :process_dao_events!, local_block
94-
benchmark :update_addresses_info, addrs_changes, local_block, refresh_balance
9593
end
9694

9795
async_update_udt_infos(local_block)
@@ -522,66 +520,6 @@ def update_mining_info(local_block)
522520
CkbUtils.update_current_block_mining_info(local_block)
523521
end
524522

525-
def update_addresses_info(addrs_change, local_block, refresh_balance)
526-
return unless refresh_balance
527-
528-
addrs_change.to_a.each_slice(100) do |batch|
529-
530-
updates = batch.map do |key, value|
531-
[key, { last_updated_block_number: local_block.number,
532-
balance: value[:balance_diff],
533-
balance_occupied: value[:balance_occupied_diff].presence || 0,
534-
ckb_transactions_count: value[:ckb_txs].present? ? value[:ckb_txs].size : 0,
535-
live_cells_count: value[:cells_diff],
536-
dao_transactions_count: value[:dao_txs].present? ? value[:dao_txs].size : 0
537-
}]
538-
end.to_h
539-
540-
case_clauses = { last_updated_block_number: [], balance: [], balance_occupied: [], ckb_transactions_count: [], live_cells_count: [], dao_transactions_count: [] }
541-
ids = []
542-
543-
updates.each do |id, attrs|
544-
ids << id
545-
attrs.each do |column, value|
546-
if column == :last_updated_block_number
547-
case_clauses[column] << "WHEN #{id} THEN '#{ActiveRecord::Base.connection.quote(value)}'"
548-
else
549-
case_clauses[column] << "WHEN #{id} THEN #{column} + #{value}"
550-
end
551-
end
552-
end
553-
554-
set_clauses = case_clauses.map do |column, clauses|
555-
if clauses.any?
556-
" #{column} = CASE id\n #{clauses.join("\n ")}\n ELSE #{column}\n END"
557-
else
558-
nil
559-
end
560-
end.compact.join(",\n")
561-
562-
id_list = ids.join(', ')
563-
564-
sql = <<-SQL
565-
UPDATE addresses
566-
SET
567-
#{set_clauses}
568-
WHERE id IN (#{id_list})
569-
SQL
570-
571-
# puts sql
572-
573-
ActiveRecord::Base.connection.execute(sql)
574-
575-
updated_records = Address.where(id: ids).select(:lock_hash)
576-
updated_records.each do |record|
577-
$redis.pipelined do
578-
Rails.cache.delete_multi(%W(#{record.class.name}/#{record.lock_hash}))
579-
end
580-
end
581-
582-
end
583-
end
584-
585523
def update_block_info!(local_block)
586524
local_block.update!(
587525
total_transaction_fee: local_block.ckb_transactions.sum(:transaction_fee),
@@ -792,7 +730,7 @@ def update_ckb_txs_rel_and_fee(
792730

793731
def build_cells_and_locks!(
794732
local_block, node_block, ckb_txs, inputs, outputs, tags, udt_address_ids,
795-
contained_udt_ids, contained_addr_ids, addrs_changes, token_transfer_ckb_tx_ids, cell_deps
733+
contained_udt_ids, contained_addr_ids, token_transfer_ckb_tx_ids, cell_deps
796734
)
797735
cell_outputs_attributes = []
798736
cell_inputs_attributes = []
@@ -815,7 +753,7 @@ def build_cells_and_locks!(
815753
build_addresses!(outputs, local_block)
816754

817755
build_cell_outputs!(node_block, outputs, ckb_txs, local_block, cell_outputs_attributes, output_capacities, tags,
818-
udt_address_ids, contained_udt_ids, contained_addr_ids, addrs_changes, token_transfer_ckb_tx_ids)
756+
udt_address_ids, contained_udt_ids, contained_addr_ids, token_transfer_ckb_tx_ids)
819757
if cell_outputs_attributes.present?
820758
tx_hashes = cell_outputs_attributes.pluck(:tx_hash)
821759
binary_hashes = CkbUtils.hexes_to_bins_sql(tx_hashes)
@@ -872,7 +810,7 @@ def build_cells_and_locks!(
872810
prev_outputs = nil
873811
build_cell_inputs(inputs, ckb_txs, local_block.id, cell_inputs_attributes, prev_cell_outputs_attributes,
874812
input_capacities, tags, udt_address_ids, contained_udt_ids, contained_addr_ids,
875-
prev_outputs, addrs_changes, token_transfer_ckb_tx_ids)
813+
prev_outputs, token_transfer_ckb_tx_ids)
876814

877815
CellInput.import cell_inputs_attributes, validate: false, batch_size: 500
878816
if prev_cell_outputs_attributes.present?
@@ -980,7 +918,7 @@ def script_attributes(script, script_hash)
980918

981919
def build_cell_inputs(
982920
inputs, ckb_txs, local_block_id, cell_inputs_attributes, prev_cell_outputs_attributes,
983-
input_capacities, tags, udt_address_ids, contained_udt_ids, contained_addr_ids, prev_outputs, addrs_changes, token_transfer_ckb_tx_ids
921+
input_capacities, tags, udt_address_ids, contained_udt_ids, contained_addr_ids, prev_outputs, token_transfer_ckb_tx_ids
984922
)
985923
conditions = []
986924

@@ -1020,7 +958,6 @@ def build_cell_inputs(
1020958
# attributes[2] is previous_cell_output capacity
1021959
# attributes[3] is previous_cell_output type_hash
1022960
# attributes[4] is previous_cell address_id
1023-
# attributes[5] is previous_cell data
1024961
attributes = cell_input_attributes(item, ckb_txs[tx_index]["id"],
1025962
local_block_id, prev_outputs, index)
1026963
cell_inputs_attributes << attributes[:cell_input]
@@ -1029,28 +966,13 @@ def build_cell_inputs(
1029966
address_id = attributes[:address_id]
1030967
capacity = attributes[:capacity]
1031968
type_hash = attributes[:type_hash]
1032-
data = attributes[:data]
1033-
change_rec = addrs_changes[address_id]
1034-
1035969
addr_tx_changes[tx_index][address_id] -= capacity
1036-
change_rec[:balance_diff] ||= 0
1037-
change_rec[:balance_diff] -= capacity
1038-
change_rec[:balance_occupied_diff] ||= 0
1039-
change_rec[:balance_occupied_diff] -= capacity if occupied?(
1040-
type_hash, data
1041-
)
1042-
change_rec[:cells_diff] ||= 0
1043-
change_rec[:cells_diff] -= 1
1044-
change_rec[:ckb_txs] ||= Set.new
1045-
change_rec[:ckb_txs] << ckb_txs[tx_index]["tx_hash"]
1046970

1047971
prev_cell_outputs_attributes << previous_output
1048972
contained_addr_ids[tx_index] << address_id
1049973
cell_type = previous_output[:cell_type].to_s
1050974
if cell_type.in?(%w(nervos_dao_withdrawing))
1051975
tags[tx_index] << "dao"
1052-
change_rec[:dao_txs] ||= Set.new
1053-
change_rec[:dao_txs] << ckb_txs[tx_index]["tx_hash"]
1054976
elsif cell_type.in?(%w(m_nft_token nrc_721_token spore_cell did_cell))
1055977
token_transfer_ckb_tx_ids << ckb_txs[tx_index]["id"]
1056978
end
@@ -1098,7 +1020,7 @@ def build_cell_inputs(
10981020

10991021
def build_cell_outputs!(
11001022
node_block, outputs, ckb_txs, local_block, cell_outputs_attributes, output_capacities,
1101-
tags, udt_address_ids, contained_udt_ids, contained_addr_ids, addrs_changes, token_transfer_ckb_tx_ids
1023+
tags, udt_address_ids, contained_udt_ids, contained_addr_ids, token_transfer_ckb_tx_ids
11021024
)
11031025
outputs.each do |tx_index, items|
11041026
cell_index = 0
@@ -1121,24 +1043,9 @@ def build_cell_outputs!(
11211043
@redis_keys << key
11221044
end
11231045
end
1124-
cell_data = node_block.transactions[tx_index].outputs_data[cell_index]
1125-
change_rec = addrs_changes[address_id]
1046+
11261047
addr_tx_changes[tx_index][address_id] += item.capacity
1127-
1128-
change_rec[:balance_diff] ||= 0
1129-
change_rec[:balance_diff] += item.capacity
1130-
1131-
change_rec[:balance_occupied_diff] ||= 0
11321048
type_script_hash = item.type&.compute_hash
1133-
change_rec[:balance_occupied_diff] += item.capacity if occupied?(
1134-
type_script_hash, cell_data
1135-
)
1136-
1137-
change_rec[:cells_diff] ||= 0
1138-
change_rec[:cells_diff] += 1
1139-
1140-
change_rec[:ckb_txs] ||= Set.new
1141-
change_rec[:ckb_txs] << ckb_txs[tx_index]["tx_hash"]
11421049

11431050
contained_addr_ids[tx_index] << address_id
11441051
attr = cell_output_attributes(item, address_id, ckb_txs[tx_index], local_block, cell_index,
@@ -1147,8 +1054,6 @@ def build_cell_outputs!(
11471054

11481055
if attr[:cell_type].in?(%w(nervos_dao_deposit nervos_dao_withdrawing))
11491056
tags[tx_index] << "dao"
1150-
change_rec[:dao_txs] ||= Set.new
1151-
change_rec[:dao_txs] << ckb_txs[tx_index]["tx_hash"]
11521057
end
11531058

11541059
if attr[:cell_type] == "udt"
@@ -1191,10 +1096,6 @@ def build_cell_outputs!(
11911096
end
11921097
end
11931098

1194-
def occupied?(type_hash, cell_data)
1195-
(cell_data.present? && cell_data != "0x") || type_hash.present?
1196-
end
1197-
11981099
def cell_output_attributes(output, address_id, ckb_transaction, local_block, cell_index, output_data)
11991100
lock_script_hash = output.lock.compute_hash
12001101
key = "lock_script_hash_#{lock_script_hash}"
@@ -1306,7 +1207,6 @@ def cell_input_attributes(input, ckb_transaction_id, local_block_id,
13061207
capacity: previous_output.capacity,
13071208
type_hash: previous_output.type_hash,
13081209
address_id: previous_output.address_id,
1309-
# data: previous_output.data
13101210
}
13111211
end
13121212
end

app/workers/bitcoin_transaction_detect_worker.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def perform(number)
1515
@btc_time_cell_ids = [] # btc time cells
1616

1717
ApplicationRecord.transaction do
18-
block.ckb_transactions.each do |transaction|
18+
block.ckb_transactions.includes(input_cells: [:lock_script], cell_outputs: [:lock_script]).each do |transaction|
1919
inputs = transaction.input_cells
2020
outputs = transaction.cell_outputs
2121
(inputs + outputs).each { collect_rgb_ids(_1) }
@@ -32,7 +32,7 @@ def perform(number)
3232
ImportBtcTimeCellsJob.perform_now(_1)
3333
end
3434
# update bitcoin annotation
35-
build_bitcoin_annotations!
35+
build_bitcoin_annotations! unless @txids.empty?
3636
end
3737
end
3838

@@ -91,7 +91,7 @@ def cache_raw_transactions!
9191
def build_bitcoin_annotations!
9292
annotations = []
9393

94-
@block.ckb_transactions.each do |transaction|
94+
@block.ckb_transactions.where(id: @txids).each do |transaction|
9595
leap_direction, transfer_step = annotation_workflow_attributes(transaction)
9696
tags = annotation_tags(transaction)
9797

0 commit comments

Comments
 (0)