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

Allow searching for runes #2522

Merged
merged 9 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,17 @@ impl Index {
)
}

pub(crate) fn get_rune_by_id(&self, id: RuneId) -> Result<Option<Rune>> {
Ok(
self
.database
.begin_read()?
.open_table(RUNE_ID_TO_RUNE_ENTRY)?
.get(&id.store())?
.map(|entry| RuneEntry::load(entry.value()).rune),
)
}

pub(crate) fn rune(&self, rune: Rune) -> Result<Option<(RuneId, RuneEntry)>> {
let rtx = self.database.begin_read()?;

Expand Down
31 changes: 31 additions & 0 deletions src/runes/rune_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ impl Display for RuneId {
}
}

impl FromStr for RuneId {
type Err = crate::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let (height, index) = s
.split_once('/')
.ok_or_else(|| anyhow!("invalid rune ID: {s}"))?;

Ok(Self {
height: height.parse()?,
index: index.parse()?,
})
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -57,6 +72,22 @@ mod tests {
);
}

#[test]
fn from_str() {
assert!("/".parse::<RuneId>().is_err());
assert!("1/".parse::<RuneId>().is_err());
assert!("/2".parse::<RuneId>().is_err());
assert!("a/2".parse::<RuneId>().is_err());
assert!("1/a".parse::<RuneId>().is_err());
assert_eq!(
"1/2".parse::<RuneId>().unwrap(),
RuneId {
height: 1,
index: 2
}
);
}

#[test]
fn try_from() {
assert_eq!(
Expand Down
70 changes: 68 additions & 2 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl Server {
.route("/runes", get(Self::runes))
.route("/sat/:sat", get(Self::sat))
.route("/search", get(Self::search_by_query))
.route("/search/:query", get(Self::search_by_path))
.route("/search/*query", get(Self::search_by_path))
.route("/static/*path", get(Self::static_asset))
.route("/status", get(Self::status))
.route("/tx/:txid", get(Self::transaction))
Expand Down Expand Up @@ -717,8 +717,10 @@ impl Server {
fn search_inner(index: &Index, query: &str) -> ServerResult<Redirect> {
lazy_static! {
static ref HASH: Regex = Regex::new(r"^[[:xdigit:]]{64}$").unwrap();
static ref OUTPOINT: Regex = Regex::new(r"^[[:xdigit:]]{64}:\d+$").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();
Expand All @@ -733,6 +735,16 @@ impl Server {
Ok(Redirect::to(&format!("/output/{query}")))
} else if INSCRIPTION_ID.is_match(query) {
Ok(Redirect::to(&format!("/inscription/{query}")))
} else if RUNE.is_match(query) {
Ok(Redirect::to(&format!("/rune/{query}")))
} else if RUNE_ID.is_match(query) {
let id = query
.parse::<RuneId>()
.map_err(|err| ServerError::BadRequest(err.to_string()))?;

let rune = index.get_rune_by_id(id)?.ok_or_not_found(|| "rune ID")?;

Ok(Redirect::to(&format!("/rune/{rune}")))
} else {
Ok(Redirect::to(&format!("/sat/{query}")))
}
Expand Down Expand Up @@ -1693,6 +1705,11 @@ mod tests {
TestServer::new().assert_redirect("/search?query=0", "/sat/0");
}

#[test]
fn search_by_query_returns_rune() {
TestServer::new().assert_redirect("/search?query=ABCD", "/rune/ABCD");
}

#[test]
fn search_by_query_returns_inscription() {
TestServer::new().assert_redirect(
Expand Down Expand Up @@ -1743,6 +1760,55 @@ mod tests {
);
}

#[test]
fn search_by_path_returns_rune() {
TestServer::new().assert_redirect("/search/ABCD", "/rune/ABCD");
}

#[test]
fn search_by_rune_id_returns_rune() {
let server = TestServer::new_with_regtest_with_index_runes();

server.mine_blocks(1);

let rune = Rune(u128::from(21_000_000 * COIN_VALUE));

server.assert_response_regex(format!("/rune/{rune}"), StatusCode::NOT_FOUND, ".*");

server.bitcoin_rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0, inscription("text/plain", "hello").to_witness())],
op_return: Some(
Runestone {
edicts: vec![Edict {
id: 0,
amount: u128::max_value(),
output: 0,
}],
etching: Some(Etching {
rune,
..Default::default()
}),
..Default::default()
}
.encipher(),
),
..Default::default()
});

server.mine_blocks(1);

server.assert_redirect("/search/2/1", "/rune/NVTDIJZYIPU");
server.assert_redirect("/search?query=2/1", "/rune/NVTDIJZYIPU");

server.assert_response_regex("/rune/100/200", StatusCode::NOT_FOUND, ".*");

server.assert_response_regex(
"/search/100000000000000000000/200000000000000000",
StatusCode::BAD_REQUEST,
".*",
);
}

#[test]
fn http_to_https_redirect_with_path() {
TestServer::new_with_args(&[], &["--redirect-http-to-https", "--https"]).assert_redirect(
Expand Down