Skip to content

Commit 7f0851c

Browse files
authored
Merge pull request #2823 from nervosnetwork/develop
Deploy to testnet
2 parents 73435cb + 1978b24 commit 7f0851c

File tree

9 files changed

+36
-93
lines changed

9 files changed

+36
-93
lines changed

app/jobs/revert_block_job.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,13 @@ def revert_dao_contract_related_operations(local_tip_block)
162162
dao_transactions_count = local_tip_block.ckb_transactions.where("tags @> array[?]::varchar[]", ["dao"]).count
163163
dao_contract = DaoContract.default_contract
164164

165-
withdraw_transactions_count, withdraw_total_deposit = revert_withdraw_from_dao(dao_events)
165+
withdraw_total_deposit = revert_withdraw_from_dao(dao_events)
166166
claimed_compensation = revert_issue_interest(dao_events)
167-
deposit_transactions_count, deposit_total_deposit = revert_deposit_to_dao(dao_events)
167+
deposit_total_deposit = revert_deposit_to_dao(dao_events)
168168

169169
dao_events.update_all(status: "reverted")
170-
dao_contract.update!(deposit_transactions_count: dao_contract.deposit_transactions_count - deposit_transactions_count,
171-
withdraw_transactions_count: dao_contract.withdraw_transactions_count - withdraw_transactions_count,
172-
total_deposit: dao_contract.total_deposit + withdraw_total_deposit - deposit_total_deposit,
170+
dao_contract.update!(total_deposit: dao_contract.total_deposit + withdraw_total_deposit - deposit_total_deposit,
173171
claimed_compensation: dao_contract.claimed_compensation - claimed_compensation,
174-
ckb_transactions_count: dao_contract.ckb_transactions_count - dao_transactions_count,
175172
depositors_count: DaoEvent.depositor.count)
176173
end
177174

@@ -218,7 +215,7 @@ def revert_withdraw_from_dao(dao_events)
218215
upsert_data = address_attrs.values
219216
Address.upsert_all(upsert_data, unique_by: :id) if upsert_data.present?
220217

221-
[withdraw_from_dao_events.size, redundant_total_deposit]
218+
redundant_total_deposit
222219
end
223220

224221
def revert_deposit_to_dao(dao_events)
@@ -242,7 +239,7 @@ def revert_deposit_to_dao(dao_events)
242239
Address.upsert_all(upsert_data, unique_by: :id) if upsert_data.present?
243240
Address.where(id: address_ids, dao_deposit: 0).update_all(is_depositor: false)
244241

245-
[deposit_to_dao_events.size, redundant_total_deposit]
242+
redundant_total_deposit
246243
end
247244

248245
def revert_block_rewards(local_tip_block)

app/models/ckb_sync/new_node_data_processor.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,6 @@ def process_dao_events!(local_tip_block = @local_tip_block)
185185
process_withdraw_dao_events!(local_block, dao_contract)
186186
process_interest_dao_events!(local_block, dao_contract)
187187
dao_contract.update(depositors_count: DaoEvent.depositor.distinct.count(:address_id))
188-
189-
# update dao contract ckb_transactions_count
190-
dao_contract.increment!(:ckb_transactions_count,
191-
local_block.ckb_transactions.where(
192-
"tags @> array[?]::varchar[]", ["dao"]
193-
).count)
194188
end
195189

196190
# Process DAO withdraw
@@ -263,8 +257,7 @@ def process_withdraw_dao_events!(local_block, dao_contract)
263257

264258
# update dao contract info
265259
dao_contract.update!(
266-
total_deposit: dao_contract.total_deposit - withdraw_amount,
267-
withdraw_transactions_count: dao_contract.withdraw_transactions_count + withdraw_transaction_ids.size,
260+
total_deposit: dao_contract.total_deposit - withdraw_amount
268261
)
269262
update_addresses_dao_info(addrs_withdraw_info)
270263
end
@@ -384,8 +377,7 @@ def process_deposit_dao_events!(local_block, dao_contract)
384377
end
385378
# update dao contract info
386379
dao_contract.update!(
387-
total_deposit: dao_contract.total_deposit + deposit_amount,
388-
deposit_transactions_count: dao_contract.deposit_transactions_count + deposit_transaction_ids.size,
380+
total_deposit: dao_contract.total_deposit + deposit_amount
389381
)
390382

391383
update_addresses_dao_info(addresses_deposit_info)

app/models/dao_contract.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class DaoContract < ApplicationRecord
2-
validates :total_deposit, :claimed_compensation, :deposit_transactions_count, :withdraw_transactions_count, :depositors_count, presence: true, numericality: { greater_than_or_equal_to: 0 }
2+
validates :total_deposit, :claimed_compensation, :depositors_count, presence: true, numericality: { greater_than_or_equal_to: 0 }
33
CONTRACT_NAME = "nervos_dao".freeze
44
GENESIS_ISSUANCE = 336 * 10**8
55
ANNUAL_PRIMARY_ISSUANCE_BASE = GENESIS_ISSUANCE / 8
@@ -121,15 +121,11 @@ def secondary_issuance(start_epoch)
121121
#
122122
# Table name: dao_contracts
123123
#
124-
# id :bigint not null, primary key
125-
# total_deposit :decimal(30, ) default(0)
126-
# claimed_compensation :decimal(30, ) default(0)
127-
# deposit_transactions_count :bigint default(0)
128-
# withdraw_transactions_count :bigint default(0)
129-
# depositors_count :integer default(0)
130-
# total_depositors_count :bigint default(0)
131-
# created_at :datetime not null
132-
# updated_at :datetime not null
133-
# unclaimed_compensation :decimal(30, )
134-
# ckb_transactions_count :decimal(30, ) default(0)
124+
# id :bigint not null, primary key
125+
# total_deposit :decimal(30, ) default(0)
126+
# claimed_compensation :decimal(30, ) default(0)
127+
# depositors_count :integer default(0)
128+
# created_at :datetime not null
129+
# updated_at :datetime not null
130+
# unclaimed_compensation :decimal(30, )
135131
#
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class RemoveTotalDepositorsCountFromDao < ActiveRecord::Migration[7.0]
2+
def change
3+
remove_columns :dao_contracts, :total_depositors_count, type: :bigint
4+
remove_columns :dao_contracts, :deposit_transactions_count, type: :bigint
5+
remove_columns :dao_contracts, :withdraw_transactions_count, type: :bigint
6+
remove_columns :dao_contracts, :ckb_transactions_count, type: :bigint
7+
end
8+
end

db/structure.sql

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,14 +1215,10 @@ CREATE TABLE public.dao_contracts (
12151215
id bigint NOT NULL,
12161216
total_deposit numeric(30,0) DEFAULT 0.0,
12171217
claimed_compensation numeric(30,0) DEFAULT 0.0,
1218-
deposit_transactions_count bigint DEFAULT 0,
1219-
withdraw_transactions_count bigint DEFAULT 0,
12201218
depositors_count integer DEFAULT 0,
1221-
total_depositors_count bigint DEFAULT 0,
12221219
created_at timestamp(6) without time zone NOT NULL,
12231220
updated_at timestamp(6) without time zone NOT NULL,
1224-
unclaimed_compensation numeric(30,0),
1225-
ckb_transactions_count numeric(30,0) DEFAULT 0.0
1221+
unclaimed_compensation numeric(30,0)
12261222
);
12271223

12281224

@@ -5940,6 +5936,7 @@ INSERT INTO "schema_migrations" (version) VALUES
59405936
('20250715035736'),
59415937
('20250715043211'),
59425938
('20250826022054'),
5943-
('20250827065749');
5939+
('20250827065749'),
5940+
('20250930015526');
59445941

59455942

test/models/ckb_sync/dao_events_test.rb

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ class DaoEventsTest < ActiveSupport::TestCase
6464
create(:block, :with_block_hash, number: node_block.header.number - 1)
6565
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
6666
fake_dao_withdraw_transaction(node_block)
67-
68-
assert_difference -> { DaoContract.default_contract.withdraw_transactions_count }, 1 do
69-
node_data_processor.process_block(node_block)
70-
end
67+
node_data_processor.process_block(node_block)
7168

7269
deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "withdraw_from_dao")
7370
assert_equal ["processed"], deposit_to_dao_events.pluck(:status).uniq
@@ -220,7 +217,6 @@ class DaoEventsTest < ActiveSupport::TestCase
220217

