Skip to content

Commit

Permalink
feat!: upgrade forc@0.58.0 and remove V0 encoding (#2238)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate authored May 14, 2024
1 parent 0651a5f commit 64e9659
Show file tree
Hide file tree
Showing 120 changed files with 475 additions and 4,782 deletions.
11 changes: 11 additions & 0 deletions .changeset/twelve-brooms-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@fuel-ts/transactions": patch
"@fuel-ts/abi-coder": minor
"@fuel-ts/versions": patch
"@fuel-ts/account": minor
"@fuel-ts/program": patch
"@fuel-ts/script": minor
"@fuel-ts/forc": patch
---

feat!: upgrade `forc@0.58.0` and remove `V0` encoding
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ describe('Transaction Request', () => {
// #endregion transaction-request-7

expect(transactionId).toBe(
'0x20995f5333a9cd4d7acfab212ee723faec022fd0bd17b7778d552f624ea55d8e'
'0xb82d9e3da13a1b56b623238c0d7a7ab4d602f2c7f886bf7a9c05ecfb99267ce3'
);
});
});
6 changes: 1 addition & 5 deletions apps/docs/src/guide/types/vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,4 @@ Some functions require you to pass in bytecode to the function. The type of the

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:

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

## Returning vectors

Currently, returning vectors is not supported by Sway. If you try returning a type that is or contains a Vector, you will get a compile-time error.
<<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-bytecode-input-ts{ts:line-numbers}
2 changes: 1 addition & 1 deletion packages/abi-coder/src/AbiCoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export abstract class AbiCoder {
abi: JsonAbi,
argument: JsonAbiArgument,
options: EncodingOptions = {
isSmallBytes: false,
padToWordSize: false,
}
): Coder {
const resolvedAbiType = new ResolvedAbiType(abi, argument);
Expand Down
58 changes: 5 additions & 53 deletions packages/abi-coder/src/FunctionFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,22 @@ import { arrayify } from '@fuel-ts/utils';
import { AbiCoder } from './AbiCoder';
import { ResolvedAbiType } from './ResolvedAbiType';
import type { DecodedValue, InputValue } from './encoding/coders/AbstractCoder';
import { ByteCoder } from './encoding/coders/v0/ByteCoder';
import { TupleCoder } from './encoding/coders/v0/TupleCoder';
import { VecCoder } from './encoding/coders/v0/VecCoder';
import { StdStringCoder } from './encoding/coders/v1/StdStringCoder';
import { TupleCoder as TupleCoderV1 } from './encoding/coders/v1/TupleCoder';
import { StdStringCoder } from './encoding/coders/StdStringCoder';
import { TupleCoder } from './encoding/coders/TupleCoder';
import type {
JsonAbi,
JsonAbiArgument,
JsonAbiFunction,
JsonAbiFunctionAttribute,
} from './types/JsonAbi';
import type { EncodingVersion } from './utils/constants';
import { ENCODING_V1, OPTION_CODER_TYPE } from './utils/constants';
import { OPTION_CODER_TYPE } from './utils/constants';
import {
findFunctionByName,
findNonEmptyInputs,
findTypeById,
getEncodingVersion,
} from './utils/json-abi';
import type { Uint8ArrayWithDynamicData } from './utils/utilities';
import { isHeapType, isPointerType, unpackDynamicData } from './utils/utilities';

export class FunctionFragment<
TAbi extends JsonAbi = JsonAbi,
Expand All @@ -41,11 +36,6 @@ export class FunctionFragment<
readonly name: string;
readonly jsonFn: JsonAbiFunction;
readonly attributes: readonly JsonAbiFunctionAttribute[];
readonly isInputDataPointer: boolean;
readonly outputMetadata: {
isHeapType: boolean;
encodedLength: number;
};

private readonly jsonAbi: JsonAbi;

Expand All @@ -58,11 +48,6 @@ export class FunctionFragment<
this.selector = FunctionFragment.getFunctionSelector(this.signature);
this.selectorBytes = new StdStringCoder().encode(name);
this.encoding = getEncodingVersion(jsonAbi.encoding);
this.isInputDataPointer = this.#isInputDataPointer();
this.outputMetadata = {
isHeapType: this.#isOutputDataHeap(),
encodedLength: this.#getOutputEncodedLength(),
};

this.attributes = this.jsonFn.attributes ?? [];
}
Expand All @@ -80,35 +65,7 @@ export class FunctionFragment<
return bn(hashedFunctionSignature.slice(0, 10)).toHex(8);
}

#isInputDataPointer(): boolean {
const inputTypes = this.jsonFn.inputs.map((i) => findTypeById(this.jsonAbi, i.type));

return this.jsonFn.inputs.length > 1 || isPointerType(inputTypes[0]?.type || '');
}

