-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New encoding for contract calls (#5427)
## 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.
- Loading branch information
Showing
142 changed files
with
5,584 additions
and
1,439 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.