Skip to content

Commit

Permalink
Make Tag::take leave invalid entries
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Mar 20, 2024
1 parent 9db05a2 commit e674dcf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 33 deletions.
90 changes: 64 additions & 26 deletions src/runes/runestone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,44 +73,40 @@ impl Runestone {
mut fields,
} = Message::from_integers(transaction, &integers);

let claim = Tag::Claim.take_with(&mut fields, |id| RuneId::try_from(id).ok());
let claim = Tag::Claim.take(&mut fields, |id| RuneId::try_from(id).ok());

let deadline = Tag::Deadline
.take(&mut fields)
.and_then(|deadline| u32::try_from(deadline).ok());
let deadline = Tag::Deadline.take(&mut fields, |deadline| u32::try_from(deadline).ok());

let default_output = Tag::DefaultOutput
.take(&mut fields)
.and_then(|default| u32::try_from(default).ok());
let default_output =
Tag::DefaultOutput.take(&mut fields, |default| u32::try_from(default).ok());

let divisibility = Tag::Divisibility
.take(&mut fields)
.and_then(|divisibility| u8::try_from(divisibility).ok())
.and_then(|divisibility| (divisibility <= MAX_DIVISIBILITY).then_some(divisibility))
.take(&mut fields, |divisibility| {
let divisibility = u8::try_from(divisibility).ok()?;
(divisibility <= MAX_DIVISIBILITY).then_some(divisibility)
})
.unwrap_or_default();

let limit = Tag::Limit
.take(&mut fields)
.map(|limit| limit.min(MAX_LIMIT));
let limit = Tag::Limit.take(&mut fields, |limit| (limit <= MAX_LIMIT).then_some(limit));

let rune = Tag::Rune.take(&mut fields).map(Rune);
let rune = Tag::Rune.take(&mut fields, |rune| Some(Rune(rune)));

let spacers = Tag::Spacers
.take(&mut fields)
.and_then(|spacers| u32::try_from(spacers).ok())
.and_then(|spacers| (spacers <= MAX_SPACERS).then_some(spacers))
.take(&mut fields, |spacers| {
let spacers = u32::try_from(spacers).ok()?;
(spacers <= MAX_SPACERS).then_some(spacers)
})
.unwrap_or_default();

let symbol = Tag::Symbol
.take(&mut fields)
.and_then(|symbol| u32::try_from(symbol).ok())
.and_then(char::from_u32);
let symbol = Tag::Symbol.take(&mut fields, |symbol| {
char::from_u32(u32::try_from(symbol).ok()?)
});

let term = Tag::Term
.take(&mut fields)
.and_then(|term| u32::try_from(term).ok());
let term = Tag::Term.take(&mut fields, |term| u32::try_from(term).ok());

let mut flags = Tag::Flags.take(&mut fields).unwrap_or_default();
let mut flags = Tag::Flags
.take(&mut fields, |flags| Some(flags))
.unwrap_or_default();

let etch = Flag::Etch.take(&mut flags);

Expand Down Expand Up @@ -1534,7 +1530,7 @@ mod tests {
}

#[test]
fn etching_with_term_greater_than_maximum_is_ignored() {
fn etching_with_term_greater_than_maximum_is_still_an_etching() {
assert_eq!(
decipher(&[
Tag::Flags.into(),
Expand All @@ -1544,6 +1540,7 @@ mod tests {
]),
Runestone {
etching: Some(Etching::default()),
cenotaph: true,
..Default::default()
},
);
Expand Down Expand Up @@ -1752,4 +1749,45 @@ mod tests {
fn invalid_claim_produces_cenotaph() {
assert!(decipher(&[Tag::Claim.into(), 1]).cenotaph);
}

#[test]
fn invalid_deadline_produces_cenotaph() {
assert!(decipher(&[Tag::Deadline.into(), u128::MAX]).cenotaph);
}

#[test]
fn invalid_default_output_produces_cenotaph() {
assert!(decipher(&[Tag::DefaultOutput.into(), u128::MAX]).cenotaph);
}

#[test]
fn invalid_divisibility_does_not_produce_cenotaph() {
assert!(!decipher(&[Tag::Divisibility.into(), u128::MAX]).cenotaph);
}

#[test]
fn invalid_limit_produces_cenotaph() {
assert!(decipher(&[Tag::Limit.into(), u128::MAX]).cenotaph);
}

#[test]
fn min_and_max_runes_are_not_cenotaphs() {
assert!(!decipher(&[Tag::Rune.into(), 0]).cenotaph);
assert!(!decipher(&[Tag::Rune.into(), u128::MAX]).cenotaph);
}

#[test]
fn invalid_spacers_does_not_produce_cenotaph() {
assert!(!decipher(&[Tag::Spacers.into(), u128::MAX]).cenotaph);
}

#[test]
fn invalid_symbol_does_not_produce_cenotaph() {
assert!(!decipher(&[Tag::Symbol.into(), u128::MAX]).cenotaph);
}

#[test]
fn invalid_term_produces_cenotaph() {
assert!(decipher(&[Tag::Term.into(), u128::MAX]).cenotaph);
}
}
14 changes: 7 additions & 7 deletions src/runes/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ pub(super) enum Tag {
}

impl Tag {
pub(super) fn take(self, fields: &mut HashMap<u128, u128>) -> Option<u128> {
fields.remove(&self.into())
}

pub(super) fn take_with<T>(
pub(super) fn take<T>(
self,
fields: &mut HashMap<u128, u128>,
with: impl Fn(u128) -> Option<T>,
Expand Down Expand Up @@ -77,11 +73,15 @@ mod tests {
fn take() {
let mut fields = vec![(2, 3)].into_iter().collect::<HashMap<u128, u128>>();

assert_eq!(Tag::Flags.take(&mut fields), Some(3));
assert_eq!(Tag::Flags.take(&mut fields, |_| None::<u128>), None);

assert!(!fields.is_empty());

assert_eq!(Tag::Flags.take(&mut fields, |flags| Some(flags)), Some(3));

assert!(fields.is_empty());

assert_eq!(Tag::Flags.take(&mut fields), None);
assert_eq!(Tag::Flags.take(&mut fields, |_| Some(1)), None);
}

#[test]
Expand Down

0 comments on commit e674dcf

Please sign in to comment.