Skip to content

Commit

Permalink
chore: refactor for perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed May 21, 2024
1 parent f76aa57 commit 7d96d88
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/small-doors-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/abi-coder": patch
"@fuel-ts/fuel-core": patch
---

chore: refactor for performance improvements
16 changes: 16 additions & 0 deletions packages/abi-coder/src/ResolvedAbiType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FuelError, ErrorCode } from '@fuel-ts/errors';

import type { JsonAbi, JsonAbiArgument } from './types/JsonAbi';
import { arrayRegEx, enumRegEx, genericRegEx, stringRegEx, structRegEx } from './utils/constants';
import { findTypeById } from './utils/json-abi';
Expand Down Expand Up @@ -132,6 +134,13 @@ export class ResolvedAbiType {
}

private getArgSignaturePrefix(): string {
if (this.type.length > 1000) {
throw new FuelError(
ErrorCode.INVALID_COMPONENT,
`The provided ABI type is too long: ${this.type}.`
);
}

const structMatch = structRegEx.test(this.type);
if (structMatch) {
return 's';
Expand Down Expand Up @@ -168,6 +177,13 @@ export class ResolvedAbiType {
return this.type;
}

if (this.type.length > 1000) {
throw new FuelError(
ErrorCode.INVALID_COMPONENT,
`The provided ABI type is too long: ${this.type}.`
);
}

const arrayMatch = arrayRegEx.exec(this.type)?.groups;

if (arrayMatch) {
Expand Down
8 changes: 8 additions & 0 deletions packages/abi-coder/src/encoding/strategies/getCoderV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ export const getCoder: GetCoderFn = (
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const components = resolvedAbiType.components!;

if (resolvedAbiType.type.length > 1000) {
throw new FuelError(
ErrorCode.INVALID_COMPONENT,
`The provided ABI type is too long: ${resolvedAbiType.type}.`
);
}

const arrayMatch = arrayRegEx.exec(resolvedAbiType.type)?.groups;

if (arrayMatch) {
const length = parseInt(arrayMatch.length, 10);
const arg = components[0];
Expand Down
6 changes: 3 additions & 3 deletions packages/fuel-core/lib/install.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { execSync } from 'child_process';
import { spawnSync } from 'child_process';
import { error } from 'console';
import { existsSync, rmSync, writeFileSync, renameSync } from 'fs';
import fetch from 'node-fetch';
Expand Down Expand Up @@ -33,7 +33,7 @@ import {
let versionMatches = false;

if (existsSync(binPath)) {
const binRawVersion = execSync(`${binPath} --version`).toString().trim();
const binRawVersion = spawnSync(binPath, ['--version'], { encoding: 'utf8' }).stdout.trim();
const binVersion = binRawVersion.match(/([.0-9]+)/)?.[0];

versionMatches = binVersion === fuelCoreVersion;
Expand Down Expand Up @@ -67,7 +67,7 @@ import {
writeFileSync(pkgPath, buf);

// Extract
execSync(`tar xzf "${pkgPath}" -C "${rootDir}"`);
spawnSync('tar', ['xzf', pkgPath, '-C', rootDir]);

// Take the contents of the directory containing the extracted
// binaries and move them to the `fuel-core-binaries` directory
Expand Down
18 changes: 10 additions & 8 deletions packages/fuel-core/lib/shared.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { execSync } from 'child_process';
import { spawnSync } from 'child_process';
import { cpSync, mkdirSync, rmSync, readFileSync, writeFileSync, existsSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';


// eslint-disable-next-line @typescript-eslint/naming-convention
export const __dirname = dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -54,13 +55,14 @@ export const buildFromGitBranch = (branchName) => {
const fuelCoreRepoDebugDir = join(fuelCoreRepoDir, 'target', 'debug');
const stdioOpts = { stdio: 'inherit' };

if (existsSync(fuelCoreRepoDir)) {
execSync(`cd ${fuelCoreRepoDir} && git pull && git checkout ${branchName}`, stdioOpts);
execSync(`cd ${fuelCoreRepoDir} && cargo build`, stdioOpts);
} else {
execSync(`git clone --branch ${branchName} ${fuelCoreRepoUrl} ${fuelCoreRepoDir}`, stdioOpts);
execSync(`cd ${fuelCoreRepoDir} && cargo build`, stdioOpts);
}
if (existsSync(fuelCoreRepoDir)) {
spawnSync('git', ['pull'], { cwd: fuelCoreRepoDir, ...stdioOpts });
spawnSync('git', ['checkout', branchName], { cwd: fuelCoreRepoDir, ...stdioOpts });
spawnSync('cargo', ['build'], { cwd: fuelCoreRepoDir, ...stdioOpts });
} else {
spawnSync('git', ['clone', '--branch', branchName, fuelCoreRepoUrl, fuelCoreRepoDir], stdioOpts);
spawnSync('cargo', ['build'], { cwd: fuelCoreRepoDir, ...stdioOpts });
}

const [from, to] = [fuelCoreRepoDebugDir, fuelCoreBinDirPath];

Expand Down

0 comments on commit 7d96d88

Please sign in to comment.