Skip to content

Commit

Permalink
Rename snapshot.tar.bz2 to snapshot-<slot>-<hash>.tar.bz2 (bp #8482) (#…
Browse files Browse the repository at this point in the history
…8500)

automerge
  • Loading branch information
mergify[bot] authored Feb 27, 2020
1 parent eb3b5d7 commit 85637e8
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 175 deletions.
53 changes: 32 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ num-traits = "0.2"
rand = "0.6.5"
rand_chacha = "0.1.1"
rayon = "1.2.0"
regex = "1.3.4"
serde = "1.0.104"
serde_derive = "1.0.103"
serde_json = "1.0.44"
Expand Down
57 changes: 47 additions & 10 deletions core/src/rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use jsonrpc_http_server::{
hyper, AccessControlAllowOrigin, CloseHandle, DomainsValidation, RequestMiddleware,
RequestMiddlewareAction, ServerBuilder,
};
use regex::Regex;
use solana_ledger::{bank_forks::BankForks, blockstore::Blockstore};
use solana_sdk::hash::Hash;
use std::{
Expand All @@ -28,13 +29,17 @@ pub struct JsonRpcService {
close_handle: Option<CloseHandle>,
}

#[derive(Default)]
struct RpcRequestMiddleware {
ledger_path: PathBuf,
snapshot_archive_path_regex: Regex,
}
impl RpcRequestMiddleware {
pub fn new(ledger_path: PathBuf) -> Self {
Self { ledger_path }
Self {
ledger_path,
snapshot_archive_path_regex: Regex::new(r"/snapshot-\d+-[[:alnum:]]+\.tar\.bz2$")
.unwrap(),
}
}

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

fn get(&self, filename: &str) -> RequestMiddlewareAction {
info!("get {}", filename);
let filename = self.ledger_path.join(filename);
fn is_get_path(&self, path: &str) -> bool {
match path {
"/genesis.tar.bz2" => true,
_ => self.snapshot_archive_path_regex.is_match(path),
}
}

fn get(&self, path: &str) -> RequestMiddlewareAction {
let filename = self.ledger_path.join(
path.split_at(1).1, // Drop leading '/' from path
);
info!("get {} -> {:?}", path, filename);

RequestMiddlewareAction::Respond {
should_validate_hosts: true,
response: Box::new(
Expand All @@ -73,13 +88,14 @@ impl RpcRequestMiddleware {
impl RequestMiddleware for RpcRequestMiddleware {
fn on_request(&self, request: hyper::Request<hyper::Body>) -> RequestMiddlewareAction {
trace!("request uri: {}", request.uri());
match request.uri().path() {
"/snapshot.tar.bz2" => self.get("snapshot.tar.bz2"),
"/genesis.tar.bz2" => self.get("genesis.tar.bz2"),
_ => RequestMiddlewareAction::Proceed {

if self.is_get_path(request.uri().path()) {
self.get(request.uri().path())
} else {
RequestMiddlewareAction::Proceed {
should_continue_on_invalid_cors: false,
request,
},
}
}
}
}
Expand Down Expand Up @@ -234,4 +250,25 @@ mod tests {
rpc_service.exit();
rpc_service.join().unwrap();
}

#[test]
fn test_is_get_path() {
let rrm = RpcRequestMiddleware::new(PathBuf::from("/"));

assert!(rrm.is_get_path("/genesis.tar.bz2"));
assert!(!rrm.is_get_path("genesis.tar.bz2"));

assert!(!rrm.is_get_path("/snapshot.tar.bz2"));

assert!(
rrm.is_get_path("/snapshot-100-AvFf9oS8A8U78HdjT9YG2sTTThLHJZmhaMn2g8vkWYnr.tar.bz2")
);
assert!(!rrm.is_get_path(
"/snapshot-notaslotnumber-AvFf9oS8A8U78HdjT9YG2sTTThLHJZmhaMn2g8vkWYnr.tar.bz2"
));

assert!(!rrm.is_get_path("/"));
assert!(!rrm.is_get_path(".."));
assert!(!rrm.is_get_path("🎣"));
}
}
6 changes: 4 additions & 2 deletions core/src/snapshot_packager_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ mod tests {
}

// Create a packageable snapshot
let output_tar_path =
snapshot_utils::get_snapshot_archive_path(&snapshot_package_output_path);
let output_tar_path = snapshot_utils::get_snapshot_archive_path(
&snapshot_package_output_path,
&(42, Hash::default()),
);
let snapshot_package = SnapshotPackage::new(
5,
vec![],
Expand Down
Loading

0 comments on commit 85637e8

Please sign in to comment.