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

add length param to decode function #3

Merged
merged 2 commits into from
Jan 24, 2022
Merged

add length param to decode function #3

merged 2 commits into from
Jan 24, 2022

Conversation

roaminro
Copy link
Contributor

Thanks for you awesome work @piotr-oles! I just needed to be able to give a decoder the length of the buffer, so I added an optional parameter to the decode function.

@piotr-oles
Copy link
Owner

Thanks for the PR! It looks good, but I'm curious what is your use case? :)

@roaminro
Copy link
Contributor Author

roaminro commented Jan 23, 2022

So, I'm basically creating a "Contract Development Toolkit" (CDT) for a blockchain called Koinos (https://koinos.io). This CDT is just an interface between AseemblyScript and the WASM virtual machine (VM), they use "Fizzy" as their WASM VM. Their VM exposes an environment function that you can call to trigger different "system calls" (syscalls) and these system calls use Protobuf, so you need to use Protobuf to interact with them. When you call the "invoke_system_call" function, you give them a pointer to a buffer that they will use to return data (as Protobuf). I had some cases where these system calls were getting "trapped" because the "as-proto" deserializer was trying to read the entire "read buffer" when it should actually only read the number of bytes that were written by the WASM VM. The read buffer is always 1024 bytes, but the result of a syscall can be way less than that. The syscalls return the number of bytes that were written to the read buffer, so by giving this number to "as-proto" I can ensure that your library will only try to deserialize what's needed. Here's an example of how I use it (with a modified version of "as-proto":

export function getEntryPoint(): u32 {
    const args = new chain.get_entry_point_arguments();
    const encodedArgs = Protobuf.encode(args, chain.get_entry_point_arguments.encode);
    const readBuffer = new Uint8Array(MAX_BUFFER_SIZE);
    const len = env.invoke_system_call(system_call_id.get_entry_point, readBuffer.dataStart as u32, MAX_BUFFER_SIZE, encodedArgs.dataStart as u32, encodedArgs.byteLength);
    const result = Protobuf.decode<chain.get_entry_point_result>(readBuffer, chain.get_entry_point_result.decode, len);

    return result.value;
  }

@piotr-oles
Copy link
Owner

Make sense :) Could you rebase the branch to the latest main? I added missing CI actions for pull request event

@roaminro
Copy link
Contributor Author

done, thanks!

@piotr-oles piotr-oles added the minor Increment the minor version when merged label Jan 24, 2022
@piotr-oles piotr-oles self-assigned this Jan 24, 2022
@piotr-oles piotr-oles merged commit d1d0299 into piotr-oles:main Jan 24, 2022
@github-actions
Copy link

🚀 PR was released in v0.2.0 🚀

@github-actions github-actions bot added the released This issue/pull request has been released. label Jan 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor Increment the minor version when merged released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants