diff --git a/.changeset/rotten-bananas-explode.md b/.changeset/rotten-bananas-explode.md new file mode 100644 index 0000000000..a845151cc8 --- /dev/null +++ b/.changeset/rotten-bananas-explode.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/apps/docs-snippets/src/guide/types/vector.test.ts b/apps/docs-snippets/src/guide/types/vector.test.ts index febc7b1c04..c5e10928f2 100644 --- a/apps/docs-snippets/src/guide/types/vector.test.ts +++ b/apps/docs-snippets/src/guide/types/vector.test.ts @@ -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 } + ); }); diff --git a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml index 416c953741..f6e52ea153 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml +++ b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml @@ -3,8 +3,4 @@ authors = ["Fuel Labs "] 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] diff --git a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw index 601d0cc508..82ac781982 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw +++ b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw @@ -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) -> b256; - fn compute_bytecode_root(bytecode_input: Vec) -> bool; + fn compute_bytecode_root(bytecode_input: Vec) -> b256; } impl MyContract for Contract { // #region vector-bytecode-input-sway - fn compute_bytecode_root(bytecode_input: Vec) -> bool { - // let root = compute_bytecode_root(bytecode_input); - // return root; - return true; + fn compute_bytecode_root(bytecode_input: Vec) -> b256 { + //simply return a fixed b256 value created from a hexadecimal string from testing purposes + return 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; } // #endregion vector-bytecode-input-sway } diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index 19f771b943..9c2f974f0f 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -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`. +Some functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec`, 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. - - - - - -```rust -contract; - -use bytecode::*; - -abi MyContract { - fn compute_bytecode_root(bytecode_input: Vec) -> b256; -} - -impl MyContract for Contract { - fn compute_bytecode_root(bytecode_input: Vec) -> 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` type and pass it the function like so: diff --git a/packages/fuel-gauge/src/bytecode-sway-lib.test.ts b/packages/fuel-gauge/src/bytecode-sway-lib.test.ts index 7129afccf4..4621d0b293 100644 --- a/packages/fuel-gauge/src/bytecode-sway-lib.test.ts +++ b/packages/fuel-gauge/src/bytecode-sway-lib.test.ts @@ -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 @@ -42,7 +41,7 @@ describe.skip('bytecode computations', () => { { bits: contract.id.toB256(), }, - arrayify(bytecodeFromFile) + Array.from(arrayify(bytecodeFromFile)) ) .call(); @@ -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()); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml index 3f1ac99ca7..b4a52117f8 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml @@ -3,8 +3,4 @@ authors = ["Fuel Labs "] 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] diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw index c9ff9034fa..09d33c9eaf 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw @@ -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); fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) -> bool; - // fn compute_predicate_address(bytecode: Vec) -> Address; - fn compute_predicate_address(bytecode: Vec) -> bool; + fn compute_predicate_address(bytecode: Vec) -> Address; } impl MyContract for Contract { fn compute_bytecode_root(bytecode_input: Vec) { - // 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) -> bool { - // verify_contract_bytecode(contract_id, bytecode); return true; } - fn compute_predicate_address(bytecode: Vec) -> bool { - // return compute_predicate_address(bytecode); - return true; + fn compute_predicate_address(bytecode: Vec) -> Address { + return Address::from(0x6b6ef590390f0a7de75f8275ab5d7877c17236caba2514039c6565ec15f79111); } }