Skip to content

Commit

Permalink
Merge pull request #1510 from nervosnetwork/develop
Browse files Browse the repository at this point in the history
Deploy to testnet
  • Loading branch information
zmcNotafraid authored Nov 20, 2023
2 parents 8a85d88 + 26aaf01 commit dacdb42
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
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

0 comments on commit dacdb42

Please sign in to comment.