Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit a64b8a2

Browse files
authored
Rename snapshot.tar.bz2 to snapshot-<slot>-<hash>.tar.bz2 (bp #8482) (#8501)
automerge
1 parent 0198f9e commit a64b8a2

File tree

13 files changed

+304
-175
lines changed

13 files changed

+304
-175
lines changed

Cargo.lock

Lines changed: 32 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ num-traits = "0.2"
3737
rand = "0.6.5"
3838
rand_chacha = "0.1.1"
3939
rayon = "1.2.0"
40+
regex = "1.3.4"
4041
serde = "1.0.104"
4142
serde_derive = "1.0.103"
4243
serde_json = "1.0.46"

core/src/rpc_service.rs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use jsonrpc_http_server::{
99
hyper, AccessControlAllowOrigin, CloseHandle, DomainsValidation, RequestMiddleware,
1010
RequestMiddlewareAction, ServerBuilder,
1111
};
12+
use regex::Regex;
1213
use solana_ledger::{bank_forks::BankForks, blockstore::Blockstore};
1314
use solana_sdk::hash::Hash;
1415
use std::{
@@ -28,13 +29,17 @@ pub struct JsonRpcService {
2829
close_handle: Option<CloseHandle>,
2930
}
3031

31-
#[derive(Default)]
3232
struct RpcRequestMiddleware {
3333
ledger_path: PathBuf,
34+
snapshot_archive_path_regex: Regex,
3435
}
3536
impl RpcRequestMiddleware {
3637
pub fn new(ledger_path: PathBuf) -> Self {
37-
Self { ledger_path }
38+
Self {
39+
ledger_path,
40+
snapshot_archive_path_regex: Regex::new(r"/snapshot-\d+-[[:alnum:]]+\.tar\.bz2$")
41+
.unwrap(),
42+
}
3843
}
3944

4045
fn not_found() -> hyper::Response<hyper::Body> {
@@ -51,9 +56,19 @@ impl RpcRequestMiddleware {
5156
.unwrap()
5257
}
5358

54-
fn get(&self, filename: &str) -> RequestMiddlewareAction {
55-
info!("get {}", filename);
56-
let filename = self.ledger_path.join(filename);
59+
fn is_get_path(&self, path: &str) -> bool {
60+
match path {
61+
"/genesis.tar.bz2" => true,
62+
_ => self.snapshot_archive_path_regex.is_match(path),
63+
}
64+
}
65+
66+
fn get(&self, path: &str) -> RequestMiddlewareAction {
67+
let filename = self.ledger_path.join(
68+
path.split_at(1).1, // Drop leading '/' from path
69+
);
70+
info!("get {} -> {:?}", path, filename);
71+
5772
RequestMiddlewareAction::Respond {
5873
should_validate_hosts: true,
5974
response: Box::new(
@@ -73,13 +88,14 @@ impl RpcRequestMiddleware {
7388
impl RequestMiddleware for RpcRequestMiddleware {
7489
fn on_request(&self, request: hyper::Request<hyper::Body>) -> RequestMiddlewareAction {
7590
trace!("request uri: {}", request.uri());
76-
match request.uri().path() {
77-
"/snapshot.tar.bz2" => self.get("snapshot.tar.bz2"),
78-
"/genesis.tar.bz2" => self.get("genesis.tar.bz2"),
79-
_ => RequestMiddlewareAction::Proceed {
91+
92+
if self.is_get_path(request.uri().path()) {
93+
self.get(request.uri().path())
94+
} else {
95+
RequestMiddlewareAction::Proceed {
8096
should_continue_on_invalid_cors: false,
8197
request,
82-
},
98+
}
8399
}
84100
}
85101
}
@@ -234,4 +250,25 @@ mod tests {
234250
rpc_service.exit();
235251
rpc_service.join().unwrap();
236252
}
253+
254+
#[test]
255+
fn test_is_get_path() {
256+
let rrm = RpcRequestMiddleware::new(PathBuf::from("/"));
257+
258+
assert!(rrm.is_get_path("/genesis.tar.bz2"));
259+
assert!(!rrm.is_get_path("genesis.tar.bz2"));
260+
261+
assert!(!rrm.is_get_path("/snapshot.tar.bz2"));
262+
263+
assert!(
264+
rrm.is_get_path("/snapshot-100-AvFf9oS8A8U78HdjT9YG2sTTThLHJZmhaMn2g8vkWYnr.tar.bz2")
265+
);
266+
assert!(!rrm.is_get_path(
267+
"/snapshot-notaslotnumber-AvFf9oS8A8U78HdjT9YG2sTTThLHJZmhaMn2g8vkWYnr.tar.bz2"
268+
));
269+
270+
assert!(!rrm.is_get_path("/"));
271+
assert!(!rrm.is_get_path(".."));
272+
assert!(!rrm.is_get_path("🎣"));
273+
}
237274
}

core/src/snapshot_packager_service.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ mod tests {
154154
}
155155

156156
// Create a packageable snapshot
157-
let output_tar_path =
158-
snapshot_utils::get_snapshot_archive_path(&snapshot_package_output_path);
157+
let output_tar_path = snapshot_utils::get_snapshot_archive_path(
158+
&snapshot_package_output_path,
159+
&(42, Hash::default()),
160+
);
159161
let snapshot_package = SnapshotPackage::new(
160162
5,
161163
vec![],

0 commit comments

Comments
 (0)