Skip to content

Commit 789cd4d

Browse files
committed
rpc/types 32 bit support
1 parent 44981f2 commit 789cd4d

File tree

6 files changed

+23
-30
lines changed

6 files changed

+23
-30
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

net/epee-encoding/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ default = ["std"]
1515
std = ["dep:thiserror", "bytes/std", "cuprate-fixed-bytes/std"]
1616

1717
[dependencies]
18-
cuprate-helper = { workspace = true, default-features = false, features = ["cast"] }
1918
cuprate-fixed-bytes = { workspace = true, default-features = false }
2019

2120
paste = "1.0.15"

net/epee-encoding/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ use core::str::from_utf8 as str_from_utf8;
6969

7070
use bytes::{Buf, BufMut, Bytes, BytesMut};
7171

72-
use cuprate_helper::cast::{u64_to_usize, usize_to_u64};
73-
7472
pub mod container_as_blob;
7573
pub mod error;
7674
mod io;
@@ -89,7 +87,7 @@ pub use varint::{read_varint, write_varint};
8987
/// this binary serialization format.
9088
const HEADER: &[u8] = b"\x01\x11\x01\x01\x01\x01\x02\x01\x01";
9189
/// The maximum length a byte array (marked as a string) can be.
92-
const MAX_STRING_LEN_POSSIBLE: u64 = 2000000000;
90+
const MAX_STRING_LEN_POSSIBLE: usize = 2000000000;
9391
/// The maximum depth of skipped objects.
9492
const MAX_DEPTH_OF_SKIPPED_OBJECTS: u8 = 20;
9593
/// The maximum number of fields in an object.
@@ -248,7 +246,7 @@ pub fn write_bytes<T: AsRef<[u8]>, B: BufMut>(t: T, w: &mut B) -> Result<()> {
248246
let bytes = t.as_ref();
249247
let len = bytes.len();
250248

251-
write_varint(usize_to_u64(len), w)?;
249+
write_varint(len, w)?;
252250

253251
if w.remaining_mut() < len {
254252
return Err(Error::IO("Not enough capacity to write bytes"));
@@ -292,7 +290,7 @@ where
292290
I: Iterator<Item = T> + ExactSizeIterator,
293291
B: BufMut,
294292
{
295-
write_varint(usize_to_u64(iterator.len()), w)?;
293+
write_varint(iterator.len(), w)?;
296294
for item in iterator {
297295
item.write(w)?;
298296
}
@@ -337,7 +335,7 @@ fn skip_epee_value<B: Buf>(r: &mut B, skipped_objects: &mut u8) -> Result<()> {
337335

338336
if let Some(size) = marker.inner_marker.size() {
339337
let bytes_to_skip = size
340-
.checked_mul(u64_to_usize(len))
338+
.checked_mul(len.try_into()?)
341339
.ok_or(Error::Value("List is too big".to_string()))?;
342340
return advance(bytes_to_skip, r);
343341
};
@@ -355,7 +353,7 @@ fn skip_epee_value<B: Buf>(r: &mut B, skipped_objects: &mut u8) -> Result<()> {
355353
| InnerMarker::U8
356354
| InnerMarker::Bool => unreachable!("These types are constant size."),
357355
InnerMarker::String => {
358-
let len = u64_to_usize(read_varint(r)?);
356+
let len = read_varint(r)?;
359357
advance(len, r)?;
360358
}
361359
InnerMarker::Object => {

net/epee-encoding/src/value.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use core::fmt::Debug;
77
use bytes::{Buf, BufMut, Bytes, BytesMut};
88

99
use cuprate_fixed_bytes::{ByteArray, ByteArrayVec};
10-
use cuprate_helper::cast::u64_to_usize;
1110

1211
use crate::{
1312
io::{checked_read_primitive, checked_write_primitive},
@@ -67,7 +66,7 @@ impl<T: EpeeObject> EpeeValue for Vec<T> {
6766
"Marker is not sequence when a sequence was expected",
6867
));
6968
}
70-
let len = u64_to_usize(read_varint(r)?);
69+
let len = read_varint(r)?;
7170

7271
let individual_marker = Marker::new(marker.inner_marker);
7372

@@ -168,8 +167,6 @@ impl EpeeValue for Vec<u8> {
168167
return Err(Error::Format("Byte array exceeded max length"));
169168
}
170169

171-
let len = u64_to_usize(len);
172-
173170
if r.remaining() < len {
174171
return Err(Error::IO("Not enough bytes to fill object"));
175172
}
@@ -206,8 +203,6 @@ impl EpeeValue for Bytes {
206203
return Err(Error::Format("Byte array exceeded max length"));
207204
}
208205

209-
let len = u64_to_usize(len);
210-
211206
if r.remaining() < len {
212207
return Err(Error::IO("Not enough bytes to fill object"));
213208
}
@@ -241,8 +236,6 @@ impl EpeeValue for BytesMut {
241236
return Err(Error::Format("Byte array exceeded max length"));
242237
}
243238

244-
let len = u64_to_usize(len);
245-
246239
if r.remaining() < len {
247240
return Err(Error::IO("Not enough bytes to fill object"));
248241
}
@@ -274,13 +267,11 @@ impl<const N: usize> EpeeValue for ByteArrayVec<N> {
274267
return Err(Error::Format("Marker does not match expected Marker"));
275268
}
276269

277-
let len = read_varint(r)?;
270+
let len = read_varint::<_, usize>(r)?;
278271
if len > MAX_STRING_LEN_POSSIBLE {
279272
return Err(Error::Format("Byte array exceeded max length"));
280273
}
281274

282-
let len = u64_to_usize(len);
283-
284275
if r.remaining() < len {
285276
return Err(Error::IO("Not enough bytes to fill object"));
286277
}
@@ -310,7 +301,7 @@ impl<const N: usize> EpeeValue for ByteArray<N> {
310301
return Err(Error::Format("Marker does not match expected Marker"));
311302
}
312303

313-
let len = u64_to_usize(read_varint(r)?);
304+
let len = read_varint::<_, usize>(r)?;
314305
if len != N {
315306
return Err(Error::Format("Byte array has incorrect length"));
316307
}
@@ -377,7 +368,7 @@ impl<const N: usize> EpeeValue for Vec<[u8; N]> {
377368
));
378369
}
379370

380-
let len = u64_to_usize(read_varint(r)?);
371+
let len = read_varint(r)?;
381372

382373
let individual_marker = Marker::new(marker.inner_marker);
383374

@@ -413,7 +404,7 @@ macro_rules! epee_seq {
413404
));
414405
}
415406

416-
let len = u64_to_usize(read_varint(r)?);
407+
let len = read_varint(r)?;
417408

418409
let individual_marker = Marker::new(marker.inner_marker.clone());
419410

net/epee-encoding/src/varint.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const FITS_IN_FOUR_BYTES: u64 = 2_u64.pow(32 - SIZE_OF_SIZE_MARKER) - 1;
1919
/// assert_eq!(read_varint(&mut [254, 255, 255, 255].as_slice()).unwrap(), 1_073_741_823);
2020
/// assert_eq!(read_varint(&mut [3, 0, 0, 0, 1, 0, 0, 0].as_slice()).unwrap(), 1_073_741_824);
2121
/// ```
22-
pub fn read_varint<B: Buf>(r: &mut B) -> Result<u64> {
22+
pub fn read_varint<B: Buf, T: TryFrom<u64>>(r: &mut B) -> Result<T> {
2323
if !r.has_remaining() {
2424
return Err(Error::IO("Not enough bytes to build VarInt"));
2525
}
@@ -35,7 +35,8 @@ pub fn read_varint<B: Buf>(r: &mut B) -> Result<u64> {
3535
for i in 1..len {
3636
vi |= u64::from(r.get_u8()) << (((i - 1) * 8) + 6);
3737
}
38-
Ok(vi)
38+
39+
vi.try_into().map_err(|_| Error::IO("VarInt is too big"))
3940
}
4041

4142
/// Write an epee variable sized number into `w`.
@@ -58,7 +59,12 @@ pub fn read_varint<B: Buf>(r: &mut B) -> Result<u64> {
5859
/// assert_eq!(buf.as_slice(), expected_bytes);
5960
/// }
6061
/// ```
61-
pub fn write_varint<B: BufMut>(number: u64, w: &mut B) -> Result<()> {
62+
pub fn write_varint<B: BufMut, T: TryInto<u64>>(number: T, w: &mut B) -> Result<()> {
63+
let number = number
64+
.try_into()
65+
.map_err(|_| "Tried to write a varint bigger than 64-bits")
66+
.unwrap();
67+
6268
let size_marker = match number {
6369
0..=FITS_IN_ONE_BYTE => 0,
6470
64..=FITS_IN_TWO_BYTES => 1,
@@ -101,7 +107,7 @@ mod tests {
101107
}
102108

103109
fn assert_varint_val(mut varint: &[u8], val: u64) {
104-
assert_eq!(read_varint(&mut varint).unwrap(), val);
110+
assert_eq!(read_varint::<_, u64>(&mut varint).unwrap(), val);
105111
}
106112

107113
#[test]

rpc/types/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ keywords = ["cuprate", "rpc", "types", "monero"]
1010

1111
[features]
1212
default = ["serde", "epee"]
13-
serde = ["dep:serde", "cuprate-fixed-bytes/serde"]
14-
epee = ["dep:cuprate-epee-encoding"]
13+
serde = ["dep:serde", "cuprate-fixed-bytes/serde", "cuprate-types/serde"]
14+
epee = ["dep:cuprate-epee-encoding", "cuprate-types/epee"]
1515

1616
[dependencies]
1717
cuprate-epee-encoding = { workspace = true, optional = true }
1818
cuprate-fixed-bytes = { workspace = true }
19-
cuprate-types = { workspace = true, default-features = false, features = ["epee", "serde"] }
19+
cuprate-types = { workspace = true, default-features = false }
2020

2121
paste = { workspace = true }
2222
serde = { workspace = true, optional = true }

0 commit comments

Comments
 (0)