-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move abi types and implement piece size (#283)
* Move abi types and implement piece size * header lol * fine, but should be same amount of bytes
- Loading branch information
1 parent
7c8fb8c
commit d7f0199
Showing
10 changed files
with
139 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2020 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use cid::Cid; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
/// Size of a piece in bytes | ||
#[derive(PartialEq, Debug, Eq, Clone, Copy)] | ||
pub struct UnpaddedPieceSize(pub u64); | ||
|
||
impl UnpaddedPieceSize { | ||
/// Converts unpadded piece size into padded piece size | ||
pub fn padded(self) -> PaddedPieceSize { | ||
PaddedPieceSize(self.0 + (self.0 / 127)) | ||
} | ||
|
||
/// Validates piece size | ||
pub fn validate(self) -> Result<(), &'static str> { | ||
if self.0 < 127 { | ||
return Err("minimum piece size is 127 bytes"); | ||
} | ||
|
||
// is 127 * 2^n | ||
if self.0 >> self.0.trailing_zeros() != 127 { | ||
return Err("unpadded piece size must be a power of 2 multiple of 127"); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// Size of a piece in bytes with padding | ||
#[derive(PartialEq, Debug, Eq, Clone, Copy)] | ||
pub struct PaddedPieceSize(pub u64); | ||
|
||
impl PaddedPieceSize { | ||
/// Converts padded piece size into an unpadded piece size | ||
pub fn unpadded(self) -> UnpaddedPieceSize { | ||
UnpaddedPieceSize(self.0 - (self.0 / 128)) | ||
} | ||
|
||
/// Validates piece size | ||
pub fn validate(self) -> Result<(), &'static str> { | ||
if self.0 < 128 { | ||
return Err("minimum piece size is 128 bytes"); | ||
} | ||
|
||
if self.0.count_ones() != 1 { | ||
return Err("padded piece size must be a power of 2"); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Serialize for PaddedPieceSize { | ||
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
self.0.serialize(s) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for PaddedPieceSize { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
Ok(Self(Deserialize::deserialize(deserializer)?)) | ||
} | ||
} | ||
|
||
// Piece information for part or a whole file | ||
pub struct PieceInfo { | ||
/// Size in nodes. For BLS12-381 (capacity 254 bits), must be >= 16. (16 * 8 = 128) | ||
pub size: PaddedPieceSize, | ||
/// Content identifier for piece | ||
pub cid: Cid, | ||
} | ||
|
||
impl Serialize for PieceInfo { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
(&self.size, &self.cid).serialize(serializer) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for PieceInfo { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let (size, cid) = Deserialize::deserialize(deserializer)?; | ||
Ok(Self { size, cid }) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn round_trip_piece_size() { | ||
let p_piece = PaddedPieceSize(0b10000000); | ||
p_piece.validate().unwrap(); | ||
let up_piece = p_piece.unpadded(); | ||
up_piece.validate().unwrap(); | ||
assert_eq!(&up_piece, &UnpaddedPieceSize(127)); | ||
assert_eq!(&p_piece, &up_piece.padded()); | ||
} | ||
#[test] | ||
fn invalid_piece_checks() { | ||
let p = PaddedPieceSize(127); | ||
assert_eq!(p.validate(), Err("minimum piece size is 128 bytes")); | ||
let p = UnpaddedPieceSize(126); | ||
assert_eq!(p.validate(), Err("minimum piece size is 127 bytes")); | ||
let p = PaddedPieceSize(0b10000001); | ||
assert_eq!(p.validate(), Err("padded piece size must be a power of 2")); | ||
assert_eq!(UnpaddedPieceSize(0b1111111000).validate(), Ok(())); | ||
assert_eq!( | ||
UnpaddedPieceSize(0b1110111000).validate(), | ||
Err("unpadded piece size must be a power of 2 multiple of 127") | ||
); | ||
} | ||
} |
File renamed without changes.