Merge pull request #627 from RCasatta/bigendian

Bigendian fixes and CI test
This commit is contained in:
Andrew Poelstra 2021-07-20 20:56:16 +00:00 committed by GitHub
commit df4d70a37e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 9 deletions

View File

@ -43,6 +43,25 @@ jobs:
env: ${{ matrix.env }} env: ${{ matrix.env }}
run: ./contrib/test.sh 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: Docs:
name: Docs name: Docs
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@ -338,7 +338,7 @@ macro_rules! impl_int_encodable{
impl Decodable for $ty { impl Decodable for $ty {
#[inline] #[inline]
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> { fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
ReadExt::$meth_dec(&mut d).map($ty::from_le) ReadExt::$meth_dec(&mut d)
} }
} }
impl Encodable for $ty { impl Encodable for $ty {
@ -347,7 +347,7 @@ macro_rules! impl_int_encodable{
&self, &self,
mut s: S, mut s: S,
) -> Result<usize, io::Error> { ) -> Result<usize, io::Error> {
s.$meth_enc(self.to_le())?; s.$meth_enc(*self)?;
Ok(mem::size_of::<$ty>()) Ok(mem::size_of::<$ty>())
} }
} }

View File

@ -70,8 +70,13 @@ impl Address {
} }
fn addr_to_be(addr: [u16; 8]) -> [u16; 8] { 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(), // consensus_encode always encodes in LE, and we want to encode in BE.
addr[4].to_be(), addr[5].to_be(), addr[6].to_be(), addr[7].to_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 { impl Encodable for Address {
@ -82,7 +87,10 @@ impl Encodable for Address {
) -> Result<usize, io::Error> { ) -> Result<usize, io::Error> {
let len = self.services.consensus_encode(&mut s)? let len = self.services.consensus_encode(&mut s)?
+ addr_to_be(self.address).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) Ok(len)
} }
} }
@ -93,7 +101,7 @@ impl Decodable for Address {
Ok(Address { Ok(Address {
services: Decodable::consensus_decode(&mut d)?, services: Decodable::consensus_decode(&mut d)?,
address: addr_to_be(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 += self.time.consensus_encode(&mut e)?;
len += VarInt(self.services.as_u64()).consensus_encode(&mut e)?; len += VarInt(self.services.as_u64()).consensus_encode(&mut e)?;
len += self.addr.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) Ok(len)
} }
} }
@ -279,7 +290,7 @@ impl Decodable for AddrV2Message {
time: Decodable::consensus_decode(&mut d)?, time: Decodable::consensus_decode(&mut d)?,
services: ServiceFlags::from(VarInt::consensus_decode(&mut d)?.0), services: ServiceFlags::from(VarInt::consensus_decode(&mut d)?.0),
addr: Decodable::consensus_decode(&mut d)?, addr: Decodable::consensus_decode(&mut d)?,
port: u16::from_be(Decodable::consensus_decode(d)?), port: u16::swap_bytes(Decodable::consensus_decode(d)?),
}) })
} }
} }

View File

@ -176,7 +176,7 @@ macro_rules! construct_uint {
fn cmp(&self, other: &$name) -> ::core::cmp::Ordering { fn cmp(&self, other: &$name) -> ::core::cmp::Ordering {
// We need to manually implement ordering because we use little-endian // We need to manually implement ordering because we use little-endian
// and the auto derive is a lexicographic ordering(i.e. memcmp) // 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 { 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::Less; }
if self[$n_words - 1 - i] > other[$n_words - 1 - i] { return ::core::cmp::Ordering::Greater; } if self[$n_words - 1 - i] > other[$n_words - 1 - i] { return ::core::cmp::Ordering::Greater; }