Skip to content

Commit 3a0a563

Browse files
authored
Merge pull request #2610 from nervosnetwork/testnet
Deploy to mainnet
2 parents a8fa6e3 + c6da8f7 commit 3a0a563

File tree

9 files changed

+160
-113
lines changed

9 files changed

+160
-113
lines changed

app/jobs/revert_block_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def revert_withdraw_from_dao(dao_events)
144144
withdraw_from_dao_events = dao_events.includes(:address).withdraw_from_dao
145145

146146
ids = withdraw_from_dao_events.pluck(:ckb_transaction_id)
147-
DaoEvent.processed.where(withdrawn_transaction_id: ids).update_all(withdrawn_transaction_id: nil)
147+
DaoEvent.processed.where(consumed_transaction_id: ids).update_all(consumed_transaction_id: nil)
148148

149149
redundant_total_deposit = 0
150150
address_attrs = {}

app/models/ckb_sync/new_node_data_processor.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,12 @@ def process_withdraw_dao_events!(local_block, dao_contract)
223223
addrs_withdraw_info[address.id][:is_depositor] = false
224224
end
225225

226-
updated_deposit_dao_events_attributes << { block_id: previous_cell_output.block_id, ckb_transaction_id: previous_cell_output.ckb_transaction_id, cell_index: dao_input.index, event_type: "deposit_to_dao",
227-
withdrawn_transaction_id: dao_input.ckb_transaction_id }
226+
updated_deposit_dao_events_attributes << { block_id: previous_cell_output.block_id,
227+
ckb_transaction_id: previous_cell_output.ckb_transaction_id,
228+
cell_index: previous_cell_output.cell_index,
229+
event_type: "deposit_to_dao",
230+
consumed_transaction_id: dao_input.ckb_transaction_id,
231+
consumed_block_timestamp: local_block.timestamp }
228232
dao_events_attributes << {
229233
ckb_transaction_id: dao_input.ckb_transaction_id,
230234
cell_index: dao_input.index,
@@ -242,7 +246,7 @@ def process_withdraw_dao_events!(local_block, dao_contract)
242246
DaoEvent.upsert_all(dao_events_attributes, unique_by: %i[block_id ckb_transaction_id cell_index event_type]) if dao_events_attributes.present?
243247
if updated_deposit_dao_events_attributes.present?
244248
DaoEvent.upsert_all(updated_deposit_dao_events_attributes, unique_by: %i[block_id ckb_transaction_id cell_index event_type],
245-
update_only: :withdrawn_transaction_id)
249+
update_only: %i[consumed_transaction_id consumed_block_timestamp])
246250
end
247251
end
248252

