From 3feb70fed5012c15c453f46ab2a21edd1af45bf3 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 30 Dec 2024 14:19:37 +0900 Subject: [PATCH] feat: add cell_index and withdrawan_transaction_id to dao_event (#2374) * feat: add cell_index and withdrawan_transaction_id to dao_event Signed-off-by: Miles Zhang * test: fix test Signed-off-by: Miles Zhang --------- Signed-off-by: Miles Zhang --- .../ckb_sync/new_node_data_processor.rb | 10 ++-- app/models/dao_event.rb | 24 +++++---- ...41223060331_add_cell_index_to_dao_event.rb | 6 +++ db/structure.sql | 5 +- .../fill_cell_index_to_dao_event.rake | 54 +++++++++++++++++++ test/models/ckb_sync/dao_events_test.rb | 18 +++---- test/models/dao_event_test.rb | 2 +- 7 files changed, 94 insertions(+), 25 deletions(-) create mode 100644 db/migrate/20241223060331_add_cell_index_to_dao_event.rb create mode 100644 lib/tasks/migration/fill_cell_index_to_dao_event.rake diff --git a/app/models/ckb_sync/new_node_data_processor.rb b/app/models/ckb_sync/new_node_data_processor.rb index c68679762..7e27c55de 100644 --- a/app/models/ckb_sync/new_node_data_processor.rb +++ b/app/models/ckb_sync/new_node_data_processor.rb @@ -229,7 +229,7 @@ def process_withdraw_dao_events!(local_block, _new_dao_depositors, take_away_all_deposit_count = 0 # When DAO Deposit Cell appears in cell inputs, the transaction is DAO withdrawal local_block.cell_inputs.nervos_dao_deposit.select(:id, :ckb_transaction_id, - :previous_cell_output_id).find_in_batches do |dao_inputs| + :previous_cell_output_id, :index).find_in_batches do |dao_inputs| dao_events_attributes = [] dao_inputs.each do |dao_input| previous_cell_output = @@ -253,6 +253,7 @@ def process_withdraw_dao_events!(local_block, _new_dao_depositors, end dao_events_attributes << { ckb_transaction_id: dao_input.ckb_transaction_id, + cell_index: dao_input.index, block_id: local_block.id, block_timestamp: local_block.timestamp, address_id: previous_cell_output.address_id, @@ -268,6 +269,7 @@ def process_withdraw_dao_events!(local_block, _new_dao_depositors, addrs_withdraw_info[address.id][:is_depositor] = false dao_events_attributes << { ckb_transaction_id: dao_input.ckb_transaction_id, + cell_index: nil, block_id: local_block.id, block_timestamp: local_block.timestamp, address_id: previous_cell_output.address_id, @@ -303,7 +305,7 @@ def process_interest_dao_events!(local_block, dao_contract) addrs_withdraw_info = {} claimed_compensation = 0 local_block.cell_inputs.nervos_dao_withdrawing.select(:id, :ckb_transaction_id, :block_id, - :previous_cell_output_id).find_in_batches do |dao_inputs| + :previous_cell_output_id, :index).find_in_batches do |dao_inputs| dao_events_attributes = [] dao_inputs.each do |dao_input| previous_cell_output = CellOutput. @@ -324,6 +326,7 @@ def process_interest_dao_events!(local_block, dao_contract) # addrs_withdraw_info[address.id][:dao_deposit] = 0 if addrs_withdraw_info[address.id][:dao_deposit] < 0 dao_events_attributes << { ckb_transaction_id: dao_input.ckb_transaction_id, + cell_index: dao_input.index, block_id: local_block.id, block_timestamp: local_block.timestamp, address_id: previous_cell_output.address_id, @@ -351,7 +354,7 @@ def process_deposit_dao_events!(local_block, new_dao_depositors, dao_contract) addresses_deposit_info = {} # build deposit dao events local_block.cell_outputs.nervos_dao_deposit.select(:id, :address_id, :capacity, - :ckb_transaction_id).find_in_batches do |dao_outputs| + :ckb_transaction_id, :cell_index).find_in_batches do |dao_outputs| deposit_dao_events_attributes = [] dao_outputs.each do |dao_output| address = dao_output.address @@ -374,6 +377,7 @@ def process_deposit_dao_events!(local_block, new_dao_depositors, dao_contract) deposit_transaction_ids << dao_output.ckb_transaction_id deposit_dao_events_attributes << { ckb_transaction_id: dao_output.ckb_transaction_id, + cell_index: dao_output.cell_index, block_id: local_block.id, address_id: address.id, event_type: "deposit_to_dao", diff --git a/app/models/dao_event.rb b/app/models/dao_event.rb index cd4c8fa89..fd33ee514 100644 --- a/app/models/dao_event.rb +++ b/app/models/dao_event.rb @@ -54,17 +54,19 @@ def get_froms # # Table name: dao_events # -# id :bigint not null, primary key -# block_id :bigint -# ckb_transaction_id :bigint -# address_id :bigint -# contract_id :bigint -# event_type :integer -# value :decimal(30, ) default(0) -# status :integer default("pending") -# created_at :datetime not null -# updated_at :datetime not null -# block_timestamp :decimal(30, ) +# id :bigint not null, primary key +# block_id :bigint +# ckb_transaction_id :bigint +# address_id :bigint +# contract_id :bigint +# event_type :integer +# value :decimal(30, ) default(0) +# status :integer default("pending") +# created_at :datetime not null +# updated_at :datetime not null +# block_timestamp :decimal(30, ) +# withdrawn_transaction_id :bigint +# cell_index :integer # # Indexes # diff --git a/db/migrate/20241223060331_add_cell_index_to_dao_event.rb b/db/migrate/20241223060331_add_cell_index_to_dao_event.rb new file mode 100644 index 000000000..e2a25d2dc --- /dev/null +++ b/db/migrate/20241223060331_add_cell_index_to_dao_event.rb @@ -0,0 +1,6 @@ +class AddCellIndexToDaoEvent < ActiveRecord::Migration[7.0] + def change + add_column :dao_events, :withdrawn_transaction_id, :bigint + add_column :dao_events, :cell_index, :integer + end +end diff --git a/db/structure.sql b/db/structure.sql index a8e7a0280..1b35e256c 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1651,7 +1651,9 @@ CREATE TABLE public.dao_events ( status smallint DEFAULT 0, created_at timestamp(6) without time zone NOT NULL, updated_at timestamp(6) without time zone NOT NULL, - block_timestamp numeric(30,0) + block_timestamp numeric(30,0), + withdrawn_transaction_id bigint, + cell_index integer ); @@ -6329,6 +6331,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20241212022531'), ('20241213053309'), ('20241218085721'), +('20241223060331'), ('20241225045757'); diff --git a/lib/tasks/migration/fill_cell_index_to_dao_event.rake b/lib/tasks/migration/fill_cell_index_to_dao_event.rake new file mode 100644 index 000000000..75d60e9d3 --- /dev/null +++ b/lib/tasks/migration/fill_cell_index_to_dao_event.rake @@ -0,0 +1,54 @@ +namespace :migration do + desc "Usage: RAILS_ENV=production bundle exec rake migration:fill_cell_index_to_dao_event" + task fill_cell_index_to_dao_event: :environment do + CellOutput.dead.where(cell_type: ["nervos_dao_deposit", "nervos_dao_withdrawing"]).find_each do |output| + input = output.cell_inputs.first + if input.cell_type != output.cell_type + puts "input id: #{input.id}" + input.update(cell_type: output.cell_type) + end + end + CkbTransaction.where("tags @> array[?]::varchar[]", ["dao"]).find_each do |tx| + tx.outputs.where(cell_type: :nervos_dao_deposit).each do |output| + 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 + if event + event.update(cell_index: output.cell_index) + else + 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, + block_timestamp: tx.block_timestamp, contract_id: 1) + end + end + tx.cell_inputs.where(cell_type: :nervos_dao_withdrawing).each do |input| + previous_cell_output = CellOutput. + where(id: input.previous_cell_output_id). + select(:address_id, :block_id, :ckb_transaction_id, :dao, :cell_index, :capacity, :occupied_capacity). + take! + interest = CkbUtils.dao_interest(previous_cell_output) + 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 + if event + event.update(cell_index: input.index) + else + 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, + block_timestamp: tx.block_timestamp, contract_id: 1) + end + end + tx.cell_inputs.where(cell_type: :nervos_dao_deposit).each do |input| + previous_cell_output = + CellOutput. + where(id: input.previous_cell_output_id). + select(:address_id, :ckb_transaction_id, :cell_index, :capacity). + take! + 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", + cell_index: nil).limit(1).first + 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", + cell_index: previous_cell_output.cell_index).update(withdrawn_transaction_id: tx.id) + if event + event.update(cell_index: input.index) + else + 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, + block_timestamp: tx.block_timestamp, contract_id: 1) + end + end + end + end +end diff --git a/test/models/ckb_sync/dao_events_test.rb b/test/models/ckb_sync/dao_events_test.rb index b4bbd7637..4198fde77 100644 --- a/test/models/ckb_sync/dao_events_test.rb +++ b/test/models/ckb_sync/dao_events_test.rb @@ -71,9 +71,9 @@ class DaoEventsTest < ActiveSupport::TestCase deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "withdraw_from_dao") assert_equal ["processed"], deposit_to_dao_events.pluck(:status).uniq - 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| - attribute.in?(%w(created_at updated_at id)) - } + 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| + attribute.in?(%w(created_at updated_at id)) + } end end @@ -420,9 +420,9 @@ class DaoEventsTest < ActiveSupport::TestCase deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "withdraw_from_dao") assert_equal ["processed"], deposit_to_dao_events.pluck(:status).uniq - 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| - attribute.in?(%w(created_at updated_at id)) - } + 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| + attribute.in?(%w(created_at updated_at id)) + } end end @@ -732,9 +732,9 @@ class DaoEventsTest < ActiveSupport::TestCase deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "new_dao_depositor") assert_equal ["processed"], deposit_to_dao_events.pluck(:status).uniq - 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| - attribute.in?(%w(created_at updated_at id)) - } + 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| + attribute.in?(%w(created_at updated_at id)) + } end end diff --git a/test/models/dao_event_test.rb b/test/models/dao_event_test.rb index d2a7f9199..05ff7da7c 100644 --- a/test/models/dao_event_test.rb +++ b/test/models/dao_event_test.rb @@ -7,7 +7,7 @@ class DaoEventTest < ActiveSupport::TestCase test "should have correct columns" do dao_event = create(:dao_event) - expected_attributes = %w(address_id block_id block_timestamp contract_id created_at event_type id status ckb_transaction_id updated_at value).sort + 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 assert_equal expected_attributes, dao_event.attributes.keys.sort end end