Skip to content

Commit

Permalink
Merge pull request #2377 from nervosnetwork/testnet
Browse files Browse the repository at this point in the history
Deploy to mainnet
  • Loading branch information
zmcNotafraid authored Dec 30, 2024
2 parents 31e5677 + b0df727 commit 19e6bd9
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 25 deletions.
10 changes: 7 additions & 3 deletions app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand Down
24 changes: 13 additions & 11 deletions app/models/dao_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20241223060331_add_cell_index_to_dao_event.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);


Expand Down Expand Up @@ -6329,6 +6331,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20241212022531'),
('20241213053309'),
('20241218085721'),
('20241223060331'),
('20241225045757');


54 changes: 54 additions & 0 deletions lib/tasks/migration/fill_cell_index_to_dao_event.rake
Original file line number Diff line number Diff line change
@@ -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
18 changes: 9 additions & 9 deletions test/models/ckb_sync/dao_events_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/models/dao_event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 19e6bd9

Please sign in to comment.