221218
assert_equal ["dao"], tx.tags
222219
assert_equal ["dao"], tx1.tags
223-
assert_equal 2, DaoContract.default_contract.ckb_transactions_count
224220
end
225221

226222
test "should increase address dao_deposit when block is invalid and previous output is a dao cell" do
@@ -278,7 +274,7 @@ class DaoEventsTest < ActiveSupport::TestCase
278274
end
279275
end
280276

281-
test "should decrease dao contract withdraw_transactions_count when block is invalid and previous output is a dao cell" do
277+
test "when block is invalid and previous output is a dao cell" do
282278
DaoCompensationCalculator.any_instance.stubs(:call).returns(1000)
283279
DaoContract.default_contract.update(total_deposit: 100000000000000, depositors_count: 1)
284280
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
@@ -296,10 +292,7 @@ class DaoEventsTest < ActiveSupport::TestCase
296292
local_block.update(block_hash: "0x419c632366c8eb9635acbb39ea085f7552ae62e1fdd480893375334a0f37d1bx")
297293

298294
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}", record: :new_episodes) do
299-
assert_difference -> { DaoContract.default_contract.reload.withdraw_transactions_count }, -1 do
300-
node_data_processor.call
301-
end
302-
295+
node_data_processor.call
303296
dao_events = local_block.dao_events.where(event_type: "withdraw_from_dao")
304297
assert_equal ["reverted"], dao_events.pluck(:status).uniq
305298
end
@@ -426,10 +419,8 @@ class DaoEventsTest < ActiveSupport::TestCase
426419
init_total_deposit = (10**8) * 10000
427420
init_depositors_count = 3
428421
init_interest_granted = (10**8) * 100
429-
init_deposit_transactions_count = 2
430-
init_withdraw_transactions_count = 1
431422
dao_contract.update(total_deposit: init_total_deposit, depositors_count: init_depositors_count,
432-
claimed_compensation: init_interest_granted, deposit_transactions_count: init_deposit_transactions_count, withdraw_transactions_count: init_withdraw_transactions_count)
423+
claimed_compensation: init_interest_granted)
433424
create_list(:dao_event, 3, status: :processed, contract_id: 1, event_type: "deposit_to_dao")
434425
prepare_node_data(HAS_UNCLES_BLOCK_NUMBER)
435426
local_block = Block.find_by(number: HAS_UNCLES_BLOCK_NUMBER)
@@ -441,8 +432,6 @@ class DaoEventsTest < ActiveSupport::TestCase
441432
assert_equal init_total_deposit, dao_contract.total_deposit
442433
assert_equal init_depositors_count, dao_contract.depositors_count
443434
assert_equal init_interest_granted, dao_contract.claimed_compensation
444-
assert_equal init_deposit_transactions_count, dao_contract.deposit_transactions_count
445-
assert_equal init_withdraw_transactions_count, dao_contract.withdraw_transactions_count
446435
end
447436
end
448437

