Skip to content

Commit

Permalink
fix: return default zksolc version of fetching fails (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-bozin-txfusion authored Sep 25, 2024
1 parent f8e78f3 commit b5e4582
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 27 deletions.
8 changes: 7 additions & 1 deletion packages/hardhat-zksync-solc/src/compile/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
ZKSOLC_BIN_REPOSITORY_NAME,
USER_AGENT,
ZKSOLC_COMPILER_PATH_VERSION,
fallbackLatestZkSolcVersion,
} from '../constants';
import { ZkSyncSolcPluginError } from './../errors';

Expand Down Expand Up @@ -210,7 +211,12 @@ export class ZksolcCompilerDownloader {
We are currently limited in that each new version requires an update of the plugin version.
*/
private static async _downloadCompilerVersionInfo(compilersDir: string): Promise<void> {
const latestRelease = await getLatestRelease(ZKSOLC_BIN_OWNER, ZKSOLC_BIN_REPOSITORY_NAME, USER_AGENT);
const latestRelease = await getLatestRelease(
ZKSOLC_BIN_OWNER,
ZKSOLC_BIN_REPOSITORY_NAME,
USER_AGENT,
fallbackLatestZkSolcVersion,
);

const releaseToSave = {
latest: latestRelease,
Expand Down
3 changes: 3 additions & 0 deletions packages/hardhat-zksync-solc/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ export const MISSING_LIBRARY_LINK =

export const SOLCJS_EXECUTABLE_CODE = `#!/usr/bin/env node
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,o){void 0===o&&(o=r);var i=Object.getOwnPropertyDescriptor(t,r);i&&("get"in i?t.__esModule:!i.writable&&!i.configurable)||(i={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,o,i)}:function(e,t,r,o){void 0===o&&(o=r),e[o]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&__createBinding(t,e,r);return __setModuleDefault(t,e),t},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports._loadCompilerSources=void 0;const os_1=__importDefault(require("os")),fs_1=__importDefault(require("fs")),path_1=__importDefault(require("path"));function packageExists(e){try{return require.resolve(e),!0}catch(e){return!1}}function findPackagePath(e,t){let r=t,o=r+"/node_modules/"+e;for(;"/"!==r;){if(packageExists(o))return o;r=path_1.default.dirname(r),o=r+"/node_modules/"+e}}async function getSolc(e,t){var r=findPackagePath("solc/wrapper",t);const{default:o}=await Promise.resolve().then(()=>__importStar(require(r)));return o(_loadCompilerSources(e))}function _loadCompilerSources(e){const t=module.constructor;if(void 0===t._extensions)return require(e);var r=t._extensions[".js"];t._extensions[".js"]=function(e,t){var r=fs_1.default.readFileSync(t,"utf8");Object.getPrototypeOf(e)._compile.call(e,r,t)};e=require(e);return t._extensions[".js"]=r,e}exports._loadCompilerSources=_loadCompilerSources;async function readStdin(){return new Promise(e=>{let t="";process.stdin.on("data",e=>t+=e),process.stdin.on("end",()=>e(t))})}!async function(){var e;const t=await getSolc("SOLCJS_PATH","WORKING_DIR");process.argv.includes("--version")?(e=await t.version(),process.stdout.write("solc, the solidity compiler commandline interface"+os_1.default.EOL),process.stdout.write("Version: "+e+os_1.default.EOL)):(e=await readStdin(),e=t.compile(e),process.stdout.write(e))}();`;

export const fallbackLatestZkSolcVersion = '1.5.3';
export const fallbackLatestEraCompilerVersion = '1.0.1';
66 changes: 41 additions & 25 deletions packages/hardhat-zksync-solc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
COMPILER_ZKSOLC_IS_SYSTEM_USE,
COMPILER_ZKSOLC_FORCE_EVMLA_USE,
COMPILER_MIN_LINUX_VERSION_WITH_GNU_TOOLCHAIN,
fallbackLatestEraCompilerVersion,
} from './constants';
import { ZkSyncSolcPluginError } from './errors';
import {
Expand Down Expand Up @@ -348,6 +349,7 @@ export async function getLatestRelease(
owner: string,
repo: string,
userAgent: string,
defaultValue: string,
tagPrefix: string = 'v',
timeout: number = DEFAULT_TIMEOUT_MILISECONDS,
): Promise<any> {
Expand All @@ -356,33 +358,39 @@ export async function getLatestRelease(

const { request } = await import('undici');

const response = await request(url, {
headersTimeout: timeout,
maxRedirections: 0,
method: 'GET',
headers: {
'User-Agent': `${userAgent}`,
},
});

// Check if the response is a redirect
if (response.statusCode >= 300 && response.statusCode < 400) {
// Get the URL from the 'location' header
if (response.headers.location && typeof response.headers.location === 'string') {
// Check if the redirect URL matches the expected pattern
if (response.headers.location.startsWith(redirectUrlPattern)) {
// Extract the tag from the redirect URL
return response.headers.location.substring(redirectUrlPattern.length);
try {
const response = await request(url, {
headersTimeout: timeout,
maxRedirections: 0,
method: 'GET',
headers: {
'User-Agent': `${userAgent}`,
},
});

// Check if the response is a redirect
if (response.statusCode >= 300 && response.statusCode < 400) {
// Get the URL from the 'location' header
if (response.headers.location && typeof response.headers.location === 'string') {
// Check if the redirect URL matches the expected pattern
if (response.headers.location.startsWith(redirectUrlPattern)) {
// Extract the tag from the redirect URL
return response.headers.location.substring(redirectUrlPattern.length);
}

throw new ZkSyncSolcPluginError(
`Unexpected redirect URL: ${response.headers.location} for URL: ${url}`,
);
} else {
// Throw an error if the 'location' header is missing in a redirect response
throw new ZkSyncSolcPluginError(`Redirect location not found for URL: ${url}`);
}

throw new ZkSyncSolcPluginError(`Unexpected redirect URL: ${response.headers.location} for URL: ${url}`);
} else {
// Throw an error if the 'location' header is missing in a redirect response
throw new ZkSyncSolcPluginError(`Redirect location not found for URL: ${url}`);
// Throw an error for non-redirect responses
throw new ZkSyncSolcPluginError(`Unexpected response status: ${response.statusCode} for URL: ${url}`);
}
} else {
// Throw an error for non-redirect responses
throw new ZkSyncSolcPluginError(`Unexpected response status: ${response.statusCode} for URL: ${url}`);
} catch {
return defaultValue;
}
}

Expand All @@ -396,5 +404,13 @@ export function getZkVmNormalizedVersion(solcVersion: string, zkVmSolcVersion: s
}

export async function getLatestEraVersion(): Promise<string> {
return (await getLatestRelease(ZKSOLC_BIN_OWNER, ZKVM_SOLC_BIN_REPOSITORY_NAME, USER_AGENT, '')).split('-')[1];
return (
await getLatestRelease(
ZKSOLC_BIN_OWNER,
ZKVM_SOLC_BIN_REPOSITORY_NAME,
USER_AGENT,
fallbackLatestEraCompilerVersion,
'',
)
).split('-')[1];
}
3 changes: 2 additions & 1 deletion packages/hardhat-zksync-solc/test/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
sha1,
updateBreakableCompilerConfig,
} from '../../src/utils';
import { fallbackLatestZkSolcVersion } from '../../src/constants';

describe('Utils', () => {
describe('filterSupportedOutputSelections', () => {
Expand Down Expand Up @@ -343,7 +344,7 @@ describe('Utils', () => {
},
);

const release = await getLatestRelease(owner, repo, userAgent);
const release = await getLatestRelease(owner, repo, userAgent, fallbackLatestZkSolcVersion);

expect(release).to.deep.equal('1.0.0');
});
Expand Down

0 comments on commit b5e4582

Please sign in to comment.