From a12184d19d7c78c351653c19cf210a114c4968db Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 8 Apr 2026 21:55:29 +0300 Subject: [PATCH 1/2] fix: correct reshapeArray for N-dimensional tensors and restore gas estimation --- src/client.ts | 9 ++++----- src/utils.ts | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/client.ts b/src/client.ts index fa4a666..0b23f6f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -139,7 +139,7 @@ export class Client { event.topics.slice(1), ); - var modelOutput = convertToModelOutput(decodedLog); + const modelOutput = convertToModelOutput(decodedLog); // if model out is empty, check inference event through precompile contract if (Object.keys(modelOutput).length === 0) { const precompileEventAbi = this.precompileContract.options.jsonInterface.find( @@ -299,10 +299,9 @@ export class Client { this.account.address, "pending", ); - // const estimatedGas = await runFunction.estimateGas({ - // from: this.account.address, - // }); - const estimatedGas = 100000; + const estimatedGas = await runFunction.estimateGas({ + from: this.account.address, + }); const gasLimit = Math.floor(estimatedGas * 1.5); const gasPrice = await this.web3.eth.getGasPrice(); diff --git a/src/utils.ts b/src/utils.ts index 1d77543..838ffa4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -205,7 +205,7 @@ function reshapeArray(array: any[], shape: number[]): any { const result = []; const lastDimSize = shape[shape.length - 1]; - const subShape = shape.slice(0, -1); + const subShape = shape.slice(1); for (let i = 0; i < shape[0]; i++) { if (shape.length === 2) { From ed67620f1bd056c6ee17545c8c3388dd4852a2cb Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 8 Apr 2026 22:43:42 +0300 Subject: [PATCH 2/2] test: add unit tests for reshapeArray covering 3D+ dimensional tensors --- src/__tests__/reshapeArray.test.ts | 77 ++++++++++++++++++++++++++++++ src/utils.ts | 2 +- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/reshapeArray.test.ts diff --git a/src/__tests__/reshapeArray.test.ts b/src/__tests__/reshapeArray.test.ts new file mode 100644 index 0000000..d327e56 --- /dev/null +++ b/src/__tests__/reshapeArray.test.ts @@ -0,0 +1,77 @@ +import { reshapeArray } from "../utils"; + +describe("reshapeArray", () => { + it("should return the flat array as-is for a 1D shape", () => { + const flat = [1, 2, 3, 4, 5]; + const result = reshapeArray(flat, [5]); + expect(result).toEqual([1, 2, 3, 4, 5]); + }); + + it("should reshape a flat array into a 2D array with shape [2, 3]", () => { + const flat = [1, 2, 3, 4, 5, 6]; + const result = reshapeArray(flat, [2, 3]); + expect(result).toEqual([ + [1, 2, 3], + [4, 5, 6], + ]); + }); + + it("should reshape a flat array into a 3D array with shape [2, 3, 4]", () => { + // This is the critical case that was broken before the fix. + // shape.slice(0, -1) would have passed [2, 3] instead of [3, 4], + // causing silent data corruption. + const flat = Array.from({ length: 24 }, (_, i) => i + 1); + const result = reshapeArray(flat, [2, 3, 4]); + expect(result).toEqual([ + [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + ], + [ + [13, 14, 15, 16], + [17, 18, 19, 20], + [21, 22, 23, 24], + ], + ]); + }); + + it("should reshape a flat array into a 4D array with shape [2, 2, 2, 3]", () => { + const flat = Array.from({ length: 24 }, (_, i) => i + 1); + const result = reshapeArray(flat, [2, 2, 2, 3]); + expect(result).toEqual([ + [ + [ + [1, 2, 3], + [4, 5, 6], + ], + [ + [7, 8, 9], + [10, 11, 12], + ], + ], + [ + [ + [13, 14, 15], + [16, 17, 18], + ], + [ + [19, 20, 21], + [22, 23, 24], + ], + ], + ]); + }); + + it("should handle a single-element shape [1, 1, 1]", () => { + const flat = [42]; + const result = reshapeArray(flat, [1, 1, 1]); + expect(result).toEqual([[[42]]]); + }); + + it("should return the array unchanged when shape is empty", () => { + const flat = [1, 2, 3]; + const result = reshapeArray(flat, []); + expect(result).toEqual([1, 2, 3]); + }); +}); diff --git a/src/utils.ts b/src/utils.ts index 838ffa4..d095a23 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -194,7 +194,7 @@ export function convertToModelOutput(eventData: any): RawModelInput { } } -function reshapeArray(array: any[], shape: number[]): any { +export function reshapeArray(array: any[], shape: number[]): any { if (!shape || shape.length === 0) { return array; }