Merge pull request #1028 from lightning-signer/2021-08-no-std
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Tue, 3 Aug 2021 17:06:59 +0000 (17:06 +0000)
committerGitHub <noreply@github.com>
Tue, 3 Aug 2021 17:06:59 +0000 (17:06 +0000)
Actual no_std support

1  2 
.github/workflows/build.yml
lightning/src/util/ser_macros.rs

index 738a54f91a50b8159160b8ac09bc16e79fa5c5c3,f18f3f64d78eda3c96ec61b56f42ea506dc463d2..34337769a26677969bc8a3246c4cbb36f35648f7
@@@ -16,7 -16,7 +16,7 @@@ jobs
                       1.41.0,
                       # 1.45.2 is MSRV for lightning-net-tokio, lightning-block-sync, and coverage generation
                       1.45.2,
-                      # 1.47.0 will be the MSRV for no_std builds using hashbrown once core2 is updated
+                      # 1.47.0 will be the MSRV for no-std builds using hashbrown once core2 is updated
                       1.47.0]
          include:
            - toolchain: stable
              platform: macos-latest
              build-net-tokio: true
              build-no-std: true
 +          - toolchain: beta
 +            platform: macos-latest
 +            build-net-tokio: true
 +            build-no-std: true
            - toolchain: stable
              platform: windows-latest
              build-net-tokio: true
              build-no-std: true
 +          - toolchain: beta
 +            platform: windows-latest
 +            build-net-tokio: true
 +            build-no-std: true
            - toolchain: beta
              build-net-tokio: true
              build-no-std: true
        - name: Test on Rust ${{ matrix.toolchain }} with net-tokio and full code-linking for coverage generation
          if: matrix.coverage
          run: RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always
-       - name: Test on no_std bullds Rust ${{ matrix.toolchain }}
+       - name: Test on no-std bullds Rust ${{ matrix.toolchain }}
          if: "matrix.build-no-std && !matrix.coverage"
          run: |
            cd lightning
-           cargo test --verbose --color always --no-default-features --features no_std
-           # check if there is a conflict between no_std and the default std feature
-           cargo test --verbose --color always --features no_std
+           cargo test --verbose --color always --no-default-features --features no-std
+           # check if there is a conflict between no-std and the default std feature
+           cargo test --verbose --color always --features no-std
            cd ..
-       - name: Test on no_std bullds Rust ${{ matrix.toolchain }} and full code-linking for coverage generation
+       - name: Test on no-std builds Rust ${{ matrix.toolchain }} and full code-linking for coverage generation
          if: "matrix.build-no-std && matrix.coverage"
          run: |
            cd lightning
-           RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --no-default-features --features no_std
+           RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --no-default-features --features no-std
            cd ..
        - name: Test on Rust ${{ matrix.toolchain }}
          if: "! matrix.build-net-tokio"
            rustup component add clippy
        - name: Run default clippy linting
          run: |
 -          cargo clippy -- -Aclippy::erasing_op -Aclippy::never_loop -Aclippy::if_same_then_else
 +          cargo clippy -- -Aclippy::erasing_op -Aclippy::never_loop -Aclippy::if_same_then_else -Dclippy::try_err
