Skip to content

Commit 741f07c

Browse files
committed
test: fix test
Signed-off-by: Miles Zhang <mingchang555@hotmail.com>
1 parent 7855b28 commit 741f07c

File tree

6 files changed

+24
-221
lines changed

6 files changed

+24
-221
lines changed

test/factories/lock_script.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
hash_type { "type" }
55
args { "0x#{SecureRandom.hex(20)}" }
66
code_hash { Settings.secp_cell_type_hash }
7+
before(:create) do |lock_script|
8+
lock_script.script_hash = CKB::Types::Script.new(code_hash: lock_script.code_hash, hash_type: lock_script.hash_type, args: lock_script.args).compute_hash
9+
end
710
end
811
end

test/models/ckb_sync/dao_events_test.rb

Lines changed: 14 additions & 168 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 withdrawn_transaction_id cell_index), deposit_to_dao_events.first.attribute_names.reject { |attribute|
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 do |attribute|
7575
attribute.in?(%w(created_at updated_at id))
76-
}
76+
end)
7777
end
7878
end
7979

@@ -116,26 +116,6 @@ class DaoEventsTest < ActiveSupport::TestCase
116116
end
117117
end
118118

119-
test "#process_block should decrease dao contract depositors count when previous output is a dao cell and address interest change to zero" do
120-
DaoCompensationCalculator.any_instance.stubs(:call).returns(1000)
121-
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
122-
create(:block, :with_block_hash, number: node_block.header.number - 1)
123-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
124-
tx = fake_dao_withdraw_transaction(node_block)
125-
output = tx.cell_outputs.first
126-
address = output.address
127-
address.update(dao_deposit: output.capacity)
128-
DaoContract.default_contract.update(total_deposit: 100000000000, depositors_count: 1)
129-
130-
assert_difference -> { DaoContract.default_contract.reload.depositors_count }, -1 do
131-
node_data_processor.process_block(node_block)
132-
end
133-
134-
take_away_all_deposit_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "take_away_all_deposit")
135-
assert_equal ["processed"], take_away_all_deposit_events.pluck(:status).uniq
136-
end
137-
end
138-
139119
test "should update tx's tags when input have nervos_dao_withdrawing cells" do
140120
DaoContract.default_contract.update(total_deposit: 100000000000000)
141121
block1 = create(:block, :with_block_hash, number: DEFAULT_NODE_BLOCK_NUMBER - 2)
@@ -381,32 +361,6 @@ class DaoEventsTest < ActiveSupport::TestCase
381361
end
382362
end
383363

384-
test "should increase dao contract depositors_count when block is invalid and previous output is a dao cell" do
385-
DaoCompensationCalculator.any_instance.stubs(:call).returns(1000)
386-
DaoContract.default_contract.update(total_deposit: 100000000000000, depositors_count: 1)
387-
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
388-
create(:block, :with_block_hash, number: node_block.header.number - 1, timestamp: 1557282351075)
389-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
390-
tx = fake_dao_withdraw_transaction(node_block)
391-
output = tx.cell_outputs.first
392-
address = output.address
393-
address.update(dao_deposit: output.capacity)
394-
node_data_processor.process_block(node_block)
395-
end
396-
397-
local_block = Block.find_by(block_hash: "0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
398-
local_block.update(block_hash: "0x419c632366c8eb9635acbb39ea085f7552ae62e1fdd480893375334a0f37d1bx")
399-
400-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}", record: :new_episodes) do
401-
assert_difference -> { DaoContract.default_contract.depositors_count }, 1 do
402-
node_data_processor.call
403-
end
404-
405-
deposit_to_dao_events = local_block.dao_events.where(event_type: "take_away_all_deposit")
406-
assert_equal ["reverted"], deposit_to_dao_events.pluck(:status).uniq
407-
end
408-
end
409-
410364
test "#process_block should create dao_event which event_type is withdraw_from_dao when previous output is a dao cell" do
411365
DaoCompensationCalculator.any_instance.stubs(:call).returns(1000)
412366
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
@@ -420,9 +374,9 @@ class DaoEventsTest < ActiveSupport::TestCase
420374

421375
deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "withdraw_from_dao")
422376
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 withdrawn_transaction_id cell_index), deposit_to_dao_events.first.attribute_names.reject { |attribute|
377+
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 do |attribute|
424378
attribute.in?(%w(created_at updated_at id))
425-
}
379+
end)
426380
end
427381
end
428382

