Skip to content

Commit b0df727

Browse files
authored
Merge pull request #2375 from nervosnetwork/develop
Deploy to testnet
2 parents fbaebd5 + 3feb70f commit b0df727

File tree

7 files changed

+94
-25
lines changed

7 files changed

+94
-25
lines changed

app/models/ckb_sync/new_node_data_processor.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def process_withdraw_dao_events!(local_block, _new_dao_depositors,
229229
take_away_all_deposit_count = 0
230230
# When DAO Deposit Cell appears in cell inputs, the transaction is DAO withdrawal
231231
local_block.cell_inputs.nervos_dao_deposit.select(:id, :ckb_transaction_id,
232-
:previous_cell_output_id).find_in_batches do |dao_inputs|
232+
:previous_cell_output_id, :index).find_in_batches do |dao_inputs|
233233
dao_events_attributes = []
234234
dao_inputs.each do |dao_input|
235235
previous_cell_output =
@@ -253,6 +253,7 @@ def process_withdraw_dao_events!(local_block, _new_dao_depositors,
253253
end
254254
dao_events_attributes << {
255255
ckb_transaction_id: dao_input.ckb_transaction_id,
256+
cell_index: dao_input.index,
256257
block_id: local_block.id,
257258
block_timestamp: local_block.timestamp,
258259
address_id: previous_cell_output.address_id,
@@ -268,6 +269,7 @@ def process_withdraw_dao_events!(local_block, _new_dao_depositors,
268269
addrs_withdraw_info[address.id][:is_depositor] = false
269270
dao_events_attributes << {
270271
ckb_transaction_id: dao_input.ckb_transaction_id,
272+
cell_index: nil,
271273
block_id: local_block.id,
272274
block_timestamp: local_block.timestamp,
273275
address_id: previous_cell_output.address_id,
@@ -303,7 +305,7 @@ def process_interest_dao_events!(local_block, dao_contract)
303305
addrs_withdraw_info = {}
304306
claimed_compensation = 0
305307
local_block.cell_inputs.nervos_dao_withdrawing.select(:id, :ckb_transaction_id, :block_id,
306-
:previous_cell_output_id).find_in_batches do |dao_inputs|
308+
:previous_cell_output_id, :index).find_in_batches do |dao_inputs|
307309
dao_events_attributes = []
308310
dao_inputs.each do |dao_input|
309311
previous_cell_output = CellOutput.
@@ -324,6 +326,7 @@ def process_interest_dao_events!(local_block, dao_contract)
324326
# addrs_withdraw_info[address.id][:dao_deposit] = 0 if addrs_withdraw_info[address.id][:dao_deposit] < 0
325327
dao_events_attributes << {
326328
ckb_transaction_id: dao_input.ckb_transaction_id,
329+
cell_index: dao_input.index,
327330
block_id: local_block.id,
328331
block_timestamp: local_block.timestamp,
329332
address_id: previous_cell_output.address_id,
@@ -351,7 +354,7 @@ def process_deposit_dao_events!(local_block, new_dao_depositors, dao_contract)
351354
addresses_deposit_info = {}
352355
# build deposit dao events
353356
local_block.cell_outputs.nervos_dao_deposit.select(:id, :address_id, :capacity,
354-
:ckb_transaction_id).find_in_batches do |dao_outputs|
357+
:ckb_transaction_id, :cell_index).find_in_batches do |dao_outputs|
355358
deposit_dao_events_attributes = []
356359
dao_outputs.each do |dao_output|
357360
address = dao_output.address
@@ -374,6 +377,7 @@ def process_deposit_dao_events!(local_block, new_dao_depositors, dao_contract)
374377
deposit_transaction_ids << dao_output.ckb_transaction_id
375378
deposit_dao_events_attributes << {
376379
ckb_transaction_id: dao_output.ckb_transaction_id,
380+
cell_index: dao_output.cell_index,
377381
block_id: local_block.id,
378382
address_id: address.id,
379383
event_type: "deposit_to_dao",

app/models/dao_event.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,19 @@ def get_froms
5454
#
5555
# Table name: dao_events
5656
#
57-
# id :bigint not null, primary key
58-
# block_id :bigint
59-
# ckb_transaction_id :bigint
60-
# address_id :bigint
61-
# contract_id :bigint
62-
# event_type :integer
63-
# value :decimal(30, ) default(0)
64-
# status :integer default("pending")
65-
# created_at :datetime not null
66-
# updated_at :datetime not null
67-
# block_timestamp :decimal(30, )
57+
# id :bigint not null, primary key
58+
# block_id :bigint
59+
# ckb_transaction_id :bigint
60+
# address_id :bigint
61+
# contract_id :bigint
62+
# event_type :integer
63+
# value :decimal(30, ) default(0)
64+
# status :integer default("pending")
65+
# created_at :datetime not null
66+
# updated_at :datetime not null
67+
# block_timestamp :decimal(30, )
68+
# withdrawn_transaction_id :bigint
69+
# cell_index :integer
6870
#
6971
# Indexes
7072
#
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddCellIndexToDaoEvent < ActiveRecord::Migration[7.0]
2+
def change
3+
add_column :dao_events, :withdrawn_transaction_id, :bigint
4+
add_column :dao_events, :cell_index, :integer
5+
end
6+
end

db/structure.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,9 @@ CREATE TABLE public.dao_events (
16511651
status smallint DEFAULT 0,
16521652
created_at timestamp(6) without time zone NOT NULL,
16531653
updated_at timestamp(6) without time zone NOT NULL,
1654-
block_timestamp numeric(30,0)
1654+
block_timestamp numeric(30,0),
1655+
withdrawn_transaction_id bigint,
1656+
cell_index integer
16551657
);
16561658

16571659

@@ -6329,6 +6331,7 @@ INSERT INTO "schema_migrations" (version) VALUES
63296331
('20241212022531'),
63306332
('20241213053309'),
63316333
('20241218085721'),
6334+
('20241223060331'),
63326335
('20241225045757');
63336336

63346337

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace :migration do
2+
desc "Usage: RAILS_ENV=production bundle exec rake migration:fill_cell_index_to_dao_event"
3+
task fill_cell_index_to_dao_event: :environment do
4+
CellOutput.dead.where(cell_type: ["nervos_dao_deposit", "nervos_dao_withdrawing"]).find_each do |output|
5+
input = output.cell_inputs.first
6+
if input.cell_type != output.cell_type
7+
puts "input id: #{input.id}"
8+
input.update(cell_type: output.cell_type)
9+
end
10+
end
11+
CkbTransaction.where("tags @> array[?]::varchar[]", ["dao"]).find_each do |tx|
12+
tx.outputs.where(cell_type: :nervos_dao_deposit).each do |output|
13+
event = DaoEvent.where(ckb_transaction_id: tx.id, address_id: output.address_id, value: output.capacity, event_type: "deposit_to_dao", cell_index: nil).limit(1).first
14+
if event
15+
event.update(cell_index: output.cell_index)
16+
else
17+
DaoEvent.create(ckb_transaction_id: tx.id, address_id: output.address_id, value: output.capacity, event_type: "deposit_to_dao", cell_index: output.cell_index, block_id: tx.block_id,
18+
block_timestamp: tx.block_timestamp, contract_id: 1)
19+
end
20+
end
21+
tx.cell_inputs.where(cell_type: :nervos_dao_withdrawing).each do |input|
22+
previous_cell_output = CellOutput.
23+
where(id: input.previous_cell_output_id).
24+
select(:address_id, :block_id, :ckb_transaction_id, :dao, :cell_index, :capacity, :occupied_capacity).
25+
take!
26+
interest = CkbUtils.dao_interest(previous_cell_output)
27+
event = DaoEvent.where(ckb_transaction_id: tx.id, address_id: previous_cell_output.address_id, value: interest, event_type: "issue_interest", cell_index: nil).limit(1).first
28+
if event
29+
event.update(cell_index: input.index)
30+
else
31+
DaoEvent.create(ckb_transaction_id: tx.id, address_id: previous_cell_output.address_id, value: interest, event_type: "issue_interest", cell_index: input.index, block_id: tx.block_id,
32+
block_timestamp: tx.block_timestamp, contract_id: 1)
33+
end
34+
end
35+
tx.cell_inputs.where(cell_type: :nervos_dao_deposit).each do |input|
36+
previous_cell_output =
37+
CellOutput.
38+
where(id: input.previous_cell_output_id).
39+
select(:address_id, :ckb_transaction_id, :cell_index, :capacity).
40+
take!
41+
event = DaoEvent.where(ckb_transaction_id: tx.id, address_id: previous_cell_output.address_id, value: previous_cell_output.capacity, event_type: "withdraw_from_dao",
42+
cell_index: nil).limit(1).first
43+
DaoEvent.find_by(ckb_transaction_id: previous_cell_output.ckb_transaction_id, address_id: previous_cell_output.address_id, value: previous_cell_output.capacity, event_type: "deposit_to_dao",
44+
cell_index: previous_cell_output.cell_index).update(withdrawn_transaction_id: tx.id)
45+
if event
46+
event.update(cell_index: input.index)
47+
else
48+
DaoEvent.create(ckb_transaction_id: tx.id, address_id: previous_cell_output.address_id, value: previous_cell_output.capacity, event_type: "withdraw_from_dao", cell_index: input.index, block_id: tx.block_id,
49+
block_timestamp: tx.block_timestamp, contract_id: 1)
50+
end
51+
end
52+
end
53+
end
54+
end

test/models/ckb_sync/dao_events_test.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ class DaoEventsTest < ActiveSupport::TestCase
7171

7272
deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "withdraw_from_dao")
7373
assert_equal ["processed"], deposit_to_dao_events.pluck(:status).uniq
74-
assert_equal %w(block_id ckb_transaction_id address_id contract_id event_type value status block_timestamp), deposit_to_dao_events.first.attribute_names.reject { |attribute|
75-
attribute.in?(%w(created_at updated_at id))
76-
}
74+
assert_equal %w(block_id ckb_transaction_id address_id contract_id event_type value status block_timestamp withdrawn_transaction_id cell_index), deposit_to_dao_events.first.attribute_names.reject { |attribute|
75+
attribute.in?(%w(created_at updated_at id))
76+
}
7777
end
7878
end
7979

@@ -420,9 +420,9 @@ class DaoEventsTest < ActiveSupport::TestCase
420420

421421
deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "withdraw_from_dao")
422422
assert_equal ["processed"], deposit_to_dao_events.pluck(:status).uniq
423-
assert_equal %w(block_id ckb_transaction_id address_id contract_id event_type value status block_timestamp), deposit_to_dao_events.first.attribute_names.reject { |attribute|
424-
attribute.in?(%w(created_at updated_at id))
425-
}
423+
assert_equal %w(block_id ckb_transaction_id address_id contract_id event_type value status block_timestamp withdrawn_transaction_id cell_index), deposit_to_dao_events.first.attribute_names.reject { |attribute|
424+
attribute.in?(%w(created_at updated_at id))
425+
}
426426
end
427427
end
428428

@@ -732,9 +732,9 @@ class DaoEventsTest < ActiveSupport::TestCase
732732

733733
deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "new_dao_depositor")
734734
assert_equal ["processed"], deposit_to_dao_events.pluck(:status).uniq
735-
assert_equal %w(block_id ckb_transaction_id address_id contract_id event_type value status block_timestamp), deposit_to_dao_events.first.attribute_names.reject { |attribute|
736-
attribute.in?(%w(created_at updated_at id))
737-
}
735+
assert_equal %w(block_id ckb_transaction_id address_id contract_id event_type value status block_timestamp withdrawn_transaction_id cell_index), deposit_to_dao_events.first.attribute_names.reject { |attribute|
736+
attribute.in?(%w(created_at updated_at id))
737+
}
738738
end
739739
end
740740

test/models/dao_event_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class DaoEventTest < ActiveSupport::TestCase
77

88
test "should have correct columns" do
99
dao_event = create(:dao_event)
10-
expected_attributes = %w(address_id block_id block_timestamp contract_id created_at event_type id status ckb_transaction_id updated_at value).sort
10+
expected_attributes = %w(address_id block_id block_timestamp contract_id created_at event_type id status ckb_transaction_id updated_at value cell_index withdrawn_transaction_id).sort
1111
assert_equal expected_attributes, dao_event.attributes.keys.sort
1212
end
1313
end

0 commit comments

Comments
 (0)