Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback Wanted: Explicitly type byte vectors using 0u8 #815

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zebra-chain/src/addresses/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl std::str::FromStr for SaplingShieldedAddress {
Ok((hrp, bytes)) => {
let mut decoded_bytes = io::Cursor::new(Vec::<u8>::from_base32(&bytes).unwrap());

let mut diversifier_bytes = [0; 11];
let mut diversifier_bytes = [0u8; 11];
decoded_bytes.read_exact(&mut diversifier_bytes)?;

let transmission_key_bytes = decoded_bytes.read_32_bytes()?;
Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/addresses/sprout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl ZcashSerialize for SproutShieldedAddress {

impl ZcashDeserialize for SproutShieldedAddress {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let mut version_bytes = [0; 2];
let mut version_bytes = [0u8; 2];
reader.read_exact(&mut version_bytes)?;

let network = match version_bytes {
Expand Down
10 changes: 5 additions & 5 deletions zebra-chain/src/addresses/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ impl ZcashSerialize for TransparentAddress {

impl ZcashDeserialize for TransparentAddress {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let mut version_bytes = [0; 2];
let mut version_bytes = [0u8; 2];
reader.read_exact(&mut version_bytes)?;

let mut hash_bytes = [0; 20];
let mut hash_bytes = [0u8; 20];
reader.read_exact(&mut hash_bytes)?;

match version_bytes {
Expand Down Expand Up @@ -207,7 +207,7 @@ impl TransparentAddress {
fn p2pkh_strategy() -> impl Strategy<Value = Self> {
(any::<Network>(), vec(any::<u8>(), 20))
.prop_map(|(network, payload_bytes)| {
let mut bytes = [0; 20];
let mut bytes = [0u8; 20];
bytes.copy_from_slice(payload_bytes.as_slice());
Self::PayToPublicKeyHash {
network,
Expand All @@ -220,7 +220,7 @@ impl TransparentAddress {
fn p2sh_strategy() -> impl Strategy<Value = Self> {
(any::<Network>(), vec(any::<u8>(), 20))
.prop_map(|(network, payload_bytes)| {
let mut bytes = [0; 20];
let mut bytes = [0u8; 20];
bytes.copy_from_slice(payload_bytes.as_slice());
Self::PayToScriptHash {
network,
Expand Down Expand Up @@ -266,7 +266,7 @@ mod tests {

#[test]
fn empty_script() {
let script = Script(vec![0; 20]);
let script = Script(vec![0u8; 20]);

let t_addr = TransparentAddress::from(script);

Expand Down
8 changes: 4 additions & 4 deletions zebra-chain/src/block/difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct ExpandedDifficulty(U256);

impl fmt::Debug for ExpandedDifficulty {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut buf = [0; 32];
let mut buf = [0u8; 32];
// Use the same byte order as BlockHeaderHash
self.0.to_little_endian(&mut buf);
f.debug_tuple("ExpandedDifficulty")
Expand Down Expand Up @@ -447,7 +447,7 @@ mod tests {
let ex_zero = ExpandedDifficulty(U256::zero());
let ex_one = ExpandedDifficulty(U256::one());
let ex_max = ExpandedDifficulty(U256::MAX);
let hash_zero = BlockHeaderHash([0; 32]);
let hash_zero = BlockHeaderHash([0u8; 32]);
let hash_max = BlockHeaderHash([0xff; 32]);

assert_eq!(hash_zero, ex_zero);
Expand Down Expand Up @@ -477,7 +477,7 @@ mod tests {
// TODO: round-trip test, once we have ExpandedDifficulty::to_compact()
let expanded = compact.to_expanded();

let hash_zero = BlockHeaderHash([0; 32]);
let hash_zero = BlockHeaderHash([0u8; 32]);
let hash_max = BlockHeaderHash([0xff; 32]);

if let Some(expanded) = expanded {
Expand All @@ -490,7 +490,7 @@ mod tests {
#[test]
fn prop_expanded_order(expanded in any::<ExpandedDifficulty>()) {
// TODO: round-trip test, once we have ExpandedDifficulty::to_compact()
let hash_zero = BlockHeaderHash([0; 32]);
let hash_zero = BlockHeaderHash([0u8; 32]);
let hash_max = BlockHeaderHash([0xff; 32]);

prop_assert!(expanded >= hash_zero);
Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/block/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ZcashDeserialize for BlockHeaderHash {
impl std::str::FromStr for BlockHeaderHash {
type Err = SerializationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut bytes = [0; 32];
let mut bytes = [0u8; 32];
if hex::decode_to_slice(s, &mut bytes[..]).is_err() {
Err(SerializationError::Parse("hex decoding error"))
} else {
Expand Down
6 changes: 3 additions & 3 deletions zebra-chain/src/equihash_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Copy for EquihashSolution {}

impl Clone for EquihashSolution {
fn clone(&self) -> Self {
let mut bytes = [0; EQUIHASH_SOLUTION_SIZE];
let mut bytes = [0u8; EQUIHASH_SOLUTION_SIZE];
bytes[..].copy_from_slice(&self.0[..]);
Self(bytes)
}
Expand All @@ -74,7 +74,7 @@ impl ZcashDeserialize for EquihashSolution {
"incorrect equihash solution size",
));
}
let mut bytes = [0; EQUIHASH_SOLUTION_SIZE];
let mut bytes = [0u8; EQUIHASH_SOLUTION_SIZE];
reader.read_exact(&mut bytes[..])?;
Ok(Self(bytes))
}
Expand All @@ -93,7 +93,7 @@ mod tests {
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(vec(any::<u8>(), EQUIHASH_SOLUTION_SIZE))
.prop_map(|v| {
let mut bytes = [0; EQUIHASH_SOLUTION_SIZE];
let mut bytes = [0u8; EQUIHASH_SOLUTION_SIZE];
bytes.copy_from_slice(v.as_slice());
Self(bytes)
})
Expand Down
4 changes: 2 additions & 2 deletions zebra-chain/src/keys/sprout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl ZcashSerialize for SpendingKey {

impl ZcashDeserialize for SpendingKey {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let mut version_bytes = [0; 2];
let mut version_bytes = [0u8; 2];
reader.read_exact(&mut version_bytes)?;

let network = match version_bytes {
Expand Down Expand Up @@ -245,7 +245,7 @@ impl ZcashSerialize for IncomingViewingKey {

impl ZcashDeserialize for IncomingViewingKey {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let mut version_bytes = [0; 3];
let mut version_bytes = [0u8; 3];
reader.read_exact(&mut version_bytes)?;

let network = match version_bytes {
Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/keys/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl ZcashSerialize for PublicKey {

impl ZcashDeserialize for PublicKey {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let mut bytes = [0; 33];
let mut bytes = [0u8; 33];
reader.read_exact(&mut bytes[..])?;
Self::from_slice(&bytes[..])
.map_err(|_| SerializationError::Parse("invalid secp256k1 compressed public key"))
Expand Down
4 changes: 2 additions & 2 deletions zebra-chain/src/notes/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<'a> TryFrom<&'a [u8]> for Memo {
type Error = &'static str;

fn try_from(input: &'a [u8]) -> Result<Self, Self::Error> {
let mut full_bytes = [0; 512];
let mut full_bytes = [0u8; 512];

match input.len().cmp(&512) {
cmp::Ordering::Less => {
Expand Down Expand Up @@ -76,7 +76,7 @@ fn memo_fmt() {
fn memo_from_string() {
let memo = Memo::try_from("foo bar baz".as_ref()).unwrap();

let mut bytes = [0; 512];
let mut bytes = [0u8; 512];
bytes[0..11].copy_from_slice(&[102, 111, 111, 32, 98, 97, 114, 32, 98, 97, 122]);

assert!(memo.0.iter().eq(bytes.iter()));
Expand Down
12 changes: 6 additions & 6 deletions zebra-chain/src/notes/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Copy for EncryptedCiphertext {}

impl Clone for EncryptedCiphertext {
fn clone(&self) -> Self {
let mut bytes = [0; 580];
let mut bytes = [0u8; 580];
bytes[..].copy_from_slice(&self.0[..]);
Self(bytes)
}
Expand All @@ -80,7 +80,7 @@ impl ZcashSerialize for EncryptedCiphertext {

impl ZcashDeserialize for EncryptedCiphertext {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let mut bytes = [0; 580];
let mut bytes = [0u8; 580];
reader.read_exact(&mut bytes[..])?;
Ok(Self(bytes))
}
Expand All @@ -93,7 +93,7 @@ impl Arbitrary for EncryptedCiphertext {
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(vec(any::<u8>(), 580))
.prop_map(|v| {
let mut bytes = [0; 580];
let mut bytes = [0u8; 580];
bytes.copy_from_slice(v.as_slice());
Self(bytes)
})
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Copy for OutCiphertext {}

impl Clone for OutCiphertext {
fn clone(&self) -> Self {
let mut bytes = [0; 80];
let mut bytes = [0u8; 80];
bytes[..].copy_from_slice(&self.0[..]);
Self(bytes)
}
Expand All @@ -144,7 +144,7 @@ impl ZcashSerialize for OutCiphertext {

impl ZcashDeserialize for OutCiphertext {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let mut bytes = [0; 80];
let mut bytes = [0u8; 80];
reader.read_exact(&mut bytes[..])?;
Ok(Self(bytes))
}
Expand All @@ -157,7 +157,7 @@ impl Arbitrary for OutCiphertext {
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(vec(any::<u8>(), 80))
.prop_map(|v| {
let mut bytes = [0; 80];
let mut bytes = [0u8; 80];
bytes.copy_from_slice(v.as_slice());
Self(bytes)
})
Expand Down
6 changes: 3 additions & 3 deletions zebra-chain/src/notes/sprout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Copy for EncryptedCiphertext {}

impl Clone for EncryptedCiphertext {
fn clone(&self) -> Self {
let mut bytes = [0; 601];
let mut bytes = [0u8; 601];
bytes[..].copy_from_slice(&self.0[..]);
Self(bytes)
}
Expand All @@ -76,7 +76,7 @@ impl ZcashSerialize for EncryptedCiphertext {

impl ZcashDeserialize for EncryptedCiphertext {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let mut bytes = [0; 601];
let mut bytes = [0u8; 601];
reader.read_exact(&mut bytes[..])?;
Ok(Self(bytes))
}
Expand All @@ -89,7 +89,7 @@ impl Arbitrary for EncryptedCiphertext {
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(vec(any::<u8>(), 601))
.prop_map(|v| {
let mut bytes = [0; 601];
let mut bytes = [0u8; 601];
bytes.copy_from_slice(v.as_slice());
Self(bytes)
})
Expand Down
6 changes: 3 additions & 3 deletions zebra-chain/src/proofs/bctv14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Copy for Bctv14Proof {}

impl Clone for Bctv14Proof {
fn clone(&self) -> Self {
let mut bytes = [0; 296];
let mut bytes = [0u8; 296];
bytes[..].copy_from_slice(&self.0[..]);
Self(bytes)
}
Expand All @@ -46,7 +46,7 @@ impl ZcashSerialize for Bctv14Proof {

impl ZcashDeserialize for Bctv14Proof {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let mut bytes = [0; 296];
let mut bytes = [0u8; 296];
reader.read_exact(&mut bytes[..])?;
Ok(Self(bytes))
}
Expand All @@ -62,7 +62,7 @@ impl Arbitrary for Bctv14Proof {
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(vec(any::<u8>(), 296))
.prop_map(|v| {
let mut bytes = [0; 296];
let mut bytes = [0u8; 296];
bytes.copy_from_slice(v.as_slice());
Self(bytes)
})
Expand Down
6 changes: 3 additions & 3 deletions zebra-chain/src/proofs/groth16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Copy for Groth16Proof {}

impl Clone for Groth16Proof {
fn clone(&self) -> Self {
let mut bytes = [0; 192];
let mut bytes = [0u8; 192];
bytes[..].copy_from_slice(&self.0[..]);
Self(bytes)
}
Expand All @@ -45,7 +45,7 @@ impl ZcashSerialize for Groth16Proof {

impl ZcashDeserialize for Groth16Proof {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
let mut bytes = [0; 192];
let mut bytes = [0u8; 192];
reader.read_exact(&mut bytes[..])?;
Ok(Self(bytes))
}
Expand All @@ -61,7 +61,7 @@ impl Arbitrary for Groth16Proof {
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(vec(any::<u8>(), 192))
.prop_map(|v| {
let mut bytes = [0; 192];
let mut bytes = [0u8; 192];
bytes.copy_from_slice(v.as_slice());
Self(bytes)
})
Expand Down
10 changes: 5 additions & 5 deletions zebra-chain/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,39 +265,39 @@ pub trait ReadZcashExt: io::Read {
#[inline]
fn read_string(&mut self) -> Result<String, SerializationError> {
let len = self.read_compactsize()?;
let mut buf = vec![0; len as usize];
let mut buf = vec![0u8; len as usize];
self.read_exact(&mut buf)?;
String::from_utf8(buf).map_err(|_| SerializationError::Parse("invalid utf-8"))
}

/// Convenience method to read a `[u8; 4]`.
#[inline]
fn read_4_bytes(&mut self) -> io::Result<[u8; 4]> {
let mut bytes = [0; 4];
let mut bytes = [0u8; 4];
self.read_exact(&mut bytes)?;
Ok(bytes)
}

/// Convenience method to read a `[u8; 12]`.
#[inline]
fn read_12_bytes(&mut self) -> io::Result<[u8; 12]> {
let mut bytes = [0; 12];
let mut bytes = [0u8; 12];
self.read_exact(&mut bytes)?;
Ok(bytes)
}

/// Convenience method to read a `[u8; 32]`.
#[inline]
fn read_32_bytes(&mut self) -> io::Result<[u8; 32]> {
let mut bytes = [0; 32];
let mut bytes = [0u8; 32];
self.read_exact(&mut bytes)?;
Ok(bytes)
}

/// Convenience method to read a `[u8; 64]`.
#[inline]
fn read_64_bytes(&mut self) -> io::Result<[u8; 64]> {
let mut bytes = [0; 64];
let mut bytes = [0u8; 64];
self.read_exact(&mut bytes)?;
Ok(bytes)
}
Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/transaction/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl std::str::FromStr for TransactionHash {
type Err = SerializationError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut bytes = [0; 32];
let mut bytes = [0u8; 32];
if hex::decode_to_slice(s, &mut bytes[..]).is_err() {
Err(SerializationError::Parse("hex decoding error"))
} else {
Expand Down
4 changes: 2 additions & 2 deletions zebra-chain/src/transaction/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl ZcashSerialize for TransparentInput {
data,
sequence,
} => {
writer.write_all(&[0; 32][..])?;
writer.write_all(&[0u8; 32][..])?;
writer.write_u32::<LittleEndian>(0xffff_ffff)?;
let height_len = coinbase_height_len(*height);
let total_len = height_len + data.as_ref().len();
Expand All @@ -188,7 +188,7 @@ impl ZcashDeserialize for TransparentInput {
// This inlines the OutPoint deserialization to peek at the hash value
// and detect whether we have a coinbase input.
let bytes = reader.read_32_bytes()?;
if bytes == [0; 32] {
if bytes == [0u8; 32] {
if reader.read_u32::<LittleEndian>()? != 0xffff_ffff {
return Err(SerializationError::Parse("wrong index in coinbase"));
}
Expand Down
2 changes: 1 addition & 1 deletion zebra-consensus/src/block/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn header_solution() -> Result<(), Report> {
.map_err(|e| eyre!(e))?;

// Change nonce to something invalid
block.header.nonce = [0; 32];
block.header.nonce = [0u8; 32];

let ready_verifier_service = block_verifier.ready_and().await.map_err(|e| eyre!(e))?;

Expand Down
Loading