• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stacks-network / stacks-core / 25903914664-1

15 May 2026 06:28AM UTC coverage: 47.122% (-38.8%) from 85.959%
25903914664-1

Pull #7199

github

94e391
web-flow
Merge 109f2828c into 1c7b8e6ac
Pull Request #7199: Feat: L1 and L2 early unlocks, updating signer

103343 of 219309 relevant lines covered (47.12%)

12880462.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.69
/stackslib/src/net/api/getattachmentsinv.rs
1
// Copyright (C) 2013-2020 Blockstack PBC, a public benefit corporation
2
// Copyright (C) 2020-2023 Stacks Open Internet Foundation
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

17
use std::collections::HashSet;
18

19
use regex::{Captures, Regex};
20
use stacks_common::types::chainstate::StacksBlockId;
21
use stacks_common::types::net::PeerHost;
22
use url::form_urlencoded;
23

24
use crate::net::atlas::{
25
    AttachmentPage, GetAttachmentsInvResponse, MAX_ATTACHMENT_INV_PAGES_PER_REQUEST,
26
};
27
use crate::net::http::{
28
    parse_json, Error, HttpBadRequest, HttpNotFound, HttpRequest, HttpRequestContents,
29
    HttpRequestPreamble, HttpResponse, HttpResponseContents, HttpResponsePayload,
30
    HttpResponsePreamble,
31
};
32
use crate::net::httpcore::{RPCRequestHandler, StacksHttpRequest, StacksHttpResponse};
33
use crate::net::{Error as NetError, StacksNodeState};
34

35
#[derive(Clone)]
36
pub struct RPCGetAttachmentsInvRequestHandler {
37
    pub index_block_hash: Option<StacksBlockId>,
38
    pub page_indexes: Option<Vec<u32>>,
39
}
40

41
impl RPCGetAttachmentsInvRequestHandler {
42
    pub fn new() -> Self {
1,059,041✔
43
        Self {
1,059,041✔
44
            index_block_hash: None,
1,059,041✔
45
            page_indexes: None,
1,059,041✔
46
        }
1,059,041✔
47
    }
1,059,041✔
48
}
49

50
/// Decode the HTTP request
51
impl HttpRequest for RPCGetAttachmentsInvRequestHandler {
52
    fn verb(&self) -> &'static str {
1,059,041✔
53
        "GET"
1,059,041✔
54
    }
1,059,041✔
55

56
    fn path_regex(&self) -> Regex {
2,118,082✔
57
        Regex::new("^/v2/attachments/inv$").unwrap()
2,118,082✔
58
    }
2,118,082✔
59

60
    fn metrics_identifier(&self) -> &str {
×
61
        "/v2/attachments/inv"
×
62
    }
×
63

64
    /// Try to decode this request.
65
    /// There's nothing to load here, so just make sure the request is well-formed.
66
    fn try_parse_request(
×
67
        &mut self,
×
68
        preamble: &HttpRequestPreamble,
×
69
        _captures: &Captures,
×
70
        query: Option<&str>,
×
71
        _body: &[u8],
×
72
    ) -> Result<HttpRequestContents, Error> {
×
73
        if preamble.get_content_length() != 0 {
×
74
            return Err(Error::DecodeError(
×
75
                "Invalid Http request: expected 0-length body".to_string(),
×
76
            ));
×
77
        }
×
78

79
        let query_str = if let Some(qs) = query {
×
80
            qs
×
81
        } else {
82
            return Err(Error::DecodeError(
×
83
                "Invalid Http request: expecting index_block_hash and pages_indexes".to_string(),
×
84
            ));
×
85
        };
86

87
        let mut index_block_hash = None;
×
88
        let mut page_indexes = HashSet::new();
×
89

90
        // expect index_block_hash= and page_indexes=
91
        for (key, value) in form_urlencoded::parse(query_str.as_bytes()) {
×
92
            if key == "index_block_hash" {
×
93
                index_block_hash = StacksBlockId::from_hex(&value).ok();
×
94
            } else if key == "pages_indexes" {
×
95
                let pages_indexes_value = value.to_string();
×
96
                for entry in pages_indexes_value.split(',') {
×
97
                    if let Ok(page_index) = entry.parse::<u32>() {
×
98
                        page_indexes.insert(page_index);
×
99
                    }
×
100
                }
101
            }
×
102
        }
103

104
        let index_block_hash = if let Some(ibh) = index_block_hash {
×
105
            ibh
×
106
        } else {
107
            return Err(Error::DecodeError(
×
108
                "Invalid Http request: expecting index_block_hash".to_string(),
×
109
            ));
×
110
        };
111

112
        if page_indexes.is_empty() {
×
113
            return Err(Error::DecodeError(
×
114
                "Invalid Http request: expecting pages_indexes".to_string(),
×
115
            ));
×
116
        }
×
117

118
        let mut page_index_list: Vec<u32> = page_indexes.into_iter().collect();
×
119
        page_index_list.sort();
×
120

121
        self.index_block_hash = Some(index_block_hash);
×
122
        self.page_indexes = Some(page_index_list);
×
123

124
        Ok(HttpRequestContents::new().query_string(query))
×
125
    }
×
126
}
127

