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

Lab should decode XDR stream (instead of single entry) #944

Open
Ifropc opened this issue Aug 7, 2024 · 0 comments
Open

Lab should decode XDR stream (instead of single entry) #944

Ifropc opened this issue Aug 7, 2024 · 0 comments
Assignees

Comments

@Ifropc
Copy link

Ifropc commented Aug 7, 2024

What problem does your feature solve?

Currently, contract's ScSpecEntry is an XDR stream (multiple XDR entries of the same type are appended together in a single byte buffer).
XDR parsers can either read whole buffer and parse it as a single entry of a given type (aka read_xdr_to_end in rs-xdr), or read buffer iteratively, where parser reads chunks of the buffer (for a given type) until buffer ends (aka read_xdr_iter).
In cli we can use stellar xdr decode. And different behaviors are specified using flag --input:

--input <INPUT>    [default: stream-base64] [possible values: single, single-base64, stream, stream-base64, stream-framed]

So we can decode XDR of a contract easily (reading full stream)

> stellar-cli contract info interface --network testnet --id  \ 
CC434MK5OCXTDMZTBZXS7WQP6SOYFWVAZY23PRGAPUQZXARGQRZP3FJH --output xdr-base64 |  \
stellar-cli  xdr decode --type ScSpecEntry
{"function_v0":{"doc":"","name":"initialize","inputs":[{"doc":"","name":"token_wasm_hash","type_":{"bytes_n":{"n":32}}},{"doc":"","name":"token_a","type_":"address"},{"doc":"","name":"token_b","type_":"address"}],"outputs":[]}}
{"function_v0":{"doc":"","name":"share_id","inputs":[],"outputs":["address"]}}
{"function_v0":{"doc":"","name":"deposit","inputs":[{"doc":"","name":"to","type_":"address"},{"doc":"","name":"desired_a","type_":"i128"},{"doc":"","name":"min_a","type_":"i128"},{"doc":"","name":"desired_b","type_":"i128"},{"doc":"","name":"min_b","type_":"i128"}],"outputs":[]}}
{"function_v0":{"doc":"","name":"swap","inputs":[{"doc":"","name":"to","type_":"address"},{"doc":"","name":"buy_a","type_":"bool"},{"doc":"","name":"out","type_":"i128"},{"doc":"","name":"in_max","type_":"i128"}],"outputs":[]}}
{"function_v0":{"doc":"","name":"withdraw","inputs":[{"doc":"","name":"to","type_":"address"},{"doc":"","name":"share_amount","type_":"i128"},{"doc":"","name":"min_a","type_":"i128"},{"doc":"","name":"min_b","type_":"i128"}],"outputs":[{"tuple":{"value_types":["i128","i128"]}}]}}
{"function_v0":{"doc":"","name":"get_rsrvs","inputs":[],"outputs":[{"tuple":{"value_types":["i128","i128"]}}]}}

Note that when trying to pass --single, expected error will be returned (because whole buffer doesn't represent a single ScSpecEntry, but a batch of ScSpecEntry)

> stellar-cli contract info interface --network testnet --id \
CC434MK5OCXTDMZTBZXS7WQP6SOYFWVAZY23PRGAPUQZXARGQRZP3FJH --output xdr-base64 | \
  stellar-cli  xdr decode --type ScSpecEntry --input single
error: error reading file: error decoding XDR: xdr value invalid

The same behavior can be seen in lab for the same XDR:

 > stellar-cli contract info interface --network testnet --id \
CC434MK5OCXTDMZTBZXS7WQP6SOYFWVAZY23PRGAPUQZXARGQRZP3FJH --output xdr-base64
AAAAAAAAAAAAAAAKaW5pdGlhbGl6ZQAAAAAAAwAAAAAAAAAPdG9rZW5fd2FzbV9oYXNoAAAAA+4AAAAgAAAAAAAAAAd0b2tlbl9hAAAAABMAAAAAAAAAB3Rva2VuX2IAAAAAEwAAAAAAAAAAAAAAAAAAAAhzaGFyZV9pZAAAAAAAAAABAAAAEwAAAAAAAAAAAAAAB2RlcG9zaXQAAAAABQAAAAAAAAACdG8AAAAAABMAAAAAAAAACWRlc2lyZWRfYQAAAAAAAAsAAAAAAAAABW1pbl9hAAAAAAAACwAAAAAAAAAJZGVzaXJlZF9iAAAAAAAACwAAAAAAAAAFbWluX2IAAAAAAAALAAAAAAAAAAAAAAAAAAAABHN3YXAAAAAEAAAAAAAAAAJ0bwAAAAAAEwAAAAAAAAAFYnV5X2EAAAAAAAABAAAAAAAAAANvdXQAAAAACwAAAAAAAAAGaW5fbWF4AAAAAAALAAAAAAAAAAAAAAAAAAAACHdpdGhkcmF3AAAABAAAAAAAAAACdG8AAAAAABMAAAAAAAAADHNoYXJlX2Ftb3VudAAAAAsAAAAAAAAABW1pbl9hAAAAAAAACwAAAAAAAAAFbWluX2IAAAAAAAALAAAAAQAAA+0AAAACAAAACwAAAAsAAAAAAAAAAAAAAAlnZXRfcnNydnMAAAAAAAAAAAAAAQAAA+0AAAACAAAACwAAAAs=

image

Technically, it is a valid stream of ScSpecEntry, but not a valid single ScSpecEntry.
Because Lab reads only single entry, most of contract spec XDRs can't be viewed using the lab (but still can be viewed using cli)

Internal slack discussion thread

What would you like to see?

Replace usage of read_xdr_to_end with read_xdr_iter (method's JS alternatives). This change is backward compatible, because single entry is the same as stream of 1 entry.

What alternatives are there?

Using cli

@janewang janewang added the v2 label Aug 8, 2024
@janewang janewang added this to DevX Aug 26, 2024
@github-project-automation github-project-automation bot moved this to Backlog (Not Ready) in DevX Aug 26, 2024
@janewang janewang moved this from Backlog (Not Ready) to Todo (Ready for Dev) in DevX Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

No branches or pull requests

3 participants