Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy to testnet #1510

Merged
merged 1 commit into from
Nov 20, 2023
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
11 changes: 9 additions & 2 deletions app/utils/ckb_utils.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class CkbUtils
# The block reward halves approximately every 4 years, one epoch is about 4 hours
HALVING_EPOCH = 4 * 365 * 24 / 4

def self.int_to_hex(i)
"0x#{i.to_s(16)}"
end
Expand Down Expand Up @@ -93,7 +96,7 @@ def self.base_reward(block_number, epoch_number)

epoch_info = get_epoch_info(epoch_number)
start_number = epoch_info.start_number.to_i
epoch_reward = Settings.default_epoch_reward.to_i
epoch_reward = epoch_reward_with_halving(epoch_number)
base_reward = epoch_reward / epoch_info.length.to_i
remainder_reward = epoch_reward % epoch_info.length.to_i
if block_number.to_i >= start_number && block_number.to_i < start_number + remainder_reward
Expand All @@ -103,6 +106,10 @@ def self.base_reward(block_number, epoch_number)
end
end

def self.epoch_reward_with_halving(epoch_number)
Settings.default_epoch_reward.to_i >> epoch_number / HALVING_EPOCH
end

def self.primary_reward(block_number, block_economic_state)
block_number != 0 ? block_economic_state.miner_reward.primary : 0
end
Expand Down Expand Up @@ -560,7 +567,7 @@ def self.parse_spore_cluster_data(hex_data)
description_offset = [data.slice(16, 8)].pack("H*").unpack1("l") * 2
name = [data.slice(name_offset + 8..description_offset - 1)].pack("H*")
description = [data.slice(description_offset + 8..-1)].pack("H*")
name = "#{name[0,97]}..." if name.length > 100
name = "#{name[0, 97]}..." if name.length > 100
{ name: name, description: description }
rescue => _e
{ name: nil, description: nil }
Expand Down
4 changes: 2 additions & 2 deletions test/models/ckb_sync/node_data_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase
node_block.transactions.first
local_block = node_data_processor.process_block(node_block)

assert_equal CkbUtils.base_reward(node_block.header.number, node_block.header.epoch), local_block.reward
assert_equal CkbUtils.base_reward(node_block.header.number, CkbUtils.parse_epoch_info(node_block.header).number), local_block.reward
end
end

Expand All @@ -223,7 +223,7 @@ class NodeDataProcessorTest < ActiveSupport::TestCase

local_block = node_data_processor.process_block(node_block)

assert_equal CkbUtils.base_reward(node_block.header.number, node_block.header.epoch), local_block.primary_reward
assert_equal CkbUtils.base_reward(node_block.header.number, CkbUtils.parse_epoch_info(node_block.header).number), local_block.primary_reward
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/utils/ckb_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ class CkbUtilsTest < ActiveSupport::TestCase
end
end

test ".epoch_reward_with_halving should changed after halving" do
assert_equal 95890410958904,
CkbUtils.epoch_reward_with_halving(8760)
assert_equal 95890410958904,
CkbUtils.epoch_reward_with_halving(8761)
assert_equal 47945205479452,
CkbUtils.epoch_reward_with_halving(17520)
end

test ".calculate_cell_min_capacity should return output's min capacity" do
VCR.use_cassette("blocks/#{DEFAULT_NODE_BLOCK_NUMBER}") do
node_block = CkbSync::Api.instance.get_block_by_number(DEFAULT_NODE_BLOCK_NUMBER)
Expand Down
Loading