Skip to content

Commit

Permalink
Merge pull request #6 from ipfs-force-community/chore/0x5459/upgrade_…
Browse files Browse the repository at this point in the history
…to_v14.0.0

opt: force version for v14.0.0
  • Loading branch information
0x5459 authored Apr 13, 2023
2 parents d819905 + 4646015 commit 0f97dd2
Show file tree
Hide file tree
Showing 9 changed files with 759 additions and 17 deletions.
40 changes: 30 additions & 10 deletions filecoin-proofs/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pub use storage_proofs_update::constants::TreeRHasher;
use typenum::Unsigned;

use crate::{
commitment_reader::CommitmentReader,
constants::{
DefaultBinaryTree, DefaultOctTree, DefaultPieceDomain, DefaultPieceHasher,
MINIMUM_RESERVED_BYTES_FOR_PIECE_IN_FULLY_ALIGNED_SECTOR as MINIMUM_PIECE_SIZE,
},
force_commitment_reader::CommitmentReader,
parameters::public_params,
pieces::{get_piece_alignment, sum_piece_bytes_with_alignment},
types::{
Expand Down Expand Up @@ -381,14 +381,34 @@ where
R: Read,
W: Write,
{
trace!("add_piece:start");
const DEFAULT_BUF_SIZE: usize = 64 * 1024 * 1024;
let padded_bytes_amount: usize = ensure_piece_size(piece_size)?.into();

let result = measure_op(Operation::AddPiece, || {
ensure_piece_size(piece_size)?;
let buf_size = if padded_bytes_amount >= DEFAULT_BUF_SIZE {
DEFAULT_BUF_SIZE
} else {
padded_bytes_amount
};

let source = BufReader::new(source);
let mut target = BufWriter::new(target);
let source = BufReader::with_capacity(buf_size, source);
let target = BufWriter::with_capacity(buf_size, target);
add_piece_raw(source, target, piece_size, piece_lengths)
}

/// add_piece but without BufReader and BufWriter
pub fn add_piece_raw<R, W>(
source: R,
mut target: W,
piece_size: UnpaddedBytesAmount,
piece_lengths: &[UnpaddedBytesAmount],
) -> Result<(PieceInfo, UnpaddedBytesAmount)>
where
R: Read,
W: Write,
{
trace!("add_piece:start");

let result = measure_op(Operation::AddPiece, || {
let written_bytes = sum_piece_bytes_with_alignment(piece_lengths);
let piece_alignment = get_piece_alignment(written_bytes, piece_size);
let fr32_reader = Fr32Reader::new(source);
Expand All @@ -398,7 +418,7 @@ where
target.write_all(&[0u8][..])?;
}

let mut commitment_reader = CommitmentReader::new(fr32_reader);
let mut commitment_reader = CommitmentReader::new(piece_size.into(), fr32_reader);
let n = io::copy(&mut commitment_reader, &mut target)
.context("failed to write and preprocess bytes")?;

Expand All @@ -413,7 +433,7 @@ where
target.write_all(&[0u8][..])?;
}

let commitment = commitment_reader.finish()?;
let commitment = commitment_reader.finish();
let mut comm = [0u8; 32];
comm.copy_from_slice(commitment.as_ref());

Expand All @@ -426,7 +446,7 @@ where
result
}

fn ensure_piece_size(piece_size: UnpaddedBytesAmount) -> Result<()> {
fn ensure_piece_size(piece_size: UnpaddedBytesAmount) -> Result<PaddedBytesAmount> {
ensure!(
piece_size >= UnpaddedBytesAmount(MINIMUM_PIECE_SIZE),
"Piece must be at least {} bytes",
Expand All @@ -440,7 +460,7 @@ fn ensure_piece_size(piece_size: UnpaddedBytesAmount) -> Result<()> {
padded_piece_size,
);

Ok(())
Ok(padded_piece_size)
}

/// Writes bytes from `source` to `target`, adding bit-padding ("preprocessing")
Expand Down
Loading

0 comments on commit 0f97dd2

Please sign in to comment.