diff --git a/chia-bls/src/secret_key.rs b/chia-bls/src/secret_key.rs index aa8a97f27..ceb0d38a6 100644 --- a/chia-bls/src/secret_key.rs +++ b/chia-bls/src/secret_key.rs @@ -76,13 +76,13 @@ fn to_lamport_pk(ikm: [u8; 32], idx: u32) -> [u8; 32] { let mut hasher = Sha256::new(); hasher.update(lamport0); hasher.update(lamport1); - hasher.finalize().try_into().unwrap() + hasher.finalize().into() } fn sha256(bytes: &[u8]) -> [u8; 32] { let mut hasher = Sha256::new(); hasher.update(bytes); - hasher.finalize().try_into().unwrap() + hasher.finalize().into() } pub fn is_all_zero(buf: &[u8]) -> bool { diff --git a/chia-protocol/src/bytes.rs b/chia-protocol/src/bytes.rs index 569f6b2b2..01f6d57bb 100644 --- a/chia-protocol/src/bytes.rs +++ b/chia-protocol/src/bytes.rs @@ -366,7 +366,7 @@ impl FromJsonDict for BytesImpl { N ))); } - Ok((&buf).try_into()?) + Ok((&buf).into()) } } diff --git a/chia-tools/src/bin/fast-forward-spend.rs b/chia-tools/src/bin/fast-forward-spend.rs index fa17c8062..3484bbeed 100644 --- a/chia-tools/src/bin/fast-forward-spend.rs +++ b/chia-tools/src/bin/fast-forward-spend.rs @@ -36,8 +36,7 @@ fn main() { let new_parents_parent: Bytes32 = hex::decode(args.new_parents_parent) .expect("invalid hex") .as_slice() - .try_into() - .expect("parent_id"); + .into(); let mut a = Allocator::new_limited(500000000, 62500000, 62500000); let puzzle = spend.puzzle_reveal.to_clvm(&mut a).expect("to_clvm"); diff --git a/chia-traits/src/streamable.rs b/chia-traits/src/streamable.rs index dcd307478..c7338c447 100644 --- a/chia-traits/src/streamable.rs +++ b/chia-traits/src/streamable.rs @@ -115,9 +115,12 @@ impl Streamable for Vec { fn parse(input: &mut Cursor<&[u8]>) -> Result { let len = u32::parse(input)?; - // TODO: pre-allocate capacity, but we'd need safe-guards for overflow - // attacks - let mut ret = Vec::::new(); + let mut ret = if std::mem::size_of::() == 0 { + Vec::::new() + } else { + let limit = 2 * 1024 * 1024 / std::mem::size_of::(); + Vec::::with_capacity(std::cmp::min(limit, len as usize)) + }; for _ in 0..len { ret.push(T::parse(input)?); } @@ -172,6 +175,16 @@ impl Streamable for bool { } } +impl Streamable for () { + fn update_digest(&self, _digest: &mut Sha256) {} + fn stream(&self, _out: &mut Vec) -> Result<()> { + Ok(()) + } + fn parse(_input: &mut Cursor<&[u8]>) -> Result { + Ok(()) + } +} + impl Streamable for Option { fn update_digest(&self, digest: &mut Sha256) { match self { @@ -356,6 +369,12 @@ fn test_parse_list_list_3() { from_bytes::>>(buf, vec![vec![0x01020304], vec![0x01030307], vec![0x402]]); } +#[test] +fn test_parse_list_empty() { + let buf: &[u8] = &[0, 0, 0, 3]; + from_bytes::>(buf, vec![(), (), ()]); +} + #[test] fn test_parse_long_list() { let buf: &[u8] = &[0xff, 0xff, 0xff, 0xff, 0, 0, 0]; @@ -665,6 +684,12 @@ fn test_stream_list() { ); } +#[test] +fn test_stream_list_of_empty() { + let out = stream(&vec![(), (), ()]); + assert_eq!(&out, &[0, 0, 0, 3]); +} + #[test] fn test_stream_list_list() { let out = stream(&vec![