Rewards
Rewards Blockchain Pallet
Overview
The rewards pallet is responsible for the logic of parameterizing and depositing block rewards to validators and the stake rewards vault. The stake rewards vault is a storage value of the rewards balance that is rewarded to peers on each epoch.
Terminology
IncreaseStakeVault: The function to set and increase the balance of the stake vault in the Network Pallet.
HalvingInterval: A constant of how many blocks are in each halving event.
InitialBlockSubsidy: The initial reward per block which the halvening interval equates from.
FindAuthor: A trait for finding the author of a block header based on the PreRuntime digests contained within it.
Annotation
The following is a near replication of the Bitcoin block subsidy mechanics.
Given:
block_number
: The block height for which we want to calculate the subsidy.T::HalvingInterval::get()
: The halving interval retrieved from the runtime configuration.T::InitialBlockSubsidy::get()
: The initial block subsidy retrieved from the runtime configuration.BalanceOf<T>
: The balance type for the runtime moduleT
.
Retrieve the halving interval from the runtime configuration
halving_interval: u32 = T::HalvingInterval::get()
Convert the block number to a
u64
valueblock_num_as_u64: u64 = TryInto::try_into(block_number).ok().expect("fn get_block_subsidy block_num_as_u64 Err.");
Calculate the number of halvings that have occurred by dividing the block number by the halving interval
halvings: u64 = block_num_as_u64 / halving_interval as u64
If the number of halvings exceeds or equals 64, return zero subsidy
if halvings >= 64 { return (0 as u128).saturated_into::<BalanceOf<T>>(); }
Otherwise, calculate the block subsidy:
a. Retrieve the initial block subsidy from the runtime configuration
initial_block_subsidy: u128 = T::InitialBlockSubsidy::get();
b. Right shift the initial subsidy based on the number of halvings
initial_block_subsidy >>= halvings;
c. Convert the calculated block subsidy to BalanceOf
block_subsidy: BalanceOf = initial_block_subsidy.saturated_into::<BalanceOf>();
Return the calculated block subsidy
block_subsidy