Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Rlp::at_with_offset method. #269

Merged
merged 2 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions rlp/src/rlpin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,25 @@ impl<'a> Rlp<'a> {
}
}

/// Returns an Rlp item in a list at the given index.
///
/// Returns an error if this Rlp is not a list or if the index is out of range.
pub fn at<'view>(&'view self, index: usize) -> Result<Rlp<'a>, DecoderError>
where
'a: 'view,
{
let (rlp, _offset) = self.at_with_offset(index)?;
Ok(rlp)
}

/// Returns an Rlp item in a list at the given index along with the byte offset into the
/// raw data slice.
///
/// Returns an error if this Rlp is not a list or if the index is out of range.
pub fn at_with_offset<'view>(&'view self, index: usize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of adding this as a new method rather than changing the return value of at()? Just to reduce the breakage in parity-ethereum? If yes then I think I'd prefer to do a breaking release of rlp and do the grunt work.

Copy link
Contributor Author

@jimpo jimpo Nov 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, most of the time you don't need the offset, so even if designing the API from scratch I think having two separate methods probably makes sense. At the moment this is only used for rlp-encoded trie nodes.

But yeah, probably the main reason is backwards compatibility. Just doesn't seem like there's a good reason to break the current API, IMO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most of the time you don't need the offset,

In your eth branch it's used 4 times and only for RlpNodeCodec so I guess you're right. at() is used much more extensively, which kind of surprised me: I'd have hoped it was used much less.

-> Result<(Rlp<'a>, usize), DecoderError>
where
'a: 'view,
{
if !self.is_list() {
return Err(DecoderError::RlpExpectedToBeList);
Expand All @@ -211,11 +227,12 @@ impl<'a> Rlp<'a> {
let (bytes, consumed) = Rlp::consume_items(bytes, indexes_to_skip)?;

// update the cache
self.offset_cache.set(Some(OffsetCache::new(index, bytes_consumed + consumed)));
let offset = bytes_consumed + consumed;
self.offset_cache.set(Some(OffsetCache::new(index, offset)));

// construct new rlp
let found = BasicDecoder::payload_info(bytes)?;
Ok(Rlp::new(&bytes[0..found.header_len + found.value_len]))
Ok((Rlp::new(&bytes[0..found.header_len + found.value_len]), offset))
}

pub fn is_null(&self) -> bool {
Expand Down
29 changes: 29 additions & 0 deletions rlp/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ fn rlp_at() {
}
}

#[test]
fn rlp_at_with_offset() {
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
{
let rlp = Rlp::new(&data);
assert!(rlp.is_list());
let animals: Vec<String> = rlp.as_list().unwrap();
assert_eq!(animals, vec!["cat".to_owned(), "dog".to_owned()]);

let (cat, cat_offset) = rlp.at_with_offset(0).unwrap();
assert!(cat.is_data());
assert_eq!(cat_offset, 1);
assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']);
assert_eq!(cat.as_val::<String>().unwrap(), "cat".to_owned());

let (dog, dog_offset) = rlp.at_with_offset(1).unwrap();
assert!(dog.is_data());
assert_eq!(dog_offset, 5);
assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']);
assert_eq!(dog.as_val::<String>().unwrap(), "dog".to_owned());

let (cat_again, cat_offset) = rlp.at_with_offset(0).unwrap();
assert!(cat_again.is_data());
assert_eq!(cat_offset, 1);
assert_eq!(cat_again.as_raw(), &[0x83, b'c', b'a', b't']);
assert_eq!(cat_again.as_val::<String>().unwrap(), "cat".to_owned());
}
}

#[test]
fn rlp_at_err() {
let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o'];
Expand Down