|
| 1 | +import chai from "chai"; |
| 2 | +import chaiHttp from "chai-http"; |
| 3 | +import { SolidityJsonInput } from "@ethereum-sourcify/compilers-types"; |
| 4 | +import { |
| 5 | + deployFromAbiAndBytecodeForCreatorTxHash, |
| 6 | + hookIntoVerificationWorkerRun, |
| 7 | +} from "../../../../helpers/helpers"; |
| 8 | +import { LocalChainFixture } from "../../../../helpers/LocalChainFixture"; |
| 9 | +import { ServerFixture } from "../../../../helpers/ServerFixture"; |
| 10 | +import { assertJobVerification } from "../../../../helpers/assertions"; |
| 11 | +import sinon from "sinon"; |
| 12 | +import path from "path"; |
| 13 | +import fs from "fs"; |
| 14 | + |
| 15 | +chai.use(chaiHttp); |
| 16 | + |
| 17 | +describe.only("POST /v2/verify/:chainId/:address - Compiler Version Tests", function () { |
| 18 | + const chainFixture = new LocalChainFixture(); |
| 19 | + const serverFixture = new ServerFixture(); |
| 20 | + const sandbox = sinon.createSandbox(); |
| 21 | + const makeWorkersWait = hookIntoVerificationWorkerRun(sandbox, serverFixture); |
| 22 | + |
| 23 | + // Test cases for minimum supported version boundary |
| 24 | + const COMPILER_VERSION_TESTS = [ |
| 25 | + // 0.4.11 - minimum supported version (should pass) |
| 26 | + { |
| 27 | + version: "0.4.11+commit.68ef5810", |
| 28 | + expectMatch: "match" as const, |
| 29 | + shouldPass: true, |
| 30 | + }, |
| 31 | + // 0.4.10 - unsupported version (should fail) |
| 32 | + { |
| 33 | + version: "0.4.10+commit.f0d539ae", |
| 34 | + expectMatch: null, |
| 35 | + shouldPass: false, |
| 36 | + }, |
| 37 | + ]; |
| 38 | + |
| 39 | + // Universal contract that compiles across all Solidity versions |
| 40 | + const SIMPLE_STORAGE_SOURCE = fs.readFileSync( |
| 41 | + path.join(__dirname, "Storage.sol"), |
| 42 | + "utf8", |
| 43 | + ); |
| 44 | + |
| 45 | + afterEach(async () => { |
| 46 | + sandbox.restore(); |
| 47 | + }); |
| 48 | + |
| 49 | + // Helper function to create standard JSON input for a given version |
| 50 | + function createSolcJsonInput(): SolidityJsonInput { |
| 51 | + return { |
| 52 | + language: "Solidity", |
| 53 | + sources: { |
| 54 | + "Storage.sol": { |
| 55 | + content: SIMPLE_STORAGE_SOURCE, |
| 56 | + }, |
| 57 | + }, |
| 58 | + settings: {}, |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + COMPILER_VERSION_TESTS.forEach(({ version, expectMatch, shouldPass }) => { |
| 63 | + const testTitle = shouldPass |
| 64 | + ? `should verify SimpleStorage compiled with Solidity ${version} (minimum supported)` |
| 65 | + : `should reject unsupported Solidity ${version} with compilation error`; |
| 66 | + |
| 67 | + it(testTitle, async function () { |
| 68 | + // Increase timeout for compilation and verification |
| 69 | + this.timeout(120000); |
| 70 | + |
| 71 | + const { resolveWorkers } = makeWorkersWait(); |
| 72 | + |
| 73 | + const compilationArtifacts = JSON.parse( |
| 74 | + fs.readFileSync(path.join(__dirname, `${version}.json`), "utf8"), |
| 75 | + ); |
| 76 | + |
| 77 | + try { |
| 78 | + // Deploy the compiled contract |
| 79 | + const { contractAddress, txHash } = |
| 80 | + await deployFromAbiAndBytecodeForCreatorTxHash( |
| 81 | + chainFixture.localSigner, |
| 82 | + compilationArtifacts.abi, |
| 83 | + compilationArtifacts.bytecode, |
| 84 | + ); |
| 85 | + |
| 86 | + // Verify via API v2 |
| 87 | + const verifyRes = await chai |
| 88 | + .request(serverFixture.server.app) |
| 89 | + .post(`/v2/verify/${chainFixture.chainId}/${contractAddress}`) |
| 90 | + .send({ |
| 91 | + stdJsonInput: createSolcJsonInput(), |
| 92 | + compilerVersion: version, |
| 93 | + contractIdentifier: "Storage.sol:Storage", |
| 94 | + creationTransactionHash: txHash, |
| 95 | + }); |
| 96 | + |
| 97 | + if (shouldPass) { |
| 98 | + // Assert successful verification with correct match type |
| 99 | + await assertJobVerification( |
| 100 | + serverFixture, |
| 101 | + verifyRes, |
| 102 | + resolveWorkers, |
| 103 | + chainFixture.chainId, |
| 104 | + contractAddress, |
| 105 | + expectMatch!, |
| 106 | + ); |
| 107 | + } else { |
| 108 | + // Initial request should return 202 (accepted) |
| 109 | + chai.expect(verifyRes.status).to.equal(202); |
| 110 | + chai.expect(verifyRes.body).to.have.property("verificationId"); |
| 111 | + |
| 112 | + await resolveWorkers(); |
| 113 | + |
| 114 | + // Check job status to see it failed with unsupported_compiler_version |
| 115 | + const jobStatusRes = await chai |
| 116 | + .request(serverFixture.server.app) |
| 117 | + .get(`/v2/verify/${verifyRes.body.verificationId}`); |
| 118 | + |
| 119 | + chai.expect(jobStatusRes.status).to.equal(200); |
| 120 | + chai.expect(jobStatusRes.body.isJobCompleted).to.equal(true); |
| 121 | + chai.expect(jobStatusRes.body.error).to.exist; |
| 122 | + chai |
| 123 | + .expect(jobStatusRes.body.error.customCode) |
| 124 | + .to.equal("unsupported_compiler_version"); |
| 125 | + } |
| 126 | + } catch (error) { |
| 127 | + console.error(`Test failed for Solidity version ${version}:`, error); |
| 128 | + throw error; |
| 129 | + } |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments