Skip to content

Commit

Permalink
Retry chunks until status 200
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebEverett committed Feb 26, 2022
2 parents 5d856da + 39afb6c commit b89d8ef
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/check_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use reqwest;

#[tokio::main]
async fn main() {
let txid = "8kEv0wGeAuk5-lateTOwFiWGffZomRS3sYZgwiDiY74";
let txid = "690t_L2ALtdT8mFvfKmO_u5zGel_x3EtKcKTyo2x6JY";

let peers = reqwest::get("https://arweave.net/peers")
.await
Expand Down
6 changes: 3 additions & 3 deletions examples/upload_bundles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use url::Url;

// For smaller sample sizes, you may have to increase this to have the transactions mined.
const REWARD_MULTIPLIER: f32 = 2.0;
const NUM_FILES: usize = 10;
const FILE_SIZE: usize = 10_000_000;
const BUNDLE_SIZE: u64 = 200_000_000;
const NUM_FILES: usize = 200;
const FILE_SIZE: usize = 5_000_000;
const BUNDLE_SIZE: u64 = 100_000_000;
const BUFFER: usize = 5;

#[tokio::main]
Expand Down
41 changes: 41 additions & 0 deletions examples/verify_bundle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use arloader::transaction::{stringify, Base64};
use reqwest;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct Offset {
#[serde(with = "stringify")]
size: usize,
#[serde(with = "stringify")]
offset: usize,
}

#[derive(Deserialize, Debug)]
struct RawChunk {
tx_path: Base64,
data_path: Base64,
chunk: Base64,
}

#[tokio::main]
async fn main() {
let txid = "690t_L2ALtdT8mFvfKmO_u5zGel_x3EtKcKTyo2x6JY";

let offset = reqwest::get(format!("https://arweave.net/tx/{}/offset", txid))
.await
.unwrap()
.json::<Offset>()
.await
.unwrap();

println!("{:?}", offset);

let chunk = reqwest::get(format!("https://arweave.net/chunk/{}", offset.offset))
.await
.unwrap()
.json::<RawChunk>()
.await
.unwrap();

println!("{:?}", chunk);
}
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub enum Error {
RingUnspecified(#[from] Unspecified),
#[error("serde json: {0}")]
SerdeJson(#[from] serde_json::Error),
#[error("status code not ok")]
StatusCodeNotOk,
#[error("status not found")]
StatusNotFound,
#[error("solana hash parse {0}")]
Expand Down
36 changes: 24 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ impl Arweave {
let url = self.base_url.join("chunk/")?;
let client = reqwest::Client::new();

client
let resp = client
.post(url)
.json(&chunk)
.header(&ACCEPT, "application/json")
Expand All @@ -875,7 +875,10 @@ impl Arweave {
.await
.map_err(|e| Error::ArweavePostError(e))?;

Ok(chunk.offset)
match resp.status() {
reqwest::StatusCode::OK => Ok(chunk.offset),
_ => Err(Error::StatusCodeNotOk),
}
}

pub async fn post_chunk_with_retries(&self, chunk: Chunk) -> Result<usize, Error> {
Expand Down Expand Up @@ -903,19 +906,28 @@ impl Arweave {
return Err(error::Error::UnsignedTransaction.into());
}

let mut retries = 0;
let mut status = reqwest::StatusCode::NOT_FOUND;
let url = self.base_url.join("tx/")?;
let client = reqwest::Client::new();
let resp = client
.post(url)
.json(&signed_transaction)
.header(&ACCEPT, "application/json")
.header(&CONTENT_TYPE, "application/json")
.send()
.await?;
debug!("post_transaction {:?}", &resp);
assert_eq!(resp.status().as_u16(), 200);

Ok((signed_transaction.id.clone(), signed_transaction.reward))
while (retries < CHUNKS_RETRIES) & (status != reqwest::StatusCode::OK) {
status = client
.post(url.clone())
.json(&signed_transaction)
.header(&ACCEPT, "application/json")
.header(&CONTENT_TYPE, "application/json")
.send()
.await?
.status();
if status == reqwest::StatusCode::OK {
return Ok((signed_transaction.id.clone(), signed_transaction.reward));
}
sleep(Duration::from_secs(CHUNKS_RETRY_SLEEP)).await;
retries += 1;
}

Err(Error::StatusCodeNotOk)
}

pub async fn post_transaction_chunks(
Expand Down

0 comments on commit b89d8ef

Please sign in to comment.