@@ -525,10 +514,7 @@ class DaoEventsTest < ActiveSupport::TestCase
525514
local_block.update(block_hash: "0x419c632366c8eb9635acbb39ea085f7552ae62e1fdd480893375334a0f37d1bx")
526515

527516
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}", record: :new_episodes) do
528-
assert_difference -> { dao_contract.reload.deposit_transactions_count }, -1 do
529-
node_data_processor.call
530-
end
531-
517+
node_data_processor.call
532518
deposit_to_dao_events = local_block.dao_events.where(event_type: "deposit_to_dao")
533519
assert_equal ["reverted"], deposit_to_dao_events.pluck(:status).uniq
534520
end
@@ -577,19 +563,14 @@ class DaoEventsTest < ActiveSupport::TestCase
577563
create(:block, :with_block_hash, number: node_block.header.number - 1)
578564
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
579565
fake_dao_deposit_transaction(node_block)
580-
581-
assert_difference -> { DaoContract.default_contract.deposit_transactions_count }, 1 do
582-
node_data_processor.process_block(node_block)
583-
end
584-
566+
node_data_processor.process_block(node_block)
585567
deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "deposit_to_dao")
586568
assert_equal ["processed"], deposit_to_dao_events.pluck(:status).uniq
587569
end
588570
end
589571

590572
test "#process_block should not update dao contract total depositors count when depositors is already has been recorded" do
591-
DaoContract.default_contract.update(total_deposit: 100000000000000, depositors_count: 1,
592-
total_depositors_count: 1)
573+
DaoContract.default_contract.update(total_deposit: 100000000000000, depositors_count: 1)
593574
CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x2faf0be8")
594575

595576
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
@@ -614,10 +595,6 @@ class DaoEventsTest < ActiveSupport::TestCase
614595
output1.capacity = (10**8) * 1000
615596
tx1.outputs << output1
616597
tx1.outputs_data << CKB::Utils.bin_to_hex("\x00" * 8)
617-
618-
assert_no_changes -> { DaoContract.default_contract.total_depositors_count } do
619-
node_data_processor.process_block(node_block)
620-
end
621598
end
622599

623600
test "#process_block should not update dao contract depositors count when depositors is already has been recorded" do
@@ -728,7 +705,6 @@ class DaoEventsTest < ActiveSupport::TestCase
728705
tx1 = block.ckb_transactions.where(is_cellbase: false).second
729706
assert_equal ["dao"], tx.tags
730707
assert_equal ["dao"], tx1.tags
731-
assert_equal 2, DaoContract.default_contract.ckb_transactions_count
732708
end
733709

