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:
parent
a8ed95ea07
commit
702e8bf82d
|
@ -1027,10 +1027,7 @@ impl serde::Serialize for Script {
|
||||||
|
|
||||||
impl Encodable for Script {
|
impl Encodable for Script {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
self.0.consensus_encode(s)
|
self.0.consensus_encode(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -547,10 +547,7 @@ impl Transaction {
|
||||||
impl_consensus_encoding!(TxOut, value, script_pubkey);
|
impl_consensus_encoding!(TxOut, value, script_pubkey);
|
||||||
|
|
||||||
impl Encodable for OutPoint {
|
impl Encodable for OutPoint {
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let len = self.txid.consensus_encode(&mut s)?;
|
let len = self.txid.consensus_encode(&mut s)?;
|
||||||
Ok(len + self.vout.consensus_encode(s)?)
|
Ok(len + self.vout.consensus_encode(s)?)
|
||||||
}
|
}
|
||||||
|
@ -565,10 +562,7 @@ impl Decodable for OutPoint {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for TxIn {
|
impl Encodable for TxIn {
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
len += self.previous_output.consensus_encode(&mut s)?;
|
len += self.previous_output.consensus_encode(&mut s)?;
|
||||||
len += self.script_sig.consensus_encode(&mut s)?;
|
len += self.script_sig.consensus_encode(&mut s)?;
|
||||||
|
@ -588,10 +582,7 @@ impl Decodable for TxIn {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for Transaction {
|
impl Encodable for Transaction {
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
len += self.version.consensus_encode(&mut s)?;
|
len += self.version.consensus_encode(&mut s)?;
|
||||||
// To avoid serialization ambiguity, no inputs means we use BIP141 serialization (see
|
// To avoid serialization ambiguity, no inputs means we use BIP141 serialization (see
|
||||||
|
|
|
@ -343,10 +343,7 @@ macro_rules! impl_int_encodable {
|
||||||
}
|
}
|
||||||
impl Encodable for $ty {
|
impl Encodable for $ty {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: WriteExt>(
|
fn consensus_encode<S: WriteExt>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
s.$meth_enc(*self)?;
|
s.$meth_enc(*self)?;
|
||||||
Ok(mem::size_of::<$ty>())
|
Ok(mem::size_of::<$ty>())
|
||||||
}
|
}
|
||||||
|
@ -500,10 +497,7 @@ macro_rules! impl_array {
|
||||||
( $size:expr ) => (
|
( $size:expr ) => (
|
||||||
impl Encodable for [u8; $size] {
|
impl Encodable for [u8; $size] {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: WriteExt>(
|
fn consensus_encode<S: WriteExt>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
s.emit_slice(&self[..])?;
|
s.emit_slice(&self[..])?;
|
||||||
Ok(self.len())
|
Ok(self.len())
|
||||||
}
|
}
|
||||||
|
@ -553,10 +547,7 @@ macro_rules! impl_vec {
|
||||||
($type: ty) => {
|
($type: ty) => {
|
||||||
impl Encodable for Vec<$type> {
|
impl Encodable for Vec<$type> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
len += VarInt(self.len() as u64).consensus_encode(&mut s)?;
|
len += VarInt(self.len() as u64).consensus_encode(&mut s)?;
|
||||||
for c in self.iter() {
|
for c in self.iter() {
|
||||||
|
|
|
@ -82,10 +82,7 @@ fn addr_to_be(addr: [u16; 8]) -> [u16; 8] {
|
||||||
|
|
||||||
impl Encodable for Address {
|
impl Encodable for Address {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let len = self.services.consensus_encode(&mut s)?
|
let len = self.services.consensus_encode(&mut s)?
|
||||||
+ addr_to_be(self.address).consensus_encode(&mut s)?
|
+ addr_to_be(self.address).consensus_encode(&mut s)?
|
||||||
|
|
||||||
|
|
|
@ -274,10 +274,7 @@ impl ops::BitXorAssign for ServiceFlags {
|
||||||
|
|
||||||
impl Encodable for ServiceFlags {
|
impl Encodable for ServiceFlags {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
self.0.consensus_encode(&mut s)
|
self.0.consensus_encode(&mut s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,10 +75,7 @@ impl AsRef<str> for CommandString {
|
||||||
|
|
||||||
impl Encodable for CommandString {
|
impl Encodable for CommandString {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let mut rawbytes = [0u8; 12];
|
let mut rawbytes = [0u8; 12];
|
||||||
let strbytes = self.0.as_bytes();
|
let strbytes = self.0.as_bytes();
|
||||||
debug_assert!(strbytes.len() <= 12);
|
debug_assert!(strbytes.len() <= 12);
|
||||||
|
@ -281,10 +278,7 @@ struct HeaderSerializationWrapper<'a>(&'a Vec<block::BlockHeader>);
|
||||||
|
|
||||||
impl<'a> Encodable for HeaderSerializationWrapper<'a> {
|
impl<'a> Encodable for HeaderSerializationWrapper<'a> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
len += VarInt(self.0.len() as u64).consensus_encode(&mut s)?;
|
len += VarInt(self.0.len() as u64).consensus_encode(&mut s)?;
|
||||||
for header in self.0.iter() {
|
for header in self.0.iter() {
|
||||||
|
@ -296,10 +290,7 @@ impl<'a> Encodable for HeaderSerializationWrapper<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for RawNetworkMessage {
|
impl Encodable for RawNetworkMessage {
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
len += self.magic.consensus_encode(&mut s)?;
|
len += self.magic.consensus_encode(&mut s)?;
|
||||||
len += self.command().consensus_encode(&mut s)?;
|
len += self.command().consensus_encode(&mut s)?;
|
||||||
|
|
|
@ -54,10 +54,7 @@ pub enum Inventory {
|
||||||
|
|
||||||
impl Encodable for Inventory {
|
impl Encodable for Inventory {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
macro_rules! encode_inv {
|
macro_rules! encode_inv {
|
||||||
($code:expr, $item:expr) => {
|
($code:expr, $item:expr) => {
|
||||||
u32::consensus_encode(&$code, &mut s)? +
|
u32::consensus_encode(&$code, &mut s)? +
|
||||||
|
|
|
@ -348,10 +348,7 @@ impl PartialMerkleTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for PartialMerkleTree {
|
impl Encodable for PartialMerkleTree {
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let ret = self.num_transactions.consensus_encode(&mut s)?
|
let ret = self.num_transactions.consensus_encode(&mut s)?
|
||||||
+ self.hashes.consensus_encode(&mut s)?;
|
+ self.hashes.consensus_encode(&mut s)?;
|
||||||
let mut bytes: Vec<u8> = vec![0; (self.bits.len() + 7) / 8];
|
let mut bytes: Vec<u8> = vec![0; (self.bits.len() + 7) / 8];
|
||||||
|
@ -502,10 +499,7 @@ impl MerkleBlock {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for MerkleBlock {
|
impl Encodable for MerkleBlock {
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let len = self.header.consensus_encode(&mut s)?
|
let len = self.header.consensus_encode(&mut s)?
|
||||||
+ self.txn.consensus_encode(s)?;
|
+ self.txn.consensus_encode(s)?;
|
||||||
Ok(len)
|
Ok(len)
|
||||||
|
|
|
@ -32,16 +32,10 @@ pub(super) trait Map {
|
||||||
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error>;
|
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error>;
|
||||||
|
|
||||||
/// Encodes map data with bitcoin consensus encoding.
|
/// Encodes map data with bitcoin consensus encoding.
|
||||||
fn consensus_encode_map<S: io::Write>(
|
fn consensus_encode_map<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
for pair in Map::get_pairs(self)? {
|
for pair in Map::get_pairs(self)? {
|
||||||
len += encode::Encodable::consensus_encode(
|
len += encode::Encodable::consensus_encode(&pair, &mut s)?;
|
||||||
&pair,
|
|
||||||
&mut s,
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(len + encode::Encodable::consensus_encode(&0x00_u8, s)?)
|
Ok(len + encode::Encodable::consensus_encode(&0x00_u8, s)?)
|
||||||
|
|
|
@ -238,10 +238,7 @@ mod display_from_str {
|
||||||
pub use self::display_from_str::PsbtParseError;
|
pub use self::display_from_str::PsbtParseError;
|
||||||
|
|
||||||
impl Encodable for PartiallySignedTransaction {
|
impl Encodable for PartiallySignedTransaction {
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
len += b"psbt".consensus_encode(&mut s)?;
|
len += b"psbt".consensus_encode(&mut s)?;
|
||||||
|
|
||||||
|
|
|
@ -107,10 +107,7 @@ impl Decodable for Key {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for Key {
|
impl Encodable for Key {
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
len += VarInt((self.key.len() + 1) as u64).consensus_encode(&mut s)?;
|
len += VarInt((self.key.len() + 1) as u64).consensus_encode(&mut s)?;
|
||||||
|
|
||||||
|
@ -125,10 +122,7 @@ impl Encodable for Key {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for Pair {
|
impl Encodable for Pair {
|
||||||
fn consensus_encode<S: io::Write>(
|
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, io::Error> {
|
||||||
&self,
|
|
||||||
mut s: S,
|
|
||||||
) -> Result<usize, io::Error> {
|
|
||||||
let len = self.key.consensus_encode(&mut s)?;
|
let len = self.key.consensus_encode(&mut s)?;
|
||||||
Ok(len + self.value.consensus_encode(s)?)
|
Ok(len + self.value.consensus_encode(s)?)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue