diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fea2bd8f..ea74fb99 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -43,6 +43,25 @@ jobs: env: ${{ matrix.env }} run: ./contrib/test.sh + Cross: + name: Cross testing + runs-on: ubuntu-latest + steps: + - name: Checkout Crate + uses: actions/checkout@v2 + - name: Checkout Toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - name: Install target + run: rustup target add s390x-unknown-linux-gnu + - name: install cross + run: cargo install cross + - name: run cross test + run: cross test --target s390x-unknown-linux-gnu + Docs: name: Docs runs-on: ubuntu-latest diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index 7fae244a..a2f5bcf5 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -338,7 +338,7 @@ macro_rules! impl_int_encodable{ impl Decodable for $ty { #[inline] fn consensus_decode(mut d: D) -> Result { - ReadExt::$meth_dec(&mut d).map($ty::from_le) + ReadExt::$meth_dec(&mut d) } } impl Encodable for $ty { @@ -347,7 +347,7 @@ macro_rules! impl_int_encodable{ &self, mut s: S, ) -> Result { - s.$meth_enc(self.to_le())?; + s.$meth_enc(*self)?; Ok(mem::size_of::<$ty>()) } } diff --git a/src/network/address.rs b/src/network/address.rs index 21874a50..5c260049 100644 --- a/src/network/address.rs +++ b/src/network/address.rs @@ -70,8 +70,13 @@ impl Address { } fn addr_to_be(addr: [u16; 8]) -> [u16; 8] { - [addr[0].to_be(), addr[1].to_be(), addr[2].to_be(), addr[3].to_be(), - addr[4].to_be(), addr[5].to_be(), addr[6].to_be(), addr[7].to_be()] + // consensus_encode always encodes in LE, and we want to encode in BE. + // this utility fn swap bytes before encoding so that the encoded result will be BE + let mut result = addr.clone(); + for i in 0..8 { + result[i] = result[i].swap_bytes(); + } + result } impl Encodable for Address { @@ -82,7 +87,10 @@ impl Encodable for Address { ) -> Result { let len = self.services.consensus_encode(&mut s)? + addr_to_be(self.address).consensus_encode(&mut s)? - + self.port.to_be().consensus_encode(s)?; + + // consensus_encode always encodes in LE, and we want to encode in BE. + //TODO `len += io::Write::write(&mut e, &self.port.to_be_bytes())?;` when MSRV >= 1.32 + + self.port.swap_bytes().consensus_encode(s)?; Ok(len) } } @@ -93,7 +101,7 @@ impl Decodable for Address { Ok(Address { services: Decodable::consensus_decode(&mut d)?, address: addr_to_be(Decodable::consensus_decode(&mut d)?), - port: u16::from_be(Decodable::consensus_decode(d)?) + port: u16::swap_bytes(Decodable::consensus_decode(d)?) }) } } @@ -268,7 +276,10 @@ impl Encodable for AddrV2Message { len += self.time.consensus_encode(&mut e)?; len += VarInt(self.services.as_u64()).consensus_encode(&mut e)?; len += self.addr.consensus_encode(&mut e)?; - len += self.port.to_be().consensus_encode(e)?; + + // consensus_encode always encodes in LE, and we want to encode in BE. + //TODO `len += io::Write::write(&mut e, &self.port.to_be_bytes())?;` when MSRV >= 1.32 + len += self.port.swap_bytes().consensus_encode(e)?; Ok(len) } } @@ -279,7 +290,7 @@ impl Decodable for AddrV2Message { time: Decodable::consensus_decode(&mut d)?, services: ServiceFlags::from(VarInt::consensus_decode(&mut d)?.0), addr: Decodable::consensus_decode(&mut d)?, - port: u16::from_be(Decodable::consensus_decode(d)?), + port: u16::swap_bytes(Decodable::consensus_decode(d)?), }) } } diff --git a/src/util/uint.rs b/src/util/uint.rs index 67645f85..a96f431b 100644 --- a/src/util/uint.rs +++ b/src/util/uint.rs @@ -176,7 +176,7 @@ macro_rules! construct_uint { fn cmp(&self, other: &$name) -> ::core::cmp::Ordering { // We need to manually implement ordering because we use little-endian // and the auto derive is a lexicographic ordering(i.e. memcmp) - // which with numbers is equivilant to big-endian + // which with numbers is equivalent to big-endian for i in 0..$n_words { if self[$n_words - 1 - i] < other[$n_words - 1 - i] { return ::core::cmp::Ordering::Less; } if self[$n_words - 1 - i] > other[$n_words - 1 - i] { return ::core::cmp::Ordering::Greater; }