Skip to content

Commit

Permalink
Revert remove _with_configurables functions from Bytecode Library and…
Browse files Browse the repository at this point in the history
… use Option instead (#293)

## Type of change

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

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

## Changes

The following changes have been made:

- Removes the breaking changes from
#285
- Leaves the `BytecodeRoot` and `Contractconfigurables` types

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] 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)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] 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).
- [ ] I have requested a review from the relevant team or maintainers.
- [ ] I have updated the changelog to reflect the changes on this PR.
  • Loading branch information
bitzoic authored Aug 30, 2024
1 parent 74ab168 commit 93cfa35
Show file tree
Hide file tree
Showing 15 changed files with 761 additions and 534 deletions.
46 changes: 1 addition & 45 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Description of the upcoming release here.

### Added v0.24.0

- [#285](https://github.com/FuelLabs/sway-libs/pull/285) Adds the `BytecodeRoot` and `ContractConfigurables` types to the Bytecode Library.
- [#293](https://github.com/FuelLabs/sway-libs/pull/293) Adds the `BytecodeRoot` and `ContractConfigurables` types to the Bytecode Library.
- [#286](https://github.com/FuelLabs/sway-libs/pull/286) Adds the `_metadata()` function to the Asset Library.

### Changed v0.24.0
Expand All @@ -48,50 +48,6 @@ Description of the upcoming release here.

### Breaking v0.24.0

- [#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
```

- [#286](https://github.com/FuelLabs/sway-libs/pull/286) The support functions for `Metadata` have been removed. They have been moved to sway-standards.

Before:
Expand Down
12 changes: 8 additions & 4 deletions docs/book/src/bytecode/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ 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 @@ -51,15 +55,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()` function.
To compute a contract's bytecode root you may call the `compute_bytecode_root()` or `compute_bytecode_root_with_configurables()` functions.

```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()` function.
To verify a contract's bytecode root you may call `verify_bytecode_root()` or `verify_contract_bytecode_with_configurables()` functions.

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

### Computing the Address from Bytecode

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

```sway
{{#include ../../../../examples/bytecode/src/main.sw:compute_predicate_address}}
Expand All @@ -85,7 +89,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()` function.
To verify a predicates's address you may call `verify_predicate_address()` or `verify_predicate_address_with_configurables()` functions.

```sway
{{#include ../../../../examples/bytecode/src/main.sw:verify_predicate_address}}
Expand Down
47 changes: 34 additions & 13 deletions examples/bytecode/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,57 @@ fn make_mutable(not_mutable_bytecode: Vec<u8>) {
// ANCHOR_END: known_issue

// ANCHOR: swap_configurables
fn swap(my_bytecode: Vec<u8>, my_configurables: Vec<(u64, Vec<u8>)>) {
fn swap(
my_bytecode: Vec<u8>,
my_configurables: ContractConfigurables,
) {
let mut my_bytecode = my_bytecode;
let resulting_bytecode: Vec<u8> = swap_configurables(my_bytecode, my_configurables);
}
// ANCHOR_END: swap_configurables

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

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

// ANCHOR: verify_contract_bytecode
fn verify_contract(
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(
my_contract: ContractId,
my_bytecode: Vec<u8>,
my_configurables: Option<ContractConfigurables>,
my_configurables: ContractConfigurables,
) {
let mut my_bytecode = my_bytecode;
verify_contract_bytecode(my_contract, my_bytecode, my_configurables);
verify_contract_bytecode_with_configurables(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(
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: Option<ContractConfigurables>,
my_configurables: ContractConfigurables,
) {
let mut my_bytecode = my_bytecode;
let address: Address = compute_predicate_address(my_bytecode, my_configurables);
let address: Address = compute_predicate_address_with_configurables(my_bytecode, my_configurables);
}
// ANCHOR_END: compute_predicate_address

Expand All @@ -68,13 +84,18 @@ fn predicate_address(my_root: BytecodeRoot) {
// ANCHOR_END: predicate_address_from_root

// ANCHOR: verify_predicate_address
fn verify_predicate(
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(
my_predicate: Address,
my_bytecode: Vec<u8>,
my_configurables: Option<ContractConfigurables>,
my_configurables: ContractConfigurables,
) {
let mut my_bytecode = my_bytecode;
verify_predicate_address(my_predicate, my_bytecode, my_configurables);
verify_predicate_address_with_configurables(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 93cfa35

Please sign in to comment.