@@ -443,40 +397,6 @@ class DaoEventsTest < ActiveSupport::TestCase
443397
end
444398
end
445399

446-
test "#process_block should create dao_event which event_type is take away all deposit when previous output is a dao cell and address interest change to zero" do
447-
DaoCompensationCalculator.any_instance.stubs(:call).returns(1000)
448-
DaoContract.default_contract.update(total_deposit: 100000000000000, depositors_count: 1)
449-
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
450-
create(:block, :with_block_hash, number: node_block.header.number - 1)
451-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
452-
tx = fake_dao_withdraw_transaction(node_block)
453-
output = tx.cell_outputs.first
454-
address = output.address
455-
address.update(dao_deposit: output.capacity)
456-
457-
assert_difference -> { DaoEvent.where(event_type: "take_away_all_deposit").count }, 1 do
458-
node_data_processor.process_block(node_block)
459-
end
460-
461-
deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "take_away_all_deposit")
462-
assert_equal ["processed"], deposit_to_dao_events.pluck(:status).uniq
463-
end
464-
end
465-
466-
test "#process_block should keep address deposit 0 when only have dao withdrawal event" do
467-
DaoContract.default_contract.update(total_deposit: 100000000000000, depositors_count: 1)
468-
DaoCompensationCalculator.any_instance.stubs(:call).returns(1000)
469-
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
470-
create(:block, :with_block_hash, number: node_block.header.number - 1)
471-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
472-
tx = fake_dao_withdraw_transaction(node_block)
473-
output = tx.cell_outputs.first
474-
address = output.address
475-
476-
assert_equal 0, address.reload.dao_deposit
477-
end
478-
end
479-
480400
test "#process_block should increase address interest when previous output is a withdrawing cell" do
481401
DaoCompensationCalculator.any_instance.stubs(:call).returns(100800000000)
482402
DaoContract.default_contract.update(total_deposit: 100000000000000, depositors_count: 1)
@@ -506,9 +426,9 @@ class DaoEventsTest < ActiveSupport::TestCase
506426
init_interest_granted = 10**8 * 100
507427
init_deposit_transactions_count = 2
508428
init_withdraw_transactions_count = 1
509-
init_total_depositors_count = 2
510429
dao_contract.update(total_deposit: init_total_deposit, depositors_count: init_depositors_count,
511-
claimed_compensation: init_interest_granted, deposit_transactions_count: init_deposit_transactions_count, withdraw_transactions_count: init_withdraw_transactions_count, total_depositors_count: init_total_depositors_count)
430+
claimed_compensation: init_interest_granted, deposit_transactions_count: init_deposit_transactions_count, withdraw_transactions_count: init_withdraw_transactions_count)
431+
create_list(:dao_event, 3, status: :processed, contract_id: 1, event_type: "deposit_to_dao")
512432
prepare_node_data(HAS_UNCLES_BLOCK_NUMBER)
513433
local_block = Block.find_by(number: HAS_UNCLES_BLOCK_NUMBER)
514434
local_block.update(block_hash: "0x419c632366c8eb9635acbb39ea085f7552ae62e1fdd480893375334a0f37d1bx")
@@ -521,7 +441,6 @@ class DaoEventsTest < ActiveSupport::TestCase
521441
assert_equal init_interest_granted, dao_contract.claimed_compensation
522442
assert_equal init_deposit_transactions_count, dao_contract.deposit_transactions_count
523443
assert_equal init_withdraw_transactions_count, dao_contract.withdraw_transactions_count
524-
assert_equal init_total_depositors_count, dao_contract.total_depositors_count
525444
end
526445
end
527446

@@ -613,40 +532,6 @@ class DaoEventsTest < ActiveSupport::TestCase
613532
end
614533
end
615534

616-
test "should revert dao contract depositors count when block is invalid and there is dao cell" do
617-
CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x2faf0be8")
618-
node_block = fake_node_block
619-
create(:block, :with_block_hash, number: node_block.header.number - 1, timestamp: 1557282351075)
620-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
621-
fake_dao_deposit_transaction(node_block)
622-
node_data_processor.process_block(node_block)
623-
end
624-
dao_contract = DaoContract.default_contract
625-
local_block = Block.find_by(number: DEFAULT_NODE_BLOCK_NUMBER)
626-
local_block.update(block_hash: "0x419c632366c8eb9635acbb39ea085f7552ae62e1fdd480893375334a0f37d1bx")
627-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}", record: :new_episodes) do
628-
assert_difference -> { dao_contract.reload.depositors_count }, -1 do
629-
node_data_processor.call
630-
end
631-
632-
deposit_to_dao_events = local_block.dao_events.where(event_type: "new_dao_depositor")
633-
assert_equal ["reverted"], deposit_to_dao_events.pluck(:status).uniq
634-
end
635-
end
636-
637-
test "#process_block should create dao_event which event_type is new_dao_depositor when output is a dao cell" do
638-
CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x2faf0be8")
639-
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
640-
create(:block, :with_block_hash, number: node_block.header.number - 1)
641-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
642-
fake_dao_deposit_transaction(node_block)
643-
644-
assert_difference -> { DaoEvent.where(event_type: "new_dao_depositor").count }, 1 do
645-
node_data_processor.process_block(node_block)
646-
end
647-
end
648-
end
649-
650535
test "#process_block should update address deposits when dao_event is deposit_to_dao and output is a dao cell" do
651536
CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x2faf0be8")
652537
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
@@ -698,46 +583,6 @@ class DaoEventsTest < ActiveSupport::TestCase
698583
end
699584
end
700585

701-
test "#process_block should update dao contract depositors count when dao_event is new_dao_depositor and output is a dao cell" do
702-
CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x2faf0be8")
703-
704-
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
705-
create(:block, :with_block_hash, number: node_block.header.number - 1)
706-
707-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
708-
fake_dao_deposit_transaction(node_block)
709-
710-
assert_difference -> { DaoContract.default_contract.depositors_count }, 1 do
711-
node_data_processor.process_block(node_block)
712-
end
713-
end
714-
715-
deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "new_dao_depositor")
716-
assert_equal ["processed"], deposit_to_dao_events.pluck(:status).uniq
717-
assert_not_empty DaoEvent.where(event_type: "new_dao_depositor")
718-
end
719-
720-
test "#process_block should update dao contract total depositors count when dao_event is new_dao_depositor and output is a dao cell" do
721-
CkbSync::Api.any_instance.stubs(:calculate_dao_maximum_withdraw).returns("0x2faf0be8")
722-
723-
node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063")
724-
create(:block, :with_block_hash, number: node_block.header.number - 1)
725-
726-
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
727-
fake_dao_deposit_transaction(node_block)
728-
729-
assert_difference -> { DaoContract.default_contract.total_depositors_count }, 1 do
730-
node_data_processor.process_block(node_block)
731-
end
732-
733-
deposit_to_dao_events = Block.find_by(number: node_block.header.number).dao_events.where(event_type: "new_dao_depositor")
734-
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 withdrawn_transaction_id cell_index), deposit_to_dao_events.first.attribute_names.reject { |attribute|
736-
attribute.in?(%w(created_at updated_at id))
737-
}
738-
end
739-
end
740-
741586
test "#process_block should not update dao contract total depositors count when depositors is already has been recorded" do
742587
DaoContract.default_contract.update(total_deposit: 100000000000000, depositors_count: 1,
743588
total_depositors_count: 1)
@@ -807,11 +652,11 @@ class DaoEventsTest < ActiveSupport::TestCase
807652
tx3 = create(:ckb_transaction, block: block2)
808653
tx4 = create(:ckb_transaction, block: block2)
809654
tx5 = create(:ckb_transaction, block: block2)
810-
input_address1 = create(:address)
811-
input_address2 = create(:address)
655+
input_address1 = create(:address, dao_deposit: 50000 * 10**8)
656+
input_address2 = create(:address, dao_deposit: 60000 * 10**8)
812657
input_address3 = create(:address)
813-
input_address4 = create(:address)
814-
input_address5 = create(:address)
658+
input_address4 = create(:address, dao_deposit: 70000 * 10**8)
659+
input_address5 = create(:address, dao_deposit: 70000 * 10**8)
815660
create(:cell_output, ckb_transaction: tx1, block: block1, capacity: 50000 * 10**8, tx_hash: tx1.tx_hash,
816661
cell_index: 0, address: input_address1, cell_type: "nervos_dao_deposit")
817662
create(:cell_output, ckb_transaction: tx2, block: block2, capacity: 60000 * 10**8, tx_hash: tx2.tx_hash,
@@ -889,8 +734,8 @@ class DaoEventsTest < ActiveSupport::TestCase
889734
block2 = create(:block, :with_block_hash, number: DEFAULT_NODE_BLOCK_NUMBER - 1)
890735
tx2 = create(:ckb_transaction, block: block2)
891736
tx3 = create(:ckb_transaction, block: block2)
892-
input_address1 = create(:address)
893-
input_address2 = create(:address)
737+
input_address1 = create(:address, dao_deposit: 50000 * 10**8)
738+
input_address2 = create(:address, dao_deposit: 60000 * 10**8)
894739
input_address3 = create(:address)
895740
create(:cell_output, ckb_transaction: tx1, block: block1, capacity: 50000 * 10**8, tx_hash: tx1.tx_hash,
896741
cell_index: 0, address: input_address1, cell_type: "nervos_dao_deposit")
@@ -975,10 +820,11 @@ def fake_dao_withdraw_transaction(node_block)
975820
tx_hash: "0x598315db9c7ba144cca74d2e9122ac9b3a3da1641b2975ae321d91ec34f1c0e3",
976821
block:, capacity: 10**8 * 1000,
977822
lock_script_id: lock.id)
978-
cell_output1.address.update(balance: 10**8 * 1000)
823+
cell_output1.address.update(balance: 10**8 * 1000, dao_deposit: 10**8 * 1000)
979824
cell_output2.address.update(balance: 10**8 * 1000)
980825
tx = node_block.transactions.last
981826
output = tx.outputs.first
827+
982828
output.type = CKB::Types::Script.new(args: "0xb2e61ff569acf041b3c2c17724e2379c581eeac3", hash_type: "type",
983829
code_hash: Settings.dao_type_hash)
984830
tx.outputs_data[0] = CKB::Utils.bin_to_hex("\x02" * 8)

