Merge rust-bitcoin/rust-bitcoin#2022: Make Encodable/Decodable usage uniform
4f43965ade
Make Encodable/Decodable usage uniform (Tobin C. Harding) Pull request description: One encodes to a writer and decodes from a reader, most of the time in the consensus `Encodable`/`Decodable` traits we use generic `R`/`W` and variable `r`/`w` but there are other places that use other characters. While touching these lines note also that there are a bunch of unneeded `mut`s, I'm not sure why since usually between the compiler and the linter `mut` is handled correctly. Make implementations of `Encodable` and `Decodable` uniform by: - Use R/W and r/w for trait and variable name - Remove unneeded mut (This is split out of #1891 to assist review.) ACKs for top commit: apoelstra: ACK4f43965ade
stevenroose: ACK4f43965ade
Tree-SHA512: 256d080b32e60a7cabf6db4945a18d7ff5b296cb848712238e33f9b1ff3cabbe7d76723fed61a04e4b2a6c9423f12c32ec10e3ebb633761122add50e6a6cc7c9
This commit is contained in:
commit
9677247b25
|
@ -73,20 +73,18 @@ impl convert::AsRef<Transaction> for PrefilledTransaction {
|
||||||
|
|
||||||
impl Encodable for PrefilledTransaction {
|
impl Encodable for PrefilledTransaction {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write + ?Sized>(&self, mut s: &mut S) -> Result<usize, io::Error> {
|
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
|
||||||
Ok(VarInt::from(self.idx).consensus_encode(&mut s)? + self.tx.consensus_encode(&mut s)?)
|
Ok(VarInt::from(self.idx).consensus_encode(w)? + self.tx.consensus_encode(w)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decodable for PrefilledTransaction {
|
impl Decodable for PrefilledTransaction {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_decode<D: io::Read + ?Sized>(
|
fn consensus_decode<R: io::Read + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
|
||||||
mut d: &mut D,
|
let idx = VarInt::consensus_decode(r)?.0;
|
||||||
) -> Result<PrefilledTransaction, encode::Error> {
|
|
||||||
let idx = VarInt::consensus_decode(&mut d)?.0;
|
|
||||||
let idx = u16::try_from(idx)
|
let idx = u16::try_from(idx)
|
||||||
.map_err(|_| encode::Error::ParseFailed("BIP152 prefilled tx index out of bounds"))?;
|
.map_err(|_| encode::Error::ParseFailed("BIP152 prefilled tx index out of bounds"))?;
|
||||||
let tx = Transaction::consensus_decode(&mut d)?;
|
let tx = Transaction::consensus_decode(r)?;
|
||||||
Ok(PrefilledTransaction { idx, tx })
|
Ok(PrefilledTransaction { idx, tx })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,15 +129,15 @@ impl ShortId {
|
||||||
|
|
||||||
impl Encodable for ShortId {
|
impl Encodable for ShortId {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write + ?Sized>(&self, s: &mut S) -> Result<usize, io::Error> {
|
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
|
||||||
self.0.consensus_encode(s)
|
self.0.consensus_encode(w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decodable for ShortId {
|
impl Decodable for ShortId {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_decode<D: io::Read + ?Sized>(d: &mut D) -> Result<ShortId, encode::Error> {
|
fn consensus_decode<R: io::Read + ?Sized>(r: &mut R) -> Result<ShortId, encode::Error> {
|
||||||
Ok(ShortId(Decodable::consensus_decode(d)?))
|
Ok(ShortId(Decodable::consensus_decode(r)?))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,13 +258,13 @@ impl Encodable for BlockTransactionsRequest {
|
||||||
///
|
///
|
||||||
/// Panics if the index overflows [`u64::MAX`]. This happens when [`BlockTransactionsRequest::indexes`]
|
/// Panics if the index overflows [`u64::MAX`]. This happens when [`BlockTransactionsRequest::indexes`]
|
||||||
/// contains an entry with the value [`u64::MAX`] as `u64` overflows during differential encoding.
|
/// contains an entry with the value [`u64::MAX`] as `u64` overflows during differential encoding.
|
||||||
fn consensus_encode<S: io::Write + ?Sized>(&self, mut s: &mut S) -> Result<usize, io::Error> {
|
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
|
||||||
let mut len = self.block_hash.consensus_encode(&mut s)?;
|
let mut len = self.block_hash.consensus_encode(w)?;
|
||||||
// Manually encode indexes because they are differentially encoded VarInts.
|
// Manually encode indexes because they are differentially encoded VarInts.
|
||||||
len += VarInt(self.indexes.len() as u64).consensus_encode(&mut s)?;
|
len += VarInt(self.indexes.len() as u64).consensus_encode(w)?;
|
||||||
let mut last_idx = 0;
|
let mut last_idx = 0;
|
||||||
for idx in &self.indexes {
|
for idx in &self.indexes {
|
||||||
len += VarInt(*idx - last_idx).consensus_encode(&mut s)?;
|
len += VarInt(*idx - last_idx).consensus_encode(w)?;
|
||||||
last_idx = *idx + 1; // can panic here
|
last_idx = *idx + 1; // can panic here
|
||||||
}
|
}
|
||||||
Ok(len)
|
Ok(len)
|
||||||
|
@ -274,14 +272,12 @@ impl Encodable for BlockTransactionsRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decodable for BlockTransactionsRequest {
|
impl Decodable for BlockTransactionsRequest {
|
||||||
fn consensus_decode<D: io::Read + ?Sized>(
|
fn consensus_decode<R: io::Read + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
|
||||||
mut d: &mut D,
|
|
||||||
) -> Result<BlockTransactionsRequest, encode::Error> {
|
|
||||||
Ok(BlockTransactionsRequest {
|
Ok(BlockTransactionsRequest {
|
||||||
block_hash: BlockHash::consensus_decode(&mut d)?,
|
block_hash: BlockHash::consensus_decode(r)?,
|
||||||
indexes: {
|
indexes: {
|
||||||
// Manually decode indexes because they are differentially encoded VarInts.
|
// Manually decode indexes because they are differentially encoded VarInts.
|
||||||
let nb_indexes = VarInt::consensus_decode(&mut d)?.0 as usize;
|
let nb_indexes = VarInt::consensus_decode(r)?.0 as usize;
|
||||||
|
|
||||||
// Since the number of indices ultimately represent transactions,
|
// Since the number of indices ultimately represent transactions,
|
||||||
// we can limit the number of indices to the maximum number of
|
// we can limit the number of indices to the maximum number of
|
||||||
|
@ -299,14 +295,14 @@ impl Decodable for BlockTransactionsRequest {
|
||||||
let mut indexes = Vec::with_capacity(nb_indexes);
|
let mut indexes = Vec::with_capacity(nb_indexes);
|
||||||
let mut last_index: u64 = 0;
|
let mut last_index: u64 = 0;
|
||||||
for _ in 0..nb_indexes {
|
for _ in 0..nb_indexes {
|
||||||
let differential: VarInt = Decodable::consensus_decode(&mut d)?;
|
let differential: VarInt = Decodable::consensus_decode(r)?;
|
||||||
last_index = match last_index.checked_add(differential.0) {
|
last_index = match last_index.checked_add(differential.0) {
|
||||||
Some(r) => r,
|
Some(i) => i,
|
||||||
None => return Err(encode::Error::ParseFailed("block index overflow")),
|
None => return Err(encode::Error::ParseFailed("block index overflow")),
|
||||||
};
|
};
|
||||||
indexes.push(last_index);
|
indexes.push(last_index);
|
||||||
last_index = match last_index.checked_add(1) {
|
last_index = match last_index.checked_add(1) {
|
||||||
Some(r) => r,
|
Some(i) => i,
|
||||||
None => return Err(encode::Error::ParseFailed("block index overflow")),
|
None => return Err(encode::Error::ParseFailed("block index overflow")),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -642,12 +642,12 @@ impl_vec!((u32, Address));
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl_vec!(AddrV2Message);
|
impl_vec!(AddrV2Message);
|
||||||
|
|
||||||
pub(crate) fn consensus_encode_with_size<S: io::Write>(
|
pub(crate) fn consensus_encode_with_size<W: io::Write>(
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
mut s: S,
|
mut w: W,
|
||||||
) -> Result<usize, io::Error> {
|
) -> Result<usize, io::Error> {
|
||||||
let vi_len = VarInt(data.len() as u64).consensus_encode(&mut s)?;
|
let vi_len = VarInt(data.len() as u64).consensus_encode(&mut w)?;
|
||||||
s.emit_slice(data)?;
|
w.emit_slice(data)?;
|
||||||
Ok(vi_len + data.len())
|
Ok(vi_len + data.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -141,7 +141,7 @@ pub enum AddrV2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for AddrV2 {
|
impl Encodable for AddrV2 {
|
||||||
fn consensus_encode<W: io::Write + ?Sized>(&self, e: &mut W) -> Result<usize, io::Error> {
|
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
|
||||||
fn encode_addr<W: io::Write + ?Sized>(
|
fn encode_addr<W: io::Write + ?Sized>(
|
||||||
w: &mut W,
|
w: &mut W,
|
||||||
network: u8,
|
network: u8,
|
||||||
|
@ -154,13 +154,13 @@ impl Encodable for AddrV2 {
|
||||||
Ok(len)
|
Ok(len)
|
||||||
}
|
}
|
||||||
Ok(match *self {
|
Ok(match *self {
|
||||||
AddrV2::Ipv4(ref addr) => encode_addr(e, 1, &addr.octets())?,
|
AddrV2::Ipv4(ref addr) => encode_addr(w, 1, &addr.octets())?,
|
||||||
AddrV2::Ipv6(ref addr) => encode_addr(e, 2, &addr.octets())?,
|
AddrV2::Ipv6(ref addr) => encode_addr(w, 2, &addr.octets())?,
|
||||||
AddrV2::TorV2(ref bytes) => encode_addr(e, 3, bytes)?,
|
AddrV2::TorV2(ref bytes) => encode_addr(w, 3, bytes)?,
|
||||||
AddrV2::TorV3(ref bytes) => encode_addr(e, 4, bytes)?,
|
AddrV2::TorV3(ref bytes) => encode_addr(w, 4, bytes)?,
|
||||||
AddrV2::I2p(ref bytes) => encode_addr(e, 5, bytes)?,
|
AddrV2::I2p(ref bytes) => encode_addr(w, 5, bytes)?,
|
||||||
AddrV2::Cjdns(ref addr) => encode_addr(e, 6, &addr.octets())?,
|
AddrV2::Cjdns(ref addr) => encode_addr(w, 6, &addr.octets())?,
|
||||||
AddrV2::Unknown(network, ref bytes) => encode_addr(e, network, bytes)?,
|
AddrV2::Unknown(network, ref bytes) => encode_addr(w, network, bytes)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue