From b991b0ec03527266750c95277f10dade773f7aec Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Wed, 17 Jul 2024 23:01:22 +0800 Subject: [PATCH] feat: cal occupied capacity before cell_output insert Signed-off-by: Miles Zhang --- .../ckb_sync/new_node_data_processor.rb | 2 +- app/utils/ckb_utils.rb | 8 +++++++ .../generate_statistics_data_worker.rb | 21 ------------------- test/utils/ckb_utils_test.rb | 12 +++++++++++ 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/app/models/ckb_sync/new_node_data_processor.rb b/app/models/ckb_sync/new_node_data_processor.rb index 84c6fb6401..0deb67c2f4 100644 --- a/app/models/ckb_sync/new_node_data_processor.rb +++ b/app/models/ckb_sync/new_node_data_processor.rb @@ -1146,7 +1146,7 @@ def cell_output_attributes(output, address, ckb_transaction, local_block, cell_i attrs = { ckb_transaction_id: ckb_transaction["id"], capacity: output.capacity, - occupied_capacity: 0, + occupied_capacity: CkbUtils.cal_cell_min_capacity(lock_script, type_script, output.capacity, binary_data), address_id: address.id, block_id: local_block.id, tx_hash: ckb_transaction["tx_hash"], diff --git a/app/utils/ckb_utils.rb b/app/utils/ckb_utils.rb index fe547c4af9..38c555b08d 100644 --- a/app/utils/ckb_utils.rb +++ b/app/utils/ckb_utils.rb @@ -11,6 +11,14 @@ def self.calculate_cell_min_capacity(output, data) output.calculate_min_capacity(data) end + def self.cal_cell_min_capacity(lock_script, type_script, capacity, binary_data) + lock = CKB::Types::Script.new(**lock_script.to_node) + type = type_script.present? ? CKB::Types::Script.new(**type_script.to_node) : nil + CKB::Types::Output.new(capacity: capacity.to_i, lock:, type:) + CKB::Utils.byte_to_shannon([8, binary_data&.bytesize || 0, lock_script.calculate_bytesize, + type_script&.calculate_bytesize || 0].sum) + end + def self.block_cell_consumed(transactions) transactions.reduce(0) do |memo, transaction| outputs_data = transaction.outputs_data diff --git a/app/workers/generate_statistics_data_worker.rb b/app/workers/generate_statistics_data_worker.rb index 2442793dfc..e11a9c7b00 100644 --- a/app/workers/generate_statistics_data_worker.rb +++ b/app/workers/generate_statistics_data_worker.rb @@ -14,26 +14,5 @@ def perform(block_id) if epoch_stats && epoch_stats.largest_block_size.to_i < block_size epoch_stats.update(largest_block_size: block_size, largest_block_number: block.number) end - - cell_outputs = block.cell_outputs.includes(:cell_datum) - cell_outputs_attributes = [] - cell_outputs.each do |cell_output| - data_size = - if cell_output.data != "0x" - CKB::Utils.hex_to_bin(cell_output.data).bytesize - else - 0 - end - - cell_outputs_attributes << { - tx_hash: cell_output.tx_hash, - cell_index: cell_output.cell_index, - status: cell_output.status, - data_size:, - occupied_capacity: CkbUtils.calculate_cell_min_capacity(cell_output.node_output, cell_output.data), - } - end - - CellOutput.upsert_all(cell_outputs_attributes, unique_by: %i[tx_hash cell_index status], record_timestamps: true) if cell_outputs_attributes.present? end end diff --git a/test/utils/ckb_utils_test.rb b/test/utils/ckb_utils_test.rb index c6ea21a42c..ac5e2069d7 100644 --- a/test/utils/ckb_utils_test.rb +++ b/test/utils/ckb_utils_test.rb @@ -172,6 +172,7 @@ class CkbUtilsTest < ActiveSupport::TestCase end test ".base_reward should return 0 for genesis block" do + CkbUtils.stubs(:cal_cell_min_capacity).returns(1000000) VCR.use_cassette("genesis_block", record: :new_episodes) do node_block = CkbSync::Api.instance.get_block_by_number(0) @@ -191,6 +192,7 @@ class CkbUtilsTest < ActiveSupport::TestCase end test ".calculate_cell_min_capacity should return output's min capacity" do + CkbUtils.stubs(:cal_cell_min_capacity).returns(1000000) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do node_block = CkbSync::Api.instance.get_block_by_number(DEFAULT_NODE_BLOCK_NUMBER) create(:block, :with_block_hash, number: node_block.header.number - 1) @@ -205,7 +207,15 @@ class CkbUtilsTest < ActiveSupport::TestCase end end + test ".cal_cell_min_capacity should return output's min capacity" do + lock_script = create(:lock_script, code_hash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", hash_type: "type", args: "0x6a21bc1b72d1e654f8e2ded400cffa46075494c6") + type_script = create(:type_script, code_hash: "0x554cff969f3148e3c620749384004e9692e67c429f621554d139b505a281c7b8", hash_type: "type", args: "0x01") + assert_equal 9700000000, + CkbUtils.cal_cell_min_capacity(lock_script, type_script, 0.4e11, "\n\f") + end + test ".block_cell_consumed generated block's cell_consumed should equal to the sum of transactions output occupied capacity" do + CkbUtils.stubs(:cal_cell_min_capacity).returns(1000000) CkbSync::Api.any_instance.stubs(:get_block_cycles).returns( [ "0x100", "0x200", "0x300", "0x400", "0x500", "0x600", "0x700", "0x800", "0x900" @@ -227,6 +237,7 @@ class CkbUtilsTest < ActiveSupport::TestCase end test ".address_cell_consumed should return right cell consumed by the address" do + CkbUtils.stubs(:cal_cell_min_capacity).returns(1000000) prepare_node_data(12) VCR.use_cassette("blocks/12") do node_block = CkbSync::Api.instance.get_block_by_number(13) @@ -246,6 +257,7 @@ class CkbUtilsTest < ActiveSupport::TestCase end test ".ckb_transaction_fee should return right tx_fee when tx is not dao withdraw tx" do + CkbUtils.stubs(:cal_cell_min_capacity).returns(1000000) node_block = fake_node_block("0x3307186493c5da8b91917924253a5ffd35231151649d0c7e2941aa8801815063") create(:block, :with_block_hash, number: node_block.header.number - 1) VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do