734710
test "should update tx's tags when output have udt cells and nervos_dao_withdrawing cell" do
@@ -793,7 +769,6 @@ class DaoEventsTest < ActiveSupport::TestCase
793769

794770
tx = block.ckb_transactions.where(is_cellbase: false).first
795771
assert_equal %w[dao udt], tx.tags
796-
assert_equal 1, DaoContract.default_contract.ckb_transactions_count
797772
end
798773

799774
private

test/models/ckb_sync/node_data_processor_test.rb

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,10 +2306,9 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
23062306

23072307
assert_equal ["dao"], tx.tags
23082308
assert_equal ["dao"], tx1.tags
2309-
assert_equal 2, DaoContract.default_contract.ckb_transactions_count
23102309
end
23112310

2312-
test "should recalculate dao contract ckb_transactions_count when block is invalid and has dao txs" do
2311+
test "when block is invalid and has dao txs" do
23132312
block1 = create(:block, :with_block_hash,
23142313
number: DEFAULT_NODE_BLOCK_NUMBER - 2)
23152314
tx1 = create(:ckb_transaction, block: block1)
@@ -2401,16 +2400,6 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
24012400
transactions:, header:)
24022401
block = node_data_processor.process_block(node_block)
24032402
CkbSync::Api.any_instance.stubs(:get_tip_block_number).returns(block.number + 1)
2404-
DaoContract.default_contract.update(deposit_transactions_count: 4)
2405-
2406-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}",
2407-
record: :new_episodes) do
2408-
assert_changes -> {
2409-
DaoContract.default_contract.reload.ckb_transactions_count
2410-
}, from: 2, to: 0 do
2411-
node_data_processor.call
2412-
end
2413-
end
24142403
end
24152404

24162405
test "should update tx's tags when output have udt cells" do
@@ -2565,7 +2554,6 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
25652554
tx = block.ckb_transactions.where(is_cellbase: false).first
25662555

25672556
assert_equal %w[dao udt], tx.tags
2568-
assert_equal 1, DaoContract.default_contract.ckb_transactions_count
25692557
end
25702558

25712559
test "should update tx's tags when input have udt cells" do
@@ -3003,8 +2991,6 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
30032991

30042992
assert_equal %w[dao udt], tx.tags
30052993
assert_equal %w[dao udt], tx1.tags
3006-
3007-
assert_equal 2, DaoContract.default_contract.ckb_transactions_count
30082994
end
30092995

30102996
test "#process_block should not update tx's tags when there aren't dao cells and udt cells" do
@@ -3372,7 +3358,6 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
33723358
Sidekiq::Testing.inline!
33733359
block = node_data_processor.process_block(node_block)
33743360
CkbSync::Api.any_instance.stubs(:get_tip_block_number).returns(block.number + 1)
3375-
DaoContract.default_contract.update!(deposit_transactions_count: 4)
33763361
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}",
33773362
record: :new_episodes) do
33783363
node_data_processor.call

test/models/dao_contract_test.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ class DaoContractTest < ActiveSupport::TestCase
88
should validate_presence_of(:claimed_compensation)
99
should validate_numericality_of(:claimed_compensation).
1010
is_greater_than_or_equal_to(0)
11-
should validate_presence_of(:deposit_transactions_count)
12-
should validate_numericality_of(:deposit_transactions_count).
13-
is_greater_than_or_equal_to(0)
14-
should validate_presence_of(:withdraw_transactions_count)
15-
should validate_numericality_of(:withdraw_transactions_count).
16-
is_greater_than_or_equal_to(0)
1711
should validate_presence_of(:depositors_count)
1812
should validate_numericality_of(:depositors_count).
1913
is_greater_than_or_equal_to(0)
@@ -22,8 +16,8 @@ class DaoContractTest < ActiveSupport::TestCase
2216
test "should have correct columns" do
2317
dao_contract = create(:dao_contract)
2418
expected_attributes = %w(
25-
created_at deposit_transactions_count depositors_count id claimed_compensation
26-
total_deposit total_depositors_count updated_at withdraw_transactions_count unclaimed_compensation ckb_transactions_count
19+
created_at depositors_count id claimed_compensation
20+
total_deposit updated_at unclaimed_compensation
2721
)
2822
assert_equal expected_attributes.sort, dao_contract.attributes.keys.sort
2923
end

test/test_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ def expected_ranking(address1, address2, address3)
394394

395395
def fake_dao_deposit_transaction(dao_cell_count, address)
396396
block = create(:block, :with_block_hash)
397-
DaoContract.default_contract.update(ckb_transactions_count: dao_cell_count)
398397
address.update(dao_transactions_count: dao_cell_count)
399398
dao_cell_count.times do |number|
400399
if number % 2 == 0

0 commit comments

Comments
 (0)