X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fser.rs;h=1d24e26408fba830603cb3f95b9b4bbd20354d57;hb=ad09a2f00413b50481975a08aa1315f8ca57291b;hp=0f05dfe1afa81245724cb54fccb9cda8d5a377bc;hpb=4909d3cd6a05dfcba39fe08e6d8f539944c8ef66;p=rust-lightning diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 0f05dfe1..1d24e264 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -1,20 +1,29 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + //! A very simple serialization framework which is used to serialize/deserialize messages as well //! as ChannelsManagers and ChannelMonitors. -use std::result::Result; use std::io::{Read, Write}; use std::collections::HashMap; use std::hash::Hash; use std::sync::Mutex; use std::cmp; -use secp256k1::Signature; -use secp256k1::key::{PublicKey, SecretKey}; +use bitcoin::secp256k1::Signature; +use bitcoin::secp256k1::key::{PublicKey, SecretKey}; use bitcoin::blockdata::script::Script; use bitcoin::blockdata::transaction::{OutPoint, Transaction, TxOut}; use bitcoin::consensus; use bitcoin::consensus::Encodable; use bitcoin::hashes::sha256d::Hash as Sha256dHash; +use bitcoin::hash_types::{Txid, BlockHash}; use std::marker::Sized; use ln::msgs::DecodeError; use ln::channelmanager::{PaymentPreimage, PaymentHash, PaymentSecret}; @@ -22,12 +31,15 @@ use util::byte_utils; use util::byte_utils::{be64_to_array, be48_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be48, slice_to_be64}; -const MAX_BUF_SIZE: usize = 64 * 1024; +/// serialization buffer size +pub const MAX_BUF_SIZE: usize = 64 * 1024; /// A trait that is similar to std::io::Write but has one extra function which can be used to size /// buffers being written into. /// An impl is provided for any type that also impls std::io::Write which simply ignores size /// hints. +/// +/// (C-not exported) as we only export serialization to/from byte arrays instead pub trait Writer { /// Writes the given buf out. See std::io::Write::write_all for more fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error>; @@ -150,6 +162,8 @@ impl Read for ReadTrackingReader { } /// A trait that various rust-lightning types implement allowing them to be written out to a Writer +/// +/// (C-not exported) as we only export serialization to/from byte arrays instead pub trait Writeable { /// Writes self out to the given Writer fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error>; @@ -177,6 +191,8 @@ impl<'a, T: Writeable> Writeable for &'a T { } /// A trait that various rust-lightning types implement allowing them to be read in from a Read +/// +/// (C-not exported) as we only export serialization to/from byte arrays instead pub trait Readable where Self: Sized { @@ -186,6 +202,8 @@ pub trait Readable /// A trait that various higher-level rust-lightning types implement allowing them to be read in /// from a Read given some additional set of arguments which is required to deserialize. +/// +/// (C-not exported) as we only export serialization to/from byte arrays instead pub trait ReadableArgs

where Self: Sized { @@ -194,6 +212,8 @@ pub trait ReadableArgs

} /// A trait that various rust-lightning types implement allowing them to (maybe) be read in from a Read +/// +/// (C-not exported) as we only export serialization to/from byte arrays instead pub trait MaybeReadable where Self: Sized { @@ -632,6 +652,36 @@ impl Readable for Option } } +impl Writeable for Txid { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + w.write_all(&self[..]) + } +} + +impl Readable for Txid { + fn read(r: &mut R) -> Result { + use bitcoin::hashes::Hash; + + let buf: [u8; 32] = Readable::read(r)?; + Ok(Txid::from_slice(&buf[..]).unwrap()) + } +} + +impl Writeable for BlockHash { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + w.write_all(&self[..]) + } +} + +impl Readable for BlockHash { + fn read(r: &mut R) -> Result { + use bitcoin::hashes::Hash; + + let buf: [u8; 32] = Readable::read(r)?; + Ok(BlockHash::from_slice(&buf[..]).unwrap()) + } +} + impl Writeable for OutPoint { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { self.txid.write(w)?; @@ -668,7 +718,7 @@ macro_rules! impl_consensus_ser { match consensus::encode::Decodable::consensus_decode(r) { Ok(t) => Ok(t), Err(consensus::encode::Error::Io(ref e)) if e.kind() == ::std::io::ErrorKind::UnexpectedEof => Err(DecodeError::ShortRead), - Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e)), + Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e.kind())), Err(_) => Err(DecodeError::InvalidValue), } }