Skip to content

Commit edabaea

Browse files
authored
Merge pull request #2850 from nervosnetwork/testnet
Deploy to mainnet
2 parents 27b11a4 + 6b56b40 commit edabaea

File tree

4 files changed

+102
-24
lines changed

4 files changed

+102
-24
lines changed

app/models/address.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,10 @@ def unmade_dao_interests
228228
#
229229
# Indexes
230230
#
231-
# index_addresses_on_address_hash (address_hash) USING hash
232-
# index_addresses_on_is_depositor (is_depositor) WHERE (is_depositor = true)
233-
# index_addresses_on_lock_hash (lock_hash) USING hash
234-
# unique_lock_hash (lock_hash) UNIQUE
231+
# index_addresses_on_address_hash (address_hash) USING hash
232+
# index_addresses_on_balance (balance)
233+
# index_addresses_on_block_timestamp (block_timestamp)
234+
# index_addresses_on_is_depositor (is_depositor) WHERE (is_depositor = true)
235+
# index_addresses_on_lock_hash (lock_hash) USING hash
236+
# unique_lock_hash (lock_hash) UNIQUE
235237
#

app/models/daily_statistic.rb

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,23 @@ def liquidity
7676
else
7777
claimed_compensation_today = 0
7878

79-
DaoEvent.processed.withdraw_from_dao.consumed_between(started_at,
80-
ended_at).find_each do |dao_event|
81-
nervos_dao_withdrawing_cell = CellOutput.find_by(ckb_transaction_id: dao_event.ckb_transaction_id, cell_index: dao_event.cell_index)
79+
events = DaoEvent.processed.withdraw_from_dao.consumed_between(started_at, ended_at).select(:ckb_transaction_id, :cell_index).all
80+
81+
results = if events.blank?
82+
[]
83+
else
84+
pairs = events.map{|e| [e.ckb_transaction_id, e.cell_index] }
85+
conditions = pairs.map do |tx_id, cell_idx|
86+
["ckb_transaction_id = #{tx_id} AND cell_index = #{cell_idx}"]
87+
end
88+
query = conditions.map do |cond|
89+
"(#{cond[0]})"
90+
end.join(" OR ")
91+
CellOutput.where(query)
92+
end
93+
94+
95+
results.each do |nervos_dao_withdrawing_cell|
8296
claimed_compensation_today += CkbUtils.dao_interest(nervos_dao_withdrawing_cell)
8397
end
8498

@@ -92,13 +106,16 @@ def liquidity
92106
sum_interest_bearing = 0
93107
sum_uninterest_bearing = 0
94108

95-
DaoEvent.processed.withdraw_from_dao.created_before(ended_at).unconsumed_at(ended_at).find_each do |nervos_dao_withdrawing_cell|
96-
nervos_dao_deposit_cell = CellInput.find_by(ckb_transaction_id: nervos_dao_withdrawing_cell.ckb_transaction_id, index: nervos_dao_withdrawing_cell.cell_index).previous_cell_output
97-
interest_bearing_deposits += nervos_dao_deposit_cell.capacity
98-
sum_interest_bearing += nervos_dao_deposit_cell.capacity * (nervos_dao_withdrawing_cell.block_timestamp - nervos_dao_deposit_cell.block_timestamp) / MILLISECONDS_IN_DAY
109+
phase1_cells.each do |nervos_dao_withdrawing_cell|
110+
interest_bearing_deposits += nervos_dao_withdrawing_cell.capacity
111+
112+
block_number = CKB::Utils.hex_to_bin(nervos_dao_withdrawing_cell.data).unpack("Q<").pack("Q>").unpack1("H*").hex
113+
nervos_dao_deposit_block_timestamp = Block.find_by_number(block_number).timestamp
114+
115+
sum_interest_bearing += nervos_dao_withdrawing_cell.capacity * (nervos_dao_withdrawing_cell.block_timestamp - nervos_dao_deposit_block_timestamp) / MILLISECONDS_IN_DAY
99116
end
100117

101-
DaoEvent.includes(:cell_output).processed.deposit_to_dao.created_before(ended_at).unconsumed_at(ended_at).find_each do |dao_event|
118+
unmaed_cells.each do |dao_event|
102119
nervos_dao_deposit_cell = dao_event.cell_output
103120
uninterest_bearing_deposits += nervos_dao_deposit_cell.capacity
104121

@@ -139,8 +156,7 @@ def liquidity
139156
if from_scratch
140157
CellOutput.generated_before(ended_at).unconsumed_at(ended_at).count
141158
else
142-
CellOutput.generated_between(started_at, ended_at).count +
143-
yesterday_daily_statistic.live_cells_count.to_i - dead_cells_count_today
159+
CellOutput.generated_between(started_at, ended_at).count + yesterday_daily_statistic.live_cells_count.to_i - dead_cells_count_today
144160
end
145161
end
146162

@@ -188,19 +204,27 @@ def liquidity
188204
end
189205
end
190206

