Skip to content
Open
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
36 changes: 18 additions & 18 deletions consensus_encoding/src/decode/decoders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,17 @@ impl CompactSizeDecoder {
///
/// The final call to [`CompactSizeDecoder::end`] on this decoder will fail if the
/// decoded value exceeds 4,000,000 or won't fit in a `usize`.
pub const fn new() -> Self { Self { buf: internals::array_vec::ArrayVec::new(), limit: MAX_VEC_SIZE } }
pub const fn new() -> Self {
Self { buf: internals::array_vec::ArrayVec::new(), limit: MAX_VEC_SIZE }
}

/// Constructs a new compact size decoder with encoded value limited to the provided usize.
///
/// The final call to [`CompactSizeDecoder::end`] on this decoder will fail if the
/// decoded value exceeds `limit` or won't fit in a `usize`.
pub const fn new_with_limit(limit: usize) -> Self { Self { buf: internals::array_vec::ArrayVec::new(), limit } }
pub const fn new_with_limit(limit: usize) -> Self {
Self { buf: internals::array_vec::ArrayVec::new(), limit }
}
}

impl Default for CompactSizeDecoder {
Expand Down Expand Up @@ -698,24 +702,20 @@ impl Decoder for CompactSizeDecoder {

// This error is returned if dec_value is outside of the usize range, or
// if it is above the given limit.
let make_err = || {
CompactSizeDecoderError(
E::ValueExceedsLimit(LengthPrefixExceedsMaxError {
value: dec_value,
limit: self.limit,
})
)
let make_err = || {
CompactSizeDecoderError(E::ValueExceedsLimit(LengthPrefixExceedsMaxError {
value: dec_value,
limit: self.limit,
}))
};

usize::try_from(dec_value)
.map_err(|_| make_err())
.and_then(|nsize| {
if nsize > self.limit {
Err(make_err())
} else {
Ok(nsize)
}
})
usize::try_from(dec_value).map_err(|_| make_err()).and_then(|nsize| {
if nsize > self.limit {
Err(make_err())
} else {
Ok(nsize)
}
})
}

fn read_limit(&self) -> usize {
Expand Down
9 changes: 4 additions & 5 deletions consensus_encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ extern crate std;
mod decode;
mod encode;

#[cfg(feature = "alloc")]
pub use self::decode::decoders::{
ByteVecDecoder, ByteVecDecoderError, LengthPrefixExceedsMaxError,
VecDecoder, VecDecoderError,
};
pub use self::decode::decoders::{
ArrayDecoder, CompactSizeDecoder, CompactSizeDecoderError, Decoder2, Decoder2Error, Decoder3,
Decoder3Error, Decoder4, Decoder4Error, Decoder6, Decoder6Error, UnexpectedEofError,
};
#[cfg(feature = "alloc")]
pub use self::decode::decoders::{
ByteVecDecoder, ByteVecDecoderError, LengthPrefixExceedsMaxError, VecDecoder, VecDecoderError,
};
#[cfg(feature = "std")]
pub use self::decode::{
decode_from_read, decode_from_read_unbuffered, decode_from_read_unbuffered_with, ReadError,
Expand Down
2 changes: 1 addition & 1 deletion internals/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait IntoDeError: Sized {
}

mod impls {
use super::{IntoDeError, de};
use super::{de, IntoDeError};

impl IntoDeError for core::convert::Infallible {
fn into_de_error<E: de::Error>(self, _expected: Option<&dyn de::Expected>) -> E {
Expand Down
6 changes: 3 additions & 3 deletions p2p/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use core::{cmp, fmt};
use arbitrary::{Arbitrary, Unstructured};
use bitcoin::consensus::encode::{self, Decodable, Encodable, ReadExt, WriteExt};
use bitcoin::merkle_tree::MerkleBlock;
use primitives::{block, transaction};
use encoding;
use hashes::sha256d;
use internals::ToU64 as _;
use io::{self, BufRead, Read, Write};
use primitives::{block, transaction};
use units::FeeRate;

use crate::address::{AddrV2Message, Address};
Expand Down Expand Up @@ -1660,10 +1660,10 @@ mod test {
use alloc::vec;
use std::net::Ipv4Addr;

use primitives::{Block, BlockHash};
use bitcoin::consensus::encode::{deserialize, deserialize_partial, serialize};
use primitives::transaction::{Transaction, Txid};
use hex_lit::hex;
use primitives::transaction::{Transaction, Txid};
use primitives::{Block, BlockHash};
use units::BlockHeight;

use super::*;
Expand Down
4 changes: 2 additions & 2 deletions p2p/src/message_blockdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use alloc::vec::Vec;

#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
use primitives::BlockHash;
use bitcoin::consensus::encode::{self, Decodable, Encodable};
use primitives::transaction::{Txid, Wtxid};
use io::{BufRead, Write};
use primitives::transaction::{Txid, Wtxid};
use primitives::BlockHash;

use crate::consensus::impl_consensus_encoding;
use crate::ProtocolVersion;
Expand Down
2 changes: 1 addition & 1 deletion p2p/src/message_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use alloc::vec::Vec;

#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
use primitives::BlockHash;
use hashes::{sha256d, HashEngine};
use primitives::BlockHash;
use units::BlockHeight;

use crate::consensus::impl_consensus_encoding;
Expand Down
10 changes: 8 additions & 2 deletions p2p/src/message_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ impl UserAgent {
const MAX_USER_AGENT_LEN: usize = 256;

fn panic_invalid_chars(agent_str: &str) {
assert!(!agent_str.chars().any(|c| matches!(c, '/' | '(' | ')' | ':')), "user agent configuration cannot contain: / ( ) :");
assert!(
!agent_str.chars().any(|c| matches!(c, '/' | '(' | ')' | ':')),
"user agent configuration cannot contain: / ( ) :"
);
}

fn panic_max_len(agent_str: &str) {
assert!(agent_str.chars().count() <= Self::MAX_USER_AGENT_LEN, "user agent cannot exceed 256 characters.");
assert!(
agent_str.chars().count() <= Self::MAX_USER_AGENT_LEN,
"user agent cannot exceed 256 characters."
);
}
/// Builds a new user agent from the lowest level client software. For example: `Satoshi` is
/// used by Bitcoin Core.
Expand Down
8 changes: 4 additions & 4 deletions units/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ impl<'a> Arbitrary<'a> for BlockTime {

#[cfg(test)]
mod tests {
use super::*;

#[cfg(feature = "encoding")]
use encoding::{Decodable as _, Decoder as _};
#[cfg(all(feature = "encoding", feature = "alloc"))]
use encoding::UnexpectedEofError;
#[cfg(feature = "encoding")]
use encoding::{Decodable as _, Decoder as _};

use super::*;

#[test]
fn block_time_round_trip() {
Expand Down