Rename snapshot.tar.bz2 to snapshot-<slot>-<hash>.tar.bz2

This commit is contained in:
Michael Vines
2020-02-24 13:37:14 -07:00
parent 848c43a9ab
commit fcabc6f799
10 changed files with 220 additions and 97 deletions

View File

@ -37,6 +37,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.46"

View File

@ -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::{
@ -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> {
@ -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(
@ -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,
},
}
}
}
}
@ -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("🎣"));
}
}

View File

@ -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![],