index b65b8d2c0b999000198caa73868803d84bc5a406,cffa0838ddceee3c1cc08be7c4d147e889760b5a..5d5171adbb4401c1dd1fb5faf3b1f8afab919db2
@@@ -93,7 -93,7 +93,7 @@@ macro_rules! check_tlv_order 
                #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true
                let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type;
                if invalid_order {
 -                      Err(DecodeError::InvalidValue)?
 +                      return Err(DecodeError::InvalidValue);
                }
        }};
        ($last_seen_type: expr, $typ: expr, $type: expr, option) => {{
@@@ -109,7 -109,7 +109,7 @@@ macro_rules! check_missing_tlv 
                #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true
                let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type;
                if missing_req_type {
 -                      Err(DecodeError::InvalidValue)?
 +                      return Err(DecodeError::InvalidValue);
                }
        }};
        ($last_seen_type: expr, $type: expr, vec_type) => {{
@@@ -149,12 -149,12 +149,12 @@@ macro_rules! decode_tlv_stream 
                                match ser::Readable::read(&mut tracking_reader) {
                                        Err(DecodeError::ShortRead) => {
                                                if !tracking_reader.have_read {
 -                                                      break 'tlv_read
 +                                                      break 'tlv_read;
                                                } else {
 -                                                      Err(DecodeError::ShortRead)?
 +                                                      return Err(DecodeError::ShortRead);
                                                }
                                        },
 -                                      Err(e) => Err(e)?,
 +                                      Err(e) => return Err(e),
                                        Ok(t) => t,
                                }
                        };
                        // Types must be unique and monotonically increasing:
                        match last_seen_type {
                                Some(t) if typ.0 <= t => {
 -                                      Err(DecodeError::InvalidValue)?
 +                                      return Err(DecodeError::InvalidValue);
                                },
                                _ => {},
                        }
                                        decode_tlv!(s, $field, $fieldty);
                                        if s.bytes_remain() {
                                                s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes
 -                                              Err(DecodeError::InvalidValue)?
 +                                              return Err(DecodeError::InvalidValue);
                                        }
                                },)*
                                x if x % 2 == 0 => {
 -                                      Err(DecodeError::UnknownRequiredFeature)?
 +                                      return Err(DecodeError::UnknownRequiredFeature);
                                },
                                _ => {},
                        }
  macro_rules! impl_writeable {
        ($st:ident, $len: expr, {$($field:ident),*}) => {
                impl ::util::ser::Writeable for $st {
-                       fn write<W: ::util::ser::Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+                       fn write<W: ::util::ser::Writer>(&self, w: &mut W) -> Result<(), $crate::io::Error> {
                                if $len != 0 {
                                        w.size_hint($len);
                                }
                }
  
                impl ::util::ser::Readable for $st {
-                       fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
+                       fn read<R: $crate::io::Read>(r: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
                                Ok(Self {
                                        $($field: ::util::ser::Readable::read(r)?),*
                                })
  macro_rules! impl_writeable_len_match {
        ($struct: ident, $cmp: tt, ($calc_len: expr), {$({$match: pat, $length: expr}),*}, {$($field:ident),*}) => {
                impl Writeable for $struct {
-                       fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
+                       fn write<W: Writer>(&self, w: &mut W) -> Result<(), $crate::io::Error> {
                                let len = match *self {
                                        $($match => $length,)*
                                };
                }
  
                impl ::util::ser::Readable for $struct {
-                       fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
+                       fn read<R: $crate::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
                                Ok(Self {
                                        $($field: Readable::read(r)?),*
                                })
@@@ -387,7 -387,7 +387,7 @@@ macro_rules! init_tlv_field_var 
  macro_rules! impl_writeable_tlv_based {
        ($st: ident, {$(($type: expr, $field: ident, $fieldty: ident)),* $(,)*}) => {
                impl ::util::ser::Writeable for $st {
-                       fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+                       fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
                                write_tlv_fields!(writer, {
                                        $(($type, self.$field, $fieldty)),*
                                });
                }
  
                impl ::util::ser::Readable for $st {
-                       fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
+                       fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
                                $(
                                        init_tlv_field_var!($field, $fieldty);
                                )*
@@@ -445,7 -445,7 +445,7 @@@ macro_rules! impl_writeable_tlv_based_e
        ),* $(,)*;
        $(($tuple_variant_id: expr, $tuple_variant_name: ident)),*  $(,)*) => {
                impl ::util::ser::Writeable for $st {
-                       fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+                       fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
                                match self {
                                        $($st::$variant_name { $(ref $field),* } => {
                                                let id: u8 = $variant_id;
                }
  
                impl ::util::ser::Readable for $st {
-                       fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
+                       fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
                                let id: u8 = ::util::ser::Readable::read(reader)?;
                                match id {
                                        $($variant_id => {
                                                Ok($st::$tuple_variant_name(Readable::read(reader)?))
                                        }),*
                                        _ => {
 -                                              Err(DecodeError::UnknownRequiredFeature)?
 +                                              Err(DecodeError::UnknownRequiredFeature)
                                        },
                                }
                        }
  
  #[cfg(test)]
  mod tests {
+       use io::{self, Cursor};
        use prelude::*;
-       use std::io::Cursor;
        use ln::msgs::DecodeError;
        use util::ser::{Writeable, HighZeroBytesDroppedVarInt, VecWriter};
        use bitcoin::secp256k1::PublicKey;
                do_test!(concat!("fd00fe", "02", "0226"), None, None, None, Some(550));
        }
  
-       fn do_simple_test_tlv_write() -> Result<(), ::std::io::Error> {
+       fn do_simple_test_tlv_write() -> Result<(), io::Error> {
                let mut stream = VecWriter(Vec::new());
  
                stream.0.clear();