-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transpiling Hardcoded Constructor Values (#169)
* unable to replicate bug * tests passing with new files * linting, renaming * Update packages/rollup-dev-tools/test/transpiler/abi-encoded-constants-transpilation.spec.ts Co-authored-by: Kevin Ho <kevinjho1996@gmail.com> * incorporate minor PR feedback Co-authored-by: Kevin Ho <kevinjho1996@gmail.com>
- Loading branch information
Showing
7 changed files
with
179 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...llup-dev-tools/test/contracts/constants-transpilation/AbiEncodedConstantInConstructor.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pragma solidity ^0.5.16; | ||
|
||
contract AbiEncodedConstantInConstructor { | ||
bytes32 public hash; | ||
|
||
constructor() public { | ||
hash = keccak256( | ||
abi.encode( | ||
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'), | ||
1 | ||
) | ||
); | ||
} | ||
|
||
function getConstant() external returns(bytes32) { | ||
return hash; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/rollup-dev-tools/test/transpiler/abi-encoded-constants-transpilation.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* External Imports */ | ||
import { ethers } from 'ethers' | ||
import { bufToHexString, getLogger } from '@eth-optimism/core-utils' | ||
import { | ||
formatBytecode, | ||
Opcode, | ||
bufferToBytecode, | ||
} from '@eth-optimism/rollup-core' | ||
import * as AbiEncodedConstantInConstructor from '../contracts/build/AbiEncodedConstantInConstructor.json' | ||
|
||
/* Internal Imports */ | ||
import { | ||
EvmIntrospectionUtil, | ||
ExecutionResult, | ||
EvmIntrospectionUtilImpl, | ||
} from '../../src' | ||
|
||
import { TranspilerImpl, OpcodeWhitelistImpl } from '../../src/tools/transpiler' | ||
import { transpileAndDeployInitcode, mockSSTOREReplacer } from '../helpers' | ||
|
||
const log = getLogger(`test-constructor-params-new`) | ||
|
||
const getGetterReturnedVal = async ( | ||
deployedAddress: Buffer, | ||
methodId: string, | ||
evmUtil: EvmIntrospectionUtil | ||
): Promise<Buffer> => { | ||
const callRes: ExecutionResult = await evmUtil.callContract( | ||
bufToHexString(deployedAddress), | ||
methodId | ||
) | ||
if (!!callRes.error) { | ||
throw new Error( | ||
`call to ${methodId} failed with evmUtil Error: ${callRes.error}` | ||
) | ||
} | ||
return callRes.result | ||
} | ||
|
||
describe('Solitity contracts should have hardcoded values correctly accessible in transpiled initcode', () => { | ||
let evmUtil: EvmIntrospectionUtil | ||
|
||
const opcodeWhitelist = new OpcodeWhitelistImpl(Opcode.ALL_OP_CODES) | ||
const transpiler = new TranspilerImpl(opcodeWhitelist, mockSSTOREReplacer) | ||
let deployedGetterAddress: Buffer | ||
beforeEach(async () => { | ||
evmUtil = await EvmIntrospectionUtilImpl.create() | ||
log.debug( | ||
`transpiling and deploying initcode which should store hash in constructor` | ||
) | ||
deployedGetterAddress = await transpileAndDeployInitcode( | ||
AbiEncodedConstantInConstructor, | ||
[], | ||
[], | ||
transpiler, | ||
evmUtil | ||
) | ||
const code: Buffer = await evmUtil.getContractDeployedBytecode( | ||
deployedGetterAddress | ||
) | ||
log.debug( | ||
`Initcode transpiled and deployed. The code is:\n${formatBytecode( | ||
bufferToBytecode(code) | ||
)}` | ||
) | ||
}) | ||
|
||
it(`The hash of an abi.encode(hardcoded) should be correct and retrievable if stored during constructor()`, async () => { | ||
const expectedStoredVal = ethers.utils.keccak256( | ||
ethers.utils.defaultAbiCoder.encode( | ||
['bytes32', 'uint256'], | ||
[ | ||
ethers.utils.keccak256( | ||
Buffer.from( | ||
ethers.utils.toUtf8Bytes( | ||
'EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)' | ||
) | ||
) | ||
), | ||
1, | ||
] | ||
) | ||
) | ||
const res = await getGetterReturnedVal( | ||
deployedGetterAddress, | ||
'getConstant', | ||
evmUtil | ||
) | ||
bufToHexString(res).should.eq(expectedStoredVal) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters