Skip to content

Commit

Permalink
feat: /block_info rpc returns actual block reward
Browse files Browse the repository at this point in the history
Enables BlockInfo to provide both an expected coinbase amount based on
block-height and the actual amount that miner chose (which may be less
than expected amount).

* adds Block::coinbase_amount() that returns actual miner rewards
* replaces BlockInfo::mining_rewards with ::coinbase_amount
* adds BlockInfo::expected_coinbase_amount()
  • Loading branch information
dan-da committed Dec 20, 2024
1 parent edbff18 commit 5d4de11
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/models/blockchain/block/block_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct BlockInfo {
pub difficulty: Difficulty,
pub num_inputs: usize,
pub num_outputs: usize,
pub mining_reward: NeptuneCoins,
pub coinbase_amount: NeptuneCoins,
pub fee: NeptuneCoins,
pub is_genesis: bool,
pub is_tip: bool,
Expand All @@ -48,7 +48,7 @@ impl std::fmt::Display for BlockInfo {
+ &format!("difficulty: {}\n", self.difficulty)
+ &format!("num_inputs: {}\n", self.num_inputs)
+ &format!("num_outputs: {}\n", self.num_outputs)
+ &format!("mining_reward: {}\n", self.mining_reward)
+ &format!("coinbase_amount: {}\n", self.coinbase_amount)
+ &format!("fee: {}\n", self.fee)
+ &format!("is_genesis: {}\n", self.is_genesis)
+ &format!("is_tip: {}\n", self.is_tip)
Expand Down Expand Up @@ -83,11 +83,20 @@ impl BlockInfo {
num_inputs: body.transaction_kernel.inputs.len(),
num_outputs: body.transaction_kernel.outputs.len(),
fee: body.transaction_kernel.fee,
mining_reward: crate::Block::block_subsidy(header.height),
coinbase_amount: block.coinbase_amount(),
is_genesis: digest == genesis_digest,
is_tip: digest == tip_digest,
is_canonical,
sibling_blocks,
}
}

/// Returns expected (calculated) coinbase amount for this block's height.
///
/// note that this calculated value may be more than the coinbase_amount
/// field because a miner may choose to reward themself less than the
/// calculated reward amount.
pub fn expected_coinbase_amount(&self) -> NeptuneCoins {
Block::block_subsidy(self.height)
}
}
13 changes: 13 additions & 0 deletions src/models/blockchain/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,19 @@ impl Block {
reward
}

/// returns coinbase reward amount for this block.
///
/// note that this amount may differ from self::block_subsidy(self.height)
/// because a miner can choose to accept less than the calculated reward amount.
pub fn coinbase_amount(&self) -> NeptuneCoins {
// A block must always have a Coinbase.
// we impl this method in part to cement that guarantee.
self.body()
.transaction_kernel
.coinbase
.unwrap_or_else(NeptuneCoins::zero)
}

pub fn genesis_block(network: Network) -> Self {
let premine_distribution = Self::premine_distribution();
let total_premine_amount = premine_distribution
Expand Down

0 comments on commit 5d4de11

Please sign in to comment.