Skip to content

Commit

Permalink
Merge branch 'master' into st/fix/unset-cached-resource-for-failed-tx
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored Aug 9, 2024
2 parents 318fa60 + 1d2abd7 commit 6e6ba87
Show file tree
Hide file tree
Showing 27 changed files with 223 additions and 43 deletions.
4 changes: 4 additions & 0 deletions .changeset/real-crabs-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

ci: update retry and timeout on Transaction `SqueezedOut` test
6 changes: 6 additions & 0 deletions .changeset/stupid-planets-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/account": patch
"@fuel-ts/errors": patch
---

chore: add validation for TX max outputs exceeded
6 changes: 6 additions & 0 deletions .changeset/ten-otters-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/abi-typegen": patch
"@fuel-ts/utils": patch
---

feat: improve typegen bytecode compression
6 changes: 5 additions & 1 deletion apps/docs/src/guide/errors/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,8 @@ If you believe you found a bug, please report the [issue](https://github.com/Fue

### `MAX_INPUTS_EXCEEDED`

When the number of transaction inputs exceeds the maximum limit allowed by the blockchain.
When the number of transaction inputs exceeds the maximum limit allowed by the blockchain.

### `MAX_OUTPUTS_EXCEEDED`

When the number of transaction outputs exceeds the maximum limit allowed by the blockchain.
4 changes: 2 additions & 2 deletions packages/abi-typegen/src/templates/contract/factory.hbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{{header}}

import { ContractFactory } from "fuels";
import { ContractFactory, decompressBytecode } from "fuels";
import type { Provider, Account, DeployContractOptions, DeployContractResult } from "fuels";

import { {{capitalizedName}} } from "./{{capitalizedName}}";

const bytecode = "{{hexlifiedBinString}}";
const bytecode = decompressBytecode("{{compressedBytecode}}");

export class {{capitalizedName}}Factory extends ContractFactory {

Expand Down
3 changes: 3 additions & 0 deletions packages/abi-typegen/src/templates/contract/factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as utilsMod from '@fuel-ts/utils';
import { join } from 'path';

import {
Expand All @@ -24,6 +25,8 @@ describe('templates/factory', () => {
const project = getTypegenForcProject(AbiTypegenProjectsEnum.MINIMAL);
const rawContents = project.abiContents;

vi.spyOn(utilsMod, 'compressBytecode').mockReturnValueOnce('0x-bytecode-here');

// executing
const abi = new Abi({
filepath: './my-contract-abi.json',
Expand Down
4 changes: 3 additions & 1 deletion packages/abi-typegen/src/templates/contract/factory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { compressBytecode } from '@fuel-ts/utils';

import type { Abi } from '../../abi/Abi';
import { renderHbsTemplate } from '../renderHbsTemplate';

Expand All @@ -22,7 +24,7 @@ export function renderFactoryTemplate(params: { abi: Abi }) {
capitalizedName,
abiJsonString,
storageSlotsJsonString,
hexlifiedBinString,
compressedBytecode: compressBytecode(hexlifiedBinString),
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/templates/predicate/main.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type {{capitalizedName}}Parameters = Omit<

const abi = {{abiJsonString}};

const bytecode = '{{hexlifiedBinString}}';
const bytecode = decompressBytecode('{{compressedBytecode}}');

export class {{capitalizedName}} extends Predicate<
{{capitalizedName}}Inputs,
Expand Down
9 changes: 9 additions & 0 deletions packages/abi-typegen/src/templates/predicate/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { safeExec } from '@fuel-ts/errors/test-utils';
import * as utilsMod from '@fuel-ts/utils';
import { join } from 'path';

import {
Expand All @@ -18,7 +19,13 @@ import { renderMainTemplate } from './main';
* @group node
*/
describe('main.ts', () => {
afterEach(() => {
vi.restoreAllMocks();
});

test('should render main template', () => {
vi.spyOn(utilsMod, 'compressBytecode').mockReturnValueOnce('0x-bytecode-here');

const { restore } = mockVersions();

const project = getTypegenForcProject(AbiTypegenProjectsEnum.PREDICATE);
Expand Down Expand Up @@ -46,6 +53,8 @@ describe('main.ts', () => {
});

test('should render main template with configurable', () => {
vi.spyOn(utilsMod, 'compressBytecode').mockReturnValueOnce('0x-bytecode-here');

const { restore } = mockVersions();

const project = getTypegenForcProject(AbiTypegenProjectsEnum.PREDICATE_WITH_CONFIGURABLE);
Expand Down
5 changes: 3 additions & 2 deletions packages/abi-typegen/src/templates/predicate/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { compressBytecode } from '@fuel-ts/utils';

import type { Abi } from '../../abi/Abi';
import { renderHbsTemplate } from '../renderHbsTemplate';
Expand Down Expand Up @@ -32,7 +33,7 @@ export function renderMainTemplate(params: { abi: Abi }) {
const { structs } = formatStructs({ types });
const { imports } = formatImports({
types,
baseMembers: ['Predicate', 'Provider', 'InputValue', 'PredicateParams'],
baseMembers: ['Predicate', 'Provider', 'InputValue', 'PredicateParams', 'decompressBytecode'],
});

const { prefixedInputs: inputs, output } = func.attributes;
Expand All @@ -45,7 +46,7 @@ export function renderMainTemplate(params: { abi: Abi }) {
structs,
enums,
abiJsonString,
hexlifiedBinString,
compressedBytecode: compressBytecode(hexlifiedBinString),
capitalizedName,
imports,
configurables,
Expand Down
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/templates/script/main.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type {{capitalizedName}}Configurables = Partial<{

const abi = {{abiJsonString}};

const bytecode = '{{hexlifiedBinString}}';
const bytecode = decompressBytecode('{{compressedBytecode}}');

export class {{capitalizedName}} extends Script<{{capitalizedName}}Inputs, {{capitalizedName}}Output> {

Expand Down
9 changes: 9 additions & 0 deletions packages/abi-typegen/src/templates/script/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { safeExec } from '@fuel-ts/errors/test-utils';
import * as utilsMod from '@fuel-ts/utils';
import { join } from 'path';

import {
Expand All @@ -18,7 +19,13 @@ import { renderMainTemplate } from './main';
* @group node
*/
describe('main.ts', () => {
afterEach(() => {
vi.restoreAllMocks();
});

test('should render main template', () => {
vi.spyOn(utilsMod, 'compressBytecode').mockReturnValueOnce('0x-bytecode-here');

const { restore } = mockVersions();

const project = getTypegenForcProject(AbiTypegenProjectsEnum.SCRIPT);
Expand All @@ -45,6 +52,8 @@ describe('main.ts', () => {
});

test('should render main template with configurables', () => {
vi.spyOn(utilsMod, 'compressBytecode').mockReturnValueOnce('0x-bytecode-here');

const { restore } = mockVersions();

const project = getTypegenForcProject(AbiTypegenProjectsEnum.SCRIPT_WITH_CONFIGURABLE);
Expand Down
8 changes: 6 additions & 2 deletions packages/abi-typegen/src/templates/script/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { compressBytecode } from '@fuel-ts/utils';

import type { Abi } from '../../abi/Abi';
import { renderHbsTemplate } from '../renderHbsTemplate';
Expand Down Expand Up @@ -30,7 +31,10 @@ export function renderMainTemplate(params: { abi: Abi }) {

const { enums } = formatEnums({ types });
const { structs } = formatStructs({ types });
const { imports } = formatImports({ types, baseMembers: ['Script', 'Account'] });
const { imports } = formatImports({
types,
baseMembers: ['Script', 'Account', 'decompressBytecode'],
});

const { prefixedInputs: inputs, output } = func.attributes;

Expand All @@ -42,7 +46,7 @@ export function renderMainTemplate(params: { abi: Abi }) {
structs,
enums,
abiJsonString,
hexlifiedBinString,
compressedBytecode: compressBytecode(hexlifiedBinString),
capitalizedName,
imports,
configurables,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
Fuel-Core version: 33.33.33
*/

import { ContractFactory } from "fuels";
import { ContractFactory, decompressBytecode } from "fuels";
import type { Provider, Account, DeployContractOptions, DeployContractResult } from "fuels";

import { MyContract } from "./MyContract";

const bytecode = "0x-bytecode-here";
const bytecode = decompressBytecode("0x-bytecode-here");

export class MyContractFactory extends ContractFactory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import {
BigNumberish,
decompressBytecode,
InputValue,
Predicate,
PredicateParams,
Expand Down Expand Up @@ -99,7 +100,7 @@ const abi = {
]
};

const bytecode = '0x000';
const bytecode = decompressBytecode('0x-bytecode-here');

export class MyPredicate extends Predicate<
MyPredicateInputs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import {
BigNumberish,
BN,
decompressBytecode,
InputValue,
Predicate,
PredicateParams,
Expand Down Expand Up @@ -297,7 +298,7 @@ const abi = {
"configurables": []
};

const bytecode = '0x000';
const bytecode = decompressBytecode('0x-bytecode-here');

export class MyPredicate extends Predicate<
MyPredicateInputs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import {
Account,
BigNumberish,
decompressBytecode,
Script,
} from 'fuels';

Expand Down Expand Up @@ -92,7 +93,7 @@ const abi = {
]
};

const bytecode = '0x000';
const bytecode = decompressBytecode('0x-bytecode-here');

export class MyScript extends Script<MyScriptInputs, MyScriptOutput> {

Expand Down
3 changes: 2 additions & 1 deletion packages/abi-typegen/test/fixtures/templates/script/main.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Account,
BigNumberish,
BN,
decompressBytecode,
Script,
} from 'fuels';

Expand Down Expand Up @@ -289,7 +290,7 @@ const abi = {
"configurables": []
};

const bytecode = '0x000';
const bytecode = decompressBytecode('0x-bytecode-here');

export class MyScript extends Script<MyScriptInputs, MyScriptOutput> {

Expand Down
48 changes: 47 additions & 1 deletion packages/account/src/providers/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,53 @@ describe('Provider', () => {
() => sender.sendTransaction(request),
new FuelError(
ErrorCode.MAX_INPUTS_EXCEEDED,
'The transaction exceeds the maximum allowed number of inputs for funding.'
'The transaction exceeds the maximum allowed number of inputs.'
)
);
});

it('should throws if max of ouputs was exceeded', async () => {
const maxOutputs = 2;
using launched = await setupTestProviderAndWallets({
nodeOptions: {
snapshotConfig: {
chainConfig: {
consensus_parameters: {
V1: {
tx_params: {
V1: {
max_outputs: maxOutputs,
},
},
},
},
},
},
},
walletsConfig: {
count: 3,
},
});

const {
wallets: [sender, receiver1, receiver2],
provider,
} = launched;

const baseAssetId = provider.getBaseAssetId();

const request = new ScriptTransactionRequest();
const resources = await sender.getResourcesToSpend([[1000, baseAssetId]]);
request.addCoinOutput(receiver1.address, 1, baseAssetId);
request.addCoinOutput(receiver2.address, 1, baseAssetId);

request.addResources(resources);

await expectToThrowFuelError(
() => sender.sendTransaction(request),
new FuelError(
ErrorCode.MAX_OUTPUTS_EXCEEDED,
'The transaction exceeds the maximum allowed number of outputs.'
)
);
});
Expand Down
12 changes: 10 additions & 2 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,18 @@ Supported fuel-core version: ${supportedVersion}.`
}

private validateTransaction(tx: TransactionRequest, consensusParameters: ConsensusParameters) {
if (bn(tx.inputs.length).gt(consensusParameters.txParameters.maxInputs)) {
const { maxOutputs, maxInputs } = consensusParameters.txParameters;
if (bn(tx.inputs.length).gt(maxInputs)) {
throw new FuelError(
ErrorCode.MAX_INPUTS_EXCEEDED,
'The transaction exceeds the maximum allowed number of inputs for funding.'
'The transaction exceeds the maximum allowed number of inputs.'
);
}

if (bn(tx.outputs.length).gt(maxOutputs)) {
throw new FuelError(
ErrorCode.MAX_OUTPUTS_EXCEEDED,
'The transaction exceeds the maximum allowed number of outputs.'
);
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/errors/src/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export enum ErrorCode {
TRANSACTION_SQUEEZED_OUT = 'transaction-squeezed-out',
CONTRACT_SIZE_EXCEEDS_LIMIT = 'contract-size-exceeds-limit',
MAX_INPUTS_EXCEEDED = 'max-inputs-exceeded',
MAX_OUTPUTS_EXCEEDED = 'max-outputs-exceeded',

// receipt
INVALID_RECEIPT_TYPE = 'invalid-receipt-type',
Expand Down
2 changes: 1 addition & 1 deletion packages/fuel-gauge/src/transaction-response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('TransactionResponse', () => {

it(
'should throw error for a SqueezedOut status update [waitForResult]',
{ timeout: 10_000 },
{ timeout: 10_000, retry: 10 },
async () => {
/**
* a larger --tx-pool-ttl 1s is necessary to ensure that the transaction doesn't get squeezed out
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@fuel-ts/errors": "workspace:*",
"@fuel-ts/interfaces": "workspace:*",
"@fuel-ts/math": "workspace:*",
"@fuel-ts/versions": "workspace:*"
"@fuel-ts/versions": "workspace:*",
"fflate": "^0.8.2"
}
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './utils/base58';
export * from './utils/dataSlice';
export * from './utils/toUtf8Bytes';
export * from './utils/toUtf8String';
export * from './utils/bytecode';
Loading

0 comments on commit 6e6ba87

Please sign in to comment.