Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tidy-chunks-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fleet-sdk/common": patch
"@fleet-sdk/core": patch
---

Fix `chunk()` and `TransactionBuilderSettings.setMaxTokensPerChangeBox()` accepting invalid sizes: a chunk size of zero or less looped forever, a non-integer size produced irregular or empty chunks (NaN silently dropped every element), and a single-chunk result aliased the caller's array. `chunk()` now throws a `RangeError` for a non-positive or non-integer size and always returns copied chunks, and the settings setter throws a `RangeError` for values outside 1..MAX_TOKENS_PER_BOX, leaving the previous value unchanged.
23 changes: 23 additions & 0 deletions packages/common/src/utils/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ describe("Chunk arrays", () => {
it("Should return empty array if the input is empty", () => {
expect(chunk([], 2)).toEqual([]);
});

it("Should not return the input array itself as the single chunk", () => {
const array = [1, 2, 3];

const chunks = chunk(array, 10);

expect(chunks).toEqual([[1, 2, 3]]);
expect(chunks[0]).not.toBe(array);

chunks[0].push(4);
expect(array).toEqual([1, 2, 3]);
});

it("Should throw on a non-positive chunk size", () => {
expect(() => chunk([1, 2, 3], 0)).toThrow(RangeError);
expect(() => chunk([1, 2, 3], -2)).toThrow(RangeError);
});

it("Should throw on a non-integer chunk size", () => {
expect(() => chunk([1, 2, 3], 2.5)).toThrow(RangeError);
expect(() => chunk([1, 2, 3], Number.NaN)).toThrow(RangeError);
expect(() => chunk([1, 2, 3], Number.POSITIVE_INFINITY)).toThrow(RangeError);
});
});

describe("Array ordering", () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/common/src/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ export function hasDuplicatesBy<T>(array: T[], selector: ObjectSelector<T>): boo
* ```
*/
export function chunk<T>(array: T[], size: number): T[][] {
if (!Number.isInteger(size) || size <= 0) {
throw new RangeError(`Chunk size '${size}' is out of range, it must be a positive integer.`);
}

if (isEmpty(array)) return [];
if (array.length <= size) return [array];

const chunks: T[][] = [];
for (let i = 0; i < array.length; i += size) {
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/builder/transactionBuilderSettings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ describe("Transaction builder settings", () => {
expect(settings.canBurnTokensFromPlugins).toBe(true);
});

it("Should reject invalid max tokens per change box values", () => {
const settings = new TransactionBuilderSettings();

for (const invalid of [0, -5, 2.5, Number.NaN, Number.POSITIVE_INFINITY]) {
expect(() => settings.setMaxTokensPerChangeBox(invalid)).toThrow(RangeError);
}

expect(() => settings.setMaxTokensPerChangeBox(MAX_TOKENS_PER_BOX + 1)).toThrow(RangeError);
expect(settings.maxTokensPerChangeBox).toBe(MAX_TOKENS_PER_BOX);
});

it("Should accept boundary max tokens per change box values", () => {
const settings = new TransactionBuilderSettings();

expect(settings.setMaxTokensPerChangeBox(1).maxTokensPerChangeBox).toBe(1);
expect(settings.setMaxTokensPerChangeBox(MAX_TOKENS_PER_BOX).maxTokensPerChangeBox).toBe(
MAX_TOKENS_PER_BOX
);
});

it("Should allow token burning only from plugins context", () => {
const settings = new TransactionBuilderSettings().allowTokenBurningFromPlugins(true);

Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/builder/transactionBuilderSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export class TransactionBuilderSettings {
* Define max number of distinct tokens per change box
*/
public setMaxTokensPerChangeBox(max: number): TransactionBuilderSettings {
if (!Number.isInteger(max) || max < 1 || max > MAX_TOKENS_PER_BOX) {
throw new RangeError(
`Max tokens per change box '${max}' is out of range, it must be an integer between 1 and ${MAX_TOKENS_PER_BOX}.`
);
}

this._maxDistinctTokensPerChangeBox = max;

return this;
Expand Down