Skip to content
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

Remove _with_configurables functions from Bytecode Library and use Option instead #285

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Something new here 1
- Something new here 2
- [#285](https://github.com/FuelLabs/sway-libs/pull/285) Adds the `BytecodeRoot` and `ContractConfigurables` types to the Bytecode Library.

### Changed

Expand All @@ -24,8 +23,49 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Breaking

- Some breaking change here 1
- Some breaking change here 2
- [#285](https://github.com/FuelLabs/sway-libs/pull/285) Removes `_with_configurables()` functions from Bytecode Library in favor of using an `Option`.

The following demonstrates the breaking change.

Before:

```sway
// Compute bytecode root
let root_no_configurables: BytecodeRoot = compute_bytecode_root(my_bytecode);
let root_with_configurables: BytecodeRoot = compute_bytecode_root_with_configurables(my_bytecode, my_configurables);

// Compute predicate address
let address_no_configurables: Address = compute_predicate_address(my_bytecode);
let address_with_configurables: Address = compute_predicate_address_with_configurables(my_bytecode, my_configurables);

// Verify contract bytecode
verify_contract_bytecode(my_contract_id, my_bytecode); // No configurables
verify_contract_bytecode_with_configurables(my_contract_id, my_bytecode, my_configurables); // With configurables

// Verify predicate address
verify_predicate_address(my_predicate_address, my_bytecode); // No configurables
verify_predicate_address_with_configurables(my_predicate_address, my_bytecode, my_configurables); // With configurables
```

After:

```sway
// Compute bytecode root
let root_no_configurables: BytecodeRoot = compute_bytecode_root(my_bytecode, None);
let root_with_configurables: BytecodeRoot = compute_bytecode_root(my_bytecode, Some(my_configurables));

// Compute predicate address
let address_no_configurables: Address = compute_predicate_address(my_bytecode, None);
let address_with_configurables: Address = compute_predicate_address(my_bytecode, Some(my_configurables));

// Verify contract bytecode
verify_contract_bytecode(my_contract_id, my_bytecode, None); // No configurables
verify_contract_bytecode(my_contract_id, my_bytecode, Some(my_configurables)); // With configurables

// Verify predicate address
verify_predicate_address(my_predicate_address, my_bytecode, None); // No configurables
verify_predicate_address(my_predicate_address, my_bytecode, Some(my_configurables)); // With configurables
```

## [v0.23.1]

Expand Down
12 changes: 4 additions & 8 deletions docs/book/src/bytecode/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ To import the Bytecode Library to your Sway Smart Contract, add the following to
Once imported, using the Bytecode Library is as simple as calling the desired function. Here is a list of function definitions that you may use.

- `compute_bytecode_root()`
- `compute_bytecode_root_with_configurables()`
- `compute_predicate_address()`
- `compute_predicate_address_with_configurables()`
- `predicate_address_from_root()`
- `swap_configurables()`
- `verify_contract_bytecode()`
- `verify_contract_bytecode_with_configurables()`
- `verify_predicate_address()`
- `verify_predicate_address_with_configurables()`

## Known Issues

Expand All @@ -55,15 +51,15 @@ Given some bytecode, you may swap the configurables of both Contracts and Predic

### Computing the Bytecode Root

To compute a contract's bytecode root you may call the `compute_bytecode_root()` or `compute_bytecode_root_with_configurables()` functions.
To compute a contract's bytecode root you may call the `compute_bytecode_root()` function.

```sway
{{#include ../../../../examples/bytecode/src/main.sw:compute_bytecode_root}}
```

### Verifying a Contract's Bytecode Root

To verify a contract's bytecode root you may call `verify_bytecode_root()` or `verify_contract_bytecode_with_configurables()` functions.
To verify a contract's bytecode root you may call `verify_bytecode_root()` function.

```sway
{{#include ../../../../examples/bytecode/src/main.sw:verify_contract_bytecode}}
Expand All @@ -73,7 +69,7 @@ To verify a contract's bytecode root you may call `verify_bytecode_root()` or `v

### Computing the Address from Bytecode

To compute a predicates's address you may call the `compute_predicate_address()` or `compute_predicate_address_with_configurables()` functions.
To compute a predicates's address you may call the `compute_predicate_address()` function.

```sway
{{#include ../../../../examples/bytecode/src/main.sw:compute_predicate_address}}
Expand All @@ -89,7 +85,7 @@ If you have the root of a predicate, you may compute it's corresponding predicat

### Verifying the Address

To verify a predicates's address you may call `verify_predicate_address()` or `verify_predicate_address_with_configurables()` functions.
To verify a predicates's address you may call `verify_predicate_address()` function.

```sway
{{#include ../../../../examples/bytecode/src/main.sw:verify_predicate_address}}
Expand Down
46 changes: 17 additions & 29 deletions examples/bytecode/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -30,63 +30,51 @@ fn swap(my_bytecode: Vec<u8>, my_configurables: Vec<(u64, Vec<u8>)>) {
// ANCHOR_END: swap_configurables

// ANCHOR: compute_bytecode_root
fn compute_bytecode(my_bytecode: Vec<u8>) {
let root: b256 = compute_bytecode_root(my_bytecode);
}

fn compute_bytecode_configurables(my_bytecode: Vec<u8>, my_configurables: Vec<(u64, Vec<u8>)>) {
fn compute_bytecode(
my_bytecode: Vec<u8>,
my_configurables: Option<ContractConfigurables>,
) {
let mut my_bytecode = my_bytecode;
let root: b256 = compute_bytecode_root_with_configurables(my_bytecode, my_configurables);
let root: BytecodeRoot = compute_bytecode_root(my_bytecode, my_configurables);
}
// ANCHOR_END: compute_bytecode_root

// ANCHOR: verify_contract_bytecode
fn verify_contract(my_contract: ContractId, my_bytecode: Vec<u8>) {
verify_contract_bytecode(my_contract, my_bytecode);
// By reaching this line the contract has been verified to match the bytecode provided.
}

fn verify_contract_configurables(
fn verify_contract(
my_contract: ContractId,
my_bytecode: Vec<u8>,
my_configurables: Vec<(u64, Vec<u8>)>,
my_configurables: Option<ContractConfigurables>,
) {
let mut my_bytecode = my_bytecode;
verify_contract_bytecode_with_configurables(my_contract, my_bytecode, my_configurables);
verify_contract_bytecode(my_contract, my_bytecode, my_configurables);
// By reaching this line the contract has been verified to match the bytecode provided.
}
// ANCHOR_END: verify_contract_bytecode

// ANCHOR: compute_predicate_address
fn compute_predicate(my_bytecode: Vec<u8>) {
let address: Address = compute_predicate_address(my_bytecode);
}

fn compute_predicate_configurables(my_bytecode: Vec<u8>, my_configurables: Vec<(u64, Vec<u8>)>) {
fn compute_predicate(
my_bytecode: Vec<u8>,
my_configurables: Option<ContractConfigurables>,
) {
let mut my_bytecode = my_bytecode;
let address: Address = compute_predicate_address_with_configurables(my_bytecode, my_configurables);
let address: Address = compute_predicate_address(my_bytecode, my_configurables);
}
// ANCHOR_END: compute_predicate_address

// ANCHOR: predicate_address_from_root
fn predicate_address(my_root: b256) {
fn predicate_address(my_root: BytecodeRoot) {
let address: Address = predicate_address_from_root(my_root);
}
// ANCHOR_END: predicate_address_from_root

// ANCHOR: verify_predicate_address
fn verify_predicate(my_predicate: Address, my_bytecode: Vec<u8>) {
verify_predicate_address(my_predicate, my_bytecode);
// By reaching this line the predicate bytecode matches the address provided.
}

fn verify_predicate_configurables(
fn verify_predicate(
my_predicate: Address,
my_bytecode: Vec<u8>,
my_configurables: Vec<(u64, Vec<u8>)>,
my_configurables: Option<ContractConfigurables>,
) {
let mut my_bytecode = my_bytecode;
verify_predicate_address_with_configurables(my_predicate, my_bytecode, my_configurables);
verify_predicate_address(my_predicate, my_bytecode, my_configurables);
// By reaching this line the predicate bytecode matches the address provided.
}
// ANCHOR_END: verify_predicate_address
Loading
Loading