Skip to content

Commit 4dacdd4

Browse files
committed
chore: use new ClarityDB get_data_from_path() to load MARF data by key hash, instead of by key
1 parent 2cdf090 commit 4dacdd4

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

stackslib/src/net/api/getclaritymarfvalue.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use clarity::vm::representations::CONTRACT_PRINCIPAL_REGEX_STRING;
1919
use lazy_static::lazy_static;
2020
use regex::{Captures, Regex};
2121
use stacks_common::types::net::PeerHost;
22+
use stacks_common::types::chainstate::TrieHash;
2223
use stacks_common::util::hash::to_hex;
2324

2425
use crate::net::http::{
@@ -31,17 +32,6 @@ use crate::net::httpcore::{
3132
};
3233
use crate::net::{Error as NetError, StacksNodeState, TipRequest};
3334

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-
4535
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
4636
pub struct ClarityMarfResponse {
4737
pub data: String,
@@ -53,12 +43,12 @@ pub struct ClarityMarfResponse {
5343

5444
#[derive(Clone)]
5545
pub struct RPCGetClarityMarfRequestHandler {
56-
pub clarity_marf_key: Option<String>,
46+
pub marf_key_hash: Option<TrieHash>,
5747
}
5848
impl RPCGetClarityMarfRequestHandler {
5949
pub fn new() -> Self {
6050
Self {
61-
clarity_marf_key: None,
51+
marf_key_hash: None,
6252
}
6353
}
6454
}
@@ -70,15 +60,11 @@ impl HttpRequest for RPCGetClarityMarfRequestHandler {
7060
}
7161

7262
fn path_regex(&self) -> Regex {
73-
Regex::new(&format!(
74-
r"^/v2/clarity/marf/(?P<clarity_marf_key>(vm-epoch::epoch-version)|({})|({}))$",
75-
*MARF_KEY_FOR_TRIP_REGEX_STRING, *MARF_KEY_FOR_QUAD_REGEX_STRING
76-
))
77-
.unwrap()
63+
Regex::new(r#"^/v2/clarity/marf/(?P<marf_key_hash>[0-9a-f]{64})$"#).unwrap()
7864
}
7965

8066
fn metrics_identifier(&self) -> &str {
81-
"/v2/clarity/marf/:clarity_marf_key"
67+
"/v2/clarity/marf/:marf_key_hash"
8268
}
8369

8470
/// Try to decode this request.
@@ -96,13 +82,14 @@ impl HttpRequest for RPCGetClarityMarfRequestHandler {
9682
));
9783
}
9884

99-
let marf_key = if let Some(key_str) = captures.name("clarity_marf_key") {
100-
key_str.as_str().to_string()
85+
let marf_key = if let Some(key_str) = captures.name("marf_key_hash") {
86+
TrieHash::from_hex(key_str.as_str())
87+
.map_err(|e| Error::Http(400, format!("Invalid hash string: {e:?}")))?
10188
} else {
102-
return Err(Error::Http(404, "Missing `clarity_marf_key`".to_string()));
89+
return Err(Error::Http(404, "Missing `marf_key_hash`".to_string()));
10390
};
10491

105-
self.clarity_marf_key = Some(marf_key);
92+
self.marf_key_hash = Some(marf_key);
10693

10794
let contents = HttpRequestContents::new().query_string(query);
10895
Ok(contents)
@@ -113,7 +100,7 @@ impl HttpRequest for RPCGetClarityMarfRequestHandler {
113100
impl RPCRequestHandler for RPCGetClarityMarfRequestHandler {
114101
/// Reset internal state
115102
fn restart(&mut self) {
116-
self.clarity_marf_key = None;
103+
self.marf_key_hash = None;
117104
}
118105

119106
/// Make the response
@@ -123,8 +110,8 @@ impl RPCRequestHandler for RPCGetClarityMarfRequestHandler {
123110
contents: HttpRequestContents,
124111
node: &mut StacksNodeState,
125112
) -> Result<(HttpResponsePreamble, HttpResponseContents), NetError> {
126-
let clarity_marf_key = self.clarity_marf_key.take().ok_or(NetError::SendError(
127-
"`clarity_marf_key` not set".to_string(),
113+
let marf_key_hash = self.marf_key_hash.take().ok_or(NetError::SendError(
114+
"`marf_key_hash` not set".to_string(),
128115
))?;
129116

130117
let tip = match node.load_stacks_chain_tip(&preamble, &contents) {
@@ -144,13 +131,13 @@ impl RPCRequestHandler for RPCGetClarityMarfRequestHandler {
144131
clarity_tx.with_clarity_db_readonly(|clarity_db| {
145132
let (value_hex, marf_proof): (String, _) = if with_proof {
146133
clarity_db
147-
.get_data_with_proof(&clarity_marf_key)
134+
.get_data_with_proof_by_hash(&marf_key_hash)
148135
.ok()
149136
.flatten()
150137
.map(|(a, b)| (a, Some(format!("0x{}", to_hex(&b)))))?
151138
} else {
152139
clarity_db
153-
.get_data(&clarity_marf_key)
140+
.get_data_by_hash(&marf_key_hash)
154141
.ok()
155142
.flatten()
156143
.map(|a| (a, None))?
@@ -168,7 +155,7 @@ impl RPCRequestHandler for RPCGetClarityMarfRequestHandler {
168155
Ok(Some(None)) => {
169156
return StacksHttpResponse::new_error(
170157
&preamble,
171-
&HttpNotFound::new("Marf key not found".to_string()),
158+
&HttpNotFound::new("Marf key hash not found".to_string()),
172159
)
173160
.try_into_contents()
174161
.map_err(NetError::from);
@@ -205,14 +192,14 @@ impl HttpResponse for RPCGetClarityMarfRequestHandler {
205192
impl StacksHttpRequest {
206193
pub fn new_getclaritymarf(
207194
host: PeerHost,
208-
clarity_marf_key: String,
195+
marf_key_hash: TrieHash,
209196
tip_req: TipRequest,
210197
with_proof: bool,
211198
) -> StacksHttpRequest {
212199
StacksHttpRequest::new_for_peer(
213200
host,
214201
"GET".into(),
215-
format!("/v2/clarity/marf/{}", &clarity_marf_key),
202+
format!("/v2/clarity/marf/{}", &marf_key_hash),
216203
HttpRequestContents::new()
217204
.for_tip(tip_req)
218205
.query_arg("proof".into(), if with_proof { "1" } else { "0" }.into()),

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use clarity::vm::types::{QualifiedContractIdentifier, StacksAddressExtensions};
2020
use clarity::vm::{ClarityName, ContractName};
2121
use stacks_common::codec::StacksMessageCodec;
2222
use stacks_common::types::chainstate::StacksAddress;
23+
use stacks_common::types::chainstate::TrieHash;
2324
use stacks_common::types::net::PeerHost;
2425
use stacks_common::types::Address;
2526

@@ -37,15 +38,15 @@ fn test_try_parse_request() {
3738
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 33333);
3839
let mut http = StacksHttp::new(addr.clone(), &ConnectionOptions::default());
3940

40-
let vm_key_epoch = "vm-epoch::epoch-version";
41-
let vm_key_trip = "vm::ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5.counter::1::count";
42-
let vm_key_quad = "vm::ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5.counter::0::data::1234";
41+
let vm_key_epoch = TrieHash::from_key("vm-epoch::epoch-version");
42+
let vm_key_trip = TrieHash::from_key("vm::ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5.counter::1::count");
43+
let vm_key_quad = TrieHash::from_key("vm::ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5.counter::0::data::1234");
4344
let valid_keys = [vm_key_epoch, vm_key_trip, vm_key_quad];
4445

4546
for key in valid_keys {
4647
let request = StacksHttpRequest::new_getclaritymarf(
4748
addr.into(),
48-
key.to_string(),
49+
key,
4950
TipRequest::SpecificTip(StacksBlockId([0x22; 32])),
5051
true,
5152
);
@@ -72,12 +73,12 @@ fn test_try_parse_request() {
7273
let (preamble, contents) = parsed_request.destruct();
7374

7475
// consumed path args
75-
assert_eq!(handler.clarity_marf_key, Some(key.to_string()));
76+
assert_eq!(handler.marf_key_hash, Some(key.clone()));
7677

7778
assert_eq!(&preamble, request.preamble());
7879

7980
handler.restart();
80-
assert!(handler.clarity_marf_key.is_none());
81+
assert!(handler.marf_key_hash.is_none());
8182
}
8283
}
8384

@@ -90,7 +91,7 @@ fn test_try_make_response() {
9091
// query existing
9192
let request = StacksHttpRequest::new_getclaritymarf(
9293
addr.into(),
93-
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world::1::bar".to_string(),
94+
TrieHash::from_key("vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world::1::bar"),
9495
TipRequest::UseLatestAnchoredTip,
9596
true,
9697
);
@@ -99,8 +100,7 @@ fn test_try_make_response() {
99100
// query existing unconfirmed
100101
let request = StacksHttpRequest::new_getclaritymarf(
101102
addr.into(),
102-
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world-unconfirmed::1::bar-unconfirmed"
103-
.to_string(),
103+
TrieHash::from_key("vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world-unconfirmed::1::bar-unconfirmed"),
104104
TipRequest::UseLatestUnconfirmedTip,
105105
true,
106106
);
@@ -109,7 +109,7 @@ fn test_try_make_response() {
109109
// query non-existant var
110110
let request = StacksHttpRequest::new_getclaritymarf(
111111
addr.into(),
112-
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world::1::does-not-exist".to_string(),
112+
TrieHash::from_key("vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.hello-world::1::does-not-exist"),
113113
TipRequest::UseLatestAnchoredTip,
114114
true,
115115
);
@@ -118,7 +118,7 @@ fn test_try_make_response() {
118118
// query non-existant contract
119119
let request = StacksHttpRequest::new_getclaritymarf(
120120
addr.into(),
121-
"vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.does-not-exist::1::bar".to_string(),
121+
TrieHash::from_key("vm::ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.does-not-exist::1::bar"),
122122
TipRequest::UseLatestAnchoredTip,
123123
true,
124124
);

0 commit comments

Comments
 (0)