Skip to content

Commit

Permalink
feat: add occupied_capacity to ckb_transaction's display_outputs and …
Browse files Browse the repository at this point in the history
…inputs

Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid committed Oct 19, 2023
1 parent e6e1c4a commit e2797f5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
6 changes: 5 additions & 1 deletion app/models/ckb_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ def normal_tx_display_outputs(previews)
display_output = {
id: output.id,
capacity: output.capacity,
occupied_capacity: output.occupied_capacity,
address_hash: output.address_hash,
status: output.status,
consumed_tx_hash: consumed_tx_hash,
Expand Down Expand Up @@ -299,7 +300,7 @@ def cellbase_display_outputs
cellbase = Cellbase.new(block)
cell_outputs_for_display.map do |output|
consumed_tx_hash = output.live? ? nil : output.consumed_by.tx_hash
CkbUtils.hash_value_to_s(id: output.id, capacity: output.capacity, address_hash: output.address_hash,
CkbUtils.hash_value_to_s(id: output.id, capacity: output.capacity, occupied_capacity: output.occupied_capacity, address_hash: output.address_hash,
target_block_number: cellbase.target_block_number, base_reward: cellbase.base_reward, commit_reward: cellbase.commit_reward, proposal_reward: cellbase.proposal_reward, secondary_reward: cellbase.secondary_reward, status: output.status, consumed_tx_hash: consumed_tx_hash)
end
end
Expand All @@ -315,6 +316,7 @@ def normal_tx_display_inputs(previews)
next({
from_cellbase: false,
capacity: "",
occupied_capacity: "",
address_hash: "",
generated_tx_hash: cell_input.previous_tx_hash,
cell_index: cell_input.previous_index,
Expand All @@ -329,6 +331,7 @@ def normal_tx_display_inputs(previews)
id: previous_cell_output.id,
from_cellbase: false,
capacity: previous_cell_output.capacity,
occupied_capacity: previous_cell_output.occupied_capacity,
address_hash: previous_cell_output.address_hash,
generated_tx_hash: previous_cell_output.ckb_transaction.tx_hash,
cell_index: previous_cell_output.cell_index,
Expand Down Expand Up @@ -420,6 +423,7 @@ def cellbase_display_inputs
id: nil,
from_cellbase: true,
capacity: nil,
occupied_capacity: nil,
address_hash: nil,
target_block_number: cellbase.target_block_number,
generated_tx_hash: tx_hash
Expand Down
9 changes: 4 additions & 5 deletions test/models/ckb_sync/node_data_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
assert_no_changes -> { tx.reload.tx_status } do
node_data_processor.process_block(node_block)
end

end
end

Expand Down Expand Up @@ -1313,7 +1312,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
block = Block.last
cellbase = Cellbase.new(block)
expected_cellbase_display_inputs = [
CkbUtils.hash_value_to_s(id: nil, from_cellbase: true, capacity: nil,
CkbUtils.hash_value_to_s(id: nil, from_cellbase: true, capacity: nil, occupied_capacity: nil,
address_hash: nil, target_block_number: cellbase.target_block_number, generated_tx_hash: block.cellbase.tx_hash)
]

Expand Down Expand Up @@ -1351,7 +1350,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
output = local_ckb_transactions.first.outputs.order(:id).first
cellbase = Cellbase.new(local_block)
expected_display_outputs = [
CkbUtils.hash_value_to_s(id: output.id, capacity: output.capacity,
CkbUtils.hash_value_to_s(id: output.id, capacity: output.capacity, occupied_capacity: output.occupied_capacity,
address_hash: output.address_hash, target_block_number: cellbase.target_block_number, base_reward: cellbase.base_reward, commit_reward: cellbase.commit_reward, proposal_reward: cellbase.proposal_reward, secondary_reward: cellbase.secondary_reward, status: "live", consumed_tx_hash: nil)
]

Expand Down Expand Up @@ -1394,7 +1393,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
expected_cellbase_display_outputs =
block.cellbase.cell_outputs.order(:id).map do |cell_output|
consumed_tx_hash = cell_output.live? ? nil : cell_output.consumed_by.tx_hash
CkbUtils.hash_value_to_s(id: cell_output.id, capacity: cell_output.capacity,
CkbUtils.hash_value_to_s(id: cell_output.id, capacity: cell_output.capacity, occupied_capacity: cell_output.occupied_capacity,
address_hash: cell_output.address_hash, target_block_number: cellbase.target_block_number, base_reward: cellbase.base_reward, commit_reward: cellbase.commit_reward, proposal_reward: cellbase.proposal_reward, secondary_reward: cellbase.secondary_reward, status: cell_output.status, consumed_tx_hash: consumed_tx_hash)
end
assert_equal expected_cellbase_display_outputs, block.cellbase.display_outputs
Expand Down Expand Up @@ -1438,7 +1437,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
cell_output = block.cellbase.cell_outputs.first
expected_cellbase_display_outputs = [
CkbUtils.hash_value_to_s(id: cell_output.id,
capacity: cell_output.capacity, address_hash: cell_output.address_hash, target_block_number: cellbase.target_block_number, base_reward: cellbase.base_reward, commit_reward: cellbase.commit_reward, proposal_reward: cellbase.proposal_reward, secondary_reward: cellbase.secondary_reward, status: "live", consumed_tx_hash: nil)
capacity: cell_output.capacity, occupied_capacity: cell_output.occupied_capacity, address_hash: cell_output.address_hash, target_block_number: cellbase.target_block_number, base_reward: cellbase.base_reward, commit_reward: cellbase.commit_reward, proposal_reward: cellbase.proposal_reward, secondary_reward: cellbase.secondary_reward, status: "live", consumed_tx_hash: nil)
]

assert_equal expected_cellbase_display_outputs, block.cellbase.display_outputs
Expand Down
26 changes: 18 additions & 8 deletions test/models/ckb_transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
ckb_transaction = create(:ckb_transaction,
:with_multiple_inputs_and_outputs)
expected_attributes = %i(
id from_cellbase capacity address_hash
id from_cellbase capacity occupied_capacity address_hash
generated_tx_hash cell_type cell_index since
).sort

Expand All @@ -91,7 +91,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
ckb_transaction = create(:ckb_transaction, :with_single_output,
is_cellbase: true)
expected_attributes = %i(
id from_cellbase capacity address_hash
id from_cellbase capacity occupied_capacity address_hash
target_block_number generated_tx_hash
)

Expand All @@ -103,7 +103,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
ckb_transaction = create(:ckb_transaction,
:with_multiple_inputs_and_outputs)
expected_attributes = %i(
id capacity address_hash status consumed_tx_hash
id capacity occupied_capacity address_hash status consumed_tx_hash
cell_type
).sort

Expand All @@ -117,7 +117,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
ckb_transaction = create(:ckb_transaction, :with_single_output,
is_cellbase: true, block: block)
expected_attributes = %i(
id capacity address_hash target_block_number
id capacity occupied_capacity address_hash target_block_number
base_reward commit_reward proposal_reward secondary_reward status consumed_tx_hash
)

Expand Down Expand Up @@ -201,6 +201,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
id: nervos_dao_withdrawing_cell.id,
from_cellbase: false,
capacity: nervos_dao_withdrawing_cell.capacity,
occupied_capacity: nervos_dao_withdrawing_cell.occupied_capacity,
address_hash: nervos_dao_withdrawing_cell.address_hash,
generated_tx_hash: nervos_dao_withdrawing_cell.ckb_transaction.tx_hash,
compensation_started_block_number: started_block.number,
Expand All @@ -218,7 +219,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
}
).sort
expected_attributes = %i(
id from_cellbase capacity address_hash generated_tx_hash compensation_started_block_number
id from_cellbase capacity occupied_capacity address_hash generated_tx_hash compensation_started_block_number
compensation_ended_block_number compensation_started_timestamp compensation_ended_timestamp
interest cell_type cell_index since locked_until_block_number locked_until_block_timestamp
).sort
Expand Down Expand Up @@ -269,6 +270,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
id: deposit_output_cell.id,
from_cellbase: false,
capacity: deposit_output_cell.capacity,
occupied_capacity: deposit_output_cell.occupied_capacity,
address_hash: deposit_output_cell.address_hash,
generated_tx_hash: deposit_output_cell.ckb_transaction.tx_hash,
compensation_started_block_number: started_block.number,
Expand All @@ -284,7 +286,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
}
).sort
expected_attributes = %i(
id from_cellbase capacity address_hash generated_tx_hash interest cell_type
id from_cellbase capacity occupied_capacity address_hash generated_tx_hash interest cell_type
compensation_ended_block_number compensation_ended_timestamp compensation_started_block_number
compensation_started_timestamp cell_index since
).sort
Expand All @@ -301,12 +303,12 @@ class CkbTransactionTest < ActiveSupport::TestCase
dao_output = ckb_transaction.outputs.first
dao_output.update(cell_type: "nervos_dao_withdrawing")
expected_attributes = %i(
id capacity address_hash status consumed_tx_hash
id capacity occupied_capacity address_hash status consumed_tx_hash
cell_type
).sort
consumed_tx_hash = dao_output.live? ? nil : dao_output.consumed_by.tx_hash
expected_display_output = CkbUtils.hash_value_to_s(id: dao_output.id,
capacity: dao_output.capacity, address_hash: dao_output.address_hash, status: dao_output.status, consumed_tx_hash: consumed_tx_hash, cell_type: dao_output.cell_type).sort
capacity: dao_output.capacity, occupied_capacity: dao_output.occupied_capacity, address_hash: dao_output.address_hash, status: dao_output.status, consumed_tx_hash: consumed_tx_hash, cell_type: dao_output.cell_type).sort
display_outputs = ckb_transaction.display_outputs
assert_equal expected_attributes - display_outputs.first.keys, []
assert_equal expected_display_output, display_outputs.first.sort
Expand Down Expand Up @@ -343,6 +345,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
id: udt_cell_output.id,
from_cellbase: false,
capacity: udt_cell_output.capacity,
occupied_capacity: udt_cell_output.occupied_capacity,
address_hash: udt_cell_output.address_hash,
generated_tx_hash: udt_cell_output.ckb_transaction.tx_hash,
cell_index: udt_cell_output.cell_index,
Expand Down Expand Up @@ -383,6 +386,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
expected_display_input = CkbUtils.hash_value_to_s(
id: udt_cell_output.id,
capacity: udt_cell_output.capacity,
occupied_capacity: udt_cell_output.occupied_capacity,
address_hash: udt_cell_output.address_hash,
status: udt_cell_output.status,
consumed_tx_hash: nil,
Expand Down Expand Up @@ -423,6 +427,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
id: m_nft_cell_output.id,
from_cellbase: false,
capacity: m_nft_cell_output.capacity,
occupied_capacity: m_nft_cell_output.occupied_capacity,
address_hash: m_nft_cell_output.address_hash,
generated_tx_hash: m_nft_cell_output.ckb_transaction.tx_hash,
cell_index: m_nft_cell_output.cell_index,
Expand Down Expand Up @@ -465,6 +470,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
id: m_nft_cell_output.id,
from_cellbase: false,
capacity: m_nft_cell_output.capacity,
occupied_capacity: m_nft_cell_output.occupied_capacity,
address_hash: m_nft_cell_output.address_hash,
generated_tx_hash: m_nft_cell_output.ckb_transaction.tx_hash,
cell_index: m_nft_cell_output.cell_index,
Expand Down Expand Up @@ -525,6 +531,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
id: m_nft_cell_output.id,
from_cellbase: false,
capacity: m_nft_cell_output.capacity,
occupied_capacity: m_nft_cell_output.occupied_capacity,
address_hash: m_nft_cell_output.address_hash,
generated_tx_hash: m_nft_cell_output.ckb_transaction.tx_hash,
cell_index: m_nft_cell_output.cell_index,
Expand Down Expand Up @@ -558,6 +565,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
expected_display_output = CkbUtils.hash_value_to_s(
id: m_nft_cell_output.id,
capacity: m_nft_cell_output.capacity,
occupied_capacity: m_nft_cell_output.occupied_capacity,
address_hash: m_nft_cell_output.address_hash,
status: m_nft_cell_output.status,
consumed_tx_hash: nil,
Expand Down Expand Up @@ -586,6 +594,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
expected_display_output = CkbUtils.hash_value_to_s(
id: m_nft_cell_output.id,
capacity: m_nft_cell_output.capacity,
occupied_capacity: m_nft_cell_output.occupied_capacity,
address_hash: m_nft_cell_output.address_hash,
status: m_nft_cell_output.status,
consumed_tx_hash: nil,
Expand Down Expand Up @@ -639,6 +648,7 @@ class CkbTransactionTest < ActiveSupport::TestCase
expected_display_output = CkbUtils.hash_value_to_s(
id: m_nft_cell_output.id,
capacity: m_nft_cell_output.capacity,
occupied_capacity: m_nft_cell_output.occupied_capacity,
address_hash: m_nft_cell_output.address_hash,
status: m_nft_cell_output.status,
consumed_tx_hash: nil,
Expand Down

0 comments on commit e2797f5

Please sign in to comment.