@@ -263,6 +267,7 @@ def process_interest_dao_events!(local_block, dao_contract)
263267
local_block.cell_inputs.nervos_dao_withdrawing.select(:id, :ckb_transaction_id, :block_id, :index,
264268
:previous_cell_output_id).find_in_batches do |dao_inputs|
265269
dao_events_attributes = []
270+
updated_withdraw_dao_events_attributes = []
266271
dao_inputs.each do |dao_input|
267272
previous_cell_output = CellOutput.
268273
where(id: dao_input.previous_cell_output_id).
@@ -277,6 +282,13 @@ def process_interest_dao_events!(local_block, dao_contract)
277282
interest: address.interest.to_i + interest,
278283
}
279284
end
285+
updated_withdraw_dao_events_attributes << { block_id: previous_cell_output.block_id,
286+
ckb_transaction_id: previous_cell_output.ckb_transaction_id,
287+
cell_index: previous_cell_output.cell_index,
288+
event_type: "withdraw_from_dao",
289+
consumed_transaction_id: dao_input.ckb_transaction_id,
290+
consumed_block_timestamp: local_block.timestamp }
291+
280292
# addrs_withdraw_info[address.id][:dao_deposit] = 0 if addrs_withdraw_info[address.id][:dao_deposit] < 0
281293
dao_events_attributes << {
282294
ckb_transaction_id: dao_input.ckb_transaction_id,
@@ -292,6 +304,10 @@ def process_interest_dao_events!(local_block, dao_contract)
292304
claimed_compensation += interest
293305
end
294306
DaoEvent.upsert_all(dao_events_attributes, unique_by: %i[block_id ckb_transaction_id cell_index event_type]) if dao_events_attributes.present?
307+
if updated_withdraw_dao_events_attributes.present?
308+
DaoEvent.upsert_all(updated_withdraw_dao_events_attributes, unique_by: %i[block_id ckb_transaction_id cell_index event_type],
309+
update_only: %i[consumed_transaction_id consumed_block_timestamp])
310+
end
295311
end
296312
# update dao contract info
297313
dao_contract.update!(
@@ -326,6 +342,7 @@ def process_deposit_dao_events!(local_block, dao_contract)
326342
dao_events_attributes << {
327343
ckb_transaction_id: dao_output.ckb_transaction_id,
328344
cell_index: dao_output.cell_index,
345+
cell_output_id: dao_output.id,
329346
block_id: local_block.id,
330347
address_id: address.id,
331348
event_type: "deposit_to_dao",

app/models/daily_statistic.rb

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def liquidity
4646
end
4747

4848
define_logic :block_timestamp do
49-
blocks_in_current_period.recent.pick(:timestamp)
49+
current_tip_block.timestamp
5050
end
5151

5252
define_logic :total_dao_deposit do
@@ -151,8 +151,8 @@ def liquidity
151151
end
152152

153153
define_logic :avg_hash_rate do
154-
first_block_for_the_day = blocks_in_current_period.order("timestamp asc").first
155-
last_block_for_the_day = blocks_in_current_period.recent.first
154+
first_block_for_the_day = current_start_block
155+
last_block_for_the_day = current_tip_block
156156
total_block_time = last_block_for_the_day.timestamp - first_block_for_the_day.timestamp
157157

158158
BigDecimal(total_difficulties_for_the_day) / total_block_time
@@ -440,12 +440,29 @@ def ended_at
440440
@ended_at ||= CkbUtils.time_in_milliseconds(to_be_counted_date.end_of_day) - 1
441441
end
442442

443+
def current_start_block
444+
@current_start_block ||=
445+
begin
446+
end_at = started_at + (1 * 60 * 1000)
447+
start_block = Block.created_between(started_at, end_at).order("number asc").first
448+
unless start_block
449+
end_at = started_at + (10 * 60 * 1000)
450+
start_block = Block.created_between(started_at, end_at).order("number asc").first
451+
end
452+
start_block || Block.first
453+
end
454+
end
455+
443456
def current_tip_block
444457
@current_tip_block ||=
445-
if from_scratch
446-
Block.created_before(ended_at).recent.first
447-
else
448-
blocks_in_current_period.recent.first || Block.recent.first
458+
begin
459+
start_at = ended_at - (1 * 60 * 1000)
460+
tip_block = Block.created_between(start_at, ended_at).recent.first
461+
unless tip_block
462+
start_at = ended_at - (10 * 60 * 1000)
463+
tip_block = Block.created_between(start_at, ended_at).recent.first
464+
end
465+
tip_block || Block.recent.first
449466
end
450467
end
451468

app/models/dao_event.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ class DaoEvent < ApplicationRecord
1414
belongs_to :block
1515
belongs_to :ckb_transaction
1616
belongs_to :address
17+
belongs_to :consumed_transaction, class_name: "CkbTransaction", optional: true
18+
belongs_to :cell_output, optional: true
1719

18-
scope :depositor, -> { processed.where(event_type: "deposit_to_dao", withdrawn_transaction_id: nil) }
20+
scope :depositor, -> { processed.where(event_type: "deposit_to_dao", consumed_transaction_id: nil) }
1921
scope :created_after, ->(block_timestamp) { where("block_timestamp >= ?", block_timestamp) }
2022
scope :created_before, ->(block_timestamp) { where("block_timestamp <= ?", block_timestamp) }
2123
scope :created_between, ->(start_block_timestamp, end_block_timestamp) {
@@ -38,8 +40,10 @@ class DaoEvent < ApplicationRecord
3840
# created_at :datetime not null
3941
# updated_at :datetime not null
4042
# block_timestamp :decimal(30, )
41-
# withdrawn_transaction_id :bigint
43+
# consumed_transaction_id :bigint
4244
# cell_index :integer
45+
# consumed_block_timestamp :decimal(20, )
46+
# cell_output_id :bigint
4347
#
4448
# Indexes
4549
#
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddConsumedBlockTimestampToDaoEvents < ActiveRecord::Migration[7.0]
2+
def change
3+
rename_column :dao_events, :withdrawn_transaction_id, :consumed_transaction_id
4+
add_column :dao_events, :consumed_block_timestamp, :decimal, precision: 20, scale: 0
5+
add_column :dao_events, :cell_output_id, :bigint
6+
end
7+
end

db/structure.sql

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ begin
174174
insert into account_books (ckb_transaction_id, address_id)
175175
values (row.id, i) ON CONFLICT DO NOTHING;
176176
end loop;
177-
END LOOP;
177+
END LOOP;
178178
close c;
179179
end
180180
$$;
@@ -196,21 +196,21 @@ DECLARE
196196
if new.contained_address_ids is null then
197197
new.contained_address_ids := array[]::int[];
198198
end if;
199-
if old is null
199+
if old is null
200200
then
201201
to_add := new.contained_address_ids;
202202
to_remove := array[]::int[];
203203
else
204-
204+
205205
to_add := array_subtract(new.contained_address_ids, old.contained_address_ids);
206-
to_remove := array_subtract(old.contained_address_ids, new.contained_address_ids);
206+
to_remove := array_subtract(old.contained_address_ids, new.contained_address_ids);
207207
end if;
208208

209209
if to_add is not null then
210210
FOREACH i IN ARRAY to_add
211-
LOOP
211+
LOOP
212212
RAISE NOTICE 'ckb_tx_addr_id(%)', i;
213-
insert into account_books (ckb_transaction_id, address_id)
213+
insert into account_books (ckb_transaction_id, address_id)
214214
values (new.id, i);
215215
END LOOP;
216216
end if;
@@ -1647,8 +1647,10 @@ CREATE TABLE public.dao_events (
16471647
created_at timestamp(6) without time zone NOT NULL,
16481648
updated_at timestamp(6) without time zone NOT NULL,
16491649
block_timestamp numeric(30,0),
1650-
withdrawn_transaction_id bigint,
1651-
cell_index integer
1650+
consumed_transaction_id bigint,
1651+
cell_index integer,
1652+
consumed_block_timestamp numeric(20,0),
1653+
cell_output_id bigint
16521654
);
16531655

16541656

@@ -6528,6 +6530,5 @@ INSERT INTO "schema_migrations" (version) VALUES
65286530
('20250513034909'),
65296531
('20250617013030'),
65306532
('20250617051653'),
6531-
('20250625024348');
6532-
6533-
6533+
('20250625024348'),
6534+
('20250708075759');

0 commit comments

Comments
 (0)