alloy-rs / rlp

Fast implementation of Ethereum RLP serialization
Apache License 2.0
77 stars 16 forks source link

Add functionality to extract payload structure without decoding it #17

Closed Wollac closed 1 week ago

Wollac commented 1 week ago

For certain use cases, the type of an RLP payload is not known in advance, but depends on its structure.

An example of this is the MPT node, which can be either a branch or a leaf node depending on the number of items in the list. Here, it would be helpful to have a function that extracts the payload structure without having to specify its type.

When implemented in the application, this adds quite a bit of error-prone boilerplate code (see for example here: decode in alloy-trie), and having it in the RLP library would be beneficial.

Such a function might look something like the following

enum Payload<'a> {
    String(&'a [u8]),
    List(Vec<&'a [u8]>),
}

fn decode_raw<'a>(buf: &mut &'a [u8]) -> Result<Payload<'a>>;

I can provide a PR with this feature, if this seems relevant for this crate.

DaniPopes commented 1 week ago

SGTM cc @rkrasiuk @rjected

rkrasiuk commented 1 week ago

sgtm as well, current interface is indeed rather footgun'y

Rjected commented 1 week ago

Yeah this would be nice