-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Encoding for Contract Calls #5512
Comments
IGI-111
added
compiler
General compiler. Should eventually become more specific as the issue is triaged
ABI
Everything to do the ABI, especially the JSON representation
labels
Jan 24, 2024
22 tasks
kamyar-tm
added
the
epic
An epic is a high-level master issue for large pieces of work.
label
Feb 22, 2024
xunilrj
added a commit
that referenced
this issue
Mar 6, 2024
## Description This PR implements the new encoding for contracts/scripts/predicates. #5512 and #5513 ### Contract Calls When the new encoding is turned on using `--experimental-new-encoding`, contract calls like the example below will be "desugarized" differently now. ```sway let base_asset_id = BASE_ASSET_ID; let other_contract_id = ContractId::from(0xa38576787f8900d66e6620548b6da8142b8bb4d129b2338609acd121ca126c10); let test_contract = abi(ContextTesting, other_contract_id.into()); let returned_contract_id = test_contract.get_id { gas: gas, coins: 0, asset_id: BASE_ASSET_ID.value}(1, 2, 3); ``` and will be transformed to ```sway let base_asset_id = BASE_ASSET_ID; let other_contract_id = ContractId::from(0xa38576787f8900d66e6620548b6da8142b8bb4d129b2338609acd121ca126c10); let test_contract = abi(ContextTesting, other_contract_id.into()); let returned_contract_id = contract_call::<ContractId, _>(other_contract_id.into(), "get_id", (1, 2, 3), coins, asset_id, gas); ``` And the important part is the `contract_call` function in the std library. This function does all the encoding as necessary and delegates the actual call to an intrinsic function `__contract_call`. Allowing the protocol to evolve entirely in Sway. ```sway pub fn contract_call<T, TArgs>(contract_id: b256, method_name: str, args: TArgs, coins: u64, asset_id: b256, gas: u64) -> T where TArgs: AbiEncode { let first_parameter = encode(method_name); let second_parameter = encode(args); let params = encode( ( contract_id, asm(a: first_parameter.ptr()) { a: u64 }, asm(a: second_parameter.ptr()) { a: u64 }, ) ); __contract_call::<T>(params.ptr(), coins, asset_id, gas) } ``` ### Contracts On the other side, when the flag `--expiremental-new-encoding` is turned on, the contract specification like the one below is being transformed into all the decoding and encoding necessary. The mains points are: - The compiler generates a function called `__entry` that decodes the method name and its arguments. The method is selected with a bunch of `if`s at the moment, because we don´t have `match` for string slices. Then we `decode` the arguments using the correct type, which is a tuple with all the function arguments, and expand this tuple calling the function; - All the contract functions are converted to global functions prefixed with `__contract_method`; - Results are encoded and returned using the intrinsic call `__retd`. Example: ```sway abi SomeContract { fn some_function(a: u64) -> u64; } impl SomeContract for Contract { fn some_function(a: u64) -> u64 { 1 } } ``` will be transformed into ```sway fn __entry() { let method_name = decode_first_parameter(); if method_name == "some_function" { let args = decode_second_parameter::<(u64,)>(); let result = __contract_method_some_function(args.0); __retd(encode(result)); } __revert(0); } ``` ### Scripts and Predicates The protocol to call scripts and predicates will also change and will be very similar to contracts. See more above. Now when the flag is turned on, the `main` function will not be entry point anymore. The compiler will actually generate an `__entry` function that will decode arguments and encode the result, like contracts. For example: ```sway fn main(a: u64) -> u64 { 1 } ``` will be transformed into ```sway fn __entry() { let args = decode_script_data::<(u64,)(); let result = main(args.0); __retd(encode(result)); } fn main(a: u64) -> u64 { 1 } ``` ## Tests To facilitate testing this PR introduces three changes to our test harness: 1 - A new parameter can be called to update test output files (abi and storage json). This facilitates when we only want to copy and paste these output files to their respective oracles. Brings the framework closer to a snapshot one. ``` > cargo r -p test --release -- --update-output-files ``` 2 - Depending on the configuration at `test.toml` multiple executions of the same test will be scheduled. At the moment, tests that depend on the encoding will run with the `--experimental-new-encoding` flag automatically. For example: ``` Testing should_pass/language/main_args/main_args_copy_copy ... ok Testing should_pass/language/main_args/main_args_copy_copy (New Encoding) ... ok ``` 3 - A new `script_data_new_encoding` was created because we want to support and run tests with the two encoding for a time. This is also what flags tests to run with the flag on automatically. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: