From bd7ac13cbfc41edb0d72eed0027d759a0ef0555d Mon Sep 17 00:00:00 2001 From: n0x <54751288+N0xMare@users.noreply.github.com> Date: Sun, 28 Apr 2024 23:36:16 -0400 Subject: [PATCH] fix(derive): fix span batch utils read_tx_data() (#170) instead of copying the entire buffer, de-reference twice to make a new mutable reference of the input u8 array --- crates/derive/src/types/batch/span_batch/utils.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/derive/src/types/batch/span_batch/utils.rs b/crates/derive/src/types/batch/span_batch/utils.rs index 6dd20e69..531b4daa 100644 --- a/crates/derive/src/types/batch/span_batch/utils.rs +++ b/crates/derive/src/types/batch/span_batch/utils.rs @@ -18,12 +18,9 @@ pub(crate) fn read_tx_data(r: &mut &[u8]) -> Result<(Vec, TxType), SpanBatch r.advance(1); } - // Copy the reader, as we need to read the header to determine if the payload is a list. - // TODO(clabby): This is horribly inefficient. It'd be nice if we could peek at this rather than - // forcibly having to advance the buffer passed, should read more into the alloy rlp docs to - // see if this is possible. - let r_copy = Vec::from(*r); - let rlp_header = Header::decode(&mut r_copy.as_slice()) + // Read the RLP header with a different reader pointer. This prevents the initial pointer from + // being advanced in the case that what we read is invalid. + let rlp_header = Header::decode(&mut (**r).as_ref()) .map_err(|_| SpanBatchError::Decoding(SpanDecodingError::InvalidTransactionData))?; let tx_payload = if rlp_header.list {