-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: script and predicate blobs (#1520)
closes: #1519 Scripts and predicates can be pre-uploaded with blobs and then run with loader code. --------- Co-authored-by: Oleksii Filonenko <12615679+Br1ght0ne@users.noreply.github.com> Co-authored-by: hal3e <git@hal3e.io> Co-authored-by: Rodrigo Araújo <rod.dearaujo@gmail.com>
- Loading branch information
1 parent
84266d2
commit 2a67e3b
Showing
32 changed files
with
1,174 additions
and
113 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Pre-uploading code | ||
|
||
If you have a script or predicate that is larger than normal or which you plan | ||
on calling often, you can pre-upload its code as a blob to the network and run a | ||
loader script/predicate instead. The loader can be configured with the | ||
script/predicate configurables, so you can change how the script/predicate is | ||
configured on each run without having large transactions due to the code | ||
duplication. | ||
|
||
## Scripts | ||
|
||
A high level pre-upload: | ||
|
||
```rust,ignore | ||
{{#include ../../e2e/tests/scripts.rs:preload_high_level}} | ||
``` | ||
|
||
The upload of the blob is handled inside of the `convert_into_loader` method. If you | ||
want more fine-grained control over it, you can create the script transaction | ||
manually: | ||
|
||
```rust,ignore | ||
{{#include ../../e2e/tests/scripts.rs:preload_low_level}} | ||
``` | ||
|
||
## Predicates | ||
|
||
You can prepare a predicate for pre-uploading without doing network requests: | ||
|
||
```rust,ignore | ||
{{#include ../../e2e/tests/predicates.rs:preparing_the_predicate}} | ||
``` | ||
|
||
Once you want to execute the predicate, you must beforehand upload the blob | ||
containing its code: | ||
|
||
```rust,ignore | ||
{{#include ../../e2e/tests/predicates.rs:uploading_the_blob}} | ||
``` | ||
|
||
By pre-uploading the predicate code, you allow for cheaper calls to the predicate | ||
from subsequent callers. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "predicate_blobs" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
predicate; | ||
|
||
configurable { | ||
SECRET_NUMBER: u64 = 9000, | ||
} | ||
|
||
fn main(arg1: u8, arg2: u8) -> bool { | ||
arg1 == 1 && arg2 == 19 && SECRET_NUMBER == 10001 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "empty" | ||
|
||
[dependencies] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
script; | ||
|
||
fn main() {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "script_blobs" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
script; | ||
|
||
configurable { | ||
SECRET_NUMBER: u64 = 9000, | ||
} | ||
|
||
enum MyEnum { | ||
A: u64, | ||
B: u8, | ||
C: (), | ||
} | ||
|
||
struct MyStruct { | ||
field_a: MyEnum, | ||
field_b: b256, | ||
} | ||
|
||
fn main(arg1: MyStruct) -> u64 { | ||
assert_eq(SECRET_NUMBER, 10001); | ||
|
||
match arg1.field_a { | ||
MyEnum::B(value) => { | ||
assert_eq(value, 99); | ||
} | ||
_ => { | ||
assert(false) | ||
} | ||
} | ||
|
||
assert_eq( | ||
arg1.field_b, | ||
0x1111111111111111111111111111111111111111111111111111111111111111, | ||
); | ||
|
||
return SECRET_NUMBER; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "script_proxy" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
script; | ||
|
||
abi Proxy { | ||
#[storage(write)] | ||
fn set_target_contract(id: ContractId); | ||
|
||
// methods of the `huge_contract` in our e2e sway contracts | ||
#[storage(read)] | ||
fn something() -> u64; | ||
|
||
#[storage(read)] | ||
fn write_some_u64(some: u64); | ||
|
||
#[storage(read)] | ||
fn read_some_u64() -> u64; | ||
} | ||
|
||
fn main(proxy_contract_id: ContractId) -> bool { | ||
let proxy_instance = abi(Proxy, proxy_contract_id.into()); | ||
let _ = proxy_instance.something(); | ||
proxy_instance.write_some_u64(10001); | ||
let read_u_64 = proxy_instance.read_some_u64(); | ||
return read_u_64 == 10001; | ||
} |
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.