Skip to content

Commit

Permalink
feat: cal occupied capacity before cell_output insert
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid committed Jul 17, 2024
1 parent 6a65b3b commit 5b493d1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
6 changes: 3 additions & 3 deletions app/models/ckb_sync/new_node_data_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -911,13 +911,13 @@ def prepare_script_ids(outputs)
local_cache.fetch("NodeData/LockScript/#{output.lock.code_hash}-#{output.lock.hash_type}-#{output.lock.args}") do
# TODO use LockScript.where(script_hash: output.lock.compute_hash).select(:id)&.first replace search by code_hash, hash_type and args query after script_hash has been filled
LockScript.where(code_hash: output.lock.code_hash, hash_type: output.lock.hash_type,
args: output.lock.args).select(:id).take!
args: output.lock.args).take!
end
if output.type.present?
local_cache.fetch("NodeData/TypeScript/#{output.type.code_hash}-#{output.type.hash_type}-#{output.type.args}") do
# TODO use TypeScript.where(script_hash: output.type.compute_hash).select(:id)&.first replace search by code_hash, hash_type and args query after script_hash has been filled
TypeScript.where(code_hash: output.type.code_hash, hash_type: output.type.hash_type,
args: output.type.args).select(:id).take!
args: output.type.args).take!
end
end
end
Expand Down Expand Up @@ -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"],
Expand Down
8 changes: 8 additions & 0 deletions app/utils/ckb_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 0 additions & 21 deletions app/workers/generate_statistics_data_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions test/utils/ckb_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 5b493d1

Please sign in to comment.