Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ def process_interest_dao_events!(local_block, dao_contract)
dao_inputs.each do |dao_input|
previous_cell_output = CellOutput.
where(id: dao_input.previous_cell_output_id).
select(:address_id, :block_id, :ckb_transaction_id, :dao, :cell_index, :capacity, :occupied_capacity).
select(:address_id, :block_id, :ckb_transaction_id, :dao, :cell_index, :capacity, :occupied_capacity, :id).
take!
address = previous_cell_output.address

interest = CkbUtils.dao_interest(previous_cell_output)
if addrs_withdraw_info.key?(address.id)
addrs_withdraw_info[address.id][:interest] += interest
Expand Down
7 changes: 4 additions & 3 deletions app/utils/ckb_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,11 @@ def self.dao_withdraw_tx_fee(ckb_transaction, tx_previous_outputs)

def self.dao_interest(nervos_dao_withdrawing_cell)
nervos_dao_withdrawing_cell_generated_tx = nervos_dao_withdrawing_cell.ckb_transaction
nervos_dao_deposit_cell = nervos_dao_withdrawing_cell_generated_tx.cell_inputs.order(:index)[nervos_dao_withdrawing_cell.cell_index].previous_cell_output
block_number = CKB::Utils.hex_to_bin(nervos_dao_withdrawing_cell.data).unpack("Q<").pack("Q>").unpack1("H*").hex
deposit_dao = Block.find_by_number(block_number).dao
withdrawing_dao_cell_block_dao = nervos_dao_withdrawing_cell.dao
DaoCompensationCalculator.new(nervos_dao_deposit_cell, withdrawing_dao_cell_block_dao,
nervos_dao_withdrawing_cell).call
DaoCompensationCalculator.new(nil, withdrawing_dao_cell_block_dao,
nervos_dao_withdrawing_cell, deposit_dao).call
end

# see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0027-block-structure/0027-block-structure.md#compact_target-uint32
Expand Down
11 changes: 8 additions & 3 deletions app/utils/dao_compensation_calculator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class DaoCompensationCalculator
attr_reader :deposit_cell_output, :withdraw_block_dao, :withdraw_cell_output
attr_reader :deposit_cell_output, :withdraw_block_dao, :withdraw_cell_output, :deposit_dao

def initialize(deposit_cell_output, withdraw_block_dao, withdraw_cell_output = nil)
def initialize(deposit_cell_output, withdraw_block_dao, withdraw_cell_output = nil, deposit_dao = nil)
@deposit_cell_output = deposit_cell_output
@withdraw_cell_output = withdraw_cell_output
@withdraw_block_dao = withdraw_block_dao
@deposit_dao = deposit_dao
end

def call
Expand All @@ -18,7 +19,11 @@ def parsed_withdraw_block_dao
end

def parsed_deposit_block_dao
CkbUtils.parse_dao(deposit_cell_output.dao)
if deposit_cell_output
CkbUtils.parse_dao(deposit_cell_output.dao)
else
CkbUtils.parse_dao(deposit_dao)
end
end

def compensation_generating_capacity
Expand Down
2 changes: 2 additions & 0 deletions test/models/address_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class AddressTest < ActiveSupport::TestCase
capacity: 10000 * 10**8,
occupied_capacity: 6100000000,
cell_index: 0,
data: CKB::Utils.bin_to_hex([previous_output_block.number].pack('Q<')),
dao: nervos_dao_withdrawing_block.dao)
create(:cell_output, block: nervos_dao_withdrawing_block,
address:,
Expand All @@ -161,6 +162,7 @@ class AddressTest < ActiveSupport::TestCase
capacity: 20000 * 10**8,
occupied_capacity: 6100000000,
cell_index: 1,
data: CKB::Utils.bin_to_hex([previous_output_block.number].pack('Q<')),
dao: nervos_dao_withdrawing_block.dao)

