Refactor consensus_encode

The implementations of `consensus_encode` use an unnecessary number of
lines. Favour more terse code with no loss of clarity.
This commit is contained in:
Tobin Harding 2022-01-24 11:33:38 +11:00
parent a8ed95ea07
commit 702e8bf82d
11 changed files with 20 additions and 80 deletions

View File

@ -1027,10 +1027,7 @@ impl serde::Serialize for Script {
impl Encodable for Script {
#[inline]
fn consensus_encode<S: io::Write>(
&self,
s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, io::Error> {
self.0.consensus_encode(s)
}
}

View File

@ -547,10 +547,7 @@ impl Transaction {
impl_consensus_encoding!(TxOut, value, script_pubkey);
impl Encodable for OutPoint {
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let len = self.txid.consensus_encode(&mut s)?;
Ok(len + self.vout.consensus_encode(s)?)
}
@ -565,10 +562,7 @@ impl Decodable for OutPoint {
}
impl Encodable for TxIn {
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
len += self.previous_output.consensus_encode(&mut s)?;
len += self.script_sig.consensus_encode(&mut s)?;
@ -588,10 +582,7 @@ impl Decodable for TxIn {
}
impl Encodable for Transaction {
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
len += self.version.consensus_encode(&mut s)?;
// To avoid serialization ambiguity, no inputs means we use BIP141 serialization (see

View File

@ -343,10 +343,7 @@ macro_rules! impl_int_encodable {
}
impl Encodable for $ty {
#[inline]
fn consensus_encode<S: WriteExt>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: WriteExt>(&self, mut s: S) -> Result<usize, io::Error> {
s.$meth_enc(*self)?;
Ok(mem::size_of::<$ty>())
}
@ -500,10 +497,7 @@ macro_rules! impl_array {
( $size:expr ) => (
impl Encodable for [u8; $size] {
#[inline]
fn consensus_encode<S: WriteExt>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: WriteExt>(&self, mut s: S) -> Result<usize, io::Error> {
s.emit_slice(&self[..])?;
Ok(self.len())
}
@ -553,10 +547,7 @@ macro_rules! impl_vec {
($type: ty) => {
impl Encodable for Vec<$type> {
#[inline]
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
len += VarInt(self.len() as u64).consensus_encode(&mut s)?;
for c in self.iter() {

View File

@ -82,10 +82,7 @@ fn addr_to_be(addr: [u16; 8]) -> [u16; 8] {
impl Encodable for Address {
#[inline]
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let len = self.services.consensus_encode(&mut s)?
+ addr_to_be(self.address).consensus_encode(&mut s)?

View File

@ -274,10 +274,7 @@ impl ops::BitXorAssign for ServiceFlags {
impl Encodable for ServiceFlags {
#[inline]
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
self.0.consensus_encode(&mut s)
}
}

View File

@ -75,10 +75,7 @@ impl AsRef<str> for CommandString {
impl Encodable for CommandString {
#[inline]
fn consensus_encode<S: io::Write>(
&self,
s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, io::Error> {
let mut rawbytes = [0u8; 12];
let strbytes = self.0.as_bytes();
debug_assert!(strbytes.len() <= 12);
@ -281,10 +278,7 @@ struct HeaderSerializationWrapper<'a>(&'a Vec<block::BlockHeader>);
impl<'a> Encodable for HeaderSerializationWrapper<'a> {
#[inline]
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
len += VarInt(self.0.len() as u64).consensus_encode(&mut s)?;
for header in self.0.iter() {
@ -296,10 +290,7 @@ impl<'a> Encodable for HeaderSerializationWrapper<'a> {
}
impl Encodable for RawNetworkMessage {
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
len += self.magic.consensus_encode(&mut s)?;
len += self.command().consensus_encode(&mut s)?;

View File

@ -54,10 +54,7 @@ pub enum Inventory {
impl Encodable for Inventory {
#[inline]
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
macro_rules! encode_inv {
($code:expr, $item:expr) => {
u32::consensus_encode(&$code, &mut s)? +

View File

@ -348,10 +348,7 @@ impl PartialMerkleTree {
}
impl Encodable for PartialMerkleTree {
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let ret = self.num_transactions.consensus_encode(&mut s)?
+ self.hashes.consensus_encode(&mut s)?;
let mut bytes: Vec<u8> = vec![0; (self.bits.len() + 7) / 8];
@ -502,10 +499,7 @@ impl MerkleBlock {
}
impl Encodable for MerkleBlock {
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let len = self.header.consensus_encode(&mut s)?
+ self.txn.consensus_encode(s)?;
Ok(len)

View File

@ -32,16 +32,10 @@ pub(super) trait Map {
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error>;
/// Encodes map data with bitcoin consensus encoding.
fn consensus_encode_map<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode_map<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
for pair in Map::get_pairs(self)? {
len += encode::Encodable::consensus_encode(
&pair,
&mut s,
)?;
len += encode::Encodable::consensus_encode(&pair, &mut s)?;
}
Ok(len + encode::Encodable::consensus_encode(&0x00_u8, s)?)

View File

@ -238,10 +238,7 @@ mod display_from_str {
pub use self::display_from_str::PsbtParseError;
impl Encodable for PartiallySignedTransaction {
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
len += b"psbt".consensus_encode(&mut s)?;

View File

@ -107,10 +107,7 @@ impl Decodable for Key {
}
impl Encodable for Key {
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let mut len = 0;
len += VarInt((self.key.len() + 1) as u64).consensus_encode(&mut s)?;
@ -125,10 +122,7 @@ impl Encodable for Key {
}
impl Encodable for Pair {
fn consensus_encode<S: io::Write>(
&self,
mut s: S,
) -> Result<usize, io::Error> {
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
let len = self.key.consensus_encode(&mut s)?;
Ok(len + self.value.consensus_encode(s)?)
}