test/models/ckb_sync/node_data_processor_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,9 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
421421
local_block = node_data_processor.process_block(node_block)
422422
expected_capacity_involved = local_block.ckb_transactions.normal.map(&:capacity_involved)
423423

424-
assert_equal expected_capacity_involved, local_block.ckb_transactions.normal.map { |transaction|
424+
assert_equal(expected_capacity_involved, local_block.ckb_transactions.normal.map do |transaction|
425425
transaction.inputs.sum(:capacity)
426-
}
426+
end)
427427
end
428428
end
429429

@@ -828,12 +828,12 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
828828
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}",
829829
record: :new_episodes) do
830830
assert_difference -> {
831-
dao_contract.reload.total_depositors_count
831+
dao_contract.reload.depositors_count
832832
}, -1 do
833833
node_data_processor.call
834834
end
835835

836-
deposit_to_dao_events = local_block.dao_events.where(event_type: "new_dao_depositor")
836+
deposit_to_dao_events = local_block.dao_events.where(event_type: "deposit_to_dao")
837837
assert_equal ["reverted"], deposit_to_dao_events.pluck(:status).uniq
838838
end
839839
end
@@ -3135,7 +3135,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
31353135
]
31363136
inputs1 = [
31373137
CKB::Types::Input.new(previous_output: CKB::Types::OutPoint.new(tx_hash: tx4.tx_hash, index: 0)), # nervos_dao_withdrawing cell
3138-
CKB::Types::Input.new(previous_output: CKB::Types::OutPoint.new(tx_hash: tx5.tx_hash, index: 0)), # udt cell
3138+
CKB::Types::Input.new(previous_output: CKB::Types::OutPoint.new(tx_hash: tx5.tx_hash, index: 0)), # udt cell
31393139
]
31403140
lock1 = CKB::Types::Script.new(
31413141
code_hash: Settings.secp_cell_type_hash,

test/models/dao_contract_test.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class DaoContractTest < ActiveSupport::TestCase
1717
should validate_presence_of(:depositors_count)
1818
should validate_numericality_of(:depositors_count).
1919
is_greater_than_or_equal_to(0)
20-
should validate_presence_of(:total_depositors_count)
21-
should validate_numericality_of(:total_depositors_count).
22-
is_greater_than_or_equal_to(0)
2320
end
2421

2522
test "should have correct columns" do

0 commit comments

Comments
 (0)