-
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.
Adds an ovm compilation plugin, compiles into artifacts and artifacts…
…/ovm (#118) * Linted files * Revert back to old artifacts structure * Fixed conflict with smock
- Loading branch information
1 parent
87d1b3c
commit d068cc8
Showing
16 changed files
with
302 additions
and
23 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
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
3 changes: 2 additions & 1 deletion
3
...ges/contracts/contracts/optimistic-ethereum/iOVM/bridge/iOVM_BaseCrossDomainMessenger.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
3 changes: 2 additions & 1 deletion
3
packages/contracts/contracts/optimistic-ethereum/iOVM/bridge/iOVM_L2CrossDomainMessenger.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
3 changes: 2 additions & 1 deletion
3
packages/contracts/contracts/optimistic-ethereum/iOVM/precompiles/iOVM_L1MessageSender.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
3 changes: 2 additions & 1 deletion
3
...ges/contracts/contracts/optimistic-ethereum/iOVM/precompiles/iOVM_L2ToL1MessagePasser.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
3 changes: 2 additions & 1 deletion
3
packages/contracts/contracts/optimistic-ethereum/libraries/resolver/Lib_AddressManager.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
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
62 changes: 62 additions & 0 deletions
62
packages/contracts/contracts/optimistic-ethereum/libraries/utils/Lib_ReentrancyGuard.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,62 @@ | ||
// SPDX-License-Identifier: MIT | ||
// +build ovm | ||
pragma solidity >0.6.0 <0.8.0; | ||
|
||
/** | ||
* @dev Contract module that helps prevent reentrant calls to a function. | ||
* | ||
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier | ||
* available, which can be applied to functions to make sure there are no nested | ||
* (reentrant) calls to them. | ||
* | ||
* Note that because there is a single `nonReentrant` guard, functions marked as | ||
* `nonReentrant` may not call one another. This can be worked around by making | ||
* those functions `private`, and then adding `external` `nonReentrant` entry | ||
* points to them. | ||
* | ||
* TIP: If you would like to learn more about reentrancy and alternative ways | ||
* to protect against it, check out our blog post | ||
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. | ||
*/ | ||
abstract contract Lib_ReentrancyGuard { | ||
// Booleans are more expensive than uint256 or any type that takes up a full | ||
// word because each write operation emits an extra SLOAD to first read the | ||
// slot's contents, replace the bits taken up by the boolean, and then write | ||
// back. This is the compiler's defense against contract upgrades and | ||
// pointer aliasing, and it cannot be disabled. | ||
|
||
// The values being non-zero value makes deployment a bit more expensive, | ||
// but in exchange the refund on every call to nonReentrant will be lower in | ||
// amount. Since refunds are capped to a percentage of the total | ||
// transaction's gas, it is best to keep them low in cases like this one, to | ||
// increase the likelihood of the full refund coming into effect. | ||
uint256 private constant _NOT_ENTERED = 1; | ||
uint256 private constant _ENTERED = 2; | ||
|
||
uint256 private _status; | ||
|
||
constructor () internal { | ||
_status = _NOT_ENTERED; | ||
} | ||
|
||
/** | ||
* @dev Prevents a contract from calling itself, directly or indirectly. | ||
* Calling a `nonReentrant` function from another `nonReentrant` | ||
* function is not supported. It is possible to prevent this from happening | ||
* by making the `nonReentrant` function external, and make it call a | ||
* `private` function that does the actual work. | ||
*/ | ||
modifier nonReentrant() { | ||
// On the first call to nonReentrant, _notEntered will be true | ||
require(_status != _ENTERED, "ReentrancyGuard: reentrant call"); | ||
|
||
// Any calls to nonReentrant after this point will fail | ||
_status = _ENTERED; | ||
|
||
_; | ||
|
||
// By storing the original value once again, a refund is triggered (see | ||
// https://eips.ethereum.org/EIPS/eip-2200) | ||
_status = _NOT_ENTERED; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import * as path from 'path' | ||
import fsExtra from 'fs-extra' | ||
import { internalTask } from '@nomiclabs/buidler/config' | ||
import { SolcInput } from '@nomiclabs/buidler/types' | ||
import { Compiler } from '@nomiclabs/buidler/internal/solidity/compiler' | ||
import { pluralize } from '@nomiclabs/buidler/internal/util/strings' | ||
import { | ||
saveArtifact, | ||
getArtifactFromContractOutput, | ||
} from '@nomiclabs/buidler/internal/artifacts' | ||
import { | ||
TASK_COMPILE_RUN_COMPILER, | ||
TASK_BUILD_ARTIFACTS, | ||
TASK_COMPILE_GET_SOURCE_PATHS, | ||
TASK_COMPILE_CHECK_CACHE, | ||
TASK_COMPILE_COMPILE, | ||
TASK_COMPILE_GET_COMPILER_INPUT, | ||
} from '@nomiclabs/buidler/builtin-tasks/task-names' | ||
|
||
internalTask(TASK_COMPILE_RUN_COMPILER).setAction( | ||
async ({ input }: { input: SolcInput }, { config }) => { | ||
// Try to find a path to @eth-optimism/solc, throw if we can't. | ||
let ovmSolcJs: any | ||
try { | ||
ovmSolcJs = require('@eth-optimism/solc') | ||
} catch (err) { | ||
if (err.toString().contains('Cannot find module')) { | ||
throw new Error( | ||
`ovm-toolchain: Could not find "@eth-optimism/solc" in your node_modules.` | ||
) | ||
} else { | ||
throw err | ||
} | ||
} | ||
|
||
const evmCompiler = new Compiler( | ||
config.solc.version, | ||
path.join(config.paths.cache, 'compilers') | ||
) | ||
|
||
const ovmCompiler = new Compiler( | ||
ovmSolcJs.version(), | ||
path.join(config.paths.cache, 'compilers') | ||
) | ||
|
||
ovmCompiler.getSolc = () => { | ||
return ovmSolcJs | ||
} | ||
|
||
const ovmInput = { | ||
language: 'Solidity', | ||
sources: {}, | ||
settings: input.settings, | ||
} | ||
const evmInput = { | ||
language: 'Solidity', | ||
sources: {}, | ||
settings: input.settings, | ||
} | ||
|
||
// Separate the EVM and OVM inputs. | ||
for (const file of Object.keys(input.sources)) { | ||
evmInput.sources[file] = input.sources[file] | ||
|
||
if (input.sources[file].content.includes('// +build ovm')) { | ||
ovmInput.sources[file] = input.sources[file] | ||
} | ||
} | ||
|
||
// Build both inputs separately. | ||
console.log('Compiling ovm contracts...') | ||
const ovmOutput = await ovmCompiler.compile(ovmInput) | ||
console.log('Compiling evm contracts...') | ||
const evmOutput = await evmCompiler.compile(evmInput) | ||
|
||
// Filter out any "No input sources specified" errors, but only if one of the two compilations | ||
// threw the error. | ||
let errors = (ovmOutput.errors || []).concat(evmOutput.errors || []) | ||
const filtered = errors.filter((error: any) => { | ||
return error.message !== 'No input sources specified.' | ||
}) | ||
if (errors.length === filtered.length + 1) { | ||
errors = filtered | ||
} | ||
|
||
for (const name of Object.keys(ovmOutput.contracts)) { | ||
ovmOutput.contracts[`${name}.ovm`] = ovmOutput.contracts[name] | ||
delete ovmOutput.contracts[name] | ||
} | ||
|
||
// Combine the outputs. | ||
const output = { | ||
contracts: { | ||
...ovmOutput.contracts, | ||
...evmOutput.contracts, | ||
}, | ||
errors, | ||
sources: { | ||
...ovmOutput.sources, | ||
...evmOutput.sources, | ||
}, | ||
} | ||
|
||
return output | ||
} | ||
) | ||
|
||
internalTask( | ||
TASK_COMPILE_GET_COMPILER_INPUT, | ||
async (_, { config, run }, runSuper) => { | ||
const input = await runSuper() | ||
|
||
// For smock. | ||
input.settings.outputSelection['*']['*'].push('storageLayout') | ||
|
||
return input | ||
} | ||
) | ||
|
||
internalTask(TASK_BUILD_ARTIFACTS, async ({ force }, { config, run }) => { | ||
const sources = await run(TASK_COMPILE_GET_SOURCE_PATHS) | ||
|
||
if (sources.length === 0) { | ||
console.log('No Solidity source file available.') | ||
return | ||
} | ||
|
||
const isCached: boolean = await run(TASK_COMPILE_CHECK_CACHE, { force }) | ||
|
||
if (isCached) { | ||
console.log( | ||
'All contracts have already been compiled, skipping compilation.' | ||
) | ||
return | ||
} | ||
|
||
const compilationOutput = await run(TASK_COMPILE_COMPILE) | ||
|
||
if (compilationOutput === undefined) { | ||
return | ||
} | ||
|
||
await fsExtra.ensureDir(config.paths.artifacts) | ||
let numberOfContracts = 0 | ||
|
||
for (const [fileName, file] of Object.entries<any>( | ||
compilationOutput.contracts | ||
)) { | ||
for (const [contractName, contractOutput] of Object.entries(file)) { | ||
const artifact = getArtifactFromContractOutput( | ||
contractName, | ||
contractOutput | ||
) | ||
numberOfContracts += 1 | ||
|
||
// For smock. | ||
;(artifact as any).storageLayout = (contractOutput as any).storageLayout | ||
|
||
if (fileName.endsWith('.ovm')) { | ||
await saveArtifact(config.paths.artifacts + '/ovm', artifact) | ||
} else { | ||
await saveArtifact(config.paths.artifacts, artifact) | ||
} | ||
} | ||
} | ||
|
||
console.log( | ||
'Compiled', | ||
numberOfContracts, | ||
pluralize(numberOfContracts, 'contract'), | ||
'successfully' | ||
) | ||
}) |
Oops, something went wrong.