Skip to content

refactor: add cargo fmt check to github CI. #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/workflows/run-tests-on-push-to-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ env:

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Build

- name: Run Format
run: cargo fmt --all -- --check

- name: Run Build
run: cargo build --verbose
- name: Run tests

- name: Run Tests
run: cargo test --verbose
41 changes: 31 additions & 10 deletions common/src/queries/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,38 @@ pub enum PoolsStateQuery {
GetPoolsListWithInfo,
GetPoolsRetiredList,
GetPoolsRetiringList,
GetPoolsActiveStakes { pools_operators: Vec<Vec<u8>>, epoch: u64 },
GetPoolsActiveStakes {
pools_operators: Vec<Vec<u8>>,
epoch: u64,
},
// Get total blocks minted for each vrf vkey hashes (not included current epoch's blocks minted)
GetPoolsTotalBlocksMinted { vrf_key_hashes: Vec<Vec<u8>>},
GetPoolInfo { pool_id: Vec<u8> },
GetPoolHistory { pool_id: Vec<u8> },
GetPoolMetadata { pool_id: Vec<u8> },
GetPoolRelays { pool_id: Vec<u8> },
GetPoolDelegators { pool_id: Vec<u8> },
GetPoolBlocks { pool_id: Vec<u8> },
GetPoolUpdates { pool_id: Vec<u8> },
GetPoolVotes { pool_id: Vec<u8> },
GetPoolsTotalBlocksMinted {
vrf_key_hashes: Vec<Vec<u8>>,
},
GetPoolInfo {
pool_id: Vec<u8>,
},
GetPoolHistory {
pool_id: Vec<u8>,
},
GetPoolMetadata {
pool_id: Vec<u8>,
},
GetPoolRelays {
pool_id: Vec<u8>,
},
GetPoolDelegators {
pool_id: Vec<u8>,
},
GetPoolBlocks {
pool_id: Vec<u8>,
},
GetPoolUpdates {
pool_id: Vec<u8>,
},
GetPoolVotes {
pool_id: Vec<u8>,
},
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion common/src/queries/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;
use anyhow::Result;
use caryatid_sdk::Context;
use std::sync::Arc;

use crate::messages::Message;

Expand Down
2 changes: 1 addition & 1 deletion common/src/rational_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub fn rational_number_from_f32(f: f32) -> Result<RationalNumber> {

#[cfg(test)]
mod tests {
use crate::rational_number::RationalNumber;
use crate::rational_number::rational_number_from_f32;
use crate::rational_number::RationalNumber;

#[test]
fn test_fractions() -> Result<(), anyhow::Error> {
Expand Down
90 changes: 45 additions & 45 deletions modules/accounts_state/src/monetary.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
//! Acropolis AccountsState: monetary (reserves, treasury) calculations

use acropolis_common::{
Lovelace, ShelleyParams,
rational_number::RationalNumber,
};
use crate::state::Pots;
use anyhow::{Result, anyhow};
use tracing::info;
use bigdecimal::{BigDecimal, ToPrimitive, One};
use acropolis_common::{rational_number::RationalNumber, Lovelace, ShelleyParams};
use anyhow::{anyhow, Result};
use bigdecimal::{BigDecimal, One, ToPrimitive};
use std::str::FromStr;
use tracing::info;

/// Result of monetary calculation
#[derive(Debug, Default, Clone)]
pub struct MonetaryResult {

/// Updated pots
pub pots: Pots,

Expand All @@ -23,10 +19,12 @@ pub struct MonetaryResult {

/// Calculate monetary change at the start of an epoch, returning updated pots and total
/// available for stake rewards
pub fn calculate_monetary_change(params: &ShelleyParams,
old_pots: &Pots,
total_fees_last_epoch: Lovelace,
total_non_obft_blocks: usize) -> Result<MonetaryResult> {
pub fn calculate_monetary_change(
params: &ShelleyParams,
old_pots: &Pots,
total_fees_last_epoch: Lovelace,
total_non_obft_blocks: usize,
) -> Result<MonetaryResult> {
let mut new_pots = old_pots.clone();

// Add fees to reserves to start with - they will get allocated to treasury and stake
Expand All @@ -44,14 +42,12 @@ pub fn calculate_monetary_change(params: &ShelleyParams,
// Top-slice some for treasury
let treasury_cut = RationalNumber::new(2, 10);
// TODO odd values again! &params.protocol_params.treasury_cut; // Tau
let treasury_increase = (&total_reward_pot
* BigDecimal::from(treasury_cut.numer())
/ BigDecimal::from(treasury_cut.denom()))
.with_scale(0);
let treasury_increase = (&total_reward_pot * BigDecimal::from(treasury_cut.numer())
/ BigDecimal::from(treasury_cut.denom()))
.with_scale(0);

let treasury_increase_u64 = treasury_increase
.to_u64()
.ok_or(anyhow!("Can't calculate integral treasury cut"))?;
let treasury_increase_u64 =
treasury_increase.to_u64().ok_or(anyhow!("Can't calculate integral treasury cut"))?;

new_pots.treasury += treasury_increase_u64;
new_pots.reserves -= treasury_increase_u64;
Expand All @@ -74,29 +70,32 @@ fn calculate_eta(params: &ShelleyParams, total_non_obft_blocks: usize) -> Result
let active_slots_coeff = BigDecimal::from_str(&params.active_slots_coeff.to_string())?;
let epoch_length = BigDecimal::from(params.epoch_length);

let eta = if decentralisation >= &RationalNumber::new(8,10) {
let eta = if decentralisation >= &RationalNumber::new(8, 10) {
BigDecimal::one()
} else {
let expected_blocks = epoch_length * active_slots_coeff *
(BigDecimal::one() - BigDecimal::from(decentralisation.numer())
/ BigDecimal::from(decentralisation.denom()));
let expected_blocks = epoch_length
* active_slots_coeff
* (BigDecimal::one()
- BigDecimal::from(decentralisation.numer())
/ BigDecimal::from(decentralisation.denom()));

(BigDecimal::from(total_non_obft_blocks as u64) / expected_blocks)
.min(BigDecimal::one())
(BigDecimal::from(total_non_obft_blocks as u64) / expected_blocks).min(BigDecimal::one())
};

Ok(eta)
}

// Calculate monetary expansion based on current reserves
fn calculate_monetary_expansion(_params: &ShelleyParams, reserves: Lovelace, eta: &BigDecimal)
-> BigDecimal {
fn calculate_monetary_expansion(
_params: &ShelleyParams,
reserves: Lovelace,
eta: &BigDecimal,
) -> BigDecimal {
let monetary_expansion_factor = RationalNumber::new(3, 1000);
// TODO odd values coming in! &params.protocol_params.monetary_expansion; // Rho
let monetary_expansion = (BigDecimal::from(reserves)
* eta
* BigDecimal::from(monetary_expansion_factor.numer())
/ BigDecimal::from(monetary_expansion_factor.denom()))
let monetary_expansion =
(BigDecimal::from(reserves) * eta * BigDecimal::from(monetary_expansion_factor.numer())
/ BigDecimal::from(monetary_expansion_factor.denom()))
.with_scale(0);

info!(eta=%eta, rho=%monetary_expansion_factor, %monetary_expansion, "Monetary:");
Expand All @@ -108,7 +107,9 @@ fn calculate_monetary_expansion(_params: &ShelleyParams, reserves: Lovelace, eta
#[cfg(test)]
mod tests {
use super::*;
use acropolis_common::{ NetworkId, Nonce, NonceVariant, ProtocolVersion, ShelleyProtocolParams };
use acropolis_common::{
NetworkId, Nonce, NonceVariant, ProtocolVersion, ShelleyProtocolParams,
};
use chrono::{DateTime, Utc};

// Known values at start of Shelley - from Java reference and DBSync
Expand All @@ -122,7 +123,7 @@ mod tests {

const EPOCH_210_RESERVES: Lovelace = 13_278_197_552_770_393;
const EPOCH_210_TREASURY: Lovelace = 16_306_644_182_013;
const EPOCH_210_REFUNDS_TO_TREASURY: Lovelace = 500_000_000; // 1 SPO with unknown reward
const EPOCH_210_REFUNDS_TO_TREASURY: Lovelace = 500_000_000; // 1 SPO with unknown reward

const EPOCH_211_RESERVES: Lovelace = 13_270_236_767_315_870;
const EPOCH_211_TREASURY: Lovelace = 24_275_595_982_960;
Expand All @@ -136,10 +137,7 @@ mod tests {
network_id: NetworkId::Mainnet,
network_magic: 76482407,
protocol_params: ShelleyProtocolParams {
protocol_version: ProtocolVersion {
major: 2,
minor: 0,
},
protocol_version: ProtocolVersion { major: 2, minor: 0 },
max_tx_size: 16384,
max_block_body_size: 65536,
max_block_header_size: 1100,
Expand All @@ -153,12 +151,12 @@ mod tests {
pool_retire_max_epoch: 18,
extra_entropy: Nonce {
tag: NonceVariant::NeutralNonce,
hash: None
hash: None,
},
decentralisation_param: RationalNumber::new(1, 1),
monetary_expansion: RationalNumber::new(3, 1000),
treasury_cut: RationalNumber::new(2, 10),
pool_pledge_influence: RationalNumber::new(3, 10)
pool_pledge_influence: RationalNumber::new(3, 10),
},
security_param: 2160,
slot_length: 1,
Expand All @@ -170,7 +168,6 @@ mod tests {

#[test]
fn epoch_208_monetary_change() {

let params = shelley_params();
let pots = Pots {
reserves: EPOCH_208_RESERVES,
Expand All @@ -182,14 +179,16 @@ mod tests {
let result = calculate_monetary_change(&params, &pots, 0, 0).unwrap();

// Epoch 209 reserves - all goes to treasury
assert_eq!(result.pots.reserves, EPOCH_208_RESERVES - EPOCH_209_TREASURY);
assert_eq!(
result.pots.reserves,
EPOCH_208_RESERVES - EPOCH_209_TREASURY
);
assert_eq!(result.pots.reserves - EPOCH_208_MIRS, EPOCH_209_RESERVES);
assert_eq!(result.pots.treasury, EPOCH_209_TREASURY);
}

#[test]
fn epoch_209_monetary_change() {

let params = shelley_params();
let pots = Pots {
reserves: EPOCH_209_RESERVES,
Expand All @@ -207,7 +206,6 @@ mod tests {

#[test]
fn epoch_210_monetary_change() {

let params = shelley_params();
let pots = Pots {
reserves: EPOCH_210_RESERVES,
Expand All @@ -220,7 +218,9 @@ mod tests {

// Epoch 211 reserves
assert_eq!(result.pots.reserves, EPOCH_211_RESERVES);
assert_eq!(result.pots.treasury + EPOCH_210_REFUNDS_TO_TREASURY, EPOCH_211_TREASURY);
assert_eq!(
result.pots.treasury + EPOCH_210_REFUNDS_TO_TREASURY,
EPOCH_211_TREASURY
);
}

}
4 changes: 1 addition & 3 deletions modules/accounts_state/src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use tokio::sync::Mutex;

use crate::state::State;
use acropolis_common::state_history::StateHistory;
use acropolis_common::{
messages::RESTResponse, DelegatedStake, Lovelace,
};
use acropolis_common::{messages::RESTResponse, DelegatedStake, Lovelace};

/// REST response structure for /accounts/{stake_address}
#[derive(serde::Serialize)]
Expand Down
Loading