From 160262db59d1562373c7d6646831651720a48cbb Mon Sep 17 00:00:00 2001 From: Lugon LQ <48004924+lugondev@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:30:15 +0700 Subject: [PATCH 1/3] query Rune by number --- src/index.rs | 19 ++++++++++++++ src/re.rs | 1 + src/subcommand/server.rs | 48 +++++++++++++++++++++++++++++++--- src/subcommand/server/query.rs | 12 ++++++--- 4 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/index.rs b/src/index.rs index bec84d0a25..cc6bf83188 100644 --- a/src/index.rs +++ b/src/index.rs @@ -867,6 +867,25 @@ impl Index { ) } + pub(crate) fn get_rune_by_number(&self, number: usize) -> Result> { + match self + .database + .begin_read()? + .open_table(RUNE_ID_TO_RUNE_ENTRY)? + .iter()? + .nth(number) + { + Some(result) => { + let rune_result = + result.map(|(_id, entry)| RuneEntry::load(entry.value()).spaced_rune.rune); + Ok(rune_result.ok()) + } + None => Ok(None), + } + + // Ok(rune_result) + } + pub(crate) fn rune( &self, rune: Rune, diff --git a/src/re.rs b/src/re.rs index ad2d50288b..b8ece332e6 100644 --- a/src/re.rs +++ b/src/re.rs @@ -10,6 +10,7 @@ lazy_static! { 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 RUNE_NUMBER: Regex = re(r"-?[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]{1,11}"); pub(crate) static ref SPACED_RUNE: Regex = re(r"[A-Z•.]+"); diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 26217801ae..b6c6e4e86b 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -676,12 +676,15 @@ impl Server { "this server has no rune index".to_string(), )); } - + println!("get_rune_by_number({:?})", rune_query); let rune = match rune_query { - query::Rune::SpacedRune(spaced_rune) => spaced_rune.rune, - query::Rune::RuneId(rune_id) => index + query::Rune::Spaced(spaced_rune) => spaced_rune.rune, + query::Rune::Id(rune_id) => index .get_rune_by_id(rune_id)? .ok_or_not_found(|| format!("rune {rune_id}"))?, + query::Rune::Number(number) => index + .get_rune_by_number(usize::try_from(number).unwrap())? + .ok_or_not_found(|| format!("rune number {number}"))?, }; let (id, entry, parent) = index @@ -2661,6 +2664,45 @@ mod tests { ); } + #[test] + fn runes_can_be_queried_by_rune_number() { + let server = TestServer::builder() + .chain(Chain::Regtest) + .index_runes() + .build(); + + server.mine_blocks(1); + + let rune = Rune(RUNE); + + server.assert_response_regex("/rune/0", StatusCode::NOT_FOUND, ".*"); + + server.etch( + Runestone { + edicts: vec![Edict { + id: RuneId::default(), + amount: u128::MAX, + output: 0, + }], + etching: Some(Etching { + rune: Some(rune), + ..default() + }), + ..default() + }, + 1, + None, + ); + + server.mine_blocks(1); + + server.assert_response_regex( + "/rune/0", + StatusCode::OK, + ".*Rune AAAAAAAAAAAAA.*", + ); + } + #[test] fn runes_are_displayed_on_runes_page() { let server = TestServer::builder() diff --git a/src/subcommand/server/query.rs b/src/subcommand/server/query.rs index 3acdc5e940..f0d1efe82c 100644 --- a/src/subcommand/server/query.rs +++ b/src/subcommand/server/query.rs @@ -50,9 +50,11 @@ impl Display for Inscription { } } +#[derive(Debug)] pub(super) enum Rune { - SpacedRune(SpacedRune), - RuneId(RuneId), + Spaced(SpacedRune), + Id(RuneId), + Number(u64), } impl FromStr for Rune { @@ -60,9 +62,11 @@ impl FromStr for Rune { fn from_str(s: &str) -> Result { if s.contains(':') { - Ok(Self::RuneId(s.parse()?)) + Ok(Self::Id(s.parse()?)) + } else if re::RUNE_NUMBER.is_match(s) { + Ok(Self::Number(s.parse()?)) } else { - Ok(Self::SpacedRune(s.parse()?)) + Ok(Self::Spaced(s.parse()?)) } } } From e2073f5a120fe3563dc452ad1a374a7d71c49fe7 Mon Sep 17 00:00:00 2001 From: Lugon LQ <48004924+lugondev@users.noreply.github.com> Date: Sat, 13 Apr 2024 16:53:55 +0700 Subject: [PATCH 2/3] add more test --- src/index.rs | 2 -- src/subcommand/server.rs | 56 +++++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/index.rs b/src/index.rs index 76750af579..72453827e2 100644 --- a/src/index.rs +++ b/src/index.rs @@ -870,8 +870,6 @@ impl Index { } None => Ok(None), } - - // Ok(rune_result) } pub(crate) fn rune( diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 1e3986f7aa..1e21dd2228 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -1,3 +1,4 @@ +pub(crate) use server_config::ServerConfig; use { self::{ accept_encoding::AcceptEncoding, @@ -40,8 +41,6 @@ use { }, }; -pub(crate) use server_config::ServerConfig; - mod accept_encoding; mod accept_json; mod error; @@ -653,7 +652,7 @@ impl Server { "this server has no rune index".to_string(), )); } - println!("get_rune_by_number({:?})", rune_query); + let rune = match rune_query { query::Rune::Spaced(spaced_rune) => spaced_rune.rune, query::Rune::Id(rune_id) => index @@ -2634,34 +2633,49 @@ mod tests { server.mine_blocks(1); - let rune = Rune(RUNE); - server.assert_response_regex("/rune/0", StatusCode::NOT_FOUND, ".*"); - server.etch( - Runestone { - edicts: vec![Edict { - id: RuneId::default(), - amount: u128::MAX, - output: 0, - }], - etching: Some(Etching { - rune: Some(rune), + for i in 0..10 { + let rune = Rune(RUNE + i); + server.etch( + Runestone { + edicts: vec![Edict { + id: RuneId::default(), + amount: u128::MAX, + output: 0, + }], + etching: Some(Etching { + rune: Some(rune), + ..default() + }), ..default() - }), - ..default() - }, - 1, - None, - ); + }, + 1, + None, + ); - server.mine_blocks(1); + server.mine_blocks(1); + } server.assert_response_regex( "/rune/0", StatusCode::OK, ".*Rune AAAAAAAAAAAAA.*", ); + + for i in 1..6 { + server.assert_response_regex( + format!("/rune/{}", i), + StatusCode::OK, + ".*Rune AAAAAAAAAAAA.*.*", + ); + } + + server.assert_response_regex( + "/rune/9", + StatusCode::OK, + ".*Rune AAAAAAAAAAAAJ.*", + ); } #[test] From 95ef26050b6c9def46aa1bfb23d59f9c86f67fad Mon Sep 17 00:00:00 2001 From: raphjaph Date: Sat, 13 Apr 2024 17:49:05 -0400 Subject: [PATCH 3/3] Amend --- src/subcommand/server.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index c650bfbde0..d75a4731ed 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -1,4 +1,3 @@ -pub(crate) use server_config::ServerConfig; use { self::{ accept_encoding::AcceptEncoding, @@ -41,10 +40,12 @@ use { }, }; +pub(crate) use server_config::ServerConfig; + mod accept_encoding; mod accept_json; mod error; -pub(crate) mod query; +pub mod query; mod server_config; enum SpawnConfig {