From 789cd4d9c3acef2cb008b0e7c82df38bb20a69cb Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Fri, 1 Nov 2024 20:41:31 +0000 Subject: [PATCH 1/6] rpc/types 32 bit support --- Cargo.lock | 1 - net/epee-encoding/Cargo.toml | 1 - net/epee-encoding/src/lib.rs | 12 +++++------- net/epee-encoding/src/value.rs | 19 +++++-------------- net/epee-encoding/src/varint.rs | 14 ++++++++++---- rpc/types/Cargo.toml | 6 +++--- 6 files changed, 23 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0f851dcd2..24dda329b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -740,7 +740,6 @@ version = "0.5.0" dependencies = [ "bytes", "cuprate-fixed-bytes", - "cuprate-helper", "hex", "paste", "ref-cast", diff --git a/net/epee-encoding/Cargo.toml b/net/epee-encoding/Cargo.toml index 4724e2d0a..0270e7ef1 100644 --- a/net/epee-encoding/Cargo.toml +++ b/net/epee-encoding/Cargo.toml @@ -15,7 +15,6 @@ default = ["std"] std = ["dep:thiserror", "bytes/std", "cuprate-fixed-bytes/std"] [dependencies] -cuprate-helper = { workspace = true, default-features = false, features = ["cast"] } cuprate-fixed-bytes = { workspace = true, default-features = false } paste = "1.0.15" diff --git a/net/epee-encoding/src/lib.rs b/net/epee-encoding/src/lib.rs index a6ff1b04f..a814cac1d 100644 --- a/net/epee-encoding/src/lib.rs +++ b/net/epee-encoding/src/lib.rs @@ -69,8 +69,6 @@ use core::str::from_utf8 as str_from_utf8; use bytes::{Buf, BufMut, Bytes, BytesMut}; -use cuprate_helper::cast::{u64_to_usize, usize_to_u64}; - pub mod container_as_blob; pub mod error; mod io; @@ -89,7 +87,7 @@ pub use varint::{read_varint, write_varint}; /// this binary serialization format. const HEADER: &[u8] = b"\x01\x11\x01\x01\x01\x01\x02\x01\x01"; /// The maximum length a byte array (marked as a string) can be. -const MAX_STRING_LEN_POSSIBLE: u64 = 2000000000; +const MAX_STRING_LEN_POSSIBLE: usize = 2000000000; /// The maximum depth of skipped objects. const MAX_DEPTH_OF_SKIPPED_OBJECTS: u8 = 20; /// The maximum number of fields in an object. @@ -248,7 +246,7 @@ pub fn write_bytes, B: BufMut>(t: T, w: &mut B) -> Result<()> { let bytes = t.as_ref(); let len = bytes.len(); - write_varint(usize_to_u64(len), w)?; + write_varint(len, w)?; if w.remaining_mut() < len { return Err(Error::IO("Not enough capacity to write bytes")); @@ -292,7 +290,7 @@ where I: Iterator + ExactSizeIterator, B: BufMut, { - write_varint(usize_to_u64(iterator.len()), w)?; + write_varint(iterator.len(), w)?; for item in iterator { item.write(w)?; } @@ -337,7 +335,7 @@ fn skip_epee_value(r: &mut B, skipped_objects: &mut u8) -> Result<()> { if let Some(size) = marker.inner_marker.size() { let bytes_to_skip = size - .checked_mul(u64_to_usize(len)) + .checked_mul(len.try_into()?) .ok_or(Error::Value("List is too big".to_string()))?; return advance(bytes_to_skip, r); }; @@ -355,7 +353,7 @@ fn skip_epee_value(r: &mut B, skipped_objects: &mut u8) -> Result<()> { | InnerMarker::U8 | InnerMarker::Bool => unreachable!("These types are constant size."), InnerMarker::String => { - let len = u64_to_usize(read_varint(r)?); + let len = read_varint(r)?; advance(len, r)?; } InnerMarker::Object => { diff --git a/net/epee-encoding/src/value.rs b/net/epee-encoding/src/value.rs index 4762c96a2..c02e6da70 100644 --- a/net/epee-encoding/src/value.rs +++ b/net/epee-encoding/src/value.rs @@ -7,7 +7,6 @@ use core::fmt::Debug; use bytes::{Buf, BufMut, Bytes, BytesMut}; use cuprate_fixed_bytes::{ByteArray, ByteArrayVec}; -use cuprate_helper::cast::u64_to_usize; use crate::{ io::{checked_read_primitive, checked_write_primitive}, @@ -67,7 +66,7 @@ impl EpeeValue for Vec { "Marker is not sequence when a sequence was expected", )); } - let len = u64_to_usize(read_varint(r)?); + let len = read_varint(r)?; let individual_marker = Marker::new(marker.inner_marker); @@ -168,8 +167,6 @@ impl EpeeValue for Vec { return Err(Error::Format("Byte array exceeded max length")); } - let len = u64_to_usize(len); - if r.remaining() < len { return Err(Error::IO("Not enough bytes to fill object")); } @@ -206,8 +203,6 @@ impl EpeeValue for Bytes { return Err(Error::Format("Byte array exceeded max length")); } - let len = u64_to_usize(len); - if r.remaining() < len { return Err(Error::IO("Not enough bytes to fill object")); } @@ -241,8 +236,6 @@ impl EpeeValue for BytesMut { return Err(Error::Format("Byte array exceeded max length")); } - let len = u64_to_usize(len); - if r.remaining() < len { return Err(Error::IO("Not enough bytes to fill object")); } @@ -274,13 +267,11 @@ impl EpeeValue for ByteArrayVec { return Err(Error::Format("Marker does not match expected Marker")); } - let len = read_varint(r)?; + let len = read_varint::<_, usize>(r)?; if len > MAX_STRING_LEN_POSSIBLE { return Err(Error::Format("Byte array exceeded max length")); } - let len = u64_to_usize(len); - if r.remaining() < len { return Err(Error::IO("Not enough bytes to fill object")); } @@ -310,7 +301,7 @@ impl EpeeValue for ByteArray { return Err(Error::Format("Marker does not match expected Marker")); } - let len = u64_to_usize(read_varint(r)?); + let len = read_varint::<_, usize>(r)?; if len != N { return Err(Error::Format("Byte array has incorrect length")); } @@ -377,7 +368,7 @@ impl EpeeValue for Vec<[u8; N]> { )); } - let len = u64_to_usize(read_varint(r)?); + let len = read_varint(r)?; let individual_marker = Marker::new(marker.inner_marker); @@ -413,7 +404,7 @@ macro_rules! epee_seq { )); } - let len = u64_to_usize(read_varint(r)?); + let len = read_varint(r)?; let individual_marker = Marker::new(marker.inner_marker.clone()); diff --git a/net/epee-encoding/src/varint.rs b/net/epee-encoding/src/varint.rs index 3f191dc7a..e5c564a4c 100644 --- a/net/epee-encoding/src/varint.rs +++ b/net/epee-encoding/src/varint.rs @@ -19,7 +19,7 @@ const FITS_IN_FOUR_BYTES: u64 = 2_u64.pow(32 - SIZE_OF_SIZE_MARKER) - 1; /// assert_eq!(read_varint(&mut [254, 255, 255, 255].as_slice()).unwrap(), 1_073_741_823); /// assert_eq!(read_varint(&mut [3, 0, 0, 0, 1, 0, 0, 0].as_slice()).unwrap(), 1_073_741_824); /// ``` -pub fn read_varint(r: &mut B) -> Result { +pub fn read_varint>(r: &mut B) -> Result { if !r.has_remaining() { return Err(Error::IO("Not enough bytes to build VarInt")); } @@ -35,7 +35,8 @@ pub fn read_varint(r: &mut B) -> Result { for i in 1..len { vi |= u64::from(r.get_u8()) << (((i - 1) * 8) + 6); } - Ok(vi) + + vi.try_into().map_err(|_| Error::IO("VarInt is too big")) } /// Write an epee variable sized number into `w`. @@ -58,7 +59,12 @@ pub fn read_varint(r: &mut B) -> Result { /// assert_eq!(buf.as_slice(), expected_bytes); /// } /// ``` -pub fn write_varint(number: u64, w: &mut B) -> Result<()> { +pub fn write_varint>(number: T, w: &mut B) -> Result<()> { + let number = number + .try_into() + .map_err(|_| "Tried to write a varint bigger than 64-bits") + .unwrap(); + let size_marker = match number { 0..=FITS_IN_ONE_BYTE => 0, 64..=FITS_IN_TWO_BYTES => 1, @@ -101,7 +107,7 @@ mod tests { } fn assert_varint_val(mut varint: &[u8], val: u64) { - assert_eq!(read_varint(&mut varint).unwrap(), val); + assert_eq!(read_varint::<_, u64>(&mut varint).unwrap(), val); } #[test] diff --git a/rpc/types/Cargo.toml b/rpc/types/Cargo.toml index e9ca5296f..56c5c1553 100644 --- a/rpc/types/Cargo.toml +++ b/rpc/types/Cargo.toml @@ -10,13 +10,13 @@ keywords = ["cuprate", "rpc", "types", "monero"] [features] default = ["serde", "epee"] -serde = ["dep:serde", "cuprate-fixed-bytes/serde"] -epee = ["dep:cuprate-epee-encoding"] +serde = ["dep:serde", "cuprate-fixed-bytes/serde", "cuprate-types/serde"] +epee = ["dep:cuprate-epee-encoding", "cuprate-types/epee"] [dependencies] cuprate-epee-encoding = { workspace = true, optional = true } cuprate-fixed-bytes = { workspace = true } -cuprate-types = { workspace = true, default-features = false, features = ["epee", "serde"] } +cuprate-types = { workspace = true, default-features = false } paste = { workspace = true } serde = { workspace = true, optional = true } From 2a91373691dfc1fde601f1eff91f9fdf32b13585 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Fri, 1 Nov 2024 22:50:16 +0000 Subject: [PATCH 2/6] fix docs --- net/epee-encoding/src/varint.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/net/epee-encoding/src/varint.rs b/net/epee-encoding/src/varint.rs index e5c564a4c..fc862dfde 100644 --- a/net/epee-encoding/src/varint.rs +++ b/net/epee-encoding/src/varint.rs @@ -12,12 +12,12 @@ const FITS_IN_FOUR_BYTES: u64 = 2_u64.pow(32 - SIZE_OF_SIZE_MARKER) - 1; /// ```rust /// use cuprate_epee_encoding::read_varint; /// -/// assert_eq!(read_varint(&mut [252].as_slice()).unwrap(), 63); -/// assert_eq!(read_varint(&mut [1, 1].as_slice()).unwrap(), 64); -/// assert_eq!(read_varint(&mut [253, 255].as_slice()).unwrap(), 16_383); -/// assert_eq!(read_varint(&mut [2, 0, 1, 0].as_slice()).unwrap(), 16_384); -/// assert_eq!(read_varint(&mut [254, 255, 255, 255].as_slice()).unwrap(), 1_073_741_823); -/// assert_eq!(read_varint(&mut [3, 0, 0, 0, 1, 0, 0, 0].as_slice()).unwrap(), 1_073_741_824); +/// assert_eq!(read_varint::<_, u64>(&mut [252].as_slice()).unwrap(), 63); +/// assert_eq!(read_varint::<_, u64>(&mut [1, 1].as_slice()).unwrap(), 64); +/// assert_eq!(read_varint::<_, u64>(&mut [253, 255].as_slice()).unwrap(), 16_383); +/// assert_eq!(read_varint::<_, u64>(&mut [2, 0, 1, 0].as_slice()).unwrap(), 16_384); +/// assert_eq!(read_varint::<_, u64>(&mut [254, 255, 255, 255].as_slice()).unwrap(), 1_073_741_823); +/// assert_eq!(read_varint::<_, u64>(&mut [3, 0, 0, 0, 1, 0, 0, 0].as_slice()).unwrap(), 1_073_741_824); /// ``` pub fn read_varint>(r: &mut B) -> Result { if !r.has_remaining() { From 345096044cfc251903f7233676215d4e7e53c1d9 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Tue, 7 Jan 2025 03:25:42 +0000 Subject: [PATCH 3/6] add 32 bit check to CI --- .github/workflows/ci.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 367e8e14d..54aea3055 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,6 +46,33 @@ jobs: - name: Spell Check uses: crate-ci/typos@master + bit-32-support: + runs-on: ubuntu-latest + + strategy: + matrix: + crate: [ + cuprate-epee-encoding, + cuprate-rpc-types, + cuprate-fixed-bytes, + cuprate-types + ] + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Install Rust + uses: dtolnay/rust-toolchain@master + with: + toolchain: stable + targets: wasm32-unknown-unknown + + - name: Build 32 bit + run: cargo build --target wasm32-unknown-unknown -p ${{ matrix.crate }} + # All other CI. ci: runs-on: ${{ matrix.os }} From c9b34a7e910f34afd2f242cf70889649ecef152a Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Tue, 7 Jan 2025 03:33:49 +0000 Subject: [PATCH 4/6] remove `cuprate-types` --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 54aea3055..187d84a40 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,7 +55,6 @@ jobs: cuprate-epee-encoding, cuprate-rpc-types, cuprate-fixed-bytes, - cuprate-types ] steps: From 43da7e59620d24b962a92ca94c2d4594b18b3b6a Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Tue, 7 Jan 2025 03:37:56 +0000 Subject: [PATCH 5/6] add comments to CI --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 187d84a40..343d43845 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,11 +46,14 @@ jobs: - name: Spell Check uses: crate-ci/typos@master + # Run 32-bit support check separately. bit-32-support: runs-on: ubuntu-latest strategy: matrix: + # The list of crates to check for 32 bit support + # TODO: check features. crate: [ cuprate-epee-encoding, cuprate-rpc-types, From 92935b8f98920def9b6ddc3ad6905600aaaaa40e Mon Sep 17 00:00:00 2001 From: Boog900 Date: Wed, 8 Jan 2025 02:10:53 +0000 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: hinto-janai --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 343d43845..9b9a17784 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,13 +46,13 @@ jobs: - name: Spell Check uses: crate-ci/typos@master - # Run 32-bit support check separately. - bit-32-support: + # Run 32-bit WASM support check separately. + wasm-32-bit-support: runs-on: ubuntu-latest strategy: matrix: - # The list of crates to check for 32 bit support + # The list of crates to check for WASM 32 bit support # TODO: check features. crate: [ cuprate-epee-encoding, @@ -72,7 +72,7 @@ jobs: toolchain: stable targets: wasm32-unknown-unknown - - name: Build 32 bit + - name: Build WASM 32-bit run: cargo build --target wasm32-unknown-unknown -p ${{ matrix.crate }} # All other CI.