128
impl RPCRequestHandler for RPCGetAttachmentsInvRequestHandler {
129
    /// Reset internal state
130
    fn restart(&mut self) {
×
131
        self.index_block_hash = None;
×
132
        self.page_indexes = None;
×
133
    }
×
134

135
    fn try_handle_request(
×
136
        &mut self,
×
137
        preamble: HttpRequestPreamble,
×
138
        _contents: HttpRequestContents,
×
139
        node: &mut StacksNodeState,
×
140
    ) -> Result<(HttpResponsePreamble, HttpResponseContents), NetError> {
×
141
        let index_block_hash = self
×
142
            .index_block_hash
×
143
            .take()
×
144
            .ok_or(NetError::SendError("Missing `index_block_hash`".into()))?;
×
145
        let page_indexes = self
×
146
            .page_indexes
×
147
            .take()
×
148
            .ok_or(NetError::SendError("Missing `page_indexes`".into()))?;
×
149

150
        // We are receiving a list of page indexes with a chain tip hash.
151
        // The amount of pages_indexes is capped by MAX_ATTACHMENT_INV_PAGES_PER_REQUEST (8)
152
        // Pages sizes are controlled by the constant ATTACHMENTS_INV_PAGE_SIZE (8), which
153
        // means that a `GET v2/attachments/inv` request can be requesting for a 64 bit vector
154
        // at once.
155
        // Since clients can be asking for non-consecutive pages indexes (1, 5_000, 10_000, ...),
156
        // we will be handling each page index separately.
157
        // We could also add the notion of "budget" so that a client could only get a limited number
158
        // of pages when they are spanning over many blocks.
159
        if page_indexes.len() > MAX_ATTACHMENT_INV_PAGES_PER_REQUEST {
×
160
            let msg = format!(
×
161
                "Number of attachment inv pages is limited by {} per request",
162
                MAX_ATTACHMENT_INV_PAGES_PER_REQUEST
163
            );
164
            warn!("{msg}");
×
165
            return StacksHttpResponse::new_error(&preamble, &HttpBadRequest::new(msg))
×
166
                .try_into_contents()
×
167
                .map_err(NetError::from);
×
168
        }
×
169
        if page_indexes.is_empty() {
×
170
            let msg = "Page indexes missing".to_string();
×
171
            warn!("{msg}");
×
172
            return StacksHttpResponse::new_error(&preamble, &HttpBadRequest::new(msg))
×
173
                .try_into_contents()
×
174
                .map_err(NetError::from);
×
175
        }
×
176

177
        let mut pages = vec![];
×
178

179
        for page_index in page_indexes.iter() {
×
180
            let page_res =
×
181
                node.with_node_state(|network, _sortdb, _chainstate, _mempool, _rpc_args| {
×
182
                    match network
×
183
                        .get_atlasdb()
×
184
                        .get_attachments_available_at_page_index(*page_index, &index_block_hash)
×
185
                    {
186
                        Ok(inventory) => Ok(AttachmentPage {
×
187
                            inventory,
×
188
                            index: *page_index,
×
189
                        }),
×
190
                        Err(e) => {
×
191
                            let msg = format!("Unable to read Atlas DB - {}", e);
×
192
                            warn!("{}", msg);
×
193
                            Err(msg)
×
194
                        }
195
                    }
196
                });
×
197

198
            match page_res {
×
199
                Ok(page) => {
×
200
                    pages.push(page);
×
201
                }
×
202
                Err(msg) => {
×
203
                    return StacksHttpResponse::new_error(&preamble, &HttpNotFound::new(msg))
×
204
                        .try_into_contents()
×
205
                        .map_err(NetError::from);
×
206
                }
207
            }
208
        }
209

210
        let content = GetAttachmentsInvResponse {
×
211
            block_id: index_block_hash.clone(),
×
212
            pages,
×
213
        };
×
214

215
        let preamble = HttpResponsePreamble::ok_json(&preamble);
×
216
        let body = HttpResponseContents::try_from_json(&content)?;
×
217
        Ok((preamble, body))
×
218
    }
×
219
}
220

221
/// Decode the HTTP response
222
impl HttpResponse for RPCGetAttachmentsInvRequestHandler {
223
    fn try_parse_response(
×
224
        &self,
×
225
        preamble: &HttpResponsePreamble,
×
226
        body: &[u8],
×
227
    ) -> Result<HttpResponsePayload, Error> {
×
228
        let pages: GetAttachmentsInvResponse = parse_json(preamble, body)?;
×
229
        Ok(HttpResponsePayload::try_from_json(pages)?)
×
230
    }
×
231
}
232

233
impl StacksHttpRequest {
234
    /// Make a new request for attachment inventory page
235
    pub fn new_getattachmentsinv(
×
236
        host: PeerHost,
×
237
        index_block_hash: StacksBlockId,
×
238
        page_indexes: HashSet<u32>,
×
239
    ) -> StacksHttpRequest {
×
240
        let page_list: Vec<String> = page_indexes.into_iter().map(|i| format!("{}", i)).collect();
×
241
        StacksHttpRequest::new_for_peer(
×
242
            host,
×
243
            "GET".into(),
×
244
            "/v2/attachments/inv".into(),
×
245
            HttpRequestContents::new()
×
246
                .query_arg("index_block_hash".into(), format!("{}", &index_block_hash))
×
247
                .query_arg("pages_indexes".into(), page_list[..].join(",")),
×
248
        )
249
        .expect("FATAL: failed to construct request from infallible data")
×
250
    }
×
251
}
252

253
impl StacksHttpResponse {
254
    pub fn decode_atlas_attachments_inv_response(
×
255
        self,
×
256
    ) -> Result<GetAttachmentsInvResponse, NetError> {
×
257
        let contents = self.get_http_payload_ok()?;
×
258
        let contents_json: serde_json::Value = contents.try_into()?;
×
259
        let resp: GetAttachmentsInvResponse = serde_json::from_value(contents_json)
×
260
            .map_err(|_e| NetError::DeserializeError("Failed to load from JSON".to_string()))?;
×
261
        Ok(resp)
×
262
    }
×
263
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc