+impl Writeable for Event {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+ match self {
+ &Event::FundingGenerationReady { .. } => {
+ 0u8.write(writer)?;
+ // We never write out FundingGenerationReady events as, upon disconnection, peers
+ // drop any channels which have not yet exchanged funding_signed.
+ },
+ &Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, ref amt, ref user_payment_id } => {
+ 1u8.write(writer)?;
+ payment_hash.write(writer)?;
+ payment_preimage.write(writer)?;
+ payment_secret.write(writer)?;
+ amt.write(writer)?;
+ user_payment_id.write(writer)?;
+ },
+ &Event::PaymentSent { ref payment_preimage } => {
+ 2u8.write(writer)?;
+ payment_preimage.write(writer)?;
+ },
+ &Event::PaymentFailed { ref payment_hash, ref rejected_by_dest,
+ #[cfg(test)]
+ ref error_code,
+ #[cfg(test)]
+ ref error_data,
+ } => {
+ 3u8.write(writer)?;
+ payment_hash.write(writer)?;
+ rejected_by_dest.write(writer)?;
+ #[cfg(test)]
+ error_code.write(writer)?;
+ #[cfg(test)]
+ error_data.write(writer)?;
+ },
+ &Event::PendingHTLCsForwardable { time_forwardable: _ } => {
+ 4u8.write(writer)?;
+ // We don't write the time_fordwardable out at all, as we presume when the user
+ // deserializes us at least that much time has elapsed.
+ },
+ &Event::SpendableOutputs { ref outputs } => {
+ 5u8.write(writer)?;
+ (outputs.len() as u64).write(writer)?;
+ for output in outputs.iter() {
+ output.write(writer)?;
+ }
+ },
+ }
+ Ok(())
+ }
+}
+impl MaybeReadable for Event {
+ fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Option<Self>, msgs::DecodeError> {
+ match Readable::read(reader)? {
+ 0u8 => Ok(None),
+ 1u8 => Ok(Some(Event::PaymentReceived {
+ payment_hash: Readable::read(reader)?,
+ payment_preimage: Readable::read(reader)?,
+ payment_secret: Readable::read(reader)?,
+ amt: Readable::read(reader)?,
+ user_payment_id: Readable::read(reader)?,
+ })),
+ 2u8 => Ok(Some(Event::PaymentSent {
+ payment_preimage: Readable::read(reader)?,
+ })),
+ 3u8 => Ok(Some(Event::PaymentFailed {
+ payment_hash: Readable::read(reader)?,
+ rejected_by_dest: Readable::read(reader)?,
+ #[cfg(test)]
+ error_code: Readable::read(reader)?,
+ #[cfg(test)]
+ error_data: Readable::read(reader)?,
+ })),
+ 4u8 => Ok(Some(Event::PendingHTLCsForwardable {
+ time_forwardable: Duration::from_secs(0)
+ })),
+ 5u8 => {
+ let outputs_len: u64 = Readable::read(reader)?;
+ let mut outputs = Vec::new();
+ for _ in 0..outputs_len {
+ outputs.push(Readable::read(reader)?);
+ }
+ Ok(Some(Event::SpendableOutputs { outputs }))
+ },
+ _ => Err(msgs::DecodeError::InvalidValue)
+ }
+ }
+}
+