Skip to content

Commit 35f8565

Browse files
authored
Clean compiler version in compilation classes (#2295)
1 parent 83af4f1 commit 35f8565

File tree

6 files changed

+85
-8
lines changed

6 files changed

+85
-8
lines changed

packages/lib-sourcify/src/Compilation/AbstractCompilation.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ import {
2121
} from '@ethereum-sourcify/compilers-types';
2222
import { logInfo, logSilly, logWarn } from '../logger';
2323

24+
function cleanCompilerVersion(version: string): string {
25+
// Remove non-numerical characters from the beginning of the version string
26+
return version.replace(/^[^\d]*/, '');
27+
}
28+
2429
export abstract class AbstractCompilation {
2530
/**
2631
* Constructor parameters
2732
*/
2833
abstract compiler: ISolidityCompiler | IVyperCompiler;
29-
abstract compilerVersion: string;
34+
compilerVersion: string;
3035
abstract compilationTarget: CompilationTarget;
3136
jsonInput: SolidityJsonInput | VyperJsonInput;
3237

@@ -50,7 +55,11 @@ export abstract class AbstractCompilation {
5055
forceEmscripten?: boolean,
5156
): Promise<void>;
5257

53-
constructor(jsonInput: SolidityJsonInput | VyperJsonInput) {
58+
constructor(
59+
compilerVersion: string,
60+
jsonInput: SolidityJsonInput | VyperJsonInput,
61+
) {
62+
this.compilerVersion = cleanCompilerVersion(compilerVersion);
5463
this.jsonInput = structuredClone(jsonInput);
5564
}
5665

packages/lib-sourcify/src/Compilation/PreRunCompilation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ export class PreRunCompilation extends AbstractCompilation {
3333

3434
public constructor(
3535
public compiler: ISolidityCompiler | IVyperCompiler,
36-
public compilerVersion: string,
36+
compilerVersion: string,
3737
jsonInput: SolidityJsonInput | VyperJsonInput,
3838
jsonOutput: SolidityOutput | VyperOutput,
3939
public compilationTarget: CompilationTarget,
4040
public _creationBytecodeCborAuxdata: CompiledContractCborAuxdata,
4141
public _runtimeBytecodeCborAuxdata: CompiledContractCborAuxdata,
4242
) {
43-
super(jsonInput);
43+
super(compilerVersion, jsonInput);
4444
this.compilerOutput = jsonOutput;
4545
this.language = jsonInput.language as CompilationLanguage;
4646
switch (this.language) {

packages/lib-sourcify/src/Compilation/SolidityCompilation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ export class SolidityCompilation extends AbstractCompilation {
3636

3737
public constructor(
3838
public compiler: ISolidityCompiler,
39-
public compilerVersion: string,
39+
compilerVersion: string,
4040
jsonInput: SolidityJsonInput,
4141
public compilationTarget: CompilationTarget,
4242
) {
43-
super(jsonInput);
43+
super(compilerVersion, jsonInput);
4444
this.initSolidityJsonInput();
4545
}
4646

packages/lib-sourcify/src/Compilation/VyperCompilation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ export class VyperCompilation extends AbstractCompilation {
162162

163163
public constructor(
164164
public compiler: IVyperCompiler,
165-
public compilerVersion: string,
165+
compilerVersion: string,
166166
jsonInput: VyperJsonInput,
167167
public compilationTarget: CompilationTarget,
168168
) {
169-
super(jsonInput);
169+
super(compilerVersion, jsonInput);
170170

171171
// Vyper beta and rc versions are not semver compliant, so we need to handle them differently
172172
this.compilerVersionCompatibleWithSemver = returnFixedVyperVersion(

packages/lib-sourcify/test/Compilation/SolidityCompilation.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,32 @@ describe('SolidityCompilation', () => {
325325
const immutableRefs = compilation.immutableReferences;
326326
expect(immutableRefs).to.deep.equal({ '3': [{ length: 32, start: 608 }] });
327327
});
328+
329+
it('should clean compiler version with v prefix', () => {
330+
const contractPath = path.join(__dirname, '..', 'sources', 'Storage');
331+
const metadata = JSON.parse(
332+
fs.readFileSync(path.join(contractPath, 'metadata.json'), 'utf8'),
333+
);
334+
const sources = {
335+
'project:/contracts/Storage.sol': {
336+
content: fs.readFileSync(
337+
path.join(contractPath, 'sources', 'Storage.sol'),
338+
'utf8',
339+
),
340+
},
341+
};
342+
343+
const compilation = new SolidityCompilation(
344+
solc,
345+
'v0.8.4+commit.c7e474f2',
346+
{
347+
language: 'Solidity',
348+
sources,
349+
settings: getSolcSettingsFromMetadata(metadata),
350+
},
351+
getCompilationTargetFromMetadata(metadata),
352+
);
353+
354+
expect(compilation.compilerVersion).to.equal('0.8.4+commit.c7e474f2');
355+
});
328356
});

packages/lib-sourcify/test/Compilation/VyperCompilation.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,4 +647,44 @@ describe('VyperCompilation', () => {
647647
version: 1,
648648
});
649649
});
650+
651+
it('should clean compiler version with v prefix', () => {
652+
const contractPath = path.join(
653+
__dirname,
654+
'..',
655+
'sources',
656+
'Vyper',
657+
'testcontract',
658+
);
659+
const contractFileName = 'test.vy';
660+
const contractContent = fs.readFileSync(
661+
path.join(contractPath, contractFileName),
662+
'utf8',
663+
);
664+
665+
const compilation = new VyperCompilation(
666+
vyperCompiler,
667+
'v0.3.10+commit.91361694',
668+
{
669+
language: 'Vyper',
670+
sources: {
671+
[contractFileName]: {
672+
content: contractContent,
673+
},
674+
},
675+
settings: {
676+
evmVersion: 'istanbul',
677+
outputSelection: {
678+
'*': ['evm.bytecode'],
679+
},
680+
},
681+
},
682+
{
683+
name: contractFileName.split('.')[0],
684+
path: contractFileName,
685+
},
686+
);
687+
688+
expect(compilation.compilerVersion).to.equal('0.3.10+commit.91361694');
689+
});
650690
});

0 commit comments

Comments
 (0)