deposit_cell = create(:cell_output, block: deposit_block,
Expand Down
21 changes: 16 additions & 5 deletions test/models/ckb_sync/dao_events_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ class DaoEventsTest < ActiveSupport::TestCase
create(:cell_input, ckb_transaction: tx2, block: block2,
previous_output: { tx_hash: deposit_tx.tx_hash, index: 1 })
create(:cell_output, ckb_transaction: tx1, block: block1, capacity: 50000 * (10**8), tx_hash: tx1.tx_hash,
cell_index: 0, address: input_address1, cell_type: "nervos_dao_withdrawing", dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", occupied_capacity: 6100000000)
cell_index: 0, address: input_address1, cell_type: "nervos_dao_withdrawing",
data: CKB::Utils.bin_to_hex([deposit_block.number].pack('Q<')),
dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", occupied_capacity: 6100000000)
create(:cell_output, ckb_transaction: tx2, block: block2, capacity: 60000 * (10**8), tx_hash: tx2.tx_hash,
cell_index: 1, address: input_address2, cell_type: "nervos_dao_withdrawing", dao: "0x2cd631702e870700b3df08d7d889230036f787487e00000000e3bad4847a0100", occupied_capacity: 6100000000)
cell_index: 1, address: input_address2, cell_type: "nervos_dao_withdrawing",
data: CKB::Utils.bin_to_hex([deposit_block.number].pack('Q<')),
dao: "0x2cd631702e870700b3df08d7d889230036f787487e00000000e3bad4847a0100", occupied_capacity: 6100000000)
create(:cell_output, ckb_transaction: tx3, block: block2, capacity: 70000 * (10**8), tx_hash: tx3.tx_hash,
cell_index: 2, address: input_address3, occupied_capacity: 6100000000)

Expand All @@ -159,9 +163,13 @@ class DaoEventsTest < ActiveSupport::TestCase
create(:cell_input, ckb_transaction: tx5, block: block2,
previous_output: { tx_hash: deposit_tx1.tx_hash, index: 1 })
create(:cell_output, ckb_transaction: tx4, block: block1, capacity: 150000 * (10**8), tx_hash: tx4.tx_hash,
cell_index: 0, address: input_address4, cell_type: "nervos_dao_withdrawing", dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", occupied_capacity: 6100000000)
cell_index: 0, address: input_address4, cell_type: "nervos_dao_withdrawing",
data: CKB::Utils.bin_to_hex([deposit_block1.number].pack('Q<')),
dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", occupied_capacity: 6100000000)
create(:cell_output, ckb_transaction: tx5, block: block2, capacity: 60000 * (10**8), tx_hash: tx5.tx_hash,
cell_index: 0, address: input_address5, cell_type: "nervos_dao_withdrawing", dao: "0x2cd631702e870700b3df08d7d889230036f787487e00000000e3bad4847a0100", occupied_capacity: 6100000000)
cell_index: 0, address: input_address5, cell_type: "nervos_dao_withdrawing",
data: CKB::Utils.bin_to_hex([deposit_block1.number].pack('Q<')),
dao: "0x2cd631702e870700b3df08d7d889230036f787487e00000000e3bad4847a0100", occupied_capacity: 6100000000)
header = CKB::Types::BlockHeader.new(compact_target: "0x1000", hash: "0x#{SecureRandom.hex(32)}",
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")
inputs = [
Expand Down Expand Up @@ -822,14 +830,16 @@ def fake_dao_interest_transaction(node_block)
tx_hash: "0x598315db9c7ba144cca74d2e9122ac9b3a3da1641b2975ae321d91ec34f1c0e3",
block:)
lock = create(:lock_script)

cell_output1 = create(:cell_output, ckb_transaction: ckb_transaction1,
cell_index: 1,
tx_hash: "0x498315db9c7ba144cca74d2e9122ac9b3a3da1641b2975ae321d91ec34f1c0e3",
block:,
cell_type: "nervos_dao_withdrawing",
capacity: (10**8) * 1000,
data: CKB::Utils.bin_to_hex("\x02" * 8),
data: CKB::Utils.bin_to_hex([block.number].pack('Q<')),
lock_script_id: lock.id)

cell_output2 = create(:cell_output, ckb_transaction: ckb_transaction2,
cell_index: 1,
tx_hash: "0x398315db9c7ba144cca74d2e9122ac9b3a3da1641b2975ae321d91ec34f1c0e2",
Expand Down Expand Up @@ -866,6 +876,7 @@ def fake_dao_interest_transaction(node_block)
tx_hash: "0x498315db9c7ba144cca74d2e9122ac9b3a3da1641b2975ae321d91ec34f1c0e3",
index: 1,
})

create(:cell_input, block: ckb_transaction1.block,
ckb_transaction: ckb_transaction1,
previous_output: {
Expand Down
3 changes: 3 additions & 0 deletions test/models/ckb_sync/node_data_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2792,6 +2792,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
cell_index: 0,
address: input_address1,
cell_type: "nervos_dao_withdrawing",
data: CKB::Utils.bin_to_hex([deposit_block.number].pack('Q<')),
dao: "0x28ef3c7ff3860700d88b1a61958923008ae424cd7200000000e3bad4847a0100", lock_script_id: lock1.id, occupied_capacity: 61 * (10**8))
create(:cell_output, ckb_transaction: tx2,
block: block2,
Expand All @@ -2800,6 +2801,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
cell_index: 1,
address: input_address2,
cell_type: "nervos_dao_withdrawing",
data: CKB::Utils.bin_to_hex([deposit_block.number].pack('Q<')),
dao: "0x2cd631702e870700b3df08d7d889230036f787487e00000000e3bad4847a0100", lock_script_id: lock2.id, occupied_capacity: 61 * (10**8))

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

# udt cell
Expand Down
4 changes: 4 additions & 0 deletions test/models/ckb_transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ class CkbTransactionTest < ActiveSupport::TestCase
previous_cell_output_id: nervos_dao_deposit_cell.id)
started_block = Block.select(:number,
:timestamp).find(nervos_dao_deposit_cell.block_id)

nervos_dao_withdrawing_cell.data = CKB::Utils.bin_to_hex([started_block.number].pack('Q<'))

interest = DaoCompensationCalculator.new(deposit_cell,
nervos_dao_withdrawing_cell.block.dao).call
# binding.pry
Expand Down Expand Up @@ -260,6 +263,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
data: "0x7512000000000000",
tx_hash: "0xf9aca16b49c7d037920ad9e5aecdac272412a5fbe0396f7d95b112bf790dd39f",
cell_index: 0,
data: CKB::Utils.bin_to_hex([block.number].pack('Q<')),
cell_type: "nervos_dao_withdrawing")
started_block = Block.select(:number,
:timestamp).find(ckb_transaction.block_id)
Expand Down
Loading