rename Encoder to WriteExt and Decoder to ReadExt

This commit is contained in:
Andrew Poelstra 2019-05-23 18:43:47 +00:00
parent 24ebc29005
commit 7e6ad7c893
16 changed files with 96 additions and 100 deletions

View File

@ -24,8 +24,7 @@
use std::fmt; use std::fmt;
use consensus::encode::{self, Decoder, Encoder}; use consensus::{encode, Decodable, Encodable, ReadExt, WriteExt};
use consensus::encode::{Decodable, Encodable};
// Note: I am deliberately not implementing PartialOrd or Ord on the // Note: I am deliberately not implementing PartialOrd or Ord on the
// opcode enum. If you want to check ranges of opcodes, etc., // opcode enum. If you want to check ranges of opcodes, etc.,
@ -715,14 +714,14 @@ impl From<u8> for All {
display_from_debug!(All); display_from_debug!(All);
impl<D: Decoder> Decodable<D> for All { impl<D: ReadExt> Decodable<D> for All {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<All, encode::Error> { fn consensus_decode(d: &mut D) -> Result<All, encode::Error> {
Ok(All::from(d.read_u8()?)) Ok(All::from(d.read_u8()?))
} }
} }
impl<S: Encoder> Encodable<S> for All { impl<S: WriteExt> Encodable<S> for All {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
s.emit_u8(self.code) s.emit_u8(self.code)

View File

@ -30,8 +30,7 @@ use std::{error, fmt};
#[cfg(feature = "serde")] use serde; #[cfg(feature = "serde")] use serde;
use blockdata::opcodes; use blockdata::opcodes;
use consensus::encode::{Decodable, Encodable}; use consensus::{encode, Decodable, Encodable, ReadExt, WriteExt};
use consensus::encode::{self, Decoder, Encoder};
use bitcoin_hashes::{hash160, sha256, Hash}; use bitcoin_hashes::{hash160, sha256, Hash};
#[cfg(feature="bitcoinconsensus")] use bitcoinconsensus; #[cfg(feature="bitcoinconsensus")] use bitcoinconsensus;
#[cfg(feature="bitcoinconsensus")] use std::convert; #[cfg(feature="bitcoinconsensus")] use std::convert;
@ -726,14 +725,14 @@ impl serde::Serialize for Script {
} }
// Network serialization // Network serialization
impl<S: Encoder> Encodable<S> for Script { impl<S: WriteExt> Encodable<S> for Script {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
self.0.consensus_encode(s) self.0.consensus_encode(s)
} }
} }
impl<D: Decoder> Decodable<D> for Script { impl<D: ReadExt> Decodable<D> for Script {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<Script, encode::Error> { fn consensus_decode(d: &mut D) -> Result<Script, encode::Error> {
Ok(Script(Decodable::consensus_decode(d)?)) Ok(Script(Decodable::consensus_decode(d)?))

View File

@ -34,8 +34,8 @@ use bitcoin_hashes::hex::FromHex;
use util::hash::BitcoinHash; use util::hash::BitcoinHash;
#[cfg(feature="bitcoinconsensus")] use blockdata::script; #[cfg(feature="bitcoinconsensus")] use blockdata::script;
use blockdata::script::Script; use blockdata::script::Script;
use consensus::encode::{self, serialize, Encoder, Decoder}; use consensus::{encode, serialize, Decodable, Encodable, ReadExt, WriteExt};
use consensus::encode::{Encodable, Decodable, VarInt}; use VarInt;
/// A reference to a transaction output /// A reference to a transaction output
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)] #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
@ -439,13 +439,13 @@ impl BitcoinHash for Transaction {
impl_consensus_encoding!(TxOut, value, script_pubkey); impl_consensus_encoding!(TxOut, value, script_pubkey);
impl<S: Encoder> Encodable<S> for OutPoint { impl<S: WriteExt> Encodable<S> for OutPoint {
fn consensus_encode(&self, s: &mut S) -> Result <(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result <(), encode::Error> {
self.txid.consensus_encode(s)?; self.txid.consensus_encode(s)?;
self.vout.consensus_encode(s) self.vout.consensus_encode(s)
} }
} }
impl<D: Decoder> Decodable<D> for OutPoint { impl<D: ReadExt> Decodable<D> for OutPoint {
fn consensus_decode(d: &mut D) -> Result<OutPoint, encode::Error> { fn consensus_decode(d: &mut D) -> Result<OutPoint, encode::Error> {
Ok(OutPoint { Ok(OutPoint {
txid: Decodable::consensus_decode(d)?, txid: Decodable::consensus_decode(d)?,
@ -454,14 +454,14 @@ impl<D: Decoder> Decodable<D> for OutPoint {
} }
} }
impl<S: Encoder> Encodable<S> for TxIn { impl<S: WriteExt> Encodable<S> for TxIn {
fn consensus_encode(&self, s: &mut S) -> Result <(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result <(), encode::Error> {
self.previous_output.consensus_encode(s)?; self.previous_output.consensus_encode(s)?;
self.script_sig.consensus_encode(s)?; self.script_sig.consensus_encode(s)?;
self.sequence.consensus_encode(s) self.sequence.consensus_encode(s)
} }
} }
impl<D: Decoder> Decodable<D> for TxIn { impl<D: ReadExt> Decodable<D> for TxIn {
fn consensus_decode(d: &mut D) -> Result<TxIn, encode::Error> { fn consensus_decode(d: &mut D) -> Result<TxIn, encode::Error> {
Ok(TxIn { Ok(TxIn {
previous_output: Decodable::consensus_decode(d)?, previous_output: Decodable::consensus_decode(d)?,
@ -472,7 +472,7 @@ impl<D: Decoder> Decodable<D> for TxIn {
} }
} }
impl<S: Encoder> Encodable<S> for Transaction { impl<S: WriteExt> Encodable<S> for Transaction {
fn consensus_encode(&self, s: &mut S) -> Result <(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result <(), encode::Error> {
self.version.consensus_encode(s)?; self.version.consensus_encode(s)?;
let mut have_witness = self.input.is_empty(); let mut have_witness = self.input.is_empty();
@ -498,7 +498,7 @@ impl<S: Encoder> Encodable<S> for Transaction {
} }
} }
impl<D: Decoder> Decodable<D> for Transaction { impl<D: ReadExt> Decodable<D> for Transaction {
fn consensus_decode(d: &mut D) -> Result<Transaction, encode::Error> { fn consensus_decode(d: &mut D) -> Result<Transaction, encode::Error> {
let version: u32 = Decodable::consensus_decode(d)?; let version: u32 = Decodable::consensus_decode(d)?;
let input: Vec<TxIn> = Decodable::consensus_decode(d)?; let input: Vec<TxIn> = Decodable::consensus_decode(d)?;

View File

@ -242,8 +242,8 @@ pub fn deserialize_partial<'a, T>(data: &'a [u8]) -> Result<(T, usize), Error>
} }
/// A simple Encoder trait /// Extensions of `Write` to encode data as per Bitcoin consensus
pub trait Encoder { pub trait WriteExt {
/// Output a 64-bit uint /// Output a 64-bit uint
fn emit_u64(&mut self, v: u64) -> Result<(), Error>; fn emit_u64(&mut self, v: u64) -> Result<(), Error>;
/// Output a 32-bit uint /// Output a 32-bit uint
@ -269,8 +269,8 @@ pub trait Encoder {
fn emit_slice(&mut self, v: &[u8]) -> Result<(), Error>; fn emit_slice(&mut self, v: &[u8]) -> Result<(), Error>;
} }
/// A simple Decoder trait /// Extensions of `Read` to decode data as per Bitcoin consensus
pub trait Decoder { pub trait ReadExt {
/// Read a 64-bit uint /// Read a 64-bit uint
fn read_u64(&mut self) -> Result<u64, Error>; fn read_u64(&mut self) -> Result<u64, Error>;
/// Read a 32-bit uint /// Read a 32-bit uint
@ -314,7 +314,7 @@ macro_rules! decoder_fn {
} }
} }
impl<W: Write> Encoder for W { impl<W: Write> WriteExt for W {
encoder_fn!(emit_u64, u64, write_u64); encoder_fn!(emit_u64, u64, write_u64);
encoder_fn!(emit_u32, u32, write_u32); encoder_fn!(emit_u32, u32, write_u32);
encoder_fn!(emit_u16, u16, write_u16); encoder_fn!(emit_u16, u16, write_u16);
@ -340,7 +340,7 @@ impl<W: Write> Encoder for W {
} }
} }
impl<R: Read> Decoder for R { impl<R: Read> ReadExt for R {
decoder_fn!(read_u64, u64, read_u64); decoder_fn!(read_u64, u64, read_u64);
decoder_fn!(read_u32, u32, read_u32); decoder_fn!(read_u32, u32, read_u32);
decoder_fn!(read_u16, u16, read_u16); decoder_fn!(read_u16, u16, read_u16);
@ -358,7 +358,7 @@ impl<R: Read> Decoder for R {
} }
#[inline] #[inline]
fn read_bool(&mut self) -> Result<bool, Error> { fn read_bool(&mut self) -> Result<bool, Error> {
Decoder::read_i8(self).map(|bit| bit != 0) ReadExt::read_i8(self).map(|bit| bit != 0)
} }
#[inline] #[inline]
fn read_slice(&mut self, slice: &mut [u8]) -> Result<(), Error> { fn read_slice(&mut self, slice: &mut [u8]) -> Result<(), Error> {
@ -370,14 +370,14 @@ impl<R: Read> Decoder for R {
pub const MAX_VEC_SIZE: usize = 32 * 1024 * 1024; pub const MAX_VEC_SIZE: usize = 32 * 1024 * 1024;
/// Data which can be encoded in a consensus-consistent way /// Data which can be encoded in a consensus-consistent way
pub trait Encodable<S: Encoder> { pub trait Encodable<S: WriteExt> {
/// Encode an object with a well-defined format, should only ever error if /// Encode an object with a well-defined format, should only ever error if
/// the underlying Encoder errors. /// the underlying WriteExt errors.
fn consensus_encode(&self, e: &mut S) -> Result<(), self::Error>; fn consensus_encode(&self, e: &mut S) -> Result<(), self::Error>;
} }
/// Data which can be encoded in a consensus-consistent way /// Data which can be encoded in a consensus-consistent way
pub trait Decodable<D: Decoder>: Sized { pub trait Decodable<D: ReadExt>: Sized {
/// Decode an object with a well-defined format /// Decode an object with a well-defined format
fn consensus_decode(d: &mut D) -> Result<Self, self::Error>; fn consensus_decode(d: &mut D) -> Result<Self, self::Error>;
} }
@ -393,12 +393,12 @@ pub struct CheckedData(pub Vec<u8>);
// Primitive types // Primitive types
macro_rules! impl_int_encodable{ macro_rules! impl_int_encodable{
($ty:ident, $meth_dec:ident, $meth_enc:ident) => ( ($ty:ident, $meth_dec:ident, $meth_enc:ident) => (
impl<D: Decoder> Decodable<D> for $ty { impl<D: ReadExt> Decodable<D> for $ty {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<$ty, self::Error> { d.$meth_dec().map($ty::from_le) } fn consensus_decode(d: &mut D) -> Result<$ty, self::Error> { d.$meth_dec().map($ty::from_le) }
} }
impl<S: Encoder> Encodable<S> for $ty { impl<S: WriteExt> Encodable<S> for $ty {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { s.$meth_enc(self.to_le()) } fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { s.$meth_enc(self.to_le()) }
} }
@ -429,7 +429,7 @@ impl VarInt {
} }
} }
impl<S: Encoder> Encodable<S> for VarInt { impl<S: WriteExt> Encodable<S> for VarInt {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
match self.0 { match self.0 {
@ -441,7 +441,7 @@ impl<S: Encoder> Encodable<S> for VarInt {
} }
} }
impl<D: Decoder> Decodable<D> for VarInt { impl<D: ReadExt> Decodable<D> for VarInt {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<VarInt, self::Error> { fn consensus_decode(d: &mut D) -> Result<VarInt, self::Error> {
let n = d.read_u8()?; let n = d.read_u8()?;
@ -477,18 +477,18 @@ impl<D: Decoder> Decodable<D> for VarInt {
// Booleans // Booleans
impl<S: Encoder> Encodable<S> for bool { impl<S: WriteExt> Encodable<S> for bool {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { s.emit_u8(if *self {1} else {0}) } fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { s.emit_u8(if *self {1} else {0}) }
} }
impl<D: Decoder> Decodable<D> for bool { impl<D: ReadExt> Decodable<D> for bool {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<bool, self::Error> { d.read_u8().map(|n| n != 0) } fn consensus_decode(d: &mut D) -> Result<bool, self::Error> { d.read_u8().map(|n| n != 0) }
} }
// Strings // Strings
impl<S: Encoder> Encodable<S> for String { impl<S: WriteExt> Encodable<S> for String {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
let b = self.as_bytes(); let b = self.as_bytes();
@ -497,7 +497,7 @@ impl<S: Encoder> Encodable<S> for String {
} }
} }
impl<D: Decoder> Decodable<D> for String { impl<D: ReadExt> Decodable<D> for String {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<String, self::Error> { fn consensus_decode(d: &mut D) -> Result<String, self::Error> {
String::from_utf8(Decodable::consensus_decode(d)?) String::from_utf8(Decodable::consensus_decode(d)?)
@ -509,14 +509,14 @@ impl<D: Decoder> Decodable<D> for String {
// Arrays // Arrays
macro_rules! impl_array { macro_rules! impl_array {
( $size:expr ) => ( ( $size:expr ) => (
impl<S: Encoder> Encodable<S> for [u8; $size] { impl<S: WriteExt> Encodable<S> for [u8; $size] {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
s.emit_slice(&self[..]) s.emit_slice(&self[..])
} }
} }
impl<D: Decoder> Decodable<D> for [u8; $size] { impl<D: ReadExt> Decodable<D> for [u8; $size] {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<[u8; $size], self::Error> { fn consensus_decode(d: &mut D) -> Result<[u8; $size], self::Error> {
let mut ret = [0; $size]; let mut ret = [0; $size];
@ -535,7 +535,7 @@ impl_array!(16);
impl_array!(32); impl_array!(32);
impl_array!(33); impl_array!(33);
impl<D: Decoder> Decodable<D> for [u16; 8] { impl<D: ReadExt> Decodable<D> for [u16; 8] {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<[u16; 8], self::Error> { fn consensus_decode(d: &mut D) -> Result<[u16; 8], self::Error> {
let mut res = [0; 8]; let mut res = [0; 8];
@ -546,7 +546,7 @@ impl<D: Decoder> Decodable<D> for [u16; 8] {
} }
} }
impl<S: Encoder> Encodable<S> for [u16; 8] { impl<S: WriteExt> Encodable<S> for [u16; 8] {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
for c in self.iter() { c.consensus_encode(s)?; } for c in self.iter() { c.consensus_encode(s)?; }
@ -557,7 +557,7 @@ impl<S: Encoder> Encodable<S> for [u16; 8] {
// Vectors // Vectors
macro_rules! impl_vec { macro_rules! impl_vec {
($type: ty) => { ($type: ty) => {
impl<S: Encoder> Encodable<S> for Vec<$type> { impl<S: WriteExt> Encodable<S> for Vec<$type> {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
VarInt(self.len() as u64).consensus_encode(s)?; VarInt(self.len() as u64).consensus_encode(s)?;
@ -566,7 +566,7 @@ macro_rules! impl_vec {
} }
} }
impl<D: Decoder> Decodable<D> for Vec<$type> { impl<D: ReadExt> Decodable<D> for Vec<$type> {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<Vec<$type>, self::Error> { fn consensus_decode(d: &mut D) -> Result<Vec<$type>, self::Error> {
let len = VarInt::consensus_decode(d)?.0; let len = VarInt::consensus_decode(d)?.0;
@ -592,7 +592,7 @@ impl_vec!(Vec<u8>);
impl_vec!((u32, Address)); impl_vec!((u32, Address));
impl_vec!(u64); impl_vec!(u64);
impl<S: Encoder> Encodable<S> for Vec<u8> { impl<S: WriteExt> Encodable<S> for Vec<u8> {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
VarInt(self.len() as u64).consensus_encode(s)?; VarInt(self.len() as u64).consensus_encode(s)?;
@ -600,7 +600,7 @@ impl<S: Encoder> Encodable<S> for Vec<u8> {
} }
} }
impl<D: Decoder> Decodable<D> for Vec<u8> { impl<D: ReadExt> Decodable<D> for Vec<u8> {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<Vec<u8>, self::Error> { fn consensus_decode(d: &mut D) -> Result<Vec<u8>, self::Error> {
let len = VarInt::consensus_decode(d)?.0; let len = VarInt::consensus_decode(d)?.0;
@ -615,7 +615,7 @@ impl<D: Decoder> Decodable<D> for Vec<u8> {
} }
} }
impl<S: Encoder> Encodable<S> for Box<[u8]> { impl<S: WriteExt> Encodable<S> for Box<[u8]> {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
VarInt(self.len() as u64).consensus_encode(s)?; VarInt(self.len() as u64).consensus_encode(s)?;
@ -623,7 +623,7 @@ impl<S: Encoder> Encodable<S> for Box<[u8]> {
} }
} }
impl<D: Decoder> Decodable<D> for Box<[u8]> { impl<D: ReadExt> Decodable<D> for Box<[u8]> {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<Box<[u8]>, self::Error> { fn consensus_decode(d: &mut D) -> Result<Box<[u8]>, self::Error> {
let len = VarInt::consensus_decode(d)?.0; let len = VarInt::consensus_decode(d)?.0;
@ -646,7 +646,7 @@ fn sha2_checksum(data: &[u8]) -> [u8; 4] {
} }
// Checked data // Checked data
impl<S: Encoder> Encodable<S> for CheckedData { impl<S: WriteExt> Encodable<S> for CheckedData {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
(self.0.len() as u32).consensus_encode(s)?; (self.0.len() as u32).consensus_encode(s)?;
@ -655,7 +655,7 @@ impl<S: Encoder> Encodable<S> for CheckedData {
} }
} }
impl<D: Decoder> Decodable<D> for CheckedData { impl<D: ReadExt> Decodable<D> for CheckedData {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<CheckedData, self::Error> { fn consensus_decode(d: &mut D) -> Result<CheckedData, self::Error> {
let len: u32 = Decodable::consensus_decode(d)?; let len: u32 = Decodable::consensus_decode(d)?;
@ -684,7 +684,7 @@ impl<D: Decoder> Decodable<D> for CheckedData {
// Tuples // Tuples
macro_rules! tuple_encode { macro_rules! tuple_encode {
($($x:ident),*) => ( ($($x:ident),*) => (
impl <S: Encoder, $($x: Encodable<S>),*> Encodable<S> for ($($x),*) { impl <S: WriteExt, $($x: Encodable<S>),*> Encodable<S> for ($($x),*) {
#[inline] #[inline]
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
@ -694,7 +694,7 @@ macro_rules! tuple_encode {
} }
} }
impl<D: Decoder, $($x: Decodable<D>),*> Decodable<D> for ($($x),*) { impl<D: ReadExt, $($x: Decodable<D>),*> Decodable<D> for ($($x),*) {
#[inline] #[inline]
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn consensus_decode(d: &mut D) -> Result<($($x),*), self::Error> { fn consensus_decode(d: &mut D) -> Result<($($x),*), self::Error> {
@ -709,13 +709,13 @@ tuple_encode!(T0, T1, T2, T3);
tuple_encode!(T0, T1, T2, T3, T4, T5); tuple_encode!(T0, T1, T2, T3, T4, T5);
tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7); tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7);
impl<S: Encoder> Encodable<S> for sha256d::Hash { impl<S: WriteExt> Encodable<S> for sha256d::Hash {
fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> {
self.into_inner().consensus_encode(s) self.into_inner().consensus_encode(s)
} }
} }
impl<D: Decoder> Decodable<D> for sha256d::Hash { impl<D: ReadExt> Decodable<D> for sha256d::Hash {
fn consensus_decode(d: &mut D) -> Result<sha256d::Hash, self::Error> { fn consensus_decode(d: &mut D) -> Result<sha256d::Hash, self::Error> {
let inner: [u8; 32] = Decodable::consensus_decode(d)?; let inner: [u8; 32] = Decodable::consensus_decode(d)?;
Ok(sha256d::Hash::from_slice(&inner).unwrap()) Ok(sha256d::Hash::from_slice(&inner).unwrap())

View File

@ -21,6 +21,6 @@
pub mod encode; pub mod encode;
pub mod params; pub mod params;
pub use self::encode::{Encodable, Decodable, Encoder, Decoder, pub use self::encode::{Encodable, Decodable, WriteExt, ReadExt};
serialize, deserialize, deserialize_partial}; pub use self::encode::{serialize, deserialize, deserialize_partial};
pub use self::params::Params; pub use self::params::Params;

View File

@ -18,7 +18,7 @@
macro_rules! impl_consensus_encoding { macro_rules! impl_consensus_encoding {
($thing:ident, $($field:ident),+) => ( ($thing:ident, $($field:ident),+) => (
impl<S: ::consensus::encode::Encoder> ::consensus::encode::Encodable<S> for $thing { impl<S: ::consensus::WriteExt> ::consensus::Encodable<S> for $thing {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), ::consensus::encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), ::consensus::encode::Error> {
$( self.$field.consensus_encode(s)?; )+ $( self.$field.consensus_encode(s)?; )+
@ -26,12 +26,11 @@ macro_rules! impl_consensus_encoding {
} }
} }
impl<D: ::consensus::encode::Decoder> ::consensus::encode::Decodable<D> for $thing { impl<D: ::consensus::ReadExt> ::consensus::Decodable<D> for $thing {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<$thing, ::consensus::encode::Error> { fn consensus_decode(d: &mut D) -> Result<$thing, ::consensus::encode::Error> {
use consensus::encode::Decodable;
Ok($thing { Ok($thing {
$( $field: Decodable::consensus_decode(d)?, )+ $( $field: ::consensus::Decodable::consensus_decode(d)?, )+
}) })
} }
} }

View File

@ -22,7 +22,7 @@ use std::io;
use std::fmt; use std::fmt;
use std::net::{SocketAddr, Ipv6Addr, SocketAddrV4, SocketAddrV6}; use std::net::{SocketAddr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
use consensus::encode::{self, Encoder, Decoder}; use consensus::{encode, ReadExt, WriteExt};
use consensus::encode::{Decodable, Encodable}; use consensus::encode::{Decodable, Encodable};
/// A message which can be sent on the Bitcoin network /// A message which can be sent on the Bitcoin network
@ -72,7 +72,7 @@ fn addr_to_be(addr: [u16; 8]) -> [u16; 8] {
addr[4].to_be(), addr[5].to_be(), addr[6].to_be(), addr[7].to_be()] addr[4].to_be(), addr[5].to_be(), addr[6].to_be(), addr[7].to_be()]
} }
impl<S: Encoder> Encodable<S> for Address { impl<S: WriteExt> Encodable<S> for Address {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
self.services.consensus_encode(s)?; self.services.consensus_encode(s)?;
@ -81,7 +81,7 @@ impl<S: Encoder> Encodable<S> for Address {
} }
} }
impl<D: Decoder> Decodable<D> for Address { impl<D: ReadExt> Decodable<D> for Address {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<Address, encode::Error> { fn consensus_decode(d: &mut D) -> Result<Address, encode::Error> {
Ok(Address { Ok(Address {

View File

@ -37,8 +37,7 @@
//! assert_eq!(&bytes[..], &[0xF9, 0xBE, 0xB4, 0xD9]); //! assert_eq!(&bytes[..], &[0xF9, 0xBE, 0xB4, 0xD9]);
//! ``` //! ```
use consensus::encode::{Decodable, Encodable}; use consensus::{encode, Decodable, Encodable, ReadExt, WriteExt};
use consensus::encode::{self, Encoder, Decoder};
/// Version of the protocol as appearing in network message headers /// Version of the protocol as appearing in network message headers
pub const PROTOCOL_VERSION: u32 = 70001; pub const PROTOCOL_VERSION: u32 = 70001;
@ -102,7 +101,7 @@ impl Network {
} }
} }
impl<S: Encoder> Encodable<S> for Network { impl<S: WriteExt> Encodable<S> for Network {
/// Encodes the magic bytes of `Network`. /// Encodes the magic bytes of `Network`.
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
@ -110,7 +109,7 @@ impl<S: Encoder> Encodable<S> for Network {
} }
} }
impl<D: Decoder> Decodable<D> for Network { impl<D: ReadExt> Decodable<D> for Network {
/// Decodes the magic bytes of `Network`. /// Decodes the magic bytes of `Network`.
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<Network, encode::Error> { fn consensus_decode(d: &mut D) -> Result<Network, encode::Error> {

View File

@ -30,14 +30,14 @@ use network::message_blockdata;
use network::message_filter; use network::message_filter;
use consensus::encode::{Decodable, Encodable}; use consensus::encode::{Decodable, Encodable};
use consensus::encode::{CheckedData, VarInt}; use consensus::encode::{CheckedData, VarInt};
use consensus::encode::{self, serialize, Encoder, Decoder}; use consensus::{encode, serialize, ReadExt, WriteExt};
use consensus::encode::MAX_VEC_SIZE; use consensus::encode::MAX_VEC_SIZE;
/// Serializer for command string /// Serializer for command string
#[derive(PartialEq, Eq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
pub struct CommandString(pub String); pub struct CommandString(pub String);
impl<S: Encoder> Encodable<S> for CommandString { impl<S: WriteExt> Encodable<S> for CommandString {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
let &CommandString(ref inner_str) = self; let &CommandString(ref inner_str) = self;
@ -53,7 +53,7 @@ impl<S: Encoder> Encodable<S> for CommandString {
} }
} }
impl<D: Decoder> Decodable<D> for CommandString { impl<D: ReadExt> Decodable<D> for CommandString {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<CommandString, encode::Error> { fn consensus_decode(d: &mut D) -> Result<CommandString, encode::Error> {
let rawbytes: [u8; 12] = Decodable::consensus_decode(d)?; let rawbytes: [u8; 12] = Decodable::consensus_decode(d)?;
@ -160,7 +160,7 @@ impl RawNetworkMessage {
} }
struct HeaderSerializationWrapper<'a>(&'a Vec<block::BlockHeader>); struct HeaderSerializationWrapper<'a>(&'a Vec<block::BlockHeader>);
impl <'a, S: Encoder> Encodable<S> for HeaderSerializationWrapper<'a> { impl <'a, S: WriteExt> Encodable<S> for HeaderSerializationWrapper<'a> {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
VarInt(self.0.len() as u64).consensus_encode(s)?; VarInt(self.0.len() as u64).consensus_encode(s)?;
@ -172,7 +172,7 @@ impl <'a, S: Encoder> Encodable<S> for HeaderSerializationWrapper<'a> {
} }
} }
impl<S: Encoder> Encodable<S> for RawNetworkMessage { impl<S: WriteExt> Encodable<S> for RawNetworkMessage {
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
self.magic.consensus_encode(s)?; self.magic.consensus_encode(s)?;
CommandString(self.command()).consensus_encode(s)?; CommandString(self.command()).consensus_encode(s)?;
@ -205,7 +205,7 @@ impl<S: Encoder> Encodable<S> for RawNetworkMessage {
} }
struct HeaderDeserializationWrapper(Vec<block::BlockHeader>); struct HeaderDeserializationWrapper(Vec<block::BlockHeader>);
impl<D: Decoder> Decodable<D> for HeaderDeserializationWrapper { impl<D: ReadExt> Decodable<D> for HeaderDeserializationWrapper {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<HeaderDeserializationWrapper, encode::Error> { fn consensus_decode(d: &mut D) -> Result<HeaderDeserializationWrapper, encode::Error> {
let len = VarInt::consensus_decode(d)?.0; let len = VarInt::consensus_decode(d)?.0;
@ -226,7 +226,7 @@ impl<D: Decoder> Decodable<D> for HeaderDeserializationWrapper {
} }
} }
impl<D: Decoder> Decodable<D> for RawNetworkMessage { impl<D: ReadExt> Decodable<D> for RawNetworkMessage {
fn consensus_decode(d: &mut D) -> Result<RawNetworkMessage, encode::Error> { fn consensus_decode(d: &mut D) -> Result<RawNetworkMessage, encode::Error> {
let magic = Decodable::consensus_decode(d)?; let magic = Decodable::consensus_decode(d)?;
let CommandString(cmd): CommandString= Decodable::consensus_decode(d)?; let CommandString(cmd): CommandString= Decodable::consensus_decode(d)?;

View File

@ -20,7 +20,7 @@
use network::constants; use network::constants;
use consensus::encode::{Decodable, Encodable}; use consensus::encode::{Decodable, Encodable};
use consensus::encode::{self, Decoder, Encoder}; use consensus::{encode, ReadExt, WriteExt};
use bitcoin_hashes::sha256d; use bitcoin_hashes::sha256d;
#[derive(PartialEq, Eq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]
@ -101,7 +101,7 @@ impl GetHeadersMessage {
impl_consensus_encoding!(GetHeadersMessage, version, locator_hashes, stop_hash); impl_consensus_encoding!(GetHeadersMessage, version, locator_hashes, stop_hash);
impl<S: Encoder> Encodable<S> for Inventory { impl<S: WriteExt> Encodable<S> for Inventory {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
match self.inv_type { match self.inv_type {
@ -115,7 +115,7 @@ impl<S: Encoder> Encodable<S> for Inventory {
} }
} }
impl<D: Decoder> Decodable<D> for Inventory { impl<D: ReadExt> Decodable<D> for Inventory {
#[inline] #[inline]
fn consensus_decode(d: &mut D) -> Result<Inventory, encode::Error> { fn consensus_decode(d: &mut D) -> Result<Inventory, encode::Error> {
let int_type: u32 = Decodable::consensus_decode(d)?; let int_type: u32 = Decodable::consensus_decode(d)?;

View File

@ -62,7 +62,7 @@ use bitcoin_hashes::{sha256d, Hash};
use blockdata::constants::{MAX_BLOCK_WEIGHT, MIN_TRANSACTION_WEIGHT}; use blockdata::constants::{MAX_BLOCK_WEIGHT, MIN_TRANSACTION_WEIGHT};
use consensus::encode::{Encodable, Error}; use consensus::encode::{Encodable, Error};
use consensus::{Decodable, Decoder, Encoder}; use consensus::{Decodable, ReadExt, WriteExt};
use util::hash::BitcoinHash; use util::hash::BitcoinHash;
use util::merkleblock::MerkleBlockError::*; use util::merkleblock::MerkleBlockError::*;
use {Block, BlockHeader}; use {Block, BlockHeader};
@ -353,7 +353,7 @@ impl PartialMerkleTree {
} }
} }
impl<S: Encoder> Encodable<S> for PartialMerkleTree { impl<S: WriteExt> Encodable<S> for PartialMerkleTree {
fn consensus_encode(&self, s: &mut S) -> Result<(), Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), Error> {
self.num_transactions.consensus_encode(s)?; self.num_transactions.consensus_encode(s)?;
self.hashes.consensus_encode(s)?; self.hashes.consensus_encode(s)?;
@ -365,7 +365,7 @@ impl<S: Encoder> Encodable<S> for PartialMerkleTree {
} }
} }
impl<D: Decoder> Decodable<D> for PartialMerkleTree { impl<D: ReadExt> Decodable<D> for PartialMerkleTree {
fn consensus_decode(d: &mut D) -> Result<Self, Error> { fn consensus_decode(d: &mut D) -> Result<Self, Error> {
let num_transactions: u32 = Decodable::consensus_decode(d)?; let num_transactions: u32 = Decodable::consensus_decode(d)?;
let hashes: Vec<sha256d::Hash> = Decodable::consensus_decode(d)?; let hashes: Vec<sha256d::Hash> = Decodable::consensus_decode(d)?;
@ -471,14 +471,14 @@ impl MerkleBlock {
} }
} }
impl<S: Encoder> Encodable<S> for MerkleBlock { impl<S: WriteExt> Encodable<S> for MerkleBlock {
fn consensus_encode(&self, s: &mut S) -> Result<(), Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), Error> {
self.header.consensus_encode(s)?; self.header.consensus_encode(s)?;
self.txn.consensus_encode(s) self.txn.consensus_encode(s)
} }
} }
impl<D: Decoder> Decodable<D> for MerkleBlock { impl<D: ReadExt> Decodable<D> for MerkleBlock {
fn consensus_decode(d: &mut D) -> Result<Self, Error> { fn consensus_decode(d: &mut D) -> Result<Self, Error> {
Ok(MerkleBlock { Ok(MerkleBlock {
header: Decodable::consensus_decode(d)?, header: Decodable::consensus_decode(d)?,

View File

@ -14,7 +14,7 @@
#[allow(unused_macros)] #[allow(unused_macros)]
macro_rules! hex_psbt { macro_rules! hex_psbt {
($s:expr) => { ::consensus::encode::deserialize(&::hex::decode($s).unwrap()) }; ($s:expr) => { ::consensus::deserialize(&::hex::decode($s).unwrap()) };
} }
macro_rules! merge { macro_rules! merge {
@ -36,7 +36,7 @@ macro_rules! impl_psbt_deserialize {
($thing:ty) => { ($thing:ty) => {
impl ::util::psbt::serialize::Deserialize for $thing { impl ::util::psbt::serialize::Deserialize for $thing {
fn deserialize(bytes: &[u8]) -> Result<Self, ::consensus::encode::Error> { fn deserialize(bytes: &[u8]) -> Result<Self, ::consensus::encode::Error> {
::consensus::encode::deserialize(&bytes[..]) ::consensus::deserialize(&bytes[..])
} }
} }
}; };
@ -46,7 +46,7 @@ macro_rules! impl_psbt_serialize {
($thing:ty) => { ($thing:ty) => {
impl ::util::psbt::serialize::Serialize for $thing { impl ::util::psbt::serialize::Serialize for $thing {
fn serialize(&self) -> Vec<u8> { fn serialize(&self) -> Vec<u8> {
::consensus::encode::serialize(self) ::consensus::serialize(self)
} }
} }
}; };
@ -54,13 +54,13 @@ macro_rules! impl_psbt_serialize {
macro_rules! impl_psbtmap_consensus_encoding { macro_rules! impl_psbtmap_consensus_encoding {
($thing:ty) => { ($thing:ty) => {
impl<S: ::consensus::encode::Encoder> ::consensus::encode::Encodable<S> for $thing { impl<S: ::consensus::WriteExt> ::consensus::Encodable<S> for $thing {
fn consensus_encode(&self, s: &mut S) -> Result<(), ::consensus::encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), ::consensus::encode::Error> {
for pair in ::util::psbt::Map::get_pairs(self)? { for pair in ::util::psbt::Map::get_pairs(self)? {
::consensus::encode::Encodable::consensus_encode(&pair, s)? ::consensus::Encodable::consensus_encode(&pair, s)?
} }
::consensus::encode::Encodable::consensus_encode(&0x00_u8, s) ::consensus::Encodable::consensus_encode(&0x00_u8, s)
} }
} }
}; };
@ -68,12 +68,12 @@ macro_rules! impl_psbtmap_consensus_encoding {
macro_rules! impl_psbtmap_consensus_decoding { macro_rules! impl_psbtmap_consensus_decoding {
($thing:ty) => { ($thing:ty) => {
impl<D: ::consensus::encode::Decoder> ::consensus::encode::Decodable<D> for $thing { impl<D: ::consensus::ReadExt> ::consensus::Decodable<D> for $thing {
fn consensus_decode(d: &mut D) -> Result<Self, ::consensus::encode::Error> { fn consensus_decode(d: &mut D) -> Result<Self, ::consensus::encode::Error> {
let mut rv: Self = ::std::default::Default::default(); let mut rv: Self = ::std::default::Default::default();
loop { loop {
match ::consensus::encode::Decodable::consensus_decode(d) { match ::consensus::Decodable::consensus_decode(d) {
Ok(pair) => ::util::psbt::Map::insert_pair(&mut rv, pair)?, Ok(pair) => ::util::psbt::Map::insert_pair(&mut rv, pair)?,
Err(::consensus::encode::Error::Psbt(::util::psbt::Error::NoMorePairs)) => return Ok(rv), Err(::consensus::encode::Error::Psbt(::util::psbt::Error::NoMorePairs)) => return Ok(rv),
Err(e) => return Err(e), Err(e) => return Err(e),

View File

@ -16,7 +16,7 @@ use std::collections::HashMap;
use std::io::Cursor; use std::io::Cursor;
use blockdata::transaction::Transaction; use blockdata::transaction::Transaction;
use consensus::encode::{self, Encodable, Decodable, Decoder}; use consensus::{encode, Encodable, Decodable, ReadExt};
use util::psbt::map::Map; use util::psbt::map::Map;
use util::psbt::raw; use util::psbt::raw;
use util::psbt; use util::psbt;
@ -120,7 +120,7 @@ impl Map for Global {
impl_psbtmap_consensus_encoding!(Global); impl_psbtmap_consensus_encoding!(Global);
impl<D: Decoder> Decodable<D> for Global { impl<D: ReadExt> Decodable<D> for Global {
fn consensus_decode(d: &mut D) -> Result<Self, encode::Error> { fn consensus_decode(d: &mut D) -> Result<Self, encode::Error> {
let mut tx: Option<Transaction> = None; let mut tx: Option<Transaction> = None;

View File

@ -20,7 +20,7 @@
use blockdata::script::Script; use blockdata::script::Script;
use blockdata::transaction::Transaction; use blockdata::transaction::Transaction;
use consensus::encode::{self, Encodable, Decodable, Encoder, Decoder}; use consensus::{encode, Encodable, Decodable, WriteExt, ReadExt};
mod error; mod error;
pub use self::error::Error; pub use self::error::Error;
@ -88,7 +88,7 @@ impl PartiallySignedTransaction {
} }
} }
impl<S: Encoder> Encodable<S> for PartiallySignedTransaction { impl<S: WriteExt> Encodable<S> for PartiallySignedTransaction {
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
b"psbt".consensus_encode(s)?; b"psbt".consensus_encode(s)?;
@ -108,7 +108,7 @@ impl<S: Encoder> Encodable<S> for PartiallySignedTransaction {
} }
} }
impl<D: Decoder> Decodable<D> for PartiallySignedTransaction { impl<D: ReadExt> Decodable<D> for PartiallySignedTransaction {
fn consensus_decode(d: &mut D) -> Result<Self, encode::Error> { fn consensus_decode(d: &mut D) -> Result<Self, encode::Error> {
let magic: [u8; 4] = Decodable::consensus_decode(d)?; let magic: [u8; 4] = Decodable::consensus_decode(d)?;

View File

@ -20,7 +20,7 @@
use std::fmt; use std::fmt;
use consensus::encode::{Decodable, Encodable, VarInt, MAX_VEC_SIZE}; use consensus::encode::{Decodable, Encodable, VarInt, MAX_VEC_SIZE};
use consensus::encode::{self, Decoder, Encoder}; use consensus::{encode, ReadExt, WriteExt};
use util::psbt::Error; use util::psbt::Error;
/// A PSBT key in its raw byte form. /// A PSBT key in its raw byte form.
@ -52,7 +52,7 @@ impl fmt::Display for Key {
} }
} }
impl<D: Decoder> Decodable<D> for Key { impl<D: ReadExt> Decodable<D> for Key {
fn consensus_decode(d: &mut D) -> Result<Self, encode::Error> { fn consensus_decode(d: &mut D) -> Result<Self, encode::Error> {
let VarInt(byte_size): VarInt = Decodable::consensus_decode(d)?; let VarInt(byte_size): VarInt = Decodable::consensus_decode(d)?;
@ -80,7 +80,7 @@ impl<D: Decoder> Decodable<D> for Key {
} }
} }
impl<S: Encoder> Encodable<S> for Key { impl<S: WriteExt> Encodable<S> for Key {
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
VarInt((self.key.len() + 1) as u64).consensus_encode(s)?; VarInt((self.key.len() + 1) as u64).consensus_encode(s)?;
@ -94,14 +94,14 @@ impl<S: Encoder> Encodable<S> for Key {
} }
} }
impl<S: Encoder> Encodable<S> for Pair { impl<S: WriteExt> Encodable<S> for Pair {
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
self.key.consensus_encode(s)?; self.key.consensus_encode(s)?;
self.value.consensus_encode(s) self.value.consensus_encode(s)
} }
} }
impl<D: Decoder> Decodable<D> for Pair { impl<D: ReadExt> Decodable<D> for Pair {
fn consensus_decode(d: &mut D) -> Result<Self, encode::Error> { fn consensus_decode(d: &mut D) -> Result<Self, encode::Error> {
Ok(Pair { Ok(Pair {
key: Decodable::consensus_decode(d)?, key: Decodable::consensus_decode(d)?,

View File

@ -336,7 +336,7 @@ macro_rules! construct_uint {
} }
} }
impl<S: ::consensus::encode::Encoder> ::consensus::encode::Encodable<S> for $name { impl<S: ::consensus::WriteExt> ::consensus::Encodable<S> for $name {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), encode::Error> {
let &$name(ref data) = self; let &$name(ref data) = self;
@ -345,9 +345,9 @@ macro_rules! construct_uint {
} }
} }
impl<D: ::consensus::encode::Decoder> ::consensus::encode::Decodable<D> for $name { impl<D: ::consensus::ReadExt> ::consensus::Decodable<D> for $name {
fn consensus_decode(d: &mut D) -> Result<$name, encode::Error> { fn consensus_decode(d: &mut D) -> Result<$name, encode::Error> {
use consensus::encode::Decodable; use consensus::Decodable;
let mut ret: [u64; $n_words] = [0; $n_words]; let mut ret: [u64; $n_words] = [0; $n_words];
for i in 0..$n_words { for i in 0..$n_words {
ret[i] = Decodable::consensus_decode(d)?; ret[i] = Decodable::consensus_decode(d)?;
@ -388,7 +388,7 @@ impl Uint256 {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use consensus::encode::{deserialize, serialize}; use consensus::{deserialize, serialize};
use util::uint::Uint256; use util::uint::Uint256;
use util::BitArray; use util::BitArray;