Skip to content

Commit 26c4487

Browse files
committed
refactor: improve clarity_mark_key and clarity_metadata request parsing
1 parent ccf60f1 commit 26c4487

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

clarity/src/vm/representations.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ lazy_static! {
5151
"({})|({})",
5252
*STANDARD_PRINCIPAL_REGEX_STRING, *CONTRACT_PRINCIPAL_REGEX_STRING
5353
);
54+
static ref CLARITY_NAME_NO_BOUNDARIES_REGEX_STRING: String =
55+
"[a-zA-Z]([a-zA-Z0-9]|[-_!?+<>=/*])*|[-+=/*]|[<>]=?".into();
5456
pub static ref CLARITY_NAME_REGEX_STRING: String =
5557
"^[a-zA-Z]([a-zA-Z0-9]|[-_!?+<>=/*])*$|^[-+=/*]$|^[<>]=?$".into();
5658
pub static ref CLARITY_NAME_REGEX: Regex =
@@ -65,16 +67,17 @@ lazy_static! {
6567
.unwrap()
6668
};
6769
pub static ref MARF_KEY_FOR_TRIP_REGEX_STRING: String = format!(
68-
r"vm::{}::\d+::.*",
70+
r"vm::{}::\d+::({})",
6971
*CONTRACT_PRINCIPAL_REGEX_STRING,
72+
*CLARITY_NAME_NO_BOUNDARIES_REGEX_STRING,
7073
);
7174
pub static ref MARF_KEY_FOR_QUAD_REGEX_STRING: String = format!(
72-
r"{}::.*",
75+
r"{}::[0-9a-fA-F]+",
7376
*MARF_KEY_FOR_TRIP_REGEX_STRING,
7477
);
7578
pub static ref METADATA_KEY_REGEX_STRING: String = format!(
76-
r"vm-metadata::\d+::.*",
77-
79+
r"vm-metadata::\d+::(contract|contract-size|contract-src|contract-data-size|({}))",
80+
*CLARITY_NAME_NO_BOUNDARIES_REGEX_STRING,
7881
);
7982
}
8083

stackslib/src/net/api/getclaritymarfvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl HttpRequest for RPCGetClarityMarfValueRequestHandler {
8686
));
8787
}
8888

89-
let marf_key = request::get_key(captures, "clarity_marf_key")?;
89+
let marf_key = request::get_clarity_key(captures, "clarity_marf_key")?;
9090

9191
self.clarity_marf_key = Some(marf_key);
9292

stackslib/src/net/api/getclaritymetadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl HttpRequest for RPCGetClarityMetadataRequestHandler {
8989
}
9090

9191
let contract_identifier = request::get_contract_address(captures, "address", "contract")?;
92-
let metadata_key = request::get_key(captures, "clarity_metadata_key")?;
92+
let metadata_key = request::get_clarity_key(captures, "clarity_metadata_key")?;
9393

9494
self.contract_identifier = Some(contract_identifier);
9595
self.clarity_metadata_key = Some(metadata_key);

stackslib/src/net/api/tests/getclaritymarfvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn test_try_make_response() {
128128

129129
// existing data
130130
let response = responses.remove(0);
131-
println!(
131+
debug!(
132132
"Response:\n{}\n",
133133
std::str::from_utf8(&response.try_serialize().unwrap()).unwrap()
134134
);

stackslib/src/net/httpcore.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,15 @@ pub mod request {
247247
Ok(txid)
248248
}
249249

250-
/// Get and parse a MARF key from a path's captures, given the name of the regex field.
251-
pub fn get_key(captures: &Captures, key: &str) -> Result<String, HttpError> {
252-
let marf_key = if let Some(marf_key_str) = captures.name(key) {
253-
marf_key_str.as_str().to_string()
250+
/// Get a clarity key (MARF or Metadata) from a path's captures, given the name of the regex field.
251+
pub fn get_clarity_key(captures: &Captures, clarity_key: &str) -> Result<String, HttpError> {
252+
let key = if let Some(key_str) = captures.name(clarity_key) {
253+
key_str.as_str().to_string()
254254
} else {
255-
return Err(HttpError::Http(404, format!("Missing `{}`", key)));
255+
return Err(HttpError::Http(404, format!("Missing `{}`", clarity_key)));
256256
};
257257

258-
Ok(marf_key)
258+
Ok(key)
259259
}
260260

261261
/// Get and parse a Clarity name from a path's captures, given the name of the regex field.

0 commit comments

Comments
 (0)