Skip to content

Commit 71cad36

Browse files
committed
refactor: rename get clarity marf value and metadata endpoints
1 parent 5619ff2 commit 71cad36

File tree

6 files changed

+62
-60
lines changed

6 files changed

+62
-60
lines changed

clarity/src/vm/representations.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ 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();
5654
pub static ref CLARITY_NAME_REGEX_STRING: String =
5755
"^[a-zA-Z]([a-zA-Z0-9]|[-_!?+<>=/*])*$|^[-+=/*]$|^[<>]=?$".into();
5856
pub static ref CLARITY_NAME_REGEX: Regex =
@@ -66,19 +64,6 @@ lazy_static! {
6664
Regex::new(format!("^{}$|^__transient$", CONTRACT_NAME_REGEX_STRING.as_str()).as_str())
6765
.unwrap()
6866
};
69-
pub static ref MARF_KEY_FOR_TRIP_REGEX_STRING: String = format!(
70-
r"vm::{}::\d+::({})",
71-
*CONTRACT_PRINCIPAL_REGEX_STRING,
72-
*CLARITY_NAME_NO_BOUNDARIES_REGEX_STRING,
73-
);
74-
pub static ref MARF_KEY_FOR_QUAD_REGEX_STRING: String = format!(
75-
r"{}::[0-9a-fA-F]+",
76-
*MARF_KEY_FOR_TRIP_REGEX_STRING,
77-
);
78-
pub static ref METADATA_KEY_REGEX_STRING: String = format!(
79-
r"vm-metadata::\d+::(contract|contract-size|contract-src|contract-data-size|({}))",
80-
*CLARITY_NAME_NO_BOUNDARIES_REGEX_STRING,
81-
);
8267
}
8368

