|
| 1 | +use flate2::read::GzDecoder; |
| 2 | +use std::io::{self, copy, Cursor, Write}; |
| 3 | +use stellar_xdr::curr::{self as xdr, Frame, Limited, ReadXdr}; |
| 4 | + |
| 5 | +#[derive(thiserror::Error, Debug)] |
| 6 | +pub enum Error { |
| 7 | + #[error("downloading history: {0}")] |
| 8 | + DownloadingHistory(reqwest::Error), |
| 9 | + |
| 10 | + #[error("downloading history: got status code {0}")] |
| 11 | + DownloadingHistoryGotStatusCode(reqwest::StatusCode), |
| 12 | + |
| 13 | + #[error("json decoding history: {0}")] |
| 14 | + JsonDecodingHistory(serde_json::Error), |
| 15 | + |
| 16 | + #[error("getting bucket: {0}")] |
| 17 | + GettingBucket(reqwest::Error), |
| 18 | + |
| 19 | + #[error("getting bucket: got status code {0}")] |
| 20 | + GettingBucketGotStatusCode(reqwest::StatusCode), |
| 21 | + |
| 22 | + #[error("streaming bucket: {0}")] |
| 23 | + StreamingBucket(io::Error), |
| 24 | + |
| 25 | + #[error("streaming history: {0}")] |
| 26 | + StreamingHistory(io::Error), |
| 27 | + |
| 28 | + #[error("xdr parsing error: {0}")] |
| 29 | + Xdr(#[from] xdr::Error), |
| 30 | +} |
| 31 | + |
| 32 | +pub fn history(archive_url: &str, ledger: u32) -> Result<History, Error> { |
| 33 | + let mut bytes = Vec::new(); |
| 34 | + get_history(archive_url, ledger, &mut bytes)?; |
| 35 | + parse_history(Cursor::new(bytes)) |
| 36 | +} |
| 37 | + |
| 38 | +pub fn get_history<W: Write + ?Sized>( |
| 39 | + archive_url: &str, |
| 40 | + ledger: u32, |
| 41 | + writer: &mut W, |
| 42 | +) -> Result<(), Error> { |
| 43 | + let history_url = { |
| 44 | + let ledger_hex = format!("{ledger:08x}"); |
| 45 | + let ledger_hex_0 = ledger_hex[0..=1].to_string(); |
| 46 | + let ledger_hex_1 = ledger_hex[2..=3].to_string(); |
| 47 | + let ledger_hex_2 = ledger_hex[4..=5].to_string(); |
| 48 | + format!("{archive_url}/history/{ledger_hex_0}/{ledger_hex_1}/{ledger_hex_2}/history-{ledger_hex}.json") |
| 49 | + }; |
| 50 | + //eprintln!("url: {history_url}"); |
| 51 | + |
| 52 | + let mut response = reqwest::blocking::Client::new() |
| 53 | + .get(&history_url) |
| 54 | + .send() |
| 55 | + .map_err(Error::DownloadingHistory)?; |
| 56 | + |
| 57 | + if !response.status().is_success() { |
| 58 | + return Err(Error::DownloadingHistoryGotStatusCode(response.status())); |
| 59 | + } |
| 60 | + |
| 61 | + copy(&mut response, writer).map_err(Error::StreamingHistory)?; |
| 62 | + Ok(()) |
| 63 | +} |
| 64 | + |
| 65 | +pub fn parse_history<R: std::io::Read>(reader: R) -> Result<History, Error> { |
| 66 | + let history: History = serde_json::from_reader(reader).map_err(Error::JsonDecodingHistory)?; |
| 67 | + Ok(history) |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)] |
| 71 | +#[serde(rename_all = "camelCase")] |
| 72 | +pub struct History { |
| 73 | + pub current_ledger: u32, |
| 74 | + pub current_buckets: Vec<HistoryBucket>, |
| 75 | + pub hot_archive_buckets: Option<Vec<HistoryBucket>>, |
| 76 | + pub network_passphrase: Option<String>, |
| 77 | +} |
| 78 | + |
| 79 | +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)] |
| 80 | +#[serde(rename_all = "camelCase")] |
| 81 | +pub struct HistoryBucket { |
| 82 | + pub curr: String, |
| 83 | + pub snap: String, |
| 84 | +} |
| 85 | + |
| 86 | +pub fn get_bucket<W: Write + ?Sized>( |
| 87 | + archive_url: &str, |
| 88 | + bucket: &str, |
| 89 | + writer: &mut W, |
| 90 | +) -> Result<Option<u64>, Error> { |
| 91 | + let bucket_0 = &bucket[0..=1]; |
| 92 | + let bucket_1 = &bucket[2..=3]; |
| 93 | + let bucket_2 = &bucket[4..=5]; |
| 94 | + let bucket_url = |
| 95 | + format!("{archive_url}/bucket/{bucket_0}/{bucket_1}/{bucket_2}/bucket-{bucket}.xdr.gz"); |
| 96 | + |
| 97 | + let response = reqwest::blocking::Client::new() |
| 98 | + .get(&bucket_url) |
| 99 | + .send() |
| 100 | + .map_err(Error::GettingBucket)?; |
| 101 | + |
| 102 | + if !response.status().is_success() { |
| 103 | + return Err(Error::GettingBucketGotStatusCode(response.status())); |
| 104 | + } |
| 105 | + |
| 106 | + let content_length = response.content_length(); |
| 107 | + |
| 108 | + let mut decoder = GzDecoder::new(response); |
| 109 | + copy(&mut decoder, writer).map_err(Error::StreamingBucket)?; |
| 110 | + Ok(content_length) |
| 111 | +} |
| 112 | + |
| 113 | +pub fn parse_bucket<'a, R: std::io::Read + 'a>( |
| 114 | + reader: &'a mut Limited<R>, |
| 115 | +) -> impl Iterator<Item = Result<Frame<xdr::BucketEntry>, xdr::Error>> + 'a { |
| 116 | + Frame::<xdr::BucketEntry>::read_xdr_iter(reader) |
| 117 | +} |
0 commit comments