Skip to content
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

Move sat, degree, epoch, height into ordinals crate #3079

Merged
merged 15 commits into from
Feb 12, 2024
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ chrono = { version = "0.4.19", features = ["serde"] }
ciborium = "0.2.1"
clap = { version = "4.4.2", features = ["derive"] }
ctrlc = { version = "3.2.1", features = ["termination"] }
derive_more = "0.99.17"
dirs = "5.0.0"
env_logger = "0.10.0"
futures = "0.3.21"
Expand Down
3 changes: 2 additions & 1 deletion crates/ordinals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ license = "CC0-1.0"
rust-version = "1.67"

[dependencies]
serde = { version = "1.0.137", features = ["derive"] }
bitcoin = { version = "0.30.1", features = ["rand"] }
derive_more = "0.99.17"
serde = { version = "1.0.137", features = ["derive"] }
thiserror = "1.0.56"

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions src/decimal_sat.rs → crates/ordinals/src/decimal_sat.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::*;

#[derive(PartialEq, Debug)]
pub(crate) struct DecimalSat {
height: Height,
offset: u64,
pub struct DecimalSat {
pub height: Height,
pub offset: u64,
}

impl From<Sat> for DecimalSat {
Expand Down
10 changes: 5 additions & 5 deletions src/degree.rs → crates/ordinals/src/degree.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::*;

#[derive(PartialEq, Debug)]
pub(crate) struct Degree {
pub(crate) hour: u32,
pub(crate) minute: u32,
pub(crate) second: u32,
pub(crate) third: u64,
pub struct Degree {
pub hour: u32,
pub minute: u32,
pub second: u32,
pub third: u64,
}

impl Display for Degree {
Expand Down
12 changes: 6 additions & 6 deletions src/epoch.rs → crates/ordinals/src/epoch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::*;

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Serialize, PartialOrd)]
pub(crate) struct Epoch(pub(crate) u32);
pub struct Epoch(pub u32);

impl Epoch {
pub(crate) const STARTING_SATS: [Sat; 34] = [
pub const STARTING_SATS: [Sat; 34] = [
Sat(0),
Sat(1050000000000000),
Sat(1575000000000000),
Expand Down Expand Up @@ -40,23 +40,23 @@ impl Epoch {
Sat(2099999997480000),
Sat(Sat::SUPPLY),
];
pub(crate) const FIRST_POST_SUBSIDY: Epoch = Self(33);
pub const FIRST_POST_SUBSIDY: Epoch = Self(33);

pub(crate) fn subsidy(self) -> u64 {
pub fn subsidy(self) -> u64 {
if self < Self::FIRST_POST_SUBSIDY {
(50 * COIN_VALUE) >> self.0
} else {
0
}
}

pub(crate) fn starting_sat(self) -> Sat {
pub fn starting_sat(self) -> Sat {
*Self::STARTING_SATS
.get(usize::try_from(self.0).unwrap())
.unwrap_or_else(|| Self::STARTING_SATS.last().unwrap())
}

pub(crate) fn starting_height(self) -> Height {
pub fn starting_height(self) -> Height {
Height(self.0 * SUBSIDY_HALVING_INTERVAL)
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/height.rs → crates/ordinals/src/height.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
use super::*;

#[derive(Copy, Clone, Debug, Display, FromStr, Ord, Eq, Serialize, PartialEq, PartialOrd)]
pub(crate) struct Height(pub(crate) u32);
pub struct Height(pub u32);

impl Height {
pub(crate) fn n(self) -> u32 {
pub fn n(self) -> u32 {
self.0
}

pub(crate) fn subsidy(self) -> u64 {
pub fn subsidy(self) -> u64 {
Epoch::from(self).subsidy()
}

pub(crate) fn starting_sat(self) -> Sat {
pub fn starting_sat(self) -> Sat {
let epoch = Epoch::from(self);
let epoch_starting_sat = epoch.starting_sat();
let epoch_starting_height = epoch.starting_height();
epoch_starting_sat + u64::from(self.n() - epoch_starting_height.n()) * epoch.subsidy()
}

pub(crate) fn period_offset(self) -> u32 {
pub fn period_offset(self) -> u32 {
self.0 % DIFFCHANGE_INTERVAL
}
}
Expand Down
18 changes: 17 additions & 1 deletion crates/ordinals/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
//! Types for interoperating with ordinals and inscriptions.

use {
bitcoin::constants::{COIN_VALUE, DIFFCHANGE_INTERVAL, SUBSIDY_HALVING_INTERVAL},
bitcoin::{
consensus::{Decodable, Encodable},
OutPoint,
},
derive_more::{Display, FromStr},
serde::{Deserialize, Deserializer, Serialize, Serializer},
std::{
cmp,
fmt::{self, Display, Formatter},
io,
num::ParseIntError,
ops::{Add, AddAssign, Sub},
str::FromStr,
},
thiserror::Error,
};

pub use sat_point::SatPoint;
pub const CYCLE_EPOCHS: u32 = 6;

pub use {
decimal_sat::DecimalSat, degree::Degree, epoch::Epoch, height::Height, rarity::Rarity, sat::Sat,
sat_point::SatPoint,
};

#[doc(hidden)]
pub use self::deserialize_from_str::DeserializeFromStr;

mod decimal_sat;
mod degree;
mod deserialize_from_str;
mod epoch;
mod height;
mod rarity;
mod sat;
mod sat_point;
16 changes: 7 additions & 9 deletions src/rarity.rs → crates/ordinals/src/rarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl From<Sat> for Rarity {
}

impl FromStr for Rarity {
type Err = Error;
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
Expand All @@ -85,7 +85,7 @@ impl FromStr for Rarity {
"epic" => Ok(Self::Epic),
"legendary" => Ok(Self::Legendary),
"mythic" => Ok(Self::Mythic),
_ => Err(anyhow!("invalid rarity: {s}")),
_ => Err(format!("invalid rarity `{s}`")),
}
}
}
Expand Down Expand Up @@ -172,13 +172,6 @@ mod tests {
case("mythic", Rarity::Mythic);
}

#[test]
fn from_str_err() {
"abc".parse::<Rarity>().unwrap_err();

"".parse::<Rarity>().unwrap_err();
}

#[test]
fn conversions_with_u8() {
for &expected in &[
Expand All @@ -196,4 +189,9 @@ mod tests {

assert_eq!(Rarity::try_from(6), Err(6));
}

#[test]
fn error() {
assert_eq!("foo".parse::<Rarity>().unwrap_err(), "invalid rarity `foo`");
}
}
Loading
Loading