I have a question about reading data in a chain extension. To be precise, I'm concerned about methods Environment<_,_,_,S: BufIn>::read*. Do I understand correctly, that every such function doesn't move starting pointer and thus I can make only a single read?

If so, then I would like to ask whether there are any reasons for that. My use case where it is kinda onerous, is when my chain extension has such an API:

fn extension(a: SomeSizedType, b: Vec<u8>) {...}

The approach that would be natural for me would be to do parsing like:

let env = env.buf_in_buf_out();

let a = env.read_as<SomeSizedType>()?;
// The caller is expected to pass length hint
let b_len = env.read_as<u32>()?;
let b = env.read(b_len);

However, when I can invoke read* only once, I have to handle decoding and byte array mess on my site. Please notice, that I cannot pack a and b into a single struct, and use read_as, because Vec<_> is not MaxEncodedLen.

1 Answer 1


The way you would handle that is to pack a and b into a single struct. As you pointed out you can't use read_as in that case since that type will not be bounded. Do this instead:

let env = env.buf_in_buf_out();
let len = env.in_len();
// TODO: sanity check len and maybe charge some weight based on that
let input: MyStruct = env.read_as_unbounded(len)?;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.