Skip to content

Commit

Permalink
Bump to fuels v0.37 and add a test and docs for configurable bloc…
Browse files Browse the repository at this point in the history
…ks (FuelLabs#4255)

## Description
Closes FuelLabs#4206 

Three main changes: 
* Add new configuration-time constants tests to `sdk-harness`. This is
important to make sure there are no regression in the backend.
* Add a new example for `configurable`
* Document `configurable`
* Had to bump to `fuels 0.37` to be able to update `configurable`
variables from the SDK.

## 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
mohammadfawaz authored Mar 9, 2023
1 parent ba5ffe9 commit b33463d
Show file tree
Hide file tree
Showing 23 changed files with 483 additions and 188 deletions.
18 changes: 7 additions & 11 deletions docs/book/src/basics/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,18 @@ If the value declared cannot be assigned to the declared type, there will be an

## Configuration-time Constants

It is possible to define and initialize constant variables in the manifest file `Forc.toml` of a Sway project. These constants then become visible and usable in the corresponding Sway program. Such variables are called configuration-time constants and have to be defined in their own section called `[constants]` in the manifest file. The syntax for declaring such constants is as follows:
Configuration-time constants (or configurable constants) are special constants that behave like regular constants in the sense that they cannot change during program execution, but they can be configured _after_ the Sway program has been built. The Rust and TS SDKs allow updating the values of these constants by injecting new values for them directly in the bytecode without having to build the program again. These are useful for contract factories and behave somewhat similarly to `immutable` variables from languages like Solidity.

Configuration-time constants are declared inside a `configurable` block and require a type ascription and an initializer as follows:

```sway
{{#include ../../../../examples/config_time_constants/Forc.toml:constants}}
{{#include ../../../../examples/config_time_constants/src/main.sw:configurable_block}}
```

Notice that each constant requires two fields: a `type` and a `value`.

> **Note**
> Because configuration-time constants are _constants_, they are immutable and cannot be made otherwise.
At most one `configurable` block is allowed in a Sway project. Moreover, `configurable` blocks are not allowed in libraries.

The constants defined above can now be used in a Sway program that uses the manifest file as follows:
Configurable constants can be read directly just like regular constants:

```sway
{{#include ../../../../examples/config_time_constants/src/main.sw}}
{{#include ../../../../examples/config_time_constants/src/main.sw:using_configurables}}
```

> **Note**
> Currently, it is only possible to define configuration-time constants that have [primitive types](built_in_types.md#primitive-types) and that are initialized using literals. This will change in the future.
8 changes: 0 additions & 8 deletions examples/config_time_constants/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,3 @@ license = "Apache-2.0"
name = "config_time_constants"

[dependencies]

#// ANCHOR: constants
[constants]
some_contract_addr = { type = "b256", value = "0x580acb6ee759d9be0c0f78d3ef24e1b59300c625b3c61999967366dbbebad31c" }
some_num = { type = "u64", value = "42" }
some_string = { type = "str[4]", value = "\"fuel\"" }
true_bool = { type = "bool", value = "true" }
#// ANCHOR_END: constants
38 changes: 31 additions & 7 deletions examples/config_time_constants/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
script;
contract;

struct S {
x: u64,
enum EnumWithGeneric<D> {
VariantOne: D,
VariantTwo: (),
}

fn main() -> u64 {
let addr = some_contract_addr;
struct StructWithGeneric<D> {
field_1: D,
field_2: u64,
}

// ANCHOR: configurable_block
configurable {
U8: u8 = 8u8,
BOOL: bool = true,
ARRAY: [u32; 3] = [253u32, 254u32, 255u32],
STR_4: str[4] = "fuel",
STRUCT: StructWithGeneric<u8> = StructWithGeneric {
field_1: 8u8,
field_2: 16,
},
ENUM: EnumWithGeneric<bool> = EnumWithGeneric::VariantOne(true),
}
// ANCHOR_END: configurable_block

let string = some_string;
abi TestContract {
fn return_configurables() -> (u8, bool, [u32; 3], str[4], StructWithGeneric<u8>);
}

return if true_bool { some_num } else { 0 };
impl TestContract for Contract {
// ANCHOR: using_configurables
fn return_configurables() -> (u8, bool, [u32; 3], str[4], StructWithGeneric<u8>) {
(U8, BOOL, ARRAY, STR_4, STRUCT)
}
// ANCHOR_END: using_configurables
}
2 changes: 1 addition & 1 deletion templates/sway-test-rs/template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["{{authors}}"]
license = "Apache-2.0"

[dev-dependencies]
fuels = { version = "0.35", features = ["fuel-core-lib"] }
fuels = { version = "0.37", features = ["fuel-core-lib"] }
tokio = { version = "1.12", features = ["rt", "macros"] }

[[test]]
Expand Down
34 changes: 17 additions & 17 deletions test/src/sdk-harness/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/src/sdk-harness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fuel-core = { version = "0.17", default-features = false }
fuel-core-client = { version = "0.17", default-features = false }
fuel-types = "0.26"
fuel-vm = "0.26"
fuels = { version = "0.36", features = ["fuel-core-lib"] }
fuels = { version = "0.37", features = ["fuel-core-lib"] }
hex = "0.4.3"
rand = "0.8"
sha2 = "0.10"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'configurables_in_contract'
source = 'member'
dependencies = ['std']

[[package]]
name = 'core'
source = 'path+from-root-3A783B9109EEADE0'

[[package]]
name = 'std'
source = 'path+from-root-3A783B9109EEADE0'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "configurables_in_contract"

[dependencies]
std = { path = "../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use fuels::{prelude::*, types::SizedAsciiString};

#[tokio::test]
async fn contract_uses_default_configurables() -> Result<()> {
abigen!(Contract(
name = "MyContract",
abi =
"test_projects/configurables_in_contract/out/debug/configurables_in_contract-abi.json"
));

let wallet = launch_provider_and_get_wallet().await;

let contract_id = Contract::deploy(
"test_projects/configurables_in_contract/out/debug/configurables_in_contract.bin",
&wallet,
TxParameters::default(),
StorageConfiguration::default(),
)
.await?;

let contract_instance = MyContract::new(contract_id, wallet.clone());

let response = contract_instance
.methods()
.return_configurables()
.call()
.await?;

let expected_value = (
8u8,
true,
[253u32, 254u32, 255u32],
"fuel".try_into()?,
StructWithGeneric {
field_1: 8u8,
field_2: 16,
},
EnumWithGeneric::VariantOne(true),
);

assert_eq!(response.value, expected_value);

Ok(())
}

#[tokio::test]
async fn contract_configurables() -> Result<()> {
abigen!(Contract(
name = "MyContract",
abi =
"test_projects/configurables_in_contract/out/debug/configurables_in_contract-abi.json"
));

let wallet = launch_provider_and_get_wallet().await;

let new_str: SizedAsciiString<4> = "FUEL".try_into()?;
let new_struct = StructWithGeneric {
field_1: 16u8,
field_2: 32,
};
let new_enum = EnumWithGeneric::VariantTwo;

let configurables = MyContractConfigurables::new()
.set_STR_4(new_str.clone())
.set_STRUCT(new_struct.clone())
.set_ENUM(new_enum.clone());

let contract_id = Contract::deploy_with_parameters(
"test_projects/configurables_in_contract/out/debug/configurables_in_contract.bin",
&wallet,
TxParameters::default(),
StorageConfiguration::default(),
configurables.into(),
Salt::default(),
)
.await?;

let contract_instance = MyContract::new(contract_id, wallet.clone());

let response = contract_instance
.methods()
.return_configurables()
.call()
.await?;

let expected_value = (
8u8,
true,
[253u32, 254u32, 255u32],
new_str,
new_struct,
new_enum,
);

assert_eq!(response.value, expected_value);

Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
contract;

enum EnumWithGeneric<D> {
VariantOne: D,
VariantTwo: (),
}

struct StructWithGeneric<D> {
field_1: D,
field_2: u64,
}

configurable {
U8: u8 = 8u8,
BOOL: bool = true,
ARRAY: [u32; 3] = [253u32, 254u32, 255u32],
STR_4: str[4] = "fuel",
STRUCT: StructWithGeneric<u8> = StructWithGeneric {
field_1: 8u8,
field_2: 16,
},
ENUM: EnumWithGeneric<bool> = EnumWithGeneric::VariantOne(true),
}

abi TestContract {
fn return_configurables() -> (u8, bool, [u32; 3], str[4], StructWithGeneric<u8>, EnumWithGeneric<bool>);
}

impl TestContract for Contract {
fn return_configurables( ) -> (u8, bool, [u32; 3], str[4], StructWithGeneric<u8>, EnumWithGeneric<bool>) {
(U8, BOOL, ARRAY, STR_4, STRUCT, ENUM)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'configurables_in_script'
source = 'member'
dependencies = ['std']

[[package]]
name = 'core'
source = 'path+from-root-6F15923604F9775D'

[[package]]
name = 'std'
source = 'path+from-root-6F15923604F9775D'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "configurables_in_script"

[dependencies]
std = { path = "../../../../../sway-lib-std" }
Loading

0 comments on commit b33463d

Please sign in to comment.