Skip to content

Commit fa5d10d

Browse files
authored
Merge pull request #2828 from nervosnetwork/testnet
Deploy to mainnet
2 parents 9a84252 + 9d25538 commit fa5d10d

File tree

7 files changed

+39
-12
lines changed

7 files changed

+39
-12
lines changed

app/models/ckb_sync/new_node_data_processor.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,10 @@ def process_interest_dao_events!(local_block, dao_contract)
275275
dao_inputs.each do |dao_input|
276276
previous_cell_output = CellOutput.
277277
where(id: dao_input.previous_cell_output_id).
278-
select(:address_id, :block_id, :ckb_transaction_id, :dao, :cell_index, :capacity, :occupied_capacity).
278+
select(:address_id, :block_id, :ckb_transaction_id, :dao, :cell_index, :capacity, :occupied_capacity, :id).
279279
take!
280280
address = previous_cell_output.address
281+
281282
interest = CkbUtils.dao_interest(previous_cell_output)
282283
if addrs_withdraw_info.key?(address.id)
283284
addrs_withdraw_info[address.id][:interest] += interest

app/utils/ckb_utils.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,11 @@ def self.dao_withdraw_tx_fee(ckb_transaction, tx_previous_outputs)
272272

273273
def self.dao_interest(nervos_dao_withdrawing_cell)
274274
nervos_dao_withdrawing_cell_generated_tx = nervos_dao_withdrawing_cell.ckb_transaction
275-
nervos_dao_deposit_cell = nervos_dao_withdrawing_cell_generated_tx.cell_inputs.order(:index)[nervos_dao_withdrawing_cell.cell_index].previous_cell_output
275+
block_number = CKB::Utils.hex_to_bin(nervos_dao_withdrawing_cell.data).unpack("Q<").pack("Q>").unpack1("H*").hex
276+
deposit_dao = Block.find_by_number(block_number).dao
276277
withdrawing_dao_cell_block_dao = nervos_dao_withdrawing_cell.dao
277-
DaoCompensationCalculator.new(nervos_dao_deposit_cell, withdrawing_dao_cell_block_dao,
278-
nervos_dao_withdrawing_cell).call
278+
DaoCompensationCalculator.new(nil, withdrawing_dao_cell_block_dao,
279+
nervos_dao_withdrawing_cell, deposit_dao).call
279280
end
280281

281282
# see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0027-block-structure/0027-block-structure.md#compact_target-uint32

app/utils/dao_compensation_calculator.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class DaoCompensationCalculator
2-
attr_reader :deposit_cell_output, :withdraw_block_dao, :withdraw_cell_output
2+
attr_reader :deposit_cell_output, :withdraw_block_dao, :withdraw_cell_output, :deposit_dao
33

4-
def initialize(deposit_cell_output, withdraw_block_dao, withdraw_cell_output = nil)
4+
def initialize(deposit_cell_output, withdraw_block_dao, withdraw_cell_output = nil, deposit_dao = nil)
55
@deposit_cell_output = deposit_cell_output
66
@withdraw_cell_output = withdraw_cell_output
77
@withdraw_block_dao = withdraw_block_dao
8+
@deposit_dao = deposit_dao
89
end
910

1011
def call
@@ -18,7 +19,11 @@ def parsed_withdraw_block_dao
1819
end
1920

2021
def parsed_deposit_block_dao
21-
CkbUtils.parse_dao(deposit_cell_output.dao)
22+
if deposit_cell_output
23+
CkbUtils.parse_dao(deposit_cell_output.dao)
24+
else
25+
CkbUtils.parse_dao(deposit_dao)
26+
end
2227
end
2328

2429
def compensation_generating_capacity

test/models/address_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class AddressTest < ActiveSupport::TestCase
153153
capacity: 10000 * 10**8,
154154
occupied_capacity: 6100000000,
155155
cell_index: 0,
156+
data: CKB::Utils.bin_to_hex([previous_output_block.number].pack('Q<')),
156157
dao: nervos_dao_withdrawing_block.dao)
157158
create(:cell_output, block: nervos_dao_withdrawing_block,
158159
address:,
@@ -161,6 +162,7 @@ class AddressTest < ActiveSupport::TestCase
161162
capacity: 20000 * 10**8,
162163
occupied_capacity: 6100000000,
163164
cell_index: 1,
165+
data: CKB::Utils.bin_to_hex([previous_output_block.number].pack('Q<')),
164166
dao: nervos_dao_withdrawing_block.dao)
165167

