Skip to content

Commit

Permalink
Move runes types into ordinals crate (ordinals#3391)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Mar 28, 2024
1 parent 082cdf7 commit 3a44161
Show file tree
Hide file tree
Showing 35 changed files with 624 additions and 407 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ reqwest = { version = "0.11.10", features = ["blocking", "brotli", "json"] }
test-bitcoincore-rpc = { path = "crates/test-bitcoincore-rpc" }
unindent = "0.2.1"

[[bench]]
name = "server"
harness = false

[[bin]]
name = "ord"
path = "src/bin/main.rs"
Expand Down
16 changes: 0 additions & 16 deletions benches/server.rs

This file was deleted.

1 change: 1 addition & 0 deletions crates/ordinals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ thiserror = "1.0.56"

[dev-dependencies]
serde_json = { version = "1.0.81", features = ["preserve_order"] }
pretty_assertions = "1.2.1"
63 changes: 63 additions & 0 deletions crates/ordinals/src/cenotaph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use super::*;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Cenotaph {
EdictOutput,
EdictRuneId,
Opcode,
SupplyOverflow,
TrailingIntegers,
TruncatedField,
UnrecognizedEvenTag,
UnrecognizedFlag,
Varint,
}

impl Cenotaph {
pub const ALL: [Self; 9] = [
Self::EdictOutput,
Self::EdictRuneId,
Self::Opcode,
Self::SupplyOverflow,
Self::TrailingIntegers,
Self::TruncatedField,
Self::UnrecognizedEvenTag,
Self::UnrecognizedFlag,
Self::Varint,
];

pub fn flag(self) -> u32 {
1 << (self as u32)
}
}

impl Display for Cenotaph {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::EdictOutput => write!(f, "edict output greater than transaction output count"),
Self::EdictRuneId => write!(f, "invalid rune ID in edict"),
Self::Opcode => write!(f, "non-pushdata opcode in OP_RETURN"),
Self::SupplyOverflow => write!(f, "supply overflows u128"),
Self::TrailingIntegers => write!(f, "trailing integers in body"),
Self::TruncatedField => write!(f, "field with missing value"),
Self::UnrecognizedEvenTag => write!(f, "unrecognized even tag"),
Self::UnrecognizedFlag => write!(f, "unrecognized field"),
Self::Varint => write!(f, "invalid varint"),
}
}
}

impl From<Cenotaph> for Runestone {
fn from(cenotaph: Cenotaph) -> Self {
Self {
cenotaph: cenotaph.flag(),
..default()
}
}
}

impl From<Cenotaph> for u32 {
fn from(cenotaph: Cenotaph) -> Self {
cenotaph.flag()
}
}
5 changes: 2 additions & 3 deletions crates/ordinals/src/charm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum Charm {
}

impl Charm {
pub const ALL: [Charm; 12] = [
pub const ALL: [Self; 12] = [
Self::Coin,
Self::Uncommon,
Self::Rare,
Expand Down Expand Up @@ -67,9 +67,8 @@ impl Charm {

pub fn charms(charms: u16) -> Vec<Charm> {
Self::ALL
.iter()
.into_iter()
.filter(|charm| charm.is_set(charms))
.copied()
.collect()
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/runes/edict.rs → crates/ordinals/src/edict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ pub struct Edict {
}

impl Edict {
pub(crate) fn from_integers(
tx: &Transaction,
id: RuneId,
amount: u128,
output: u128,
) -> Option<Self> {
pub fn from_integers(tx: &Transaction, id: RuneId, amount: u128, output: u128) -> Option<Self> {
let Ok(output) = u32::try_from(output) else {
return None;
};
Expand Down
39 changes: 39 additions & 0 deletions crates/ordinals/src/etching.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use super::*;

#[derive(Default, Serialize, Deserialize, Debug, PartialEq, Copy, Clone, Eq)]
pub struct Etching {
pub divisibility: Option<u8>,
pub premine: Option<u128>,
pub rune: Option<Rune>,
pub spacers: Option<u32>,
pub symbol: Option<char>,
pub terms: Option<Terms>,
}

impl Etching {
pub const MAX_DIVISIBILITY: u8 = 38;
pub const MAX_SPACERS: u32 = 0b00000111_11111111_11111111_11111111;
}

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

#[test]
fn max_spacers() {
let mut rune = String::new();

for (i, c) in Rune(u128::MAX).to_string().chars().enumerate() {
if i > 0 {
rune.push('•');
}

rune.push(c);
}

assert_eq!(
Etching::MAX_SPACERS,
rune.parse::<SpacedRune>().unwrap().spacers
);
}
}
35 changes: 28 additions & 7 deletions crates/ordinals/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
//! Types for interoperating with ordinals and inscriptions.
//! Types for interoperating with ordinals, inscriptions, and runes.

use {
bitcoin::constants::{COIN_VALUE, DIFFCHANGE_INTERVAL, SUBSIDY_HALVING_INTERVAL},
bitcoin::{
consensus::{Decodable, Encodable},
OutPoint,
constants::{
COIN_VALUE, DIFFCHANGE_INTERVAL, MAX_SCRIPT_ELEMENT_SIZE, SUBSIDY_HALVING_INTERVAL,
},
opcodes,
script::{self, Instruction},
Network, OutPoint, ScriptBuf, Transaction,
},
derive_more::{Display, FromStr},
serde::{Deserialize, Serialize},
serde_with::{DeserializeFromStr, SerializeDisplay},
std::{
cmp,
collections::{HashMap, VecDeque},
fmt::{self, Display, Formatter},
io,
num::ParseIntError,
Expand All @@ -20,18 +25,34 @@ use {
thiserror::Error,
};

pub const CYCLE_EPOCHS: u32 = 6;

pub use {
charm::Charm, decimal_sat::DecimalSat, degree::Degree, epoch::Epoch, height::Height,
rarity::Rarity, sat::Sat, sat_point::SatPoint,
cenotaph::Cenotaph, charm::Charm, decimal_sat::DecimalSat, degree::Degree, edict::Edict,
epoch::Epoch, etching::Etching, height::Height, pile::Pile, rarity::Rarity, rune::Rune,
rune_id::RuneId, runestone::Runestone, sat::Sat, sat_point::SatPoint, spaced_rune::SpacedRune,
terms::Terms,
};

pub const CYCLE_EPOCHS: u32 = 6;

fn default<T: Default>() -> T {
Default::default()
}

mod cenotaph;
mod charm;
mod decimal_sat;
mod degree;
mod edict;
mod epoch;
mod etching;
mod height;
mod pile;
mod rarity;
mod rune;
mod rune_id;
mod runestone;
mod sat;
mod sat_point;
mod spaced_rune;
mod terms;
pub mod varint;
2 changes: 1 addition & 1 deletion src/runes/pile.rs → crates/ordinals/src/pile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ mod tests {
assert_eq!(
Pile {
amount: u128::MAX,
divisibility: MAX_DIVISIBILITY,
divisibility: 38,
symbol: None,
}
.to_string(),
Expand Down
Loading

0 comments on commit 3a44161

Please sign in to comment.