From 0f156443d8af0429855605de8176e78db1630d0f Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 25 Mar 2024 14:38:44 -0700 Subject: [PATCH] Test mintable --- src/index/entry.rs | 114 +++++++++++++++++++++++++++++++++++++++++++-- src/runes.rs | 2 +- 2 files changed, 111 insertions(+), 5 deletions(-) diff --git a/src/index/entry.rs b/src/index/entry.rs index d61fd426f7..38c8d8d0f7 100644 --- a/src/index/entry.rs +++ b/src/index/entry.rs @@ -60,10 +60,10 @@ impl RuneEntry { } } - if let Some(cap) = mint.cap { - if self.mints > cap { - return Err(MintError::Cap(cap)); - } + let cap = mint.cap.unwrap_or_default(); + + if self.mints >= cap { + return Err(MintError::Cap(cap)); } Ok(mint.limit.unwrap_or_default()) @@ -565,4 +565,110 @@ mod tests { assert_eq!(actual, expected); } + + #[test] + fn mintable() { + assert_eq!( + RuneEntry::default().mintable(Height(0), 0), + Err(MintError::Unmintable) + ); + + assert_eq!( + RuneEntry { + mint: Some(MintEntry { + end: Some(1), + limit: Some(1000), + cap: Some(1), + ..default() + }), + ..default() + } + .mintable(Height(0), 0), + Ok(1000), + ); + + assert_eq!( + RuneEntry { + mint: Some(MintEntry { + end: Some(1), + limit: Some(1000), + cap: Some(1), + ..default() + }), + ..default() + } + .mintable(Height(1), 0), + Err(MintError::End(1)), + ); + + assert_eq!( + RuneEntry { + mint: Some(MintEntry { + deadline: Some(1), + limit: Some(1000), + cap: Some(1), + ..default() + }), + ..default() + } + .mintable(Height(0), 0), + Ok(1000), + ); + + assert_eq!( + RuneEntry { + mint: Some(MintEntry { + deadline: Some(1), + limit: Some(1000), + cap: Some(1), + ..default() + }), + ..default() + } + .mintable(Height(0), 1), + Err(MintError::Deadline(1)), + ); + + assert_eq!( + RuneEntry { + mint: Some(MintEntry { + cap: Some(1), + limit: Some(1000), + ..default() + }), + mints: 0, + ..default() + } + .mintable(Height(0), 0), + Ok(1000), + ); + + assert_eq!( + RuneEntry { + mint: Some(MintEntry { + cap: Some(1), + limit: Some(1000), + ..default() + }), + mints: 1, + ..default() + } + .mintable(Height(0), 1), + Err(MintError::Cap(1)), + ); + + assert_eq!( + RuneEntry { + mint: Some(MintEntry { + cap: None, + limit: Some(1000), + ..default() + }), + mints: 0, + ..default() + } + .mintable(Height(0), 1), + Err(MintError::Cap(0)), + ); + } } diff --git a/src/runes.rs b/src/runes.rs index 62cb696629..a6ba55d773 100644 --- a/src/runes.rs +++ b/src/runes.rs @@ -27,7 +27,7 @@ pub mod varint; type Result = std::result::Result; -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum MintError { Cap(u128), Deadline(u32),