Skip to content

Commit

Permalink
chore: remove sway-libs dep from testing fixtures (#2189)
Browse files Browse the repository at this point in the history
* test: mock the compute_bytecode functions in the sway contracts

* doc: add changeset

* build: forc format

* wip

* test: update vector test

* test: updated tests

* test: increase test timeout

* update Forc and tests

* test: remove unnecessary array conversion

* docs: update docs example

* test: update forc formatting issue
  • Loading branch information
maschad committed May 14, 2024
1 parent 667ba0f commit 054c1dc
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 82 deletions.
2 changes: 2 additions & 0 deletions .changeset/rotten-bananas-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
42 changes: 23 additions & 19 deletions apps/docs-snippets/src/guide/types/vector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,31 @@ describe(__filename, () => {
expect(value.isActive).toEqual(employees[1].isActive);
});

// TODO: Unskip test after sway-libs become compatible with latest forc (0.52+)
it.skip('should successfully execute a contract call with a bytecode input', async () => {
const bytecodeContract = await createAndDeployContractFromProject(
DocSnippetProjectsEnum.BYTECODE_INPUT
);
const bytecodePath = join(
__dirname,
'../../../test/fixtures/forc-projects/bytecode-input/out/release/bytecode-input.bin'
);
it(
'should successfully execute a contract call with a bytecode input',
async () => {
const bytecodeContract = await createAndDeployContractFromProject(
DocSnippetProjectsEnum.BYTECODE_INPUT
);
const bytecodePath = join(
__dirname,
'../../../test/fixtures/forc-projects/bytecode-input/out/release/bytecode-input.bin'
);

// #region vector-bytecode-input-ts
// #import { arrayify, readFile };
// #region vector-bytecode-input-ts
// #import { arrayify, readFile };

const bytecode = await readFile(bytecodePath);
const bytecode = await readFile(bytecodePath);
const bytecodeAsVecU8 = arrayify(bytecode);

const { value: bytecodeRoot } = await bytecodeContract.functions
.compute_bytecode_root(arrayify(bytecode))
.call();
// #endregion vector-bytecode-input-ts
const { value: bytecodeRoot } = await bytecodeContract.functions
.compute_bytecode_root(bytecodeAsVecU8)
.call();
// #endregion vector-bytecode-input-ts

expect(bytecodeRoot).toBeDefined();
expect(bytecodeRoot.length).toBe(66);
});
expect(bytecodeRoot).toBeDefined();
expect(bytecodeRoot.length).toBe(66);
},
{ timeout: 10000 }
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "bytecode-input"

# TODO: Uncomment bytecode-related stuff
# This requires sway-libs to be compatible with latest forc (0.52+)

# [dependencies]
# bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" }
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
contract;

// TODO: Uncomment bytecode-related stuff
// This requires sway-libs to be compatible with latest forc (0.52+)
// use bytecode::*;


abi MyContract {
// fn compute_bytecode_root(bytecode_input: Vec<u8>) -> b256;
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> bool;
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> b256;
}

impl MyContract for Contract {
// #region vector-bytecode-input-sway
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> bool {
// let root = compute_bytecode_root(bytecode_input);
// return root;
return true;
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> b256 {
//simply return a fixed b256 value created from a hexadecimal string from testing purposes
return 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20;
}
// #endregion vector-bytecode-input-sway
}
27 changes: 3 additions & 24 deletions apps/docs/src/guide/types/vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,11 @@ The code snippet below demonstrates how to call this Sway contract method, which

<<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-4{ts:line-numbers}

## Working with Bytecode in the SDK
## Converting Bytecode to Vectors

Some Sway functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec<u8>`.
Some functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec<u8>`, here's an example of how to pass bytecode to a function:

Take the `compute_bytecode_root` function from the [`bytecode` Sway library](https://github.com/FuelLabs/sway-libs/tree/master/libs/src/bytecode.sw), for example.

<!-- <<< @/../../docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers} -->

<!-- TODO: Uncomment swap hardcoded snippet -->

```rust
contract;

use bytecode::*;

abi MyContract {
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> b256;
}

impl MyContract for Contract {
fn compute_bytecode_root(bytecode_input: Vec<u8>) -> bool {
let root = compute_bytecode_root(bytecode_input);
return root;
}
}
```
<<< @/../../docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers}

To pass bytecode to this function, you can make use of the `arrayify` function to convert the bytecode file contents into a `UInt8Array`, the TS compatible type for Sway's `Vec<u8>` type and pass it the function like so:

Expand Down
7 changes: 3 additions & 4 deletions packages/fuel-gauge/src/bytecode-sway-lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { getSetupContract } from './utils';
/**
* @group node
*/
// TODO: Unskip test after sway-libs become compatible with latest forc (0.52+)
describe.skip('bytecode computations', () => {
describe('bytecode computations', () => {
test('compute_bytecode_root', async () => {
const { binHexlified: bytecodeFromFile } = getFuelGaugeForcProject(
FuelGaugeProjectsEnum.CALL_TEST_CONTRACT
Expand Down Expand Up @@ -42,7 +41,7 @@ describe.skip('bytecode computations', () => {
{
bits: contract.id.toB256(),
},
arrayify(bytecodeFromFile)
Array.from(arrayify(bytecodeFromFile))
)
.call();

Expand All @@ -64,7 +63,7 @@ describe.skip('bytecode computations', () => {
const contract = await setupContract();

const { value } = await contract.functions
.compute_predicate_address(arrayify(defaultPredicateBytecode))
.compute_predicate_address(Array.from(arrayify(defaultPredicateBytecode)))
.call();

expect(value.bits).toEqual(address.toB256());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "bytecode-sway-lib"

# TODO: Uncomment bytecode-related stuff
# This requires sway-libs to be compatible with latest forc (0.52+)

# [dependencies]
# bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" }
[dependencies]
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
contract;

// TODO: Uncomment bytecode-related stuff
// This requires sway-libs to be compatible with latest forc (0.52+)
// use bytecode::*;


abi MyContract {
fn compute_bytecode_root(bytecode_input: Vec<u8>);

fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec<u8>) -> bool;

// fn compute_predicate_address(bytecode: Vec<u8>) -> Address;
fn compute_predicate_address(bytecode: Vec<u8>) -> bool;
fn compute_predicate_address(bytecode: Vec<u8>) -> Address;
}

impl MyContract for Contract {
fn compute_bytecode_root(bytecode_input: Vec<u8>) {
// let mut bytecode = bytecode_input;
// let root = compute_bytecode_root(bytecode);
// log(root);
// simply logs the hexidecimal b256 string of the bytecode input for testing purposes
log(0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20);
}

fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec<u8>) -> bool {
// verify_contract_bytecode(contract_id, bytecode);
return true;
}

fn compute_predicate_address(bytecode: Vec<u8>) -> bool {
// return compute_predicate_address(bytecode);
return true;
fn compute_predicate_address(bytecode: Vec<u8>) -> Address {
return Address::from(0x6b6ef590390f0a7de75f8275ab5d7877c17236caba2514039c6565ec15f79111);
}
}

0 comments on commit 054c1dc

Please sign in to comment.