166168
deposit_cell = create(:cell_output, block: deposit_block,

test/models/ckb_sync/dao_events_test.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,13 @@ class DaoEventsTest < ActiveSupport::TestCase
144144
create(:cell_input, ckb_transaction: tx2, block: block2,
145145
previous_output: { tx_hash: deposit_tx.tx_hash, index: 1 })
146146
create(:cell_output, ckb_transaction: tx1, block: block1, capacity: 50000 * (10**8), tx_hash: tx1.tx_hash,
147-
cell_index: 0, address: input_address1, cell_type: "nervos_dao_withdrawing", dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", occupied_capacity: 6100000000)
147+
cell_index: 0, address: input_address1, cell_type: "nervos_dao_withdrawing",
148+
data: CKB::Utils.bin_to_hex([deposit_block.number].pack('Q<')),
149+
dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", occupied_capacity: 6100000000)
148150
create(:cell_output, ckb_transaction: tx2, block: block2, capacity: 60000 * (10**8), tx_hash: tx2.tx_hash,
149-
cell_index: 1, address: input_address2, cell_type: "nervos_dao_withdrawing", dao: "0x2cd631702e870700b3df08d7d889230036f787487e00000000e3bad4847a0100", occupied_capacity: 6100000000)
151+
cell_index: 1, address: input_address2, cell_type: "nervos_dao_withdrawing",
152+
data: CKB::Utils.bin_to_hex([deposit_block.number].pack('Q<')),
153+
dao: "0x2cd631702e870700b3df08d7d889230036f787487e00000000e3bad4847a0100", occupied_capacity: 6100000000)
150154
create(:cell_output, ckb_transaction: tx3, block: block2, capacity: 70000 * (10**8), tx_hash: tx3.tx_hash,
151155
cell_index: 2, address: input_address3, occupied_capacity: 6100000000)
152156

@@ -159,9 +163,13 @@ class DaoEventsTest < ActiveSupport::TestCase
159163
create(:cell_input, ckb_transaction: tx5, block: block2,
160164
previous_output: { tx_hash: deposit_tx1.tx_hash, index: 1 })
161165
create(:cell_output, ckb_transaction: tx4, block: block1, capacity: 150000 * (10**8), tx_hash: tx4.tx_hash,
162-
cell_index: 0, address: input_address4, cell_type: "nervos_dao_withdrawing", dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", occupied_capacity: 6100000000)
166+
cell_index: 0, address: input_address4, cell_type: "nervos_dao_withdrawing",
167+
data: CKB::Utils.bin_to_hex([deposit_block1.number].pack('Q<')),
168+
dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", occupied_capacity: 6100000000)
163169
create(:cell_output, ckb_transaction: tx5, block: block2, capacity: 60000 * (10**8), tx_hash: tx5.tx_hash,
164-
cell_index: 0, address: input_address5, cell_type: "nervos_dao_withdrawing", dao: "0x2cd631702e870700b3df08d7d889230036f787487e00000000e3bad4847a0100", occupied_capacity: 6100000000)
170+
cell_index: 0, address: input_address5, cell_type: "nervos_dao_withdrawing",
171+
data: CKB::Utils.bin_to_hex([deposit_block1.number].pack('Q<')),
172+
dao: "0x2cd631702e870700b3df08d7d889230036f787487e00000000e3bad4847a0100", occupied_capacity: 6100000000)
165173
header = CKB::Types::BlockHeader.new(compact_target: "0x1000", hash: "0x#{SecureRandom.hex(32)}",
166174
number: DEFAULT_NODE_BLOCK_NUMBER, parent_hash: "0x#{SecureRandom.hex(32)}", nonce: 1757392074788233522, timestamp: CkbUtils.time_in_milliseconds(Time.current), transactions_root: "0x#{SecureRandom.hex(32)}", proposals_hash: "0x#{SecureRandom.hex(32)}", extra_hash: "0x#{SecureRandom.hex(32)}", version: 0, epoch: 1, dao: "0x01000000000000000000c16ff286230000a3a65e97fd03000057c138586f0000")
167175
inputs = [
@@ -822,14 +830,16 @@ def fake_dao_interest_transaction(node_block)
822830
tx_hash: "0x598315db9c7ba144cca74d2e9122ac9b3a3da1641b2975ae321d91ec34f1c0e3",
823831
block:)
824832
lock = create(:lock_script)
833+
825834
cell_output1 = create(:cell_output, ckb_transaction: ckb_transaction1,
826835
cell_index: 1,
827836
tx_hash: "0x498315db9c7ba144cca74d2e9122ac9b3a3da1641b2975ae321d91ec34f1c0e3",
828837
block:,
829838
cell_type: "nervos_dao_withdrawing",
830839
capacity: (10**8) * 1000,
831-
data: CKB::Utils.bin_to_hex("\x02" * 8),
840+
data: CKB::Utils.bin_to_hex([block.number].pack('Q<')),
832841
lock_script_id: lock.id)
842+
833843
cell_output2 = create(:cell_output, ckb_transaction: ckb_transaction2,
834844
cell_index: 1,
835845
tx_hash: "0x398315db9c7ba144cca74d2e9122ac9b3a3da1641b2975ae321d91ec34f1c0e2",
@@ -866,6 +876,7 @@ def fake_dao_interest_transaction(node_block)
866876
tx_hash: "0x498315db9c7ba144cca74d2e9122ac9b3a3da1641b2975ae321d91ec34f1c0e3",
867877
index: 1,
868878
})
879+
869880
create(:cell_input, block: ckb_transaction1.block,
870881
ckb_transaction: ckb_transaction1,
871882
previous_output: {

test/models/ckb_sync/node_data_processor_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,6 +2792,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
27922792
cell_index: 0,
27932793
address: input_address1,
27942794
cell_type: "nervos_dao_withdrawing",
2795+
data: CKB::Utils.bin_to_hex([deposit_block.number].pack('Q<')),
27952796
dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", lock_script_id: lock1.id, occupied_capacity: 61 * (10**8))
27962797
create(:cell_output, ckb_transaction: tx2,
27972798
block: block2,
@@ -2800,6 +2801,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
28002801
cell_index: 1,
28012802
address: input_address2,
28022803
cell_type: "nervos_dao_withdrawing",
2804+
data: CKB::Utils.bin_to_hex([deposit_block.number].pack('Q<')),
28032805
dao: "0x2cd631702e870700b3df08d7d889230036f787487e00000000e3bad4847a0100", lock_script_id: lock2.id, occupied_capacity: 61 * (10**8))
28042806

28052807
# udt cell
@@ -2854,6 +2856,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
28542856
cell_index: 0,
28552857
address: input_address4,
28562858
cell_type: "nervos_dao_withdrawing",
2859+
data: CKB::Utils.bin_to_hex([deposit_block1.number].pack('Q<')),
28572860
dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", lock_script_id: lock4.id, occupied_capacity: 61 * (10**8))
28582861

28592862
# udt cell

test/models/ckb_transaction_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ class CkbTransactionTest < ActiveSupport::TestCase
192192
previous_cell_output_id: nervos_dao_deposit_cell.id)
193193
started_block = Block.select(:number,
194194
:timestamp).find(nervos_dao_deposit_cell.block_id)
195+
196+
nervos_dao_withdrawing_cell.data = CKB::Utils.bin_to_hex([started_block.number].pack('Q<'))
197+
195198
interest = DaoCompensationCalculator.new(deposit_cell,
196199
nervos_dao_withdrawing_cell.block.dao).call
197200
# binding.pry
@@ -260,6 +263,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
260263
data: "0x7512000000000000",
261264
tx_hash: "0xf9aca16b49c7d037920ad9e5aecdac272412a5fbe0396f7d95b112bf790dd39f",
262265
cell_index: 0,
266+
data: CKB::Utils.bin_to_hex([block.number].pack('Q<')),
263267
cell_type: "nervos_dao_withdrawing")
264268
started_block = Block.select(:number,
265269
:timestamp).find(ckb_transaction.block_id)

0 commit comments

Comments
 (0)