-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Attribute for checking of trivial encoding and decoding #7575
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
Open
xunilrj
wants to merge
56
commits into
master
Choose a base branch
from
xunilrj/trivial-checks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
4cd5f1f
trivial check attributes
xunilrj b008239
fmt and clippy issues
xunilrj 08d0450
update tests
xunilrj e8d5298
error message improvements
xunilrj f7f6b4f
improve docs
xunilrj fed5e78
improve docs
xunilrj 7a00a96
improve docs
xunilrj 6416551
better error message
xunilrj 8f85ad2
TrivialBool and TrivialEnum
xunilrj 7ca6ef3
fmt and clippy issues
xunilrj c3c6342
remove sway-lib-std warnings
xunilrj 6f7701c
better error message for tuples and arrays
xunilrj 3d57458
TrivialBool and TrivialEnum using auto-impl
xunilrj daa3599
TrivialBool and TrivialEnum using auto-impl
xunilrj 2130234
fmt and clippy issues
xunilrj 29727c2
correctly call is_decode_trivial
xunilrj 2f3aa39
correctly call is_decode_trivial
xunilrj 61285f3
fmt and clippy issues
xunilrj e06a197
PR fixes
xunilrj 35c64cf
fmt and clippy issues
xunilrj 6faca3e
runninf forc-fmt
xunilrj e2a194c
PR fixes
xunilrj 851bcb3
fmt and clippy issues
xunilrj 0d82ae1
fmt and clippy issues
xunilrj ea8f1c8
update tests
xunilrj 31294a9
fix tests
xunilrj 8b2aadd
update tests
xunilrj ae8ba23
link to the final documentation url
xunilrj 375ca52
link to the final documentation url
xunilrj 60976f2
fixing typos
xunilrj 7faac55
error improvements and gas benchmark
xunilrj 0e76066
improve error message
xunilrj bca153a
removing range from memory representation
xunilrj ffe4779
fmt and clippy issues
xunilrj d78914f
update tests
xunilrj 15c07ac
fmt and clippy issues
xunilrj 4fefc68
update tests
xunilrj 9ebde17
fixing typos
xunilrj 120be34
moving ir check to inside the type phase
xunilrj ec18880
rebase issues
xunilrj 42e8180
rebase issues
xunilrj a033b56
removing intrinsic
xunilrj 0dc7db9
check abi fns
xunilrj d9ac0a2
require attribute for abis
xunilrj fd52987
update tests
xunilrj a10bac8
ignore for gc test: it needs experimental features
xunilrj af5ea68
checking abi function for enums
xunilrj bfed17a
also check return type
xunilrj 6f47da7
small refactor to the check code
xunilrj b45c481
more tests
xunilrj bd80a03
fmt and clippy issues
xunilrj 83a5319
update tests
xunilrj d24f2c5
update tests
xunilrj 03bb4ff
fmt and clippy issues
xunilrj 1a7152d
remove redundant manager
xunilrj a09b65c
Fix remaining compiler panics in trivial decoding checks
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,3 +275,9 @@ Preload | |
| preloads | ||
| Preloads | ||
| VM's | ||
| Decodable | ||
| Encodable | ||
| callee | ||
| decodable | ||
| encodable | ||
| Vec | ||
This file contains hidden or 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 hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Trivially Encodable & Decodable Types | ||
|
|
||
| When a contract calls another contract, all arguments are **encoded** just before the call is actually executed, | ||
| and the callee **decodes** these arguments right before the target method starts. | ||
| This adds a small but non‑negligible gas cost, from hundreds to thousands of gas depending on the complexity of the arguments. | ||
|
|
||
| The Sway compiler mitigates this overhead for a subset of types that can be **trivially encoded** and/or **trivially decoded** – | ||
| that is, types that their *runtime representation*, how the type bytes are laid out inside the VM, is *identical* to their *encoded representation*, | ||
| how their bytes are laid out in the encoded buffer. | ||
|
|
||
| For such types the compiler can skip the encoding/decoding process entirely, saving gas and simplifying the generated code. | ||
|
|
||
| > **Trivial encoding** – encoding is replaced with a simple "transmute". | ||
| > **Trivial decoding** – encoding is replaced with a simple "transmute". | ||
|
|
||
| The compiler can skip each individually, but the whole gain comes only when both are skipped together. | ||
|
|
||
| ## Checking Triviality | ||
|
|
||
| Each struct that should be treated as trivially encodable/decodable can be annotated with the `#[trivial]` attribute: | ||
|
|
||
| ```sway | ||
| #[trivial(encode = "require", decode = "require")] | ||
| pub struct SomeArgument { | ||
| a: bool, | ||
| b: SomeEnum, | ||
| } | ||
| ``` | ||
|
|
||
| - `encode = "require"` – the compiler will check if the type is trivially encodable; if not, the build fails. | ||
| - `decode = "require"` – similarly for decoding. | ||
|
|
||
| Possible values are: | ||
|
|
||
| - required: compiler will check and error if the check fails; | ||
| - optional: compiler will only warn non-compliance; | ||
| - any: nothing will be checked. | ||
|
|
||
| This attributed can be used directly on types, but also on entry points such as "main" function for scripts and predicates; and contract methods for contracts. | ||
|
|
||
| ## Which Types Are Trivial? | ||
|
|
||
| | Type | Trivially Encodable | Trivially Decodable | Notes | | ||
| |------|---------------------|---------------------|-------| | ||
| | `bool` | ✅ | ❌ | `bool` encodes to a single byte (`0` or `1`), but decoding must validate that the byte is a legal value. | | ||
| | `u8`, `u64`, `u256`, `b256` | ✅ | ✅ | | | ||
| | `u16`, `u32` | ❌ | ❌ | Their runtime representation is actually a `u64` | | ||
| | Structs | ✅ If all their members are trivial | ✅ If all their member are trivial | Recursively evaluated. | | ||
| | Enums | ✅ If all variants are trivial | ❌ | Enums have an `u64` discriminant that cannot be trivially decodable. | | ||
| | Arrays | ✅ If the item type is trivial | ✅ if the item type is trivial | | ||
| | String Arrays | ✅ See * | ✅ See * | | | ||
| | Vec, Dictionary, String, etc. | ❌ | ❌ | Data Structures are never trivial | | ||
|
|
||
| Only when the feature "str_array_no_padding" is turned on. When the feature toggle is off, only string arrays that its length is multiple of 8. | ||
|
|
||
| ### Why `bool` and `enum` are not trivially decodable | ||
|
|
||
| Probably the most surprising non trivial base data type is `bool`. Mainly because `bool` is obviously trivially encodable. But there is no guarantee | ||
| that buffer does not have a value like `2`, that being "transmuted" into a bool would be allow its runtime representation to be `2`, which is **undefined behaviour**. | ||
|
|
||
| The same limitation applies to enums. Enums are implemented as "tagged unions" which means that their runtime representation has a discriminant value as `u64`. There | ||
| is no guarantee that the buffer would have a valid value for its discriminant. | ||
|
|
||
| --- | ||
|
|
||
| ## 3. Workaround for Non‑trivial Types | ||
|
|
||
| If you need to expose a `bool` or an enum as a public argument, you can either: | ||
|
|
||
| 1. **Manual validation** – expose a raw `u64` (or `u8`) and check its value in the callee. | ||
|
|
||
| ```sway | ||
| #[trivial(encode = "require", decode = "require")] | ||
| pub struct Flag(u8); // manually validate that value <= 1 | ||
| ``` | ||
|
|
||
| 1. **Custom wrappers** – Sway ships with `TrivialBool` and `TrivialEnum<T>` that enforce the bounds at compile time. | ||
|
|
||
| ```sway | ||
| use sway::primitive::TrivialBool; | ||
| use sway::primitive::TrivialEnum; | ||
|
|
||
| #[trivial(encode = "require", decode = "require")] | ||
| pub struct SomeArgument { | ||
| a: TrivialBool, | ||
| b: TrivialEnum<SomeEnum>, | ||
| } | ||
| ``` | ||
|
|
||
| These wrappers automatically provide the guard checks and still let the compiler treat them as trivial. | ||
| Their usage is very similar to `Option<bool>`. | ||
|
|
||
| ```sway | ||
| let a: bool = some_argument.a.unwrap(); | ||
| let b: SomeEnum = some_argument.b.unwrap(); | ||
| ``` |
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.