From 26aaf010427d9b285bff573af4449a21c41d6406 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 20 Nov 2023 15:03:37 +0800 Subject: [PATCH] feat: re-calculate epoch reward after halving (#1509) Signed-off-by: Miles Zhang --- app/utils/ckb_utils.rb | 11 +++++++++-- test/models/ckb_sync/node_data_processor_test.rb | 4 ++-- test/utils/ckb_utils_test.rb | 9 +++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/utils/ckb_utils.rb b/app/utils/ckb_utils.rb index f0d63ad4d..ca5196f56 100644 --- a/app/utils/ckb_utils.rb +++ b/app/utils/ckb_utils.rb @@ -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 @@ -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 @@ -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 @@ -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 } diff --git a/test/models/ckb_sync/node_data_processor_test.rb b/test/models/ckb_sync/node_data_processor_test.rb index ca61d1a42..0bf616845 100644 --- a/test/models/ckb_sync/node_data_processor_test.rb +++ b/test/models/ckb_sync/node_data_processor_test.rb @@ -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 @@ -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 diff --git a/test/utils/ckb_utils_test.rb b/test/utils/ckb_utils_test.rb index eb8ee8783..251ba8598 100644 --- a/test/utils/ckb_utils_test.rb +++ b/test/utils/ckb_utils_test.rb @@ -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)