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
77 changes: 77 additions & 0 deletions src/__tests__/reshapeArray.test.ts
Original file line number Diff line number Diff line change
@@ -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]);
});
});
9 changes: 4 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
event.topics.slice(1),
);

var modelOutput = convertToModelOutput(decodedLog);
const modelOutput = convertToModelOutput(decodedLog);

Check notice on line 142 in src/client.ts

View check run for this annotation

codefactor.io / CodeFactor

src/client.ts#L142

Unexpected re-assignment of `const` variable modelOutput. (eslint/no-const-assign)
// if model out is empty, check inference event through precompile contract
if (Object.keys(modelOutput).length === 0) {
const precompileEventAbi = this.precompileContract.options.jsonInterface.find(
Expand Down Expand Up @@ -299,10 +299,9 @@
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();

Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down