Skip to content

Commit

Permalink
Merge pull request #139 from acuarica/refactor-solc
Browse files Browse the repository at this point in the history
Use `fetch` instead of custom get for http requests in `solc`
  • Loading branch information
acuarica authored Sep 19, 2024
2 parents 7b5cdbb + 230406a commit d909a87
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .c8rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"exclude": [
".solc/soljson-v*.js",
"dist/test/",
"scripts/solc.mjs"
"test/scripts/solc.mjs"
]
}
2 changes: 1 addition & 1 deletion .mocharc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enable-source-maps: true

# Fetch and cache `solc` compilers
# https://mochajs.org/#global-setup-fixtures
require: scripts/solc.mjs
require: test/scripts/solc.mjs

# https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.yml
spec:
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,18 @@ Generates `function` and `event` lookup tables database for signatures in `json`
Generates ERCs function and event definitions from [`scripts/ercs.sol`](./scripts/ercs.sol).
- [`help.mjs`](./scripts/help.mjs)
Embeds [`examples`](#examples) and `sevm --help` into [`README`](./README.md).
- [`mock.mjs`](./scripts/mock.mjs)

### [`test/scripts`](./test/scripts/)

Contains utility scripts that complements the test process.

- [`mock.mjs`](./test/scripts/mock.mjs)
Mocks network requests to avoid brittle **CLI** tests, _i.e._, `::examples` and `::bin` tests.
Both `ethers`' JSON-RPC provider `eth_getCode` method and, `function` and `event` signatures from [OpenChain API](https://openchain.xyz/signatures) will be mocked.
It is loaded using Node's flag [`--import=./scripts/mock.mjs`](https://nodejs.org/api/cli.html#--importmodule).
- [`solc.mjs`](./scripts/solc.mjs)
Downloads and caches [`solc-js`](https://github.com/ethereum/solc-js) compiler versions used in tests. It is invoked via Mocha's [_Global Setup Fixtures_](https://mochajs.org/#global-setup-fixtures).
It is loaded using Node's flag [`--import=./test/scripts/mock.mjs`](https://nodejs.org/api/cli.html#--importmodule).
- [`solc.mjs`](./test/scripts/solc.mjs)
Downloads and caches [`solc-js`](https://github.com/ethereum/solc-js) compiler versions used in tests.
It is invoked via Mocha's [_Global Setup Fixtures_](https://mochajs.org/#global-setup-fixtures).

### [`examples`](./examples/)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
"coverage": "yarn compile && c8 mocha",
"bundle": "webpack --mode production",
"size": "size-limit",
"make:ercs": "scripts/ercs.mjs > src/ercs.ts",
"make:4bytedb": "node scripts/4bytedb.mjs",
"make:ercs": "scripts/ercs.mjs > src/ercs.ts",
"make:help": "scripts/help.mjs README.md",
"make:docs": "cp README.md docs && typedoc --out docs/tsdoc src/index.ts src/ast/index.ts",
"docs": "npx http-server docs",
Expand Down
79 changes: 0 additions & 79 deletions scripts/solc.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion test/bin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('::bin', function () {
* https://etherscan.io/address/0x00000000219ab540356cBB839Cbe05303d7705Fa
*/
const address = '0x00000000219ab540356cBB839Cbe05303d7705Fa';
const mock = '--import=./scripts/mock.mjs';
const mock = '--import=./test/scripts/mock.mjs';

it('should get `bytecode` from default provider', function () {
const cli = chaiExec('node', [mock, sevm, 'abi', address, '--no-color', '--no-patch', '--no-cache'], { env: sevmDebugEnv });
Expand Down
2 changes: 1 addition & 1 deletion test/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe(`::examples`, function () {
const program = ['.ts', '.mts'].includes(ext)
? `dist/examples/${file.slice(0, -ext.length)}${ext.replace('t', 'j')}`
: `examples/${file}`;
const mock = '--import=./scripts/mock.mjs';
const mock = '--import=./test/scripts/mock.mjs';
const cli = chaiExec('node', mock, program, { env });

expect(cli, cli.stderr).stderr.to.be.empty;
Expand Down
4 changes: 3 additions & 1 deletion test/exprs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import util from 'util';

import { Shanghai, sol, yul, type Ram, State, Memory } from 'sevm';
import type { Expr } from 'sevm/ast';
import { Add, Byte, CallDataLoad, CallValue, Div, Eq, Exp, IsZero, MLoad, Mod, Mul, Not, Prop, Props, Sha3, Shl, Sig, Sub, Val } from 'sevm/ast';
import { Add, Byte, CallDataLoad, CallValue, Div, Eq, Exp, IsZero, MLoad, Mod, Mul, Not, Prop, Props, Sha3, Shl, Sig, Sub, Val, Xor } from 'sevm/ast';

const id = <E>(expr: E): E => expr;

Expand Down Expand Up @@ -88,6 +88,8 @@ const $exprs = {

t([0x100n, 0x2n, 'EXP'], new Exp(new Val(0x2n), new Val(0x100n)), new Val(0x0n), '0x2 ** 0x100', 'exp(0x2, 0x100)'),
t([0x1n, 0x111n, 'SHL'], new Shl(new Val(0x1n), new Val(0x111n)), new Val(0x0n), '0x1 << 0x111', 'shl(0x1, 0x111)'),

t([0x10n, 0x101n, 'XOR'], new Xor(new Val(0x101n), new Val(0x010n)), new Val(0x111n), '0x101 ^ 0x10', 'xor(0x101, 0x10)'),
],
special: [
t(['BASEFEE'], Props['block.basefee'], id, 'block.basefee', 'basefee()'),
Expand Down
File renamed without changes.
46 changes: 46 additions & 0 deletions test/scripts/solc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env node
/* eslint-env node */

import c from 'ansi-colors';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';

const VERSIONS = ['0.5.5', '0.5.17', '0.6.12', '0.7.6', '0.8.16', '0.8.21'];

/**
* Fetch and cache `solc` compilers used in tests.
*/
export async function mochaGlobalSetup() {
/** @typedef {{ [key: string]: string }} Releases */

// `recursive` ensures dir is created instead of failing with 'file already exists'
mkdirSync('.solc', { recursive: true });
process.stdout.write(c.magenta('> setup solc-js compilers '));

const releases = await (async function () {
const path = './.solc/releases.json';
try {
const ret = /** @type {Releases} */(JSON.parse(readFileSync(path, 'utf-8')));
return ret;
} catch (_err) {
const resp = await fetch('https://binaries.soliditylang.org/bin/list.json');
// Serializes only `releases` property to avoid including `builds`
// property which is unnecessary and clutters the file.
const { releases } = /** @type {{releases: Releases}} */(await resp.json());
writeFileSync(path, JSON.stringify(releases, null, 2));
return releases;
}
})();

for (const version of VERSIONS) {
process.stdout.write(`${c.cyan('v' + version)}`);
const path = `./.solc/soljson-v${version}.js`;

if (existsSync(path)) {
process.stdout.write(c.green('\u2713 '));
} else {
const resp = await fetch(`https://binaries.soliditylang.org/bin/${releases[version]}`);
writeFileSync(path, await resp.text());
process.stdout.write(c.yellow('\u2913 '));
}
}
}

0 comments on commit d909a87

Please sign in to comment.