#isOutputDataHeap(): boolean {
const outputType = findTypeById(this.jsonAbi, this.jsonFn.output.type);

return isHeapType(outputType?.type || '');
}

#getOutputEncodedLength(): number {
try {
const heapCoder = AbiCoder.getCoder(this.jsonAbi, this.jsonFn.output);
if (heapCoder instanceof VecCoder) {
return heapCoder.coder.encodedLength;
}
if (heapCoder instanceof ByteCoder) {
return ByteCoder.memorySize;
}

return heapCoder.encodedLength;
} catch (e) {
return 0;
}
}

encodeArguments(values: InputValue[], offset = 0): Uint8Array {
encodeArguments(values: InputValue[]): Uint8Array {
FunctionFragment.verifyArgsAndInputsAlign(values, this.jsonFn.inputs, this.jsonAbi);

const shallowCopyValues = values.slice();
Expand All @@ -121,16 +78,11 @@ export class FunctionFragment<

const coders = nonEmptyInputs.map((t) =>
AbiCoder.getCoder(this.jsonAbi, t, {
isRightPadded: nonEmptyInputs.length > 1,
encoding: this.encoding,
})
);

if (this.encoding === ENCODING_V1) {
return new TupleCoderV1(coders).encode(shallowCopyValues);
}
const results: Uint8ArrayWithDynamicData = new TupleCoder(coders).encode(shallowCopyValues);
return unpackDynamicData(results, offset, results.byteLength);
return new TupleCoder(coders).encode(shallowCopyValues);
}

private static verifyArgsAndInputsAlign(
Expand Down
11 changes: 4 additions & 7 deletions packages/abi-coder/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AbiCoder } from './AbiCoder';
import { FunctionFragment } from './FunctionFragment';
import type { InputValue } from './encoding/coders/AbstractCoder';
import type { JsonAbi, JsonAbiConfigurable } from './types/JsonAbi';
import { ENCODING_V0, type EncodingVersion } from './utils/constants';
import { type EncodingVersion } from './utils/constants';
import { findTypeById, getEncodingVersion } from './utils/json-abi';

export class Interface<TAbi extends JsonAbi = JsonAbi> {
Expand Down Expand Up @@ -58,13 +58,12 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {

encodeFunctionData(
functionFragment: FunctionFragment | string,
values: Array<InputValue>,
offset = 0
values: Array<InputValue>
): Uint8Array {
const fragment =
typeof functionFragment === 'string' ? this.getFunction(functionFragment) : functionFragment;

return fragment.encodeArguments(values, offset);
return fragment.encodeArguments(values);
}

// Decode the result of a function call
Expand Down Expand Up @@ -99,9 +98,7 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {
}

return AbiCoder.encode(this.jsonAbi, configurable.configurableType, value, {
isRightPadded: true,
// TODO: Review support for configurables in v1 encoding when it becomes available
encoding: ENCODING_V0,
encoding: this.encoding,
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/abi-coder/src/encoding/coders/AbstractCoder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BytesLike } from '@fuel-ts/interfaces';
import type { BN } from '@fuel-ts/math';

import type { Option } from './v0/OptionCoder';
import type { Option } from './OptionCoder';

type Primitive = string | number | boolean;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { FuelError, ErrorCode } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';

import { U32_MAX, U8_MAX } from '../../../../test/utils/constants';
import type { EncodingOptions } from '../../../types/EncodingOptions';
import { U32_MAX, U8_MAX } from '../../../test/utils/constants';

import { ArrayCoder } from './ArrayCoder';
import { BooleanCoder } from './BooleanCoder';
Expand All @@ -13,20 +12,16 @@ import { NumberCoder } from './NumberCoder';
* @group node
*/
describe('ArrayCoder', () => {
const options: EncodingOptions = {
isSmallBytes: true,
};

it('should encode a number array with zero inputs', () => {
const coder = new ArrayCoder(new NumberCoder('u8', options), 0);
const coder = new ArrayCoder(new NumberCoder('u8'), 0);
const expected = new Uint8Array([]);
const actual = coder.encode([]);

expect(actual).toStrictEqual(expected);
});

it('should decode a number array with zero inputs', () => {
const coder = new ArrayCoder(new NumberCoder('u8', options), 0);
const coder = new ArrayCoder(new NumberCoder('u8'), 0);
const expectedValue: number[] = [];
const expectedLength = 0;
const [actualValue, actualLength] = coder.decode(new Uint8Array([]), 0);
Expand All @@ -36,15 +31,15 @@ describe('ArrayCoder', () => {
});

it('should encode a number array with four inputs', () => {
const coder = new ArrayCoder(new NumberCoder('u8', options), 4);
const coder = new ArrayCoder(new NumberCoder('u8'), 4);
const array = [0, 13, 37, U8_MAX];
const expected = new Uint8Array(array);
const actual = coder.encode(array);
expect(actual).toStrictEqual(expected);
});

it('should decode a number array with four inputs', () => {
const coder = new ArrayCoder(new NumberCoder('u8', options), 4);
const coder = new ArrayCoder(new NumberCoder('u8'), 4);
const expectedValue = [0, 13, 37, U8_MAX];
const expectedLength = expectedValue.length;
const [actualValue, actualLength] = coder.decode(new Uint8Array(expectedValue), 0);
Expand All @@ -56,8 +51,8 @@ describe('ArrayCoder', () => {
it('should encode an enum array with differently typed inputs', () => {
const coder = new ArrayCoder(
new EnumCoder('TestEnum', {
a: new NumberCoder('u8', options),
b: new BooleanCoder(options),
a: new NumberCoder('u8'),
b: new BooleanCoder(),
}),
4
);
Expand All @@ -73,8 +68,8 @@ describe('ArrayCoder', () => {
it('should decode an enum array with differently typed inputs', () => {
const coder = new ArrayCoder(
new EnumCoder('TestEnum', {
a: new NumberCoder('u8', options),
b: new BooleanCoder(options),
a: new NumberCoder('u8'),
b: new BooleanCoder(),
}),
4
);
Expand All @@ -93,7 +88,7 @@ describe('ArrayCoder', () => {
});

it('should throw when value to encode is not array', async () => {
const coder = new ArrayCoder(new NumberCoder('u8', options), 1);
const coder = new ArrayCoder(new NumberCoder('u8'), 1);
const nonArrayInput = { ...[1] };
await expectToThrowFuelError(
() => coder.encode(nonArrayInput),
Expand All @@ -102,7 +97,7 @@ describe('ArrayCoder', () => {
});

it('should throw when coder length is not match inputted array length', async () => {
const coder = new ArrayCoder(new NumberCoder('u8', options), 1);
const coder = new ArrayCoder(new NumberCoder('u8'), 1);
await expectToThrowFuelError(
() => coder.encode([1, 2]),
new FuelError(ErrorCode.ENCODE_ERROR, 'Types/values length mismatch.')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { concat } from '@fuel-ts/utils';

import { MAX_BYTES } from '../../../utils/constants';
import { concatWithDynamicData } from '../../../utils/utilities';
import type { TypesOfCoder } from '../AbstractCoder';
import { Coder } from '../AbstractCoder';
import { MAX_BYTES } from '../../utils/constants';

import type { TypesOfCoder } from './AbstractCoder';
import { Coder } from './AbstractCoder';

type InputValueOf<TCoder extends Coder> = Array<TypesOfCoder<TCoder>['Input']>;
type DecodedValueOf<TCoder extends Coder> = Array<TypesOfCoder<TCoder>['Decoded']>;
Expand All @@ -30,7 +31,7 @@ export class ArrayCoder<TCoder extends Coder> extends Coder<
throw new FuelError(ErrorCode.ENCODE_ERROR, `Types/values length mismatch.`);
}

return concatWithDynamicData(Array.from(value).map((v) => this.coder.encode(v)));
return concat(Array.from(value).map((v) => this.coder.encode(v)));
}

decode(data: Uint8Array, offset: number): [DecodedValueOf<TCoder>, number] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn, toHex } from '@fuel-ts/math';
import { arrayify } from '@fuel-ts/utils';

import { WORD_SIZE } from '../../../utils/constants';
import { Coder } from '../AbstractCoder';
import { WORD_SIZE } from '../../utils/constants';

import { Coder } from './AbstractCoder';

export class B256Coder extends Coder<string, string> {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn, toHex } from '@fuel-ts/math';
import { arrayify } from '@fuel-ts/utils';

import { WORD_SIZE } from '../../../utils/constants';
import { Coder } from '../AbstractCoder';
import { WORD_SIZE } from '../../utils/constants';

import { Coder } from './AbstractCoder';

export class B512Coder extends Coder<string, string> {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
import { BN, bn } from '@fuel-ts/math';

import { U16_MAX, U256_MAX, U32_MAX, U64_MAX, U8_MAX } from '../../../../test/utils/constants';
import { U16_MAX, U256_MAX, U32_MAX, U64_MAX, U8_MAX } from '../../../test/utils/constants';

import { BigNumberCoder } from './BigNumberCoder';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { type BNInput, type BN, toBytes, bn } from '@fuel-ts/math';

import { WORD_SIZE } from '../../../utils/constants';
import { Coder } from '../AbstractCoder';
import { WORD_SIZE } from '../../utils/constants';

import { Coder } from './AbstractCoder';

type BigNumberCoderType = 'u64' | 'u256';

Expand Down
Loading

0 comments on commit 64e9659

Please sign in to comment.