use reed_solomon_erasure::galois_8::ReedSolomon;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy)]
pub struct EccConfig {
pub data_shards: usize,
pub parity_shards: usize,
}
#[derive(Debug, Clone)]
pub struct PieceParity {
pub piece: u32,
pub original_len: usize,
pub shard_size: usize,
pub shards: Vec<Vec<u8>>,
}
pub fn encode_piece_parity(piece: u32, data: &[u8], config: EccConfig) -> Result<PieceParity> {
if data.is_empty() {
return Err(Error::Store("cannot encode parity for empty piece".into()));
}
let rs = ReedSolomon::new(config.data_shards, config.parity_shards)
.map_err(|err| Error::Store(format!("reed-solomon init: {err}")))?;
let shard_size = data.len().div_ceil(config.data_shards);
let total_shards = config.data_shards + config.parity_shards;
let mut shards = vec![vec![0u8; shard_size]; total_shards];
for (idx, chunk) in data.chunks(shard_size).enumerate() {
shards[idx][..chunk.len()].copy_from_slice(chunk);
}
rs.encode(&mut shards)
.map_err(|err| Error::Store(format!("reed-solomon encode: {err}")))?;
Ok(PieceParity {
piece,
original_len: data.len(),
shard_size,
shards,
})
}
pub fn recover_piece(parity: &PieceParity, config: EccConfig) -> Result<Vec<u8>> {
if parity.shards.len() < config.data_shards {
return Err(Error::Store("parity blob missing data shards".into()));
}
let mut out = Vec::with_capacity(parity.original_len);
for shard in parity.shards.iter().take(config.data_shards) {
out.extend_from_slice(shard);
}
out.truncate(parity.original_len);
Ok(out)
}
pub fn reconstruct_piece(parity: &PieceParity, config: EccConfig) -> Result<Vec<u8>> {
let present = vec![true; parity.shards.len()];
reconstruct_erased(parity, config, &present)
}
pub fn reconstruct_erased(
parity: &PieceParity,
config: EccConfig,
present: &[bool],
) -> Result<Vec<u8>> {
let total = config.data_shards + config.parity_shards;
if parity.shards.len() != total {
return Err(Error::Store(format!(
"parity shard count {} != expected {total}",
parity.shards.len()
)));
}
if present.len() != total {
return Err(Error::Store(format!(
"present mask length {} != expected {total}",
present.len()
)));
}
let rs = ReedSolomon::new(config.data_shards, config.parity_shards)
.map_err(|err| Error::Store(format!("reed-solomon init: {err}")))?;
let mut shards: Vec<(Vec<u8>, bool)> = parity
.shards
.iter()
.zip(present.iter())
.map(|(shard, &is_present)| (shard.clone(), is_present))
.collect();
rs.reconstruct(&mut shards)
.map_err(|err| Error::Store(format!("reed-solomon reconstruct: {err}")))?;
let mut out = Vec::with_capacity(parity.original_len);
for (shard, _) in shards.iter().take(config.data_shards) {
out.extend_from_slice(shard);
}
out.truncate(parity.original_len);
Ok(out)
}
pub fn parity_to_bytes(parity: &PieceParity) -> Vec<u8> {
let mut out = Vec::new();
out.extend(parity.piece.to_be_bytes());
out.extend((parity.original_len as u32).to_be_bytes());
out.extend((parity.shard_size as u32).to_be_bytes());
out.push(parity.shards.len() as u8);
for shard in &parity.shards {
out.extend((shard.len() as u32).to_be_bytes());
out.extend(shard);
}
out
}
pub fn parity_from_bytes(input: &[u8]) -> Result<PieceParity> {
if input.len() < 13 {
return Err(Error::Store("parity blob too short".into()));
}
let piece = u32::from_be_bytes(input[0..4].try_into().expect("piece"));
let original_len = u32::from_be_bytes(input[4..8].try_into().expect("len")) as usize;
let shard_size = u32::from_be_bytes(input[8..12].try_into().expect("shard")) as usize;
let shard_count = input[12] as usize;
let mut cursor = 13;
let mut shards = Vec::with_capacity(shard_count);
for _ in 0..shard_count {
if cursor + 4 > input.len() {
return Err(Error::Store("parity blob truncated".into()));
}
let len = u32::from_be_bytes(input[cursor..cursor + 4].try_into().expect("slen")) as usize;
cursor += 4;
if cursor + len > input.len() {
return Err(Error::Store("parity shard truncated".into()));
}
shards.push(input[cursor..cursor + len].to_vec());
cursor += len;
}
Ok(PieceParity {
piece,
original_len,
shard_size,
shards,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrip_parity_bytes() {
let data = b"piece payload for ecc".to_vec();
let config = EccConfig {
data_shards: 2,
parity_shards: 1,
};
let parity = encode_piece_parity(3, &data, config).expect("encode");
let bytes = parity_to_bytes(&parity);
let parsed = parity_from_bytes(&bytes).expect("parse");
assert_eq!(parsed.piece, 3);
assert_eq!(parsed.original_len, data.len());
let recovered = recover_piece(&parsed, config).expect("recover");
assert_eq!(recovered, data);
}
#[test]
fn reconstruct_recovers_erased_data_shard() {
let data = b"piece payload for ecc erasure coding path!!".to_vec();
let config = EccConfig {
data_shards: 2,
parity_shards: 1,
};
let mut parity = encode_piece_parity(0, &data, config).expect("encode");
parity.shards[0].fill(0);
let present = [false, true, true];
let recovered = reconstruct_erased(&parity, config, &present).expect("reconstruct");
assert_eq!(recovered, data);
}
#[test]
fn reconstruct_piece_with_all_present() {
let data = b"all shards present".to_vec();
let config = EccConfig {
data_shards: 2,
parity_shards: 1,
};
let parity = encode_piece_parity(1, &data, config).expect("encode");
let recovered = reconstruct_piece(&parity, config).expect("reconstruct");
assert_eq!(recovered, data);
}
}