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

Don't conflate cenotaphs and runestones #3417

Merged
merged 16 commits into from
Mar 30, 2024
16 changes: 16 additions & 0 deletions crates/ordinals/src/artifact.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use super::*;

#[derive(Serialize, Eq, PartialEq, Deserialize, Debug)]
pub enum Artifact {
Cenotaph(Cenotaph),
Runestone(Runestone),
}

impl Artifact {
pub fn mint(&self) -> Option<RuneId> {
match self {
Self::Cenotaph(cenotaph) => cenotaph.mint,
Self::Runestone(runestone) => runestone.mint,
}
}
}
78 changes: 23 additions & 55 deletions crates/ordinals/src/cenotaph.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,34 @@
use super::*;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Cenotaph {
EdictOutput,
EdictRuneId,
InvalidScript,
Opcode,
SupplyOverflow,
TrailingIntegers,
TruncatedField,
UnrecognizedEvenTag,
UnrecognizedFlag,
Varint,
#[derive(Serialize, Eq, PartialEq, Deserialize, Debug, Default)]
pub struct Cenotaph {
pub etching: Option<Rune>,
pub flaws: u32,
pub mint: Option<RuneId>,
}

impl Cenotaph {
pub const ALL: [Self; 10] = [
Self::EdictOutput,
Self::EdictRuneId,
Self::InvalidScript,
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::InvalidScript => write!(f, "invalid script in OP_RETURN"),
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"),
}
pub fn flaws(&self) -> Vec<Flaw> {
Flaw::ALL
.into_iter()
.filter(|flaw| self.flaws & flaw.flag() != 0)
.collect()
}
}

impl From<Cenotaph> for Runestone {
fn from(cenotaph: Cenotaph) -> Self {
Self {
cenotaph: cenotaph.flag(),
..default()
}
}
}
#[cfg(test)]
mod tests {
use super::*;

impl From<Cenotaph> for u32 {
fn from(cenotaph: Cenotaph) -> Self {
cenotaph.flag()
#[test]
fn flaws() {
assert_eq!(
Cenotaph {
flaws: Flaw::Opcode.flag() | Flaw::Varint.flag(),
..default()
}
.flaws(),
vec![Flaw::Opcode, Flaw::Varint],
);
}
}
57 changes: 57 additions & 0 deletions crates/ordinals/src/flaw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use super::*;

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

impl Flaw {
pub const ALL: [Self; 10] = [
Self::EdictOutput,
Self::EdictRuneId,
Self::InvalidScript,
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 Flaw {
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::InvalidScript => write!(f, "invalid script in OP_RETURN"),
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<Flaw> for u32 {
fn from(cenotaph: Flaw) -> Self {
cenotaph.flag()
}
}
11 changes: 7 additions & 4 deletions crates/ordinals/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Types for interoperating with ordinals, inscriptions, and runes.
#![allow(clippy::large_enum_variant)]

use {
bitcoin::{
Expand Down Expand Up @@ -26,10 +27,10 @@ use {
};

pub use {
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,
artifact::Artifact, cenotaph::Cenotaph, charm::Charm, decimal_sat::DecimalSat, degree::Degree,
edict::Edict, epoch::Epoch, etching::Etching, flaw::Flaw, 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;
Expand All @@ -38,13 +39,15 @@ fn default<T: Default>() -> T {
Default::default()
}

mod artifact;
mod cenotaph;
mod charm;
mod decimal_sat;
mod degree;
mod edict;
mod epoch;
mod etching;
mod flaw;
mod height;
mod pile;
mod rarity;
Expand Down
Loading
Loading