import assert from 'node:assert/strict'; import {readFileSync,writeFileSync} from 'node:fs'; import {createRequire} from 'node:module'; import {resolve,dirname} from 'node:path'; import {fileURLToPath} from 'node:url'; import {createHash} from 'node:crypto'; const root=dirname(fileURLToPath(import.meta.url)); const require=createRequire(resolve(process.argv[2]??resolve(root,'node_modules'),'loader.cjs')); const solc=require('solc');const {keccak256}=createRequire(require.resolve('solc'))('js-sha3'); const {createVM}=require('@ethereumjs/vm');const {createAddressFromString,hexToBytes,bytesToHex}=require('@ethereumjs/util'); const {Common,Mainnet,Hardfork}=createRequire(require.resolve('@ethereumjs/vm'))('@ethereumjs/common'); assert.match(solc.version(),/^0\.8\.30\+/); const source=readFileSync(resolve(root,'DosPasos.sol'),'utf8'); const settings={evmVersion:'shanghai',optimizer:{enabled:false,runs:200},outputSelection:{'*':{'*':['abi','evm.bytecode.object','evm.methodIdentifiers']}}}; const compiled=JSON.parse(solc.compile(JSON.stringify({language:'Solidity',sources:{'DosPasos.sol':{content:source}},settings}))); assert.deepEqual((compiled.errors??[]).filter(e=>e.severity==='error'),[]); const artifact=compiled.contracts['DosPasos.sol'].DosPasos,selectors=artifact.evm.methodIdentifiers; const uint=v=>BigInt(v).toString(16).padStart(64,'0'),topic='0x'+keccak256('Paso(uint8,uint256)'),errorSelector='0x'+keccak256('FallaSegundo()').slice(0,8); const cases=[]; for(const [initialA,initialB] of [[0,0],[5,7],[9,1]])for(const x of [0,4,9])for(const y of [0,4,9])for(const fail of [false,true]){ const vm=await createVM({common:new Common({chain:Mainnet,hardfork:Hardfork.Shanghai})}); const caller=createAddressFromString('0x0000000000000000000000000000000000000100'); const created=await vm.evm.runCall({caller,data:hexToBytes('0x'+artifact.evm.bytecode.object+uint(initialA)+uint(initialB)),gasLimit:10000000n}); assert.equal(created.execResult.exceptionError,undefined);assert.ok(created.createdAddress);const address=created.createdAddress; const call=(signature,args=[])=>vm.evm.runCall({caller,to:address,gasLimit:1000000n,data:hexToBytes('0x'+selectors[signature]+args.map(uint).join(''))}); const read=async signature=>{const v=await call(signature);assert.equal(v.execResult.exceptionError,undefined);assert.equal(v.execResult.returnValue.length,32);return Number(BigInt(bytesToHex(v.execResult.returnValue)));}; const before={a:await read('a()'),b:await read('b()')};assert.deepEqual(before,{a:initialA,b:initialB}); const result=await call('actualizar(uint256,uint256,bool)',[x,y,fail?1:0]); const after={a:await read('a()'),b:await read('b()')},logs=result.execResult.logs??[],returnData=bytesToHex(result.execResult.returnValue); if(fail){assert.ok(result.execResult.exceptionError);assert.equal(returnData,errorSelector);assert.deepEqual(after,before);assert.equal(logs.length,0);}else{assert.equal(result.execResult.exceptionError,undefined);assert.deepEqual(after,{a:x,b:y});assert.equal(logs.length,2);for(let j=0;j<2;j++){const [emitter,topics,data]=logs[j];assert.equal(bytesToHex(emitter),address.toString());assert.deepEqual(topics.map(bytesToHex),[topic]);assert.equal(bytesToHex(data),'0x'+uint(j+1)+uint(j?y:x));}} cases.push({initial:[initialA,initialB],x,y,fail,before,after,error:result.execResult.exceptionError?.error??null,returnData,logs:logs.map(([emitter,topics,data])=>({emitter:bytesToHex(emitter),topics:topics.map(bytesToHex),data:bytesToHex(data)}))}); } const sha=p=>createHash('sha256').update(readFileSync(resolve(root,p))).digest('hex'); const evidence={node:process.version,compiler:solc.version(),evm:'@ethereumjs/vm 10.0.0',target:'Shanghai',checked_on:'2026-11-10',scope:'Actual in-memory EVM messages; no signed transaction, RPC, wallet, keys, funds or public network',source_sha256:sha('DosPasos.sol'),runner_sha256:sha('evm-reference.mjs'),lock_sha256:sha('pnpm-lock.yaml'),bytecode_sha256:createHash('sha256').update(Buffer.from(artifact.evm.bytecode.object,'hex')).digest('hex'),settings,selectors,event_topic:topic,error_selector:errorSelector,diagnostics:(compiled.errors??[]).map(e=>({severity:e.severity,message:e.message})),cases}; assert.equal(cases.length,54);writeFileSync(resolve(root,'evm-reference.json'),JSON.stringify(evidence,null,2)+'\n');console.log(JSON.stringify({node:evidence.node,compiler:evidence.compiler,cases:cases.length,success:cases.filter(c=>!c.fail).length,reverts:cases.filter(c=>c.fail).length,state_readbacks:cases.length*4,logs_checked:cases.filter(c=>!c.fail).length*2}));