Skip to content

Commit

Permalink
Avoid as conversions (ordinals#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Dec 7, 2022
1 parent a42d441 commit 0927f02
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Epoch {

pub(crate) fn starting_ordinal(self) -> Ordinal {
*Self::STARTING_ORDINALS
.get(self.0 as usize)
.get(usize::try_from(self.0).unwrap())
.unwrap_or_else(|| Self::STARTING_ORDINALS.last().unwrap())
}

Expand Down
15 changes: 9 additions & 6 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,18 @@ impl Index {
}

pub(crate) fn decode_ordinal_range(bytes: OrdinalRangeArray) -> (u64, u64) {
let n = u128::from_le_bytes([
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8],
bytes[9], bytes[10], 0, 0, 0, 0, 0,
let raw_base = u64::from_le_bytes([
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], 0,
]);

// 51 bit base
let base = (n & ((1 << 51) - 1)) as u64;
let base = raw_base & ((1 << 51) - 1);

let raw_delta =
u64::from_le_bytes([bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], 0, 0, 0]);

// 33 bit delta
let delta = (n >> 51) as u64;
let delta = raw_delta >> 3;

(base, base + delta)
}
Expand Down Expand Up @@ -565,7 +568,7 @@ impl Index {
})?;

Ok(Blocktime::Expected(
Utc::now().timestamp() + 10 * 60 * expected_blocks as i64,
Utc::now().timestamp() + 10 * 60 * i64::try_from(expected_blocks).unwrap(),
))
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Updater {
let mut ordinal_ranges_written = 0;
let mut outputs_in_block = 0;

let time = Utc.timestamp_opt(block.header.time as i64, 0).unwrap();
let time = Utc.timestamp_opt(block.header.time.into(), 0).unwrap();

log::info!(
"Block {} at {} with {} transactions…",
Expand Down Expand Up @@ -441,7 +441,7 @@ impl Updater {

for (vout, output) in tx.output.iter().enumerate() {
let outpoint = OutPoint {
vout: vout as u32,
vout: vout.try_into().unwrap(),
txid,
};
let mut ordinals = Vec::new();
Expand Down Expand Up @@ -476,7 +476,7 @@ impl Updater {
let base = assigned.0;
let delta = assigned.1 - assigned.0;

let n = base as u128 | (delta as u128) << 51;
let n = u128::from(base) | u128::from(delta) << 51;

ordinals.extend_from_slice(&n.to_le_bytes()[0..11]);

Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
clippy::type_complexity,
clippy::result_large_err
)]
#![deny(
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss
)]

use {
self::{
Expand Down
5 changes: 3 additions & 2 deletions src/ordinal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,13 @@ impl Ordinal {

let last = Ordinal::LAST.n() as f64;

let n = (percentile / 100.0 * last).round() as u64;
let n = (percentile / 100.0 * last).round();

if n > Ordinal::LAST.n() {
if n > last {
bail!("invalid percentile: {}", percentile);
}

#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
Ok(Ordinal(n as u64))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Inscribe {
input: vec![TxIn {
previous_output: OutPoint {
txid: unsigned_commit_tx.txid(),
vout: vout as u32,
vout: vout.try_into().unwrap(),
},
script_sig: script::Builder::new().into_script(),
witness: Witness::new(),
Expand Down

0 comments on commit 0927f02

Please sign in to comment.