Skip to content

Commit

Permalink
Fix querying for inscriptions by sat names containing i (#3287)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Mar 14, 2024
1 parent bea2ec5 commit 4d29e07
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ mod inscriptions;
mod object;
pub mod options;
pub mod outgoing;
mod re;
mod representation;
pub mod runes;
mod server_config;
Expand Down
6 changes: 2 additions & 4 deletions src/outgoing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ impl FromStr for Outgoing {

fn from_str(s: &str) -> Result<Self, Self::Err> {
lazy_static! {
static ref SATPOINT: Regex = Regex::new(r"^[[:xdigit:]]{64}:\d+:\d+$").unwrap();
static ref INSCRIPTION_ID: Regex = Regex::new(r"^[[:xdigit:]]{64}i\d+$").unwrap();
static ref AMOUNT: Regex = Regex::new(
r"(?x)
^
Expand Down Expand Up @@ -63,9 +61,9 @@ impl FromStr for Outgoing {
.unwrap();
}

Ok(if SATPOINT.is_match(s) {
Ok(if re::SATPOINT.is_match(s) {
Self::SatPoint(s.parse()?)
} else if INSCRIPTION_ID.is_match(s) {
} else if re::INSCRIPTION_ID.is_match(s) {
Self::InscriptionId(s.parse()?)
} else if AMOUNT.is_match(s) {
Self::Amount(s.parse()?)
Expand Down
16 changes: 16 additions & 0 deletions src/re.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use super::*;

fn re(s: &'static str) -> Regex {
Regex::new(&format!("^{s}$")).unwrap()
}

lazy_static! {
pub(crate) static ref HASH: Regex = re(r"[[:xdigit:]]{64}");
pub(crate) static ref INSCRIPTION_ID: Regex = re(r"[[:xdigit:]]{64}i\d+");
pub(crate) static ref INSCRIPTION_NUMBER: Regex = re(r"-?[0-9]+");
pub(crate) static ref OUTPOINT: Regex = re(r"[[:xdigit:]]{64}:\d+");
pub(crate) static ref RUNE_ID: Regex = re(r"[0-9]+:[0-9]+");
pub(crate) static ref SATPOINT: Regex = re(r"[[:xdigit:]]{64}:\d+:\d+");
pub(crate) static ref SAT_NAME: Regex = re(r"[a-z]+");
pub(crate) static ref SPACED_RUNE: Regex = re(r"[A-Z•.]+");
}
29 changes: 15 additions & 14 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,29 +940,21 @@ impl Server {

async fn search_inner(index: Arc<Index>, query: String) -> ServerResult<Redirect> {
task::block_in_place(|| {
lazy_static! {
static ref HASH: Regex = Regex::new(r"^[[:xdigit:]]{64}$").unwrap();
static ref INSCRIPTION_ID: Regex = Regex::new(r"^[[:xdigit:]]{64}i\d+$").unwrap();
static ref OUTPOINT: Regex = Regex::new(r"^[[:xdigit:]]{64}:\d+$").unwrap();
static ref RUNE: Regex = Regex::new(r"^[A-Z•.]+$").unwrap();
static ref RUNE_ID: Regex = Regex::new(r"^[0-9]+:[0-9]+$").unwrap();
}

let query = query.trim();

if HASH.is_match(query) {
if re::HASH.is_match(query) {
if index.block_header(query.parse().unwrap())?.is_some() {
Ok(Redirect::to(&format!("/block/{query}")))
} else {
Ok(Redirect::to(&format!("/tx/{query}")))
}
} else if OUTPOINT.is_match(query) {
} else if re::OUTPOINT.is_match(query) {
Ok(Redirect::to(&format!("/output/{query}")))
} else if INSCRIPTION_ID.is_match(query) {
} else if re::INSCRIPTION_ID.is_match(query) {
Ok(Redirect::to(&format!("/inscription/{query}")))
} else if RUNE.is_match(query) {
} else if re::SPACED_RUNE.is_match(query) {
Ok(Redirect::to(&format!("/rune/{query}")))
} else if RUNE_ID.is_match(query) {
} else if re::RUNE_ID.is_match(query) {
let id = query
.parse::<RuneId>()
.map_err(|err| ServerError::BadRequest(err.to_string()))?;
Expand Down Expand Up @@ -4206,10 +4198,19 @@ mod tests {
server.assert_response_regex(
format!("/inscription/{}", Sat(5000000000).name()),
StatusCode::OK,
r".*<title>Inscription 0</title.*",
".*<title>Inscription 0</title.*",
);
}

#[test]
fn inscriptions_can_be_looked_up_by_sat_name_with_letter_i() {
let server = TestServer::builder()
.chain(Chain::Regtest)
.index_sats()
.build();
server.assert_response_regex("/inscription/i", StatusCode::NOT_FOUND, ".*");
}

#[test]
fn inscription_page_does_not_have_sat_when_sats_are_not_tracked() {
let server = TestServer::builder().chain(Chain::Regtest).build();
Expand Down
14 changes: 8 additions & 6 deletions src/subcommand/server/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ impl FromStr for Inscription {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(if s.contains('i') {
Self::Id(s.parse()?)
} else if s.chars().all(|c| c.is_ascii_lowercase()) {
Self::Sat(s.parse()?)
if re::INSCRIPTION_ID.is_match(s) {
Ok(Self::Id(s.parse()?))
} else if re::INSCRIPTION_NUMBER.is_match(s) {
Ok(Self::Number(s.parse()?))
} else if re::SAT_NAME.is_match(s) {
Ok(Self::Sat(s.parse()?))
} else {
Self::Number(s.parse()?)
})
Err(anyhow!("bad inscription query {s}"))
}
}
}

Expand Down

0 comments on commit 4d29e07

Please sign in to comment.