191-
ranges.each_with_index.map do |range, index|
207+
counts = ranges.each_with_index.map do |range, index|
192208
begin_value = range[0] * (10**8)
193209
end_value = range[1] * (10**8)
194210
if index == max_n - 1
195211
addresses_count = Address.visible.where("balance > ?", begin_value).count
196-
total_addresses_count = Address.visible.where("balance > 0").count
197212
else
198213
addresses_count = Address.visible.where("balance > ? and balance <= ?", begin_value, end_value).count
199-
total_addresses_count = Address.visible.where("balance > 0 and balance <= ?", end_value).count
200214
end
201215

202-
[range[1], addresses_count, total_addresses_count]
216+
[range[1], addresses_count]
203217
end
218+
219+
total_addresses_count = 0
220+
221+
results = []
222+
counts.each do |c|
223+
total_addresses_count += c[1]
224+
results << [c[0], c[1], total_addresses_count]
225+
end
226+
227+
results
204228
end
205229

206230
define_logic :total_tx_fee do
@@ -480,22 +504,40 @@ def phase1_dao_interests
480504
@phase1_dao_interests ||=
481505
begin
482506
total = 0
483-
DaoEvent.processed.withdraw_from_dao.
484-
created_before(ended_at).unconsumed_at(ended_at).find_each do |dao_event|
485-
nervos_dao_withdrawing_cell = CellOutput.find_by(ckb_transaction_id: dao_event.ckb_transaction_id, cell_index: dao_event.cell_index)
507+
phase1_cells.each do |nervos_dao_withdrawing_cell|
486508
total += CkbUtils.dao_interest(nervos_dao_withdrawing_cell)
487509
end
488510
total
489511
end
490512
end
491513

514+
def phase1_cells
515+
return @phase1_cells if @phase1_cells
516+
events = DaoEvent.processed.withdraw_from_dao.created_before(ended_at).unconsumed_at(ended_at).select(:ckb_transaction_id, :cell_index).all
517+
if events.blank?
518+
return @phase1_cells = []
519+
end
520+
521+
pairs = events.map{|e| [e.ckb_transaction_id, e.cell_index] }
522+
conditions = pairs.map do |tx_id, cell_idx|
523+
["ckb_transaction_id = #{tx_id} AND cell_index = #{cell_idx}"]
524+
end
525+
query = conditions.map do |cond|
526+
"(#{cond[0]})"
527+
end.join(" OR ")
528+
@phase1_cells = CellOutput.where(query).to_a
529+
end
530+
531+
def unmaed_cells
532+
@unmaed_cells ||= DaoEvent.includes(:cell_output).processed.deposit_to_dao.created_before(ended_at).unconsumed_at(ended_at).to_a
533+
end
534+
492535
def unmade_dao_interests
493536
@unmade_dao_interests ||=
494537
begin
495538
tip_dao = current_tip_block.dao
496539
total = 0
497-
DaoEvent.includes(:cell_output).processed.deposit_to_dao.
498-
created_before(ended_at).unconsumed_at(ended_at).find_each do |dao_event|
540+
unmaed_cells.each do |dao_event|
499541
total += DaoCompensationCalculator.new(dao_event.cell_output, tip_dao).call
500542
end
501543
total
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class AddIndexToAddress < ActiveRecord::Migration[7.0]
2+
def change
3+
table_name = :addresses
4+
column_name = :balance
5+
index_name = "index_#{table_name}_on_#{column_name}".to_sym
6+
unless index_exists?(table_name, column_name)
7+
execute('SET statement_timeout = 0;')
8+
add_index table_name, column_name, name: index_name
9+
end
10+
11+
table_name = :addresses
12+
column_name = :block_timestamp
13+
index_name = "index_#{table_name}_on_#{column_name}".to_sym
14+
unless index_exists?(table_name, column_name)
15+
execute('SET statement_timeout = 0;')
16+
add_index table_name, column_name, name: index_name
17+
end
18+
end
19+
end

db/structure.sql

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4397,6 +4397,20 @@ CREATE INDEX index_address_udt_transactions_on_ckb_transaction_id ON public.addr
43974397
CREATE INDEX index_addresses_on_address_hash ON public.addresses USING hash (address_hash);
43984398

43994399

4400+
--
4401+
-- Name: index_addresses_on_balance; Type: INDEX; Schema: public; Owner: -
4402+
--
4403+
4404+
CREATE INDEX index_addresses_on_balance ON public.addresses USING btree (balance);
4405+
4406+
4407+
--
4408+
-- Name: index_addresses_on_block_timestamp; Type: INDEX; Schema: public; Owner: -
4409+
--
4410+
4411+
CREATE INDEX index_addresses_on_block_timestamp ON public.addresses USING btree (block_timestamp);
4412+
4413+
44004414
--
44014415
-- Name: index_addresses_on_is_depositor; Type: INDEX; Schema: public; Owner: -
44024416
--
@@ -5990,6 +6004,7 @@ INSERT INTO "schema_migrations" (version) VALUES
59906004
('20250826022054'),
59916005
('20250827065749'),
59926006
('20250930015526'),
5993-
('20251011011714');
6007+
('20251011011714'),
6008+
('20251013082609');
59946009

59956010

0 commit comments

Comments
 (0)