pub fn add_piece<R, W>(
    source: R,
    target: W,
    piece_size: UnpaddedBytesAmount,
    piece_lengths: &[UnpaddedBytesAmount],
) -> Result<(PieceInfo, UnpaddedBytesAmount)>
where
    R: Read,
    W: Write,
{
    info!("add_piece:start");

    let result = measure_op(Operation::AddPiece, || {
        ensure_piece_size(piece_size)?;

        let source = std::io::BufReader::new(source);
        let mut target = std::io::BufWriter::new(target);

        let written_bytes = crate::pieces::sum_piece_bytes_with_alignment(&piece_lengths);
        let piece_alignment = crate::pieces::get_piece_alignment(written_bytes, piece_size);
        let fr32_reader = crate::fr32_reader::Fr32Reader::new(source);

        // write left alignment
        for _ in 0..usize::from(PaddedBytesAmount::from(piece_alignment.left_bytes)) {
            target.write_all(&[0u8][..])?;
        }

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

        ensure!(n != 0, "add_piece: read 0 bytes before EOF from source");
        let n = PaddedBytesAmount(n as u64);
        let n: UnpaddedBytesAmount = n.into();

        ensure!(n == piece_size, "add_piece: invalid bytes amount written");

        // write right alignment
        for _ in 0..usize::from(PaddedBytesAmount::from(piece_alignment.right_bytes)) {
            target.write_all(&[0u8][..])?;
        }

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

        let written = piece_alignment.left_bytes + piece_alignment.right_bytes + piece_size;

        Ok((PieceInfo::new(comm, n)?, written))
    });

    info!("add_piece:finish");
    result
}
/// Computes a NUL-byte prefix and/or suffix for `source` using the provided
/// `piece_lengths` and `piece_size` (such that the `source`, after
/// preprocessing, will occupy a subtree of a merkle tree built using the bytes
/// from `target`), runs the resultant byte stream through the preprocessor,
/// and writes the result to `target`. Returns a tuple containing the number of
/// bytes written to `target` (`source` plus alignment) and the commitment.
///
/// WARNING: Depending on the ordering and size of the pieces in
/// `piece_lengths`, this function could write a prefix of NUL bytes which
/// wastes ($SIZESECTORSIZE/2)-$MINIMUM_PIECE_SIZE space. This function will be
/// deprecated in favor of `write_and_preprocess`, and miners will be prevented
/// from sealing sectors containing more than $TOOMUCH alignment bytes.
///
/// # Arguments
///
/// * `source` - a readable source of unprocessed piece bytes.
/// * `target` - a writer where we will write the processed piece bytes.
/// * `piece_size` - the number of unpadded user-bytes which can be read from source before EOF.
/// * `piece_lengths` - the number of bytes for each previous piece in the sector.


Depending on the ordering and size of the pieces in
`piece_lengths`, this function could write a prefix of NUL bytes which
wastes ($SIZESECTORSIZE/2)-$MINIMUM_PIECE_SIZE space. This function will be
deprecated in favor of `write_and_preprocess`, and miners will be prevented
from sealing sectors containing more than $TOOMUCH alignment bytes.
Depending on the ordering and size of the pieces in ` piece_lengths ` this function could write a prefix of NUL bytes which wastes ( $SIZESECTORSIZE / 2 ) - $MINIMUM_PIECE_SIZE space . This function will be deprecated in favor of ` write_and_preprocess ` and miners will be prevented from sealing sectors containing more than $TOOMUCH alignment bytes .
pub fn add_piece<R, W>(
    source: R,
    target: W,
    piece_size: UnpaddedBytesAmount,
    piece_lengths: &[UnpaddedBytesAmount],
) -> Result<(PieceInfo, UnpaddedBytesAmount)>
where
    R: Read,
    W: Write,
{
    info!("add_piece:start");
    let result = measure_op(Operation::AddPiece, || {
        ensure_piece_size(piece_size)?;
        let source = std::io::BufReader::new(source);
        let mut target = std::io::BufWriter::new(target);
        let written_bytes = crate::pieces::sum_piece_bytes_with_alignment(&piece_lengths);
        let piece_alignment = crate::pieces::get_piece_alignment(written_bytes, piece_size);
        let fr32_reader = crate::fr32_reader::Fr32Reader::new(source);
        // write left alignment
        for _ in 0..usize::from(PaddedBytesAmount::from(piece_alignment.left_bytes)) {
            target.write_all(&[0u8][..])?;
        }
        let mut commitment_reader = CommitmentReader::new(fr32_reader);
        let n = std::io::copy(&mut commitment_reader, &mut target)
            .context("failed to write and preprocess bytes")?;
        ensure!(n != 0, "add_piece: read 0 bytes before EOF from source");
        let n = PaddedBytesAmount(n as u64);
        let n: UnpaddedBytesAmount = n.into();
        ensure!(n == piece_size, "add_piece: invalid bytes amount written");
        // write right alignment
        for _ in 0..usize::from(PaddedBytesAmount::from(piece_alignment.right_bytes)) {
            target.write_all(&[0u8][..])?;
        }
        let commitment = commitment_reader.finish()?;
        let mut comm = [0u8; 32];
        comm.copy_from_slice(commitment.as_ref());
        let written = piece_alignment.left_bytes + piece_alignment.right_bytes + piece_size;
        Ok((PieceInfo::new(comm, n)?, written))
    });
    info!("add_piece:finish");
    result
}
pub fn add_piece < R , W > ( source : R , target : W , piece_size : UnpaddedBytesAmount , piece_lengths : & [ UnpaddedBytesAmount ] , ) -> Result < ( PieceInfo , UnpaddedBytesAmount ) > where R : Read , W : Write , { info ! ( " " ) ; let result = measure_op ( Operation :: AddPiece , | | { ensure_piece_size ( piece_size ) ? ; let source = std :: io :: BufReader :: new ( source ) ; let mut target = std :: io :: BufWriter :: new ( target ) ; let written_bytes = crate :: pieces :: sum_piece_bytes_with_alignment ( & piece_lengths ) ; let piece_alignment = crate :: pieces :: get_piece_alignment ( written_bytes , piece_size ) ; let fr32_reader = crate :: fr32_reader :: Fr32Reader :: new ( source ) ; // write left alignment for _ in 0 .. usize :: from ( PaddedBytesAmount :: from ( piece_alignment . left_bytes ) ) { target . write_all ( & [ 0u8 ] [ .. ] ) ? ; } let mut commitment_reader = CommitmentReader :: new ( fr32_reader ) ; let n = std :: io :: copy ( & mut commitment_reader , & mut target ) . context ( " " ) ? ; ensure ! ( n 0 " " ) ; let n = PaddedBytesAmount ( n as u64 ) ; let n : UnpaddedBytesAmount = n . into ( ) ; ensure ! ( n piece_size " " ) ; // write right alignment for _ in 0 .. usize :: from ( PaddedBytesAmount :: from ( piece_alignment . right_bytes ) ) { target . write_all ( & [ 0u8 ] [ .. ] ) ? ; } let commitment = commitment_reader . finish ( ) ? ; let mut comm = [ 0u8 ; 32 ] ; comm . copy_from_slice ( commitment . as_ref ( ) ) ; let written = piece_alignment . left_bytes + piece_alignment . right_bytes + piece_size ; Ok ( ( PieceInfo :: new ( comm , n ) ? , written ) ) } ) ; info ! ( " " ) ; result }
Computes a NUL-byte prefix and/or suffix for `source` using the provided
`piece_lengths` and `piece_size` (such that the `source`, after
preprocessing, will occupy a subtree of a merkle tree built using the bytes
from `target`), runs the resultant byte stream through the preprocessor,
and writes the result to `target`.
Computes a NUL - byte prefix and / or suffix for ` source ` using the provided ` piece_lengths ` and ` piece_size ` ( such that the ` source ` after preprocessing will occupy a subtree of a merkle tree built using the bytes from ` target ` ) runs the resultant byte stream through the preprocessor and writes the result to ` target ` .
source
R
target
W
piece_size
UnpaddedBytesAmount
piece_lengths
&[UnpaddedBytesAmount]
source
R
target
W
piece_size
UnpaddedBytesAmount
piece_lengths
&[UnpaddedBytesAmount]