Skip to content

Commit

Permalink
Address runes review comments (#3547)
Browse files Browse the repository at this point in the history
- Fix overflow in Rune::from_str
- Use checked_pow in Pile Display implementation
- Only report first flaw in cenotaph
- Add comment to Edict::from_integers for output == tx.output.len()
- Avoid divide by zero in RuneUpdater
- Add test for zero rune ID delta
- Make varint::decode return a Result instead of an Option
  • Loading branch information
casey authored Apr 15, 2024
1 parent f07db71 commit 6cfd9d0
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 146 deletions.
6 changes: 4 additions & 2 deletions crates/mockcore/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,11 @@ impl State {
);
}

self.mempool.push(tx.clone());
let txid = tx.txid();

tx.txid()
self.mempool.push(tx);

txid
}

pub(crate) fn mempool(&self) -> &[Transaction] {
Expand Down
28 changes: 1 addition & 27 deletions crates/ordinals/src/cenotaph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,6 @@ use super::*;
#[derive(Serialize, Eq, PartialEq, Deserialize, Debug, Default)]
pub struct Cenotaph {
pub etching: Option<Rune>,
pub flaws: u32,
pub flaw: Option<Flaw>,
pub mint: Option<RuneId>,
}

impl Cenotaph {
pub fn flaws(&self) -> Vec<Flaw> {
Flaw::ALL
.into_iter()
.filter(|flaw| self.flaws & flaw.flag() != 0)
.collect()
}
}

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

#[test]
fn flaws() {
assert_eq!(
Cenotaph {
flaws: Flaw::Opcode.flag() | Flaw::Varint.flag(),
..default()
}
.flaws(),
vec![Flaw::Opcode, Flaw::Varint],
);
}
}
2 changes: 2 additions & 0 deletions crates/ordinals/src/edict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ impl Edict {
return None;
};

// note that this allows `output == tx.output.len()`, which means to divide
// amount between all non-OP_RETURN outputs
if output > u32::try_from(tx.output.len()).unwrap() {
return None;
}
Expand Down
28 changes: 2 additions & 26 deletions crates/ordinals/src/flaw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;

#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Flaw {
EdictOutput,
EdictRuneId,
Expand All @@ -14,25 +15,6 @@ pub enum Flaw {
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 {
Expand All @@ -49,9 +31,3 @@ impl Display for Flaw {
}
}
}

impl From<Flaw> for u32 {
fn from(cenotaph: Flaw) -> Self {
cenotaph.flag()
}
}
2 changes: 1 addition & 1 deletion crates/ordinals/src/pile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Pile {

impl Display for Pile {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let cutoff = 10u128.pow(self.divisibility.into());
let cutoff = 10u128.checked_pow(self.divisibility.into()).unwrap();

let whole = self.amount / cutoff;
let mut fractional = self.amount % cutoff;
Expand Down
8 changes: 6 additions & 2 deletions crates/ordinals/src/rune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl FromStr for Rune {
let mut x = 0u128;
for (i, c) in s.chars().enumerate() {
if i > 0 {
x += 1;
x = x.checked_add(1).ok_or(Error::Range)?;
}
x = x.checked_mul(26).ok_or(Error::Range)?;
match c {
Expand Down Expand Up @@ -224,7 +224,11 @@ mod tests {
fn from_str_error() {
assert_eq!(
"BCGDENLQRQWDSLRUGSNLBTMFIJAW".parse::<Rune>().unwrap_err(),
Error::Range
Error::Range,
);
assert_eq!(
"BCGDENLQRQWDSLRUGSNLBTMFIJAVX".parse::<Rune>().unwrap_err(),
Error::Range,
);
assert_eq!("x".parse::<Rune>().unwrap_err(), Error::Character('x'));
}
Expand Down
4 changes: 3 additions & 1 deletion crates/ordinals/src/rune_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ mod tests {
#[test]
fn delta() {
let mut expected = [
RuneId { block: 3, tx: 1 },
RuneId { block: 4, tx: 2 },
RuneId { block: 1, tx: 2 },
RuneId { block: 1, tx: 1 },
Expand All @@ -114,6 +115,7 @@ mod tests {
RuneId { block: 1, tx: 2 },
RuneId { block: 2, tx: 0 },
RuneId { block: 3, tx: 1 },
RuneId { block: 3, tx: 1 },
RuneId { block: 4, tx: 2 },
]
);
Expand All @@ -125,7 +127,7 @@ mod tests {
previous = id;
}

assert_eq!(deltas, [(1, 1), (0, 1), (1, 0), (1, 1), (1, 2)]);
assert_eq!(deltas, [(1, 1), (0, 1), (1, 0), (1, 1), (0, 0), (1, 2)]);

let mut previous = RuneId::default();
let mut actual = Vec::new();
Expand Down
Loading

0 comments on commit 6cfd9d0

Please sign in to comment.