Skip to content
Snippets Groups Projects

Write `get-file` and commands to get the list of blocks from another node

2 files
+ 43
28
Compare changes
  • Side-by-side
  • Inline

Files

+ 40
27
@@ -64,7 +64,6 @@ pub(crate) struct BlockResponse {
block_hash: String,
block_data: Vec<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct PeerBlockInfoRequest {
@@ -402,21 +401,15 @@ where
}
fn get_block_dir(&mut self, file_hash: String) -> PathBuf {
[
self.get_file_dir(file_hash),
PathBuf::from("blocks"),
]
.iter()
.collect()
[self.get_file_dir(file_hash), PathBuf::from("blocks")]
.iter()
.collect()
}
fn get_file_dir(&mut self, file_hash: String) -> PathBuf {
[
self.file_dir.clone(),
PathBuf::from(file_hash),
]
.iter()
.collect()
[self.file_dir.clone(), PathBuf::from(file_hash)]
.iter()
.collect()
}
async fn message_request(
@@ -443,7 +436,13 @@ where
self.swarm
.behaviour_mut()
.request_response
.send_response(channel, BlockResponse{block_hash: block_hash.clone(), block_data: ser_block})
.send_response(
channel,
BlockResponse {
block_hash: block_hash.clone(),
block_data: ser_block,
},
)
.map_err(|_| CouldNotSendBlockResponse(block_hash, file_hash, channel_info).into())
}
@@ -671,9 +670,13 @@ where
} => {
if sender
.send(
Self::decode_blocks(PathBuf::from(block_dir), &block_hashes, output_filename)
.await
.map_err(|err| err.into()),
Self::decode_blocks(
PathBuf::from(block_dir),
&block_hashes,
output_filename,
)
.await
.map_err(|err| err.into()),
)
.is_err()
{
@@ -793,7 +796,12 @@ where
/// - If it can reconstruct the file, it will close the requests for block info and blocks to all the peers it contacted, construct the file, write it to disk and send the path where the file was written to the user
/// - If it can't reconstruct the file yet, given the block combination it got from block info, it will try to find the combination of blocks that will allow for file reconstruction with a minimal block download (ie using the max number of already downloaded blocks it can)
/// - If even after all that it still can't find a combination of blocks that works, it will exit with an error
async fn get_file<P>(&mut self, file_hash: String, output_filename: String, powers_path: String) -> Result<String>
async fn get_file<P>(
&mut self,
file_hash: String,
output_filename: String,
powers_path: String,
) -> Result<String>
where
P: DenseUVPolynomial<F>,
for<'a, 'b> &'a P: Div<&'b P, Output = P>,
@@ -902,12 +910,21 @@ where
}
}
let _ = Self::decode_blocks(block_dir.clone(), &block_hashes_on_disk, output_filename.clone()).await;
let _ = Self::decode_blocks(
block_dir.clone(),
&block_hashes_on_disk,
output_filename.clone(),
)
.await;
//TODO if it fails, keep requesting block info, try to check which matrix is invertible taking k-1 blocks already on disk and one more that isn't
//TODO if it fails, do the same with k-2, etc...
//TODO when a combination of the blocks that works is found, request the missing blocks
Ok(format!("{:?}/{}", self.get_file_dir(file_hash), output_filename))
Ok(format!(
"{:?}/{}",
self.get_file_dir(file_hash),
output_filename
))
}
async fn dial(&mut self, multiaddr: String) -> Result<()> {
@@ -1023,12 +1040,8 @@ where
block_hashes: &Vec<String>,
output_filename: String,
) -> Result<()> {
let blocks = fs::read_blocks::<F, G>(
block_hashes,
&block_dir,
Compress::Yes,
Validate::Yes,
)?;
let blocks =
fs::read_blocks::<F, G>(block_hashes, &block_dir, Compress::Yes, Validate::Yes)?;
let shards: Vec<Shard<F>> = blocks.into_iter().map(|b| b.1.shard).collect();
let vec_bytes = fec::decode::<F>(shards)?;
if let Some(parent_dir_path) = Path::new(&block_dir).parent() {
@@ -1040,7 +1053,7 @@ where
file.write_all(vec_bytes.as_slice()).await?;
} else {
error!("Parent of the block directory does not exist");
let err = NoParentDirectory(format!("{:?}",block_dir));
let err = NoParentDirectory(format!("{:?}", block_dir));
return Err(err.into());
}
Ok(())
Loading