8469
guarded_string!(

stackslib/src/net/api/getclaritymarfvalue.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use clarity::vm::clarity::ClarityConnection;
18-
use clarity::vm::representations::{
19-
MARF_KEY_FOR_QUAD_REGEX_STRING, MARF_KEY_FOR_TRIP_REGEX_STRING,
20-
};
18+
use clarity::vm::representations::CONTRACT_PRINCIPAL_REGEX_STRING;
19+
use lazy_static::lazy_static;
2120
use regex::{Captures, Regex};
2221
use stacks_common::types::net::PeerHost;
2322
use stacks_common::util::hash::to_hex;
@@ -32,8 +31,19 @@ use crate::net::httpcore::{
3231
};
3332
use crate::net::{Error as NetError, StacksNodeState, TipRequest};
3433

34+
lazy_static! {
35+
static ref CLARITY_NAME_NO_BOUNDARIES_REGEX_STRING: String =
36+
"[a-zA-Z]([a-zA-Z0-9]|[-_!?+<>=/*])*|[-+=/*]|[<>]=?".into();
37+
static ref MARF_KEY_FOR_TRIP_REGEX_STRING: String = format!(
38+
r"vm::{}::\d+::({})",
39+
*CONTRACT_PRINCIPAL_REGEX_STRING, *CLARITY_NAME_NO_BOUNDARIES_REGEX_STRING,
40+
);
41+
static ref MARF_KEY_FOR_QUAD_REGEX_STRING: String =
42+
format!(r"{}::[0-9a-fA-F]+", *MARF_KEY_FOR_TRIP_REGEX_STRING,);
43+
}
44+
3545
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
36-
pub struct ClarityMarfValueResponse {
46+
pub struct ClarityMarfResponse {
3747
pub data: String,
3848
#[serde(rename = "proof")]
3949
#[serde(default)]
@@ -42,10 +52,10 @@ pub struct ClarityMarfValueResponse {
4252
}
4353

4454
#[derive(Clone)]
45-
pub struct RPCGetClarityMarfValueRequestHandler {
55+
pub struct RPCGetClarityMarfRequestHandler {
4656
pub clarity_marf_key: Option<String>,
4757
}
48-
impl RPCGetClarityMarfValueRequestHandler {
58+
impl RPCGetClarityMarfRequestHandler {
4959
pub fn new() -> Self {
5060
Self {
5161
clarity_marf_key: None,
@@ -54,21 +64,21 @@ impl RPCGetClarityMarfValueRequestHandler {
5464
}
5565

5666
/// Decode the HTTP request
57-
impl HttpRequest for RPCGetClarityMarfValueRequestHandler {
67+
impl HttpRequest for RPCGetClarityMarfRequestHandler {
5868
fn verb(&self) -> &'static str {
5969
"GET"
6070
}
6171

6272
fn path_regex(&self) -> Regex {
6373
Regex::new(&format!(
64-
r"^/v2/clarity_marf_value/(?P<clarity_marf_key>(vm-epoch::epoch-version)|({})|({}))$",
74+
r"^/v2/clarity/marf/(?P<clarity_marf_key>(vm-epoch::epoch-version)|({})|({}))$",
6575
*MARF_KEY_FOR_TRIP_REGEX_STRING, *MARF_KEY_FOR_QUAD_REGEX_STRING
6676
))
6777
.unwrap()
6878
}
6979

7080
fn metrics_identifier(&self) -> &str {
71-
"/v2/clarity_marf_value/:clarity_marf_key"
81+
"/v2/clarity/marf/:clarity_marf_key"
7282
}
7383

7484
/// Try to decode this request.
@@ -86,7 +96,11 @@ impl HttpRequest for RPCGetClarityMarfValueRequestHandler {
8696
));
8797
}
8898

89-
let marf_key = request::get_clarity_key(captures, "clarity_marf_key")?;
99+
let marf_key = if let Some(key_str) = captures.name("clarity_marf_key") {
100+
key_str.as_str().to_string()
101+
} else {
102+
return Err(Error::Http(404, "Missing `clarity_marf_key`".to_string()));
103+
};
90104

91105
self.clarity_marf_key = Some(marf_key);
92106

@@ -96,7 +110,7 @@ impl HttpRequest for RPCGetClarityMarfValueRequestHandler {
96110
}
97111

98112
/// Handle the HTTP request
99-
impl RPCRequestHandler for RPCGetClarityMarfValueRequestHandler {
113+
impl RPCRequestHandler for RPCGetClarityMarfRequestHandler {
100114
/// Reset internal state
101115
fn restart(&mut self) {
102116
self.clarity_marf_key = None;
@@ -143,7 +157,7 @@ impl RPCRequestHandler for RPCGetClarityMarfValueRequestHandler {
143157
};
144158

145159
let data = format!("0x{}", value_hex);
146-
Some(ClarityMarfValueResponse { data, marf_proof })
160+
Some(ClarityMarfResponse { data, marf_proof })
147161
})
148162
},
149163
)
@@ -177,19 +191,19 @@ impl RPCRequestHandler for RPCGetClarityMarfValueRequestHandler {
177191
}
178192

179193
/// Decode the HTTP response
180-
impl HttpResponse for RPCGetClarityMarfValueRequestHandler {
194+
impl HttpResponse for RPCGetClarityMarfRequestHandler {
181195
fn try_parse_response(
182196
&self,
183197
preamble: &HttpResponsePreamble,
184198
body: &[u8],
185199
) -> Result<HttpResponsePayload, Error> {
186-
let marf_value: ClarityMarfValueResponse = parse_json(preamble, body)?;
200+
let marf_value: ClarityMarfResponse = parse_json(preamble, body)?;
187201
Ok(HttpResponsePayload::try_from_json(marf_value)?)
188202
}
189203
}
190204

191205
impl StacksHttpRequest {
192-
pub fn new_getclaritymarfvalue(
206+
pub fn new_getclaritymarf(
193207
host: PeerHost,
194208
clarity_marf_key: String,
195209
tip_req: TipRequest,
@@ -198,7 +212,7 @@ impl StacksHttpRequest {
198212
StacksHttpRequest::new_for_peer(
199213
host,
200214
"GET".into(),
201-
format!("/v2/clarity_marf_value/{}", &clarity_marf_key),
215+
format!("/v2/clarity/marf/{}", &clarity_marf_key),
202216
HttpRequestContents::new()
203217
.for_tip(tip_req)
204218
.query_arg("proof".into(), if with_proof { "1" } else { "0" }.into()),
@@ -208,10 +222,10 @@ impl StacksHttpRequest {
208222
}
209223

210224
impl StacksHttpResponse {
211-
pub fn decode_clarity_marf_value_response(self) -> Result<ClarityMarfValueResponse, NetError> {
225+
pub fn decode_clarity_marf_response(self) -> Result<ClarityMarfResponse, NetError> {
212226
let contents = self.get_http_payload_ok()?;
213227
let contents_json: serde_json::Value = contents.try_into()?;
214-
let resp: ClarityMarfValueResponse = serde_json::from_value(contents_json)
228+
let resp: ClarityMarfResponse = serde_json::from_value(contents_json)
215229
.map_err(|_e| NetError::DeserializeError("Failed to load from JSON".to_string()))?;
216230
Ok(resp)
217231
}

stackslib/src/net/api/getclaritymetadata.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use clarity::vm::clarity::ClarityConnection;
18-
use clarity::vm::representations::{
19-
CONTRACT_NAME_REGEX_STRING, METADATA_KEY_REGEX_STRING, STANDARD_PRINCIPAL_REGEX_STRING,
20-
};
18+
use clarity::vm::representations::{CONTRACT_NAME_REGEX_STRING, STANDARD_PRINCIPAL_REGEX_STRING};
2119
use clarity::vm::types::QualifiedContractIdentifier;
2220
use clarity::vm::ContractName;
21+
use lazy_static::lazy_static;
2322
use regex::{Captures, Regex};
2423
use stacks_common::types::chainstate::StacksAddress;
2524
use stacks_common::types::net::PeerHost;
@@ -34,6 +33,15 @@ use crate::net::httpcore::{
3433
};
3534
use crate::net::{Error as NetError, StacksNodeState, TipRequest};
3635

36+
lazy_static! {
37+
static ref CLARITY_NAME_NO_BOUNDARIES_REGEX_STRING: String =
38+
"[a-zA-Z]([a-zA-Z0-9]|[-_!?+<>=/*])*|[-+=/*]|[<>]=?".into();
39+
static ref METADATA_KEY_REGEX_STRING: String = format!(
40+
r"vm-metadata::\d+::(contract|contract-size|contract-src|contract-data-size|({}))",
41+
*CLARITY_NAME_NO_BOUNDARIES_REGEX_STRING,
42+
);
43+
}
44+
3745
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
3846
pub struct ClarityMetadataResponse {
3947
pub data: String,
@@ -61,7 +69,7 @@ impl HttpRequest for RPCGetClarityMetadataRequestHandler {
6169

6270
fn path_regex(&self) -> Regex {
6371
Regex::new(&format!(
64-
r"^/v2/clarity_metadata/(?P<address>{})/(?P<contract>{})/(?P<clarity_metadata_key>(analysis)|({}))$",
72+
r"^/v2/clarity/metadata/(?P<address>{})/(?P<contract>{})/(?P<clarity_metadata_key>(analysis)|({}))$",
6573
*STANDARD_PRINCIPAL_REGEX_STRING,
6674
*CONTRACT_NAME_REGEX_STRING,
6775
*METADATA_KEY_REGEX_STRING
@@ -70,7 +78,7 @@ impl HttpRequest for RPCGetClarityMetadataRequestHandler {
7078
}
7179

7280
fn metrics_identifier(&self) -> &str {
73-
"/v2/clarity_metadata/:principal/:contract_name/:clarity_metadata_key"
81+
"/v2/clarity/metadata/:principal/:contract_name/:clarity_metadata_key"
7482
}
7583

7684
/// Try to decode this request.
@@ -89,7 +97,15 @@ impl HttpRequest for RPCGetClarityMetadataRequestHandler {
8997
}
9098

9199
let contract_identifier = request::get_contract_address(captures, "address", "contract")?;
92-
let metadata_key = request::get_clarity_key(captures, "clarity_metadata_key")?;
100+
101+
let metadata_key = if let Some(key_str) = captures.name("clarity_metadata_key") {
102+
key_str.as_str().to_string()
103+
} else {
104+
return Err(Error::Http(
105+
404,
106+
"Missing `clarity_metadata_key`".to_string(),
107+
));
108+
};
93109

94110
self.contract_identifier = Some(contract_identifier);
95111
self.clarity_metadata_key = Some(metadata_key);
@@ -197,7 +213,7 @@ impl StacksHttpRequest {
197213
host,
198214
"GET".into(),
199215
format!(
200-
"/v2/clarity_metadata/{}/{}/{}",
216+
"/v2/clarity/metadata/{}/{}/{}",
201217
&contract_addr, &contract_name, &clarity_metadata_key
202218
),
203219
HttpRequestContents::new().for_tip(tip_req),

stackslib/src/net/api/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ impl StacksHttp {
9292
self.register_rpc_endpoint(getattachmentsinv::RPCGetAttachmentsInvRequestHandler::new());
9393
self.register_rpc_endpoint(getblock::RPCBlocksRequestHandler::new());
9494
self.register_rpc_endpoint(getblock_v3::RPCNakamotoBlockRequestHandler::new());
95-
self.register_rpc_endpoint(
96-
getclaritymarfvalue::RPCGetClarityMarfValueRequestHandler::new(),
97-
);
95+
self.register_rpc_endpoint(getclaritymarfvalue::RPCGetClarityMarfRequestHandler::new());
9896
self.register_rpc_endpoint(getclaritymetadata::RPCGetClarityMetadataRequestHandler::new());
9997
self.register_rpc_endpoint(getconstantval::RPCGetConstantValRequestHandler::new());
10098
self.register_rpc_endpoint(getcontractabi::RPCGetContractAbiRequestHandler::new());

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn test_try_parse_request() {
4343
let valid_keys = [vm_key_epoch, vm_key_trip, vm_key_quad];
4444

4545
for key in valid_keys {
46-
let request = StacksHttpRequest::new_getclaritymarfvalue(
46+
let request = StacksHttpRequest::new_getclaritymarf(
4747
addr.into(),
4848
key.to_string(),
4949
TipRequest::SpecificTip(StacksBlockId([0x22; 32])),
@@ -58,7 +58,7 @@ fn test_try_parse_request() {
5858
let bytes = request.try_serialize().unwrap();
5959

6060
let (parsed_preamble, offset) = http.read_preamble(&bytes).unwrap();
61-
let mut handler = getclaritymarfvalue::RPCGetClarityMarfValueRequestHandler::new();
61+
let mut handler = getclaritymarfvalue::RPCGetClarityMarfRequestHandler::new();
6262
let mut parsed_request = http
6363
.handle_try_parse_request(
6464
&mut handler,
@@ -88,7 +88,7 @@ fn test_try_make_response() {
8888
let mut requests = vec![];
8989

9090
// query existing
91-
let request = StacksHttpRequest::new_getclaritymarfvalue(
91+
let request = StacksHttpRequest::new_getclaritymarf(
9292
addr.into(),
9393
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world::1::bar".to_string(),
9494
TipRequest::UseLatestAnchoredTip,
@@ -97,7 +97,7 @@ fn test_try_make_response() {
9797
requests.push(request);
9898

9999
// query existing unconfirmed
100-
let request = StacksHttpRequest::new_getclaritymarfvalue(
100+
let request = StacksHttpRequest::new_getclaritymarf(
101101
addr.into(),
102102
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world-unconfirmed::1::bar-unconfirmed"
103103
.to_string(),
@@ -107,7 +107,7 @@ fn test_try_make_response() {
107107
requests.push(request);
108108

109109
// query non-existant var
110-
let request = StacksHttpRequest::new_getclaritymarfvalue(
110+
let request = StacksHttpRequest::new_getclaritymarf(
111111
addr.into(),
112112
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world::1::does-not-exist".to_string(),
113113
TipRequest::UseLatestAnchoredTip,
@@ -116,7 +116,7 @@ fn test_try_make_response() {
116116
requests.push(request);
117117

118118
// query non-existant contract
119-
let request = StacksHttpRequest::new_getclaritymarfvalue(
119+
let request = StacksHttpRequest::new_getclaritymarf(
120120
addr.into(),
121121
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.does-not-exist::1::bar".to_string(),
122122
TipRequest::UseLatestAnchoredTip,

stackslib/src/net/httpcore.rs

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

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()
254-
} else {
255-
return Err(HttpError::Http(404, format!("Missing `{}`", clarity_key)));
256-
};
257-
258-
Ok(key)
259-
}
260-
261250
/// Get and parse a Clarity name from a path's captures, given the name of the regex field.
262251
pub fn get_clarity_name(captures: &Captures, key: &str) -> Result<ClarityName, HttpError> {
263252
let clarity_name = if let Some(name_str) = captures.name(key) {

0 commit comments

Comments
 (0)