Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions controller/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ version.workspace = true
[dependencies]
const-crypto = { workspace = true }
generic-array-struct = { workspace = true }
sanctum-fee-ratio = { workspace = true }
sanctum-u64-ratio = { workspace = true }

[dev-dependencies]
borsh = { workspace = true, features = ["derive", "std"] }
Expand Down
1 change: 1 addition & 0 deletions controller/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod instructions;
pub mod keys;
pub mod pda;
pub mod typedefs;
pub mod yield_release;

keys::id_str!(ID_STR, ID, "5ocnV1qiCgaQR8Jb8xWnVbApfaygJ8tNoZfgPwsgx9kx");
2 changes: 2 additions & 0 deletions controller/core/src/typedefs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod lst_state;
pub mod rps;
pub mod u8bool;
pub mod uq0_63;
85 changes: 85 additions & 0 deletions controller/core/src/typedefs/rps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use core::{error::Error, fmt::Display, ops::Deref};

use crate::typedefs::uq0_63::UQ0_63;

const MIN_RPS_RAW: u64 = 9_223_372;

/// Approx one pico (1 / 1_000_000_000_000)
pub const MIN_RPS: UQ0_63 = unsafe { UQ0_63::new_unchecked(MIN_RPS_RAW) };

/// Proportion of withheld_lamports to release per slot
#[repr(transparent)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Rps(UQ0_63);

impl Rps {
pub const MIN: Self = Self(MIN_RPS);

#[inline]
pub const fn new(raw: UQ0_63) -> Result<Self, RpsTooSmallErr> {
// have to cmp raw values to use primitive const < operator
if *raw.as_raw() < *MIN_RPS.as_raw() {
Err(RpsTooSmallErr { actual: raw })
} else {
Ok(Self(raw))
}
}

#[inline]
pub const fn as_inner(&self) -> &UQ0_63 {
&self.0
}
}

impl Deref for Rps {
type Target = UQ0_63;

#[inline]
fn deref(&self) -> &Self::Target {
self.as_inner()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RpsTooSmallErr {
pub actual: UQ0_63,
}

impl Display for RpsTooSmallErr {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let Self { actual } = self;
f.write_fmt(format_args!("{actual} < {MIN_RPS} (min)"))
}
}

impl Error for RpsTooSmallErr {}

#[cfg(test)]
pub mod test_utils {
use proptest::prelude::*;

use super::*;

pub fn any_rps_strat() -> impl Strategy<Value = Rps> {
(MIN_RPS_RAW..=*UQ0_63::ONE.as_raw())
.prop_map(UQ0_63::new)
.prop_map(Result::unwrap)
.prop_map(Rps::new)
.prop_map(Result::unwrap)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn rps_new_sc() {
const FAIL: UQ0_63 = unsafe { UQ0_63::new_unchecked(MIN_RPS_RAW - 1) };
const SUCC: UQ0_63 = unsafe { UQ0_63::new_unchecked(MIN_RPS_RAW) };

assert_eq!(Rps::new(FAIL), Err(RpsTooSmallErr { actual: FAIL }));
assert_eq!(Rps::new(SUCC), Ok(Rps(SUCC)));
}
}
Loading