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

Invalid degree bugfix #485

Merged
merged 7 commits into from
Sep 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions src/ordinal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,17 @@ impl Ordinal {

let cycle_start_epoch = cycle_number * CYCLE_EPOCHS;

let cycle_progression = period_offset
.checked_sub(epoch_offset % PERIOD_BLOCKS)
.ok_or_else(|| anyhow!("Invalid relationship between epoch offset and period offset"))?;
const HALVING_INCREMENT: u64 = Epoch::BLOCKS % PERIOD_BLOCKS;

if cycle_progression % (Epoch::BLOCKS % PERIOD_BLOCKS) != 0 {
bail!("Invalid relationship between epoch offset and period offset");
// For valid degrees the relationship between epoch_offset and period_offset
// will increment by 336 every halving.
let relationship = period_offset + Epoch::BLOCKS * CYCLE_EPOCHS - epoch_offset;

if relationship % HALVING_INCREMENT != 0 {
bail!("Relationship between epoch offset and period offset must be multiple of 336");
}

let epochs_since_cycle_start = cycle_progression / (Epoch::BLOCKS % PERIOD_BLOCKS);
let epochs_since_cycle_start = relationship % PERIOD_BLOCKS / HALVING_INCREMENT;

let epoch = cycle_start_epoch + epochs_since_cycle_start;

Expand Down Expand Up @@ -309,6 +311,33 @@ mod tests {
);
}

#[test]
fn invalid_degree_bugfix() {
// Break glass in case of emergency:
// for height in 0..(2 * CYCLE_EPOCHS * Epoch::BLOCKS) {
// // 1054200000000000
// let expected = Height(height).starting_ordinal();
// // 0°1680′0″0‴
// let degree = expected.degree();
// // 2034637500000000
// let actual = degree.to_string().parse::<Ordinal>().unwrap();
// assert_eq!(
// actual, expected,
// "Ordinal at height {height} did not round-trip from degree {degree} successfully"
// );
// }
assert_eq!(
Ordinal(1054200000000000).degree().to_string(),
"0°1680′0″0‴"
);
assert_eq!(parse("0°1680′0″0‴").unwrap(), 1054200000000000);
assert_eq!(
Ordinal(1914226250000000).degree().to_string(),
"0°122762′794″0‴"
);
assert_eq!(parse("0°122762′794″0‴").unwrap(), 1914226250000000);
}

#[test]
fn period() {
assert_eq!(Ordinal(0).period(), 0);
Expand Down