Skip to content

Commit

Permalink
Fix mainnet block subsidy (#3127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo authored Nov 20, 2024
1 parent 7371e96 commit fcc4595
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
6 changes: 5 additions & 1 deletion lib/ain-ocean/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod transactions;
use defichain_rpc::Client;
use serde::{Deserialize, Serialize};
use serde_json::json;
use stats::subsidy::{BlockSubsidy, BLOCK_SUBSIDY};

use crate::{network::Network, Result, Services};

Expand Down Expand Up @@ -64,6 +65,7 @@ pub struct AppContext {
services: Arc<Services>,
client: Arc<Client>,
network: Network,
pub block_subsidy: BlockSubsidy,
}

// NOTE(canonbrother): manually scratch cors since CorsLayer + Axum can only be supported in `tower-http 0.5`
Expand Down Expand Up @@ -93,10 +95,12 @@ pub async fn ocean_router(
client: Arc<Client>,
network: String,
) -> Result<Router> {
let network = Network::from_str(&network)?;
let context = Arc::new(AppContext {
client,
services: services.clone(),
network: Network::from_str(&network)?,
block_subsidy: BLOCK_SUBSIDY.get(&network).cloned().unwrap_or_default(),
network,
});
let main_router = Router::new()
.nest("/address/", address::router(Arc::clone(&context)))
Expand Down
8 changes: 4 additions & 4 deletions lib/ain-ocean/src/api/stats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use snafu::OptionExt;

use super::{subsidy::BLOCK_SUBSIDY, COIN};
use super::{subsidy::BlockSubsidy, COIN};
use crate::{
api::{
cache::list_pool_pairs_cached,
Expand Down Expand Up @@ -177,9 +177,9 @@ pub struct Emission {
key = "String",
convert = r#"{ format!("emission") }"#
)]
pub fn get_emission(height: u32) -> Result<Emission> {
let subsidy = Decimal::from_u64(BLOCK_SUBSIDY.get_block_subsidy(height))
.context(DecimalConversionSnafu)?;
pub fn get_emission(subsidy: &BlockSubsidy, height: u32) -> Result<Emission> {
let subsidy =
Decimal::from_u64(subsidy.get_block_subsidy(height)).context(DecimalConversionSnafu)?;
let distribution = get_block_reward_distribution(subsidy);

let masternode = distribution.masternode;
Expand Down
9 changes: 4 additions & 5 deletions lib/ain-ocean/src/api/stats/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod cache;
mod distribution;
mod subsidy;
pub mod subsidy;

use std::sync::Arc;

Expand All @@ -21,7 +21,6 @@ use self::{
get_price, get_tvl, Burned, Count, Emission, Loan, Masternodes, Price, Tvl,
},
distribution::get_block_reward_distribution,
subsidy::BLOCK_SUBSIDY,
};
use super::{cache::get_network_info_cached, response::Response, AppContext};
use crate::{
Expand Down Expand Up @@ -82,7 +81,7 @@ async fn get_stats(Extension(ctx): Extension<Arc<AppContext>>) -> Result<Respons
blocks: height,
..get_count(&ctx).await?
},
emission: get_emission(height)?,
emission: get_emission(&ctx.block_subsidy, height)?,
blockchain: Blockchain { difficulty },
loan: get_loan(&ctx.client).await?,
price: get_price(&ctx).await?,
Expand Down Expand Up @@ -116,7 +115,7 @@ async fn get_reward_distribution(
.map(|b| b.height)
.unwrap_or_default(); // Default to genesis block

let subsidy = Decimal::from_u64(BLOCK_SUBSIDY.get_block_subsidy(height))
let subsidy = Decimal::from_u64(ctx.block_subsidy.get_block_subsidy(height))
.context(DecimalConversionSnafu)?;
let distribution = get_block_reward_distribution(subsidy);

Expand Down Expand Up @@ -152,7 +151,7 @@ async fn get_supply(Extension(ctx): Extension<Arc<AppContext>>) -> Result<Respon
.map(|b| b.height)
.unwrap_or_default(); // Default to genesis block

let total = Decimal::from_u64(BLOCK_SUBSIDY.get_supply(height))
let total = Decimal::from_u64(ctx.block_subsidy.get_supply(height))
.context(DecimalConversionSnafu)?
/ Decimal::from(COIN);

Expand Down
16 changes: 14 additions & 2 deletions lib/ain-ocean/src/api/stats/subsidy.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::network::Network;

lazy_static::lazy_static! {
// TODO handle networks
// Global service caching all block subsidy reductions
pub static ref BLOCK_SUBSIDY: BlockSubsidy = BlockSubsidy::new(TEST_NET_COINBASE_SUBSIDY_OPTIONS);
pub static ref BLOCK_SUBSIDY: HashMap<Network, BlockSubsidy> = HashMap::from([
(Network::Mainnet, BlockSubsidy::new(MAIN_NET_COINBASE_SUBSIDY_OPTIONS)),
(Network::Testnet, BlockSubsidy::new(TEST_NET_COINBASE_SUBSIDY_OPTIONS))]);
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
Expand All @@ -17,7 +23,6 @@ pub struct CoinbaseSubsidyOptions {
emission_reduction_interval: u64,
}

#[allow(dead_code)]
pub static MAIN_NET_COINBASE_SUBSIDY_OPTIONS: CoinbaseSubsidyOptions = CoinbaseSubsidyOptions {
eunos_height: 894_000,
genesis_block_subsidy: 59_100_003_000_000_000,
Expand All @@ -38,12 +43,19 @@ pub static TEST_NET_COINBASE_SUBSIDY_OPTIONS: CoinbaseSubsidyOptions = CoinbaseS
emission_reduction_interval: 32690,
};

#[derive(Clone)]
pub struct BlockSubsidy {
reduction_block_subsidies: Vec<u64>,
reduction_supply_milestones: Vec<u64>,
options: CoinbaseSubsidyOptions,
}

impl Default for BlockSubsidy {
fn default() -> Self {
Self::new(MAIN_NET_COINBASE_SUBSIDY_OPTIONS)
}
}

impl BlockSubsidy {
pub fn new(options: CoinbaseSubsidyOptions) -> Self {
let reduction_block_subsidies = Self::compute_block_reduction_subsidies(&options);
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Network {
Mainnet,
Mocknet,
Expand Down

0 comments on commit fcc4595

Please sign in to comment.