Skip to content

Commit

Permalink
chore: add validation for TX max outputs exceeded (#2899)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored Aug 8, 2024
1 parent b8c9174 commit 1d2abd7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
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: 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.
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 @@ -590,7 +590,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 @@ -696,10 +696,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

0 comments on commit 1d2abd7

Please sign in to comment.