Skip to content

Commit

Permalink
Remove _with_configurables functions from Bytecode Library and use …
Browse files Browse the repository at this point in the history
…`Option` instead (#285)

## Type of change

<!--Delete points that do not apply-->

- Improvement (refactoring, restructuring repository, cleaning tech
debt, ...)
- Breaking

## Breaking

The following demonstrates the breaking change:

Before:
```sway
fn foo(my_bytecode: Vec<u8>, my_configurables: ContractConfigurables, my_contract_id: ContractId, my_predicate_address: Address) {
     // 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
fn foo(my_bytecode: Vec<u8>, my_configurables: ContractConfigurables, my_contract_id: ContractId, my_predicate_address: Address) {
     // 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
}
```

## Changes

The following changes have been made:

- `compute_bytecode_root_with_configurables()` has been removed in place
of `compute_bytecode_root()` with an `Option` argument for configurables
- `compute_predicate_address_with_configurables()` has been removed in
place of `compute_predicate_address()` with an `Option` argument for
configurables
- `verify_contract_bytecode_with_configurables()` has been removed in
place of `verify_contract_bytecode()` with an `Option` argument for
configurables
- `verify_predicate_address_with_configurables()` has been removed in
place of `verify_predicate_address()` with an `Option` argument for
configurables
- The `BytecodeRoot` type has been added
- The `ContractConfigurables` type has been added

## Related Issues

<!--Delete everything after the "#" symbol and replace it with a number.
No spaces between hash and number-->

Closes #219

## 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).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [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.
- [x] I have updated the changelog to reflect the changes on this PR.
  • Loading branch information
bitzoic authored Aug 28, 2024
1 parent 847c0d3 commit afb9fcd
Show file tree
Hide file tree
Showing 16 changed files with 550 additions and 769 deletions.
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

0 comments on commit afb9fcd

Please sign in to comment.