commit
4972bf4f23
@ -1,84 +0,0 @@ |
||||
version: 2 |
||||
# 2.1 does not yet support local run |
||||
# unless with workaround. For simplicity just use it. |
||||
# https://github.com/CircleCI-Public/circleci-cli/issues/79 |
||||
|
||||
aliases: |
||||
- &defaults |
||||
docker: |
||||
- image: circleci/node:10 |
||||
|
||||
- &cache_key_node_modules |
||||
key: v1-node_modules-{{ checksum "package-lock.json" }} |
||||
|
||||
jobs: |
||||
dependencies: |
||||
<<: *defaults |
||||
steps: |
||||
- checkout |
||||
- restore_cache: |
||||
<<: *cache_key_node_modules |
||||
- run: |
||||
name: Install npm dependencies and prepare |
||||
command: | |
||||
if [ ! -d node_modules ]; then |
||||
npm ci |
||||
else |
||||
npm run prepare |
||||
fi |
||||
- persist_to_workspace: |
||||
root: . |
||||
paths: |
||||
- node_modules |
||||
- build |
||||
- save_cache: |
||||
paths: |
||||
- node_modules |
||||
<<: *cache_key_node_modules |
||||
|
||||
lint: |
||||
<<: *defaults |
||||
steps: |
||||
- checkout |
||||
- attach_workspace: |
||||
at: . |
||||
- run: |
||||
name: Linter |
||||
command: npm run lint |
||||
test: |
||||
<<: *defaults |
||||
steps: |
||||
- checkout |
||||
- attach_workspace: |
||||
at: . |
||||
- run: |
||||
name: Unit tests |
||||
command: npm run test |
||||
|
||||
coverage: |
||||
<<: *defaults |
||||
steps: |
||||
- checkout |
||||
- attach_workspace: |
||||
at: . |
||||
- run: |
||||
name: Unit tests with coverage report |
||||
command: npm run coverage |
||||
|
||||
# TODO(xinbenlv, #1839): run SOLC_NIGHTLY to be run but allow it to fail. |
||||
|
||||
workflows: |
||||
version: 2 |
||||
everything: |
||||
jobs: |
||||
- dependencies |
||||
- lint: |
||||
requires: |
||||
- dependencies |
||||
- test: |
||||
requires: |
||||
- dependencies |
||||
- coverage: |
||||
requires: |
||||
- dependencies |
||||
|
@ -0,0 +1,45 @@ |
||||
name: Test |
||||
|
||||
on: |
||||
push: |
||||
branches: |
||||
- master |
||||
- release-v* |
||||
pull_request: {} |
||||
|
||||
jobs: |
||||
test: |
||||
runs-on: ubuntu-latest |
||||
steps: |
||||
- uses: actions/checkout@v2 |
||||
- uses: actions/setup-node@v1 |
||||
with: |
||||
node-version: 10.x |
||||
- uses: actions/cache@v2 |
||||
id: cache |
||||
with: |
||||
path: '**/node_modules' |
||||
key: npm-v2-${{ hashFiles('**/package-lock.json') }} |
||||
restore-keys: npm-v2- |
||||
- run: npm ci |
||||
if: steps.cache.outputs.cache-hit != 'true' |
||||
- run: npm run lint |
||||
- run: npm run test |
||||
|
||||
coverage: |
||||
runs-on: ubuntu-latest |
||||
steps: |
||||
- uses: actions/checkout@v2 |
||||
- uses: actions/setup-node@v1 |
||||
with: |
||||
node-version: 10.x |
||||
- uses: actions/cache@v2 |
||||
id: cache |
||||
with: |
||||
path: '**/node_modules' |
||||
key: npm-v2-${{ hashFiles('**/package-lock.json') }} |
||||
restore-keys: npm-v2- |
||||
- run: npm ci |
||||
if: steps.cache.outputs.cache-hit != 'true' |
||||
- run: npm run coverage |
||||
- uses: codecov/codecov-action@v1 |
@ -0,0 +1,20 @@ |
||||
const fs = require('fs'); |
||||
const path = require('path'); |
||||
|
||||
usePlugin('solidity-coverage'); |
||||
usePlugin('@nomiclabs/buidler-truffle5'); |
||||
|
||||
for (const f of fs.readdirSync(path.join(__dirname, 'buidler'))) { |
||||
require(path.join(__dirname, 'buidler', f)); |
||||
} |
||||
|
||||
module.exports = { |
||||
networks: { |
||||
buidlerevm: { |
||||
blockGasLimit: 10000000, |
||||
}, |
||||
}, |
||||
solc: { |
||||
version: '0.6.12', |
||||
}, |
||||
}; |
@ -0,0 +1,10 @@ |
||||
extendEnvironment(env => { |
||||
const { contract } = env; |
||||
|
||||
env.contract = function (name, body) { |
||||
// remove the default account from the accounts list used in tests, in order
|
||||
// to protect tests against accidentally passing due to the contract
|
||||
// deployer being used subsequently as function caller
|
||||
contract(name, accounts => body(accounts.slice(1))); |
||||
}; |
||||
}); |
@ -0,0 +1,281 @@ |
||||
// SPDX-License-Identifier: MIT |
||||
|
||||
pragma solidity ^0.6.0; |
||||
pragma experimental ABIEncoderV2; |
||||
|
||||
import "./../math/SafeMath.sol"; |
||||
import "./AccessControl.sol"; |
||||
|
||||
/** |
||||
* @dev Contract module which acts as a timelocked controller. When set as the |
||||
* owner of an `Ownable` smart contract, it enforces a timelock on all |
||||
* `onlyOwner` maintenance operations. This gives time for users of the |
||||
* controlled contract to exit before a potentially dangerous maintenance |
||||
* operation is applied. |
||||
* |
||||
* By default, this contract is self administered, meaning administration tasks |
||||
* have to go through the timelock process. The proposer (resp executor) role |
||||
* is in charge of proposing (resp executing) operations. A common use case is |
||||
* to position this {TimelockController} as the owner of a smart contract, with |
||||
* a multisig or a DAO as the sole proposer. |
||||
*/ |
||||
contract TimelockController is AccessControl { |
||||
|
||||
bytes32 public constant TIMELOCK_ADMIN_ROLE = keccak256("TIMELOCK_ADMIN_ROLE"); |
||||
bytes32 public constant PROPOSER_ROLE = keccak256("PROPOSER_ROLE"); |
||||
bytes32 public constant EXECUTOR_ROLE = keccak256("EXECUTOR_ROLE"); |
||||
uint256 internal constant _DONE_TIMESTAMP = uint256(1); |
||||
|
||||
mapping(bytes32 => uint256) private _timestamps; |
||||
uint256 private _minDelay; |
||||
|
||||
/** |
||||
* @dev Emitted when a call is scheduled as part of operation `id`. |
||||
*/ |
||||
event CallScheduled(bytes32 indexed id, uint256 indexed index, address target, uint256 value, bytes data, bytes32 predecessor, uint256 delay); |
||||
|
||||
/** |
||||
* @dev Emitted when a call is performed as part of operation `id`. |
||||
*/ |
||||
event CallExecuted(bytes32 indexed id, uint256 indexed index, address target, uint256 value, bytes data); |
||||
|
||||
/** |
||||
* @dev Emitted when operation `id` is cancelled. |
||||
*/ |
||||
event Cancelled(bytes32 indexed id); |
||||
|
||||
/** |
||||
* @dev Emitted when the minimum delay for future operations is modified. |
||||
*/ |
||||
event MinDelayChange(uint256 oldDuration, uint256 newDuration); |
||||
|
||||
/** |
||||
* @dev Initializes the contract with a given `minDelay`. |
||||
*/ |
||||
constructor(uint256 minDelay, address[] memory proposers, address[] memory executors) public { |
||||
_setRoleAdmin(TIMELOCK_ADMIN_ROLE, TIMELOCK_ADMIN_ROLE); |
||||
_setRoleAdmin(PROPOSER_ROLE, TIMELOCK_ADMIN_ROLE); |
||||
_setRoleAdmin(EXECUTOR_ROLE, TIMELOCK_ADMIN_ROLE); |
||||
|
||||
// deployer + self administration |
||||
_setupRole(TIMELOCK_ADMIN_ROLE, _msgSender()); |
||||
_setupRole(TIMELOCK_ADMIN_ROLE, address(this)); |
||||
|
||||
// register proposers |
||||
for (uint256 i = 0; i < proposers.length; ++i) { |
||||
_setupRole(PROPOSER_ROLE, proposers[i]); |
||||
} |
||||
|
||||
// register executors |
||||
for (uint256 i = 0; i < executors.length; ++i) { |
||||
_setupRole(EXECUTOR_ROLE, executors[i]); |
||||
} |
||||
|
||||
_minDelay = minDelay; |
||||
emit MinDelayChange(0, minDelay); |
||||
} |
||||
|
||||
/** |
||||
* @dev Modifier to make a function callable only by a certain role. In |
||||
* addition to checking the sender's role, `address(0)` 's role is also |
||||
* considered. Granting a role to `address(0)` is equivalent to enabling |
||||
* this role for everyone. |
||||
*/ |
||||
modifier onlyRole(bytes32 role) { |
||||
require(hasRole(role, _msgSender()) || hasRole(role, address(0)), "TimelockController: sender requires permission"); |
||||
_; |
||||
} |
||||
|
||||
/* |
||||
* @dev Contract might receive/hold ETH as part of the maintenance process. |
||||
*/ |
||||
receive() external payable {} |
||||
|
||||
/** |
||||
* @dev Returns whether an operation is pending or not. |
||||
*/ |
||||
function isOperationPending(bytes32 id) public view returns (bool pending) { |
||||
return _timestamps[id] > _DONE_TIMESTAMP; |
||||
} |
||||
|
||||
/** |
||||
* @dev Returns whether an operation is ready or not. |
||||
*/ |
||||
function isOperationReady(bytes32 id) public view returns (bool ready) { |
||||
// solhint-disable-next-line not-rely-on-time |
||||
return _timestamps[id] > _DONE_TIMESTAMP && _timestamps[id] <= block.timestamp; |
||||
} |
||||
|
||||
/** |
||||
* @dev Returns whether an operation is done or not. |
||||
*/ |
||||
function isOperationDone(bytes32 id) public view returns (bool done) { |
||||
return _timestamps[id] == _DONE_TIMESTAMP; |
||||
} |
||||
|
||||
/** |
||||
* @dev Returns the timestamp at with an operation becomes ready (0 for |
||||
* unset operations, 1 for done operations). |
||||
*/ |
||||
function getTimestamp(bytes32 id) public view returns (uint256 timestamp) { |
||||
return _timestamps[id]; |
||||
} |
||||
|
||||
/** |
||||
* @dev Returns the minimum delay for an operation to become valid. |
||||
*/ |
||||
function getMinDelay() public view returns (uint256 duration) { |
||||
return _minDelay; |
||||
} |
||||
|
||||
/** |
||||
* @dev Returns the identifier of an operation containing a single |
||||
* transaction. |
||||
*/ |
||||
function hashOperation(address target, uint256 value, bytes calldata data, bytes32 predecessor, bytes32 salt) public pure returns (bytes32 hash) { |
||||
return keccak256(abi.encode(target, value, data, predecessor, salt)); |
||||
} |
||||
|
||||
/** |
||||
* @dev Returns the identifier of an operation containing a batch of |
||||
* transactions. |
||||
*/ |
||||
function hashOperationBatch(address[] calldata targets, uint256[] calldata values, bytes[] calldata datas, bytes32 predecessor, bytes32 salt) public pure returns (bytes32 hash) { |
||||
return keccak256(abi.encode(targets, values, datas, predecessor, salt)); |
||||
} |
||||
|
||||
/** |
||||
* @dev Schedule an operation containing a single transaction. |
||||
* |
||||
* Emits a {CallScheduled} event. |
||||
* |
||||
* Requirements: |
||||
* |
||||
* - the caller must have the 'proposer' role. |
||||
*/ |
||||
function schedule(address target, uint256 value, bytes calldata data, bytes32 predecessor, bytes32 salt, uint256 delay) public virtual onlyRole(PROPOSER_ROLE) { |
||||
bytes32 id = hashOperation(target, value, data, predecessor, salt); |
||||
_schedule(id, delay); |
||||
emit CallScheduled(id, 0, target, value, data, predecessor, delay); |
||||
} |
||||
|
||||
/** |
||||
* @dev Schedule an operation containing a batch of transactions. |
||||
* |
||||
* Emits one {CallScheduled} event per transaction in the batch. |
||||
* |
||||
* Requirements: |
||||
* |
||||
* - the caller must have the 'proposer' role. |
||||
*/ |
||||
function scheduleBatch(address[] calldata targets, uint256[] calldata values, bytes[] calldata datas, bytes32 predecessor, bytes32 salt, uint256 delay) public virtual onlyRole(PROPOSER_ROLE) { |
||||
require(targets.length == values.length, "TimelockController: length mismatch"); |
||||
require(targets.length == datas.length, "TimelockController: length mismatch"); |
||||
|
||||
bytes32 id = hashOperationBatch(targets, values, datas, predecessor, salt); |
||||
_schedule(id, delay); |
||||
for (uint256 i = 0; i < targets.length; ++i) { |
||||
emit CallScheduled(id, i, targets[i], values[i], datas[i], predecessor, delay); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @dev Schedule an operation that is to becomes valid after a given delay. |
||||
*/ |
||||
function _schedule(bytes32 id, uint256 delay) private { |
||||
require(_timestamps[id] == 0, "TimelockController: operation already scheduled"); |
||||
require(delay >= _minDelay, "TimelockController: insufficient delay"); |
||||
// solhint-disable-next-line not-rely-on-time |
||||
_timestamps[id] = SafeMath.add(block.timestamp, delay); |
||||
} |
||||
|
||||
/** |
||||
* @dev Cancel an operation. |
||||
* |
||||
* Requirements: |
||||
* |
||||
* - the caller must have the 'proposer' role. |
||||
*/ |
||||
function cancel(bytes32 id) public virtual onlyRole(PROPOSER_ROLE) { |
||||
require(isOperationPending(id), "TimelockController: operation cannot be cancelled"); |
||||
delete _timestamps[id]; |
||||
|
||||
emit Cancelled(id); |
||||
} |
||||
|
||||
/** |
||||
* @dev Execute an (ready) operation containing a single transaction. |
||||
* |
||||
* Emits a {CallExecuted} event. |
||||
* |
||||
* Requirements: |
||||
* |
||||
* - the caller must have the 'executor' role. |
||||
*/ |
||||
function execute(address target, uint256 value, bytes calldata data, bytes32 predecessor, bytes32 salt) public payable virtual onlyRole(EXECUTOR_ROLE) { |
||||
bytes32 id = hashOperation(target, value, data, predecessor, salt); |
||||
_beforeCall(predecessor); |
||||
_call(id, 0, target, value, data); |
||||
_afterCall(id); |
||||
} |
||||
|
||||
/** |
||||
* @dev Execute an (ready) operation containing a batch of transactions. |
||||
* |
||||
* Emits one {CallExecuted} event per transaction in the batch. |
||||
* |
||||
* Requirements: |
||||
* |
||||
* - the caller must have the 'executor' role. |
||||
*/ |
||||
function executeBatch(address[] calldata targets, uint256[] calldata values, bytes[] calldata datas, bytes32 predecessor, bytes32 salt) public payable virtual onlyRole(EXECUTOR_ROLE) { |
||||
require(targets.length == values.length, "TimelockController: length mismatch"); |
||||
require(targets.length == datas.length, "TimelockController: length mismatch"); |
||||
|
||||
bytes32 id = hashOperationBatch(targets, values, datas, predecessor, salt); |
||||
_beforeCall(predecessor); |
||||
for (uint256 i = 0; i < targets.length; ++i) { |
||||
_call(id, i, targets[i], values[i], datas[i]); |
||||
} |
||||
_afterCall(id); |
||||
} |
||||
|
||||
/** |
||||
* @dev Checks before execution of an operation's calls. |
||||
*/ |
||||
function _beforeCall(bytes32 predecessor) private view { |
||||
require(predecessor == bytes32(0) || isOperationDone(predecessor), "TimelockController: missing dependency"); |
||||
} |
||||
|
||||
/** |
||||
* @dev Checks after execution of an operation's calls. |
||||
*/ |
||||
function _afterCall(bytes32 id) private { |
||||
require(isOperationReady(id), "TimelockController: operation is not ready"); |
||||
_timestamps[id] = _DONE_TIMESTAMP; |
||||
} |
||||
|
||||
/** |
||||
* @dev Execute an operation's call. |
||||
* |
||||
* Emits a {CallExecuted} event. |
||||
*/ |
||||
function _call(bytes32 id, uint256 index, address target, uint256 value, bytes calldata data) private { |
||||
// solhint-disable-next-line avoid-low-level-calls |
||||
(bool success,) = target.call{value: value}(data); |
||||
require(success, "TimelockController: underlying transaction reverted"); |
||||
|
||||
emit CallExecuted(id, index, target, value, data); |
||||
} |
||||
|
||||
/** |
||||
* @dev Changes the timelock duration for future operations. |
||||
* |
||||
* Emits a {MinDelayChange} event. |
||||
*/ |
||||
function updateDelay(uint256 newDelay) external virtual { |
||||
require(msg.sender == address(this), "TimelockController: caller must be timelock"); |
||||
emit MinDelayChange(_minDelay, newDelay); |
||||
_minDelay = newDelay; |
||||
} |
||||
} |
@ -0,0 +1,71 @@ |
||||
= Using with Upgrades |
||||
|
||||
If your contract is going to be deployed with upgradeability, such as using the xref:upgrades-plugins::index.adoc[OpenZeppelin Upgrades Plugins], you will need to use the Upgrade Safe variant of OpenZeppelin Contracts. |
||||
|
||||
This variant is available as a separate package called `@openzeppelin/contracts-upgradeable`, which is hosted in the repository https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable[OpenZeppelin/openzeppelin-contracts-upgradeable]. |
||||
|
||||
It follows all of the rules for xref:upgrades-plugins::writing-upgradeable.adoc[Writing Upgradeable Contracts]: constructors are replaced by initializer functions, state variables are initialized in initializer functions, and we additionally check for storage incompatibilities across minor versions. |
||||
|
||||
== Overview |
||||
|
||||
=== Installation |
||||
|
||||
```console |
||||
$ npm install @openzeppelin/contracts-upgradeable |
||||
``` |
||||
|
||||
=== Usage |
||||
|
||||
The package replicates the structure of the main OpenZeppelin Contracts package, but every file and contract has the suffix `Upgradeable`. |
||||
|
||||
```diff |
||||
-import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; |
||||
+import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol"; |
||||
|
||||
-contract MyCollectible is ERC721 { |
||||
+contract MyCollectible is ERC721Upgradeable { |
||||
``` |
||||
|
||||
Constructors are replaced by internal initializer functions following the naming convention `+__{ContractName}_init+`. Since these are internal, you must always define your own public initializer function and call the parent initializer of the contract you extend. |
||||
|
||||
```diff |
||||
- constructor() ERC721("MyCollectible", "MCO") public { |
||||
+ function initialize() initializer public { |
||||
+ __ERC721_init("MyCollectible", "MCO"); |
||||
} |
||||
``` |
||||
|
||||
CAUTION: Use with multiple inheritance requires special attention. See the section below titled <<multiple-inheritance>>. |
||||
|
||||
Once this contract is set up and compiled, you can deploy it using the xref:upgrades-plugins::index.adoc[Upgrades Plugins]. The following snippet shows an example deployment script using Hardhat. |
||||
|
||||
```js |
||||
// scripts/deploy-my-collectible.js |
||||
const { ethers, upgrades } = require("hardhat"); |
||||
|
||||
async function main() { |
||||
const MyCollectible = await ethers.getContractFactory("MyCollectible"); |
||||
|
||||
const mc = await upgrades.deployProxy(MyCollectible); |
||||
|
||||
await mc.deployed(); |
||||
console.log("MyCollectible deployed to:", mc.address); |
||||
} |
||||
|
||||
main(); |
||||
``` |
||||
|
||||
== Further Notes |
||||
|
||||
[[multiple-inheritance]] |
||||
=== Multiple Inheritance |
||||
|
||||
Initializer functions are not linearized by the compiler like constructors. Because of this, each `+__{ContractName}_init+` function embeds the linearized calls to all parent initializers. As a consequence, calling two of these `init` functions can potentially initialize the same contract twice. |
||||
|
||||
The function `+__{ContractName}_init_unchained+` found in every contract is the initializer function minus the calls to parent initializers, and can be used to avoid the double initialization problem, but doing this manually is not recommended. We hope to be able to implement safety checks for this in future versions of the Upgrades Plugins. |
||||
|
||||
=== Storage Gaps |
||||
|
||||
You may notice that every contract includes a state variable named `+__gap+`. This is empty reserved space in storage that is put in place in Upgrade Safe contracts. It allows us to freely add new state variables in the future without compromising the storage compatibility with existing deployments. |
||||
|
||||
It isn't safe to simply add a state variable because it "shifts down" all of the state variables below in the inheritance chain. This makes the storage layouts incompatible, as explained in xref:upgrades-plugins::writing-upgradeable.adoc#modifying-your-contracts[Writing Upgradeable Contracts]. The size of the `+__gap+` array is calculated so that the amount of storage used by a contract always adds up to the same number (in this case 50 storage slots). |
@ -1,9 +0,0 @@ |
||||
#!/usr/bin/env sh |
||||
|
||||
if [ "$SOLC_NIGHTLY" = true ]; then |
||||
docker pull ethereum/solc:nightly |
||||
fi |
||||
|
||||
export OPENZEPPELIN_NON_INTERACTIVE=true |
||||
|
||||
npx oz compile |
@ -1,25 +0,0 @@ |
||||
#!/usr/bin/env bash |
||||
|
||||
set -o errexit -o pipefail |
||||
|
||||
# Executes cleanup function at script exit. |
||||
trap cleanup EXIT |
||||
|
||||
cleanup() { |
||||
# Delete the symlink created to the allFiredEvents file solidity-coverage creates |
||||
rm -f allFiredEvents |
||||
} |
||||
|
||||
log() { |
||||
echo "$*" >&2 |
||||
} |
||||
|
||||
# The allFiredEvents file is created inside coverageEnv, but solidity-coverage |
||||
# expects it to be at the top level. We create a symlink to fix this |
||||
ln -s coverageEnv/allFiredEvents allFiredEvents |
||||
|
||||
OZ_TEST_ENV_COVERAGE=true npx solidity-coverage || log "Test run failed" |
||||
|
||||
if [ "$CI" = true ]; then |
||||
curl -s https://codecov.io/bash | bash -s -- -C "$CIRCLE_SHA1" |
||||
fi |
@ -0,0 +1,10 @@ |
||||
#!/usr/bin/env bash |
||||
|
||||
set -euo pipefail |
||||
|
||||
# cross platform `mkdir -p` |
||||
node -e 'fs.mkdirSync("build/contracts", { recursive: true })' |
||||
|
||||
cp artifacts/*.json build/contracts |
||||
|
||||
node scripts/remove-ignored-artifacts.js |
@ -0,0 +1,11 @@ |
||||
const path = require('path'); |
||||
const bre = require('@nomiclabs/buidler'); |
||||
|
||||
const { Compiler } = require('@nomiclabs/buidler/internal/solidity/compiler'); |
||||
|
||||
const compiler = new Compiler( |
||||
bre.config.solc.version, |
||||
path.join(bre.config.paths.cache, 'compilers'), |
||||
); |
||||
|
||||
module.exports = Object.assign(compiler.getSolc(), { __esModule: true }); |
@ -1,22 +0,0 @@ |
||||
const { GSNDevProvider } = require('@openzeppelin/gsn-provider'); |
||||
|
||||
module.exports = { |
||||
accounts: { |
||||
ether: 1e6, |
||||
}, |
||||
|
||||
contracts: { |
||||
type: 'truffle', |
||||
}, |
||||
|
||||
setupProvider: (baseProvider) => { |
||||
const { accounts } = require('@openzeppelin/test-environment'); |
||||
|
||||
return new GSNDevProvider(baseProvider, { |
||||
txfee: 70, |
||||
useGSN: false, |
||||
ownerAddress: accounts[8], |
||||
relayerAddress: accounts[9], |
||||
}); |
||||
}, |
||||
}; |
@ -1,3 +1,3 @@ |
||||
## Testing |
||||
|
||||
Unit test are critical to the OpenZeppelin framework. They help ensure code quality and mitigate against security vulnerabilities. The directory structure within the `/test` directory corresponds to the `/contracts` directory. OpenZeppelin uses Truffle testing framework(based on the Mocha JavaScript testing framework) and the Chai assertion library. To learn more about how tests are structured, please reference OpenZeppelin’s Testing Guide. |
||||
Unit test are critical to the OpenZeppelin framework. They help ensure code quality and mitigate against security vulnerabilities. The directory structure within the `/test` directory corresponds to the `/contracts` directory. |
||||
|
@ -0,0 +1,992 @@ |
||||
const { constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers'); |
||||
const { ZERO_BYTES32 } = constants; |
||||
|
||||
const { expect } = require('chai'); |
||||
|
||||
const TimelockController = artifacts.require('TimelockController'); |
||||
const CallReceiverMock = artifacts.require('CallReceiverMock'); |
||||
const Implementation2 = artifacts.require('Implementation2'); |
||||
const MINDELAY = time.duration.days(1); |
||||
|
||||
function genOperation (target, value, data, predecessor, salt) { |
||||
const id = web3.utils.keccak256(web3.eth.abi.encodeParameters([ |
||||
'address', |
||||
'uint256', |
||||
'bytes', |
||||
'uint256', |
||||
'bytes32', |
||||
], [ |
||||
target, |
||||
value, |
||||
data, |
||||
predecessor, |
||||
salt, |
||||
])); |
||||
return { id, target, value, data, predecessor, salt }; |
||||
} |
||||
|
||||
function genOperationBatch (targets, values, datas, predecessor, salt) { |
||||
const id = web3.utils.keccak256(web3.eth.abi.encodeParameters([ |
||||
'address[]', |
||||
'uint256[]', |
||||
'bytes[]', |
||||
'uint256', |
||||
'bytes32', |
||||
], [ |
||||
targets, |
||||
values, |
||||
datas, |
||||
predecessor, |
||||
salt, |
||||
])); |
||||
return { id, targets, values, datas, predecessor, salt }; |
||||
} |
||||
|
||||
contract('TimelockController', function (accounts) { |
||||
const [ admin, proposer, executor, other ] = accounts; |
||||
|
||||
beforeEach(async function () { |
||||
// Deploy new timelock
|
||||
this.timelock = await TimelockController.new( |
||||
MINDELAY, |
||||
[ proposer ], |
||||
[ executor ], |
||||
{ from: admin }, |
||||
); |
||||
// Mocks
|
||||
this.callreceivermock = await CallReceiverMock.new({ from: admin }); |
||||
this.implementation2 = await Implementation2.new({ from: admin }); |
||||
}); |
||||
|
||||
it('initial state', async function () { |
||||
expect(await this.timelock.getMinDelay()).to.be.bignumber.equal(MINDELAY); |
||||
}); |
||||
|
||||
describe('methods', function () { |
||||
describe('operation hashing', function () { |
||||
it('hashOperation', async function () { |
||||
this.operation = genOperation( |
||||
'0x29cebefe301c6ce1bb36b58654fea275e1cacc83', |
||||
'0xf94fdd6e21da21d2', |
||||
'0xa3bc5104', |
||||
'0xba41db3be0a9929145cfe480bd0f1f003689104d275ae912099f925df424ef94', |
||||
'0x60d9109846ab510ed75c15f979ae366a8a2ace11d34ba9788c13ac296db50e6e', |
||||
); |
||||
expect(await this.timelock.hashOperation( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
)).to.be.equal(this.operation.id); |
||||
}); |
||||
|
||||
it('hashOperationBatch', async function () { |
||||
this.operation = genOperationBatch( |
||||
Array(8).fill('0x2d5f21620e56531c1d59c2df9b8e95d129571f71'), |
||||
Array(8).fill('0x2b993cfce932ccee'), |
||||
Array(8).fill('0xcf51966b'), |
||||
'0xce8f45069cc71d25f71ba05062de1a3974f9849b004de64a70998bca9d29c2e7', |
||||
'0x8952d74c110f72bfe5accdf828c74d53a7dfb71235dfa8a1e8c75d8576b372ff', |
||||
); |
||||
expect(await this.timelock.hashOperationBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
)).to.be.equal(this.operation.id); |
||||
}); |
||||
}); |
||||
describe('simple', function () { |
||||
describe('schedule', function () { |
||||
beforeEach(async function () { |
||||
this.operation = genOperation( |
||||
'0x31754f590B97fD975Eb86938f18Cc304E264D2F2', |
||||
0, |
||||
'0x3bf92ccc', |
||||
ZERO_BYTES32, |
||||
'0x025e7b0be353a74631ad648c667493c0e1cd31caa4cc2d3520fdc171ea0cc726', |
||||
); |
||||
}); |
||||
|
||||
it('proposer can schedule', async function () { |
||||
const receipt = await this.timelock.schedule( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
expectEvent(receipt, 'CallScheduled', { |
||||
id: this.operation.id, |
||||
index: web3.utils.toBN(0), |
||||
target: this.operation.target, |
||||
value: web3.utils.toBN(this.operation.value), |
||||
data: this.operation.data, |
||||
predecessor: this.operation.predecessor, |
||||
delay: MINDELAY, |
||||
}); |
||||
|
||||
const block = await web3.eth.getBlock(receipt.receipt.blockHash); |
||||
|
||||
expect(await this.timelock.getTimestamp(this.operation.id)) |
||||
.to.be.bignumber.equal(web3.utils.toBN(block.timestamp).add(MINDELAY)); |
||||
}); |
||||
|
||||
it('prevent overwritting active operation', async function () { |
||||
await this.timelock.schedule( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
|
||||
await expectRevert( |
||||
this.timelock.schedule( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
), |
||||
'TimelockController: operation already scheduled', |
||||
); |
||||
}); |
||||
|
||||
it('prevent non-proposer from commiting', async function () { |
||||
await expectRevert( |
||||
this.timelock.schedule( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: other }, |
||||
), |
||||
'TimelockController: sender requires permission', |
||||
); |
||||
}); |
||||
|
||||
it('enforce minimum delay', async function () { |
||||
await expectRevert( |
||||
this.timelock.schedule( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY - 1, |
||||
{ from: proposer }, |
||||
), |
||||
'TimelockController: insufficient delay', |
||||
); |
||||
}); |
||||
}); |
||||
|
||||
describe('execute', function () { |
||||
beforeEach(async function () { |
||||
this.operation = genOperation( |
||||
'0xAe22104DCD970750610E6FE15E623468A98b15f7', |
||||
0, |
||||
'0x13e414de', |
||||
ZERO_BYTES32, |
||||
'0xc1059ed2dc130227aa1d1d539ac94c641306905c020436c636e19e3fab56fc7f', |
||||
); |
||||
}); |
||||
|
||||
it('revert if operation is not scheduled', async function () { |
||||
await expectRevert( |
||||
this.timelock.execute( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: operation is not ready', |
||||
); |
||||
}); |
||||
|
||||
describe('with scheduled operation', function () { |
||||
beforeEach(async function () { |
||||
({ receipt: this.receipt, logs: this.logs } = await this.timelock.schedule( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
)); |
||||
}); |
||||
|
||||
it('revert if execution comes too early 1/2', async function () { |
||||
await expectRevert( |
||||
this.timelock.execute( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: operation is not ready', |
||||
); |
||||
}); |
||||
|
||||
it('revert if execution comes too early 2/2', async function () { |
||||
const timestamp = await this.timelock.getTimestamp(this.operation.id); |
||||
await time.increaseTo(timestamp - 5); // -1 is too tight, test sometime fails
|
||||
|
||||
await expectRevert( |
||||
this.timelock.execute( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: operation is not ready', |
||||
); |
||||
}); |
||||
|
||||
describe('on time', function () { |
||||
beforeEach(async function () { |
||||
const timestamp = await this.timelock.getTimestamp(this.operation.id); |
||||
await time.increaseTo(timestamp); |
||||
}); |
||||
|
||||
it('executor can reveal', async function () { |
||||
const receipt = await this.timelock.execute( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
); |
||||
expectEvent(receipt, 'CallExecuted', { |
||||
id: this.operation.id, |
||||
index: web3.utils.toBN(0), |
||||
target: this.operation.target, |
||||
value: web3.utils.toBN(this.operation.value), |
||||
data: this.operation.data, |
||||
}); |
||||
}); |
||||
|
||||
it('prevent non-executor from revealing', async function () { |
||||
await expectRevert( |
||||
this.timelock.execute( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: other }, |
||||
), |
||||
'TimelockController: sender requires permission', |
||||
); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('batch', function () { |
||||
describe('schedule', function () { |
||||
beforeEach(async function () { |
||||
this.operation = genOperationBatch( |
||||
Array(8).fill('0xEd912250835c812D4516BBD80BdaEA1bB63a293C'), |
||||
Array(8).fill(0), |
||||
Array(8).fill('0x2fcb7a88'), |
||||
ZERO_BYTES32, |
||||
'0x6cf9d042ade5de78bed9ffd075eb4b2a4f6b1736932c2dc8af517d6e066f51f5', |
||||
); |
||||
}); |
||||
|
||||
it('proposer can schedule', async function () { |
||||
const receipt = await this.timelock.scheduleBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
for (const i in this.operation.targets) { |
||||
expectEvent(receipt, 'CallScheduled', { |
||||
id: this.operation.id, |
||||
index: web3.utils.toBN(i), |
||||
target: this.operation.targets[i], |
||||
value: web3.utils.toBN(this.operation.values[i]), |
||||
data: this.operation.datas[i], |
||||
predecessor: this.operation.predecessor, |
||||
delay: MINDELAY, |
||||
}); |
||||
} |
||||
|
||||
const block = await web3.eth.getBlock(receipt.receipt.blockHash); |
||||
|
||||
expect(await this.timelock.getTimestamp(this.operation.id)) |
||||
.to.be.bignumber.equal(web3.utils.toBN(block.timestamp).add(MINDELAY)); |
||||
}); |
||||
|
||||
it('prevent overwritting active operation', async function () { |
||||
await this.timelock.scheduleBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
|
||||
await expectRevert( |
||||
this.timelock.scheduleBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
), |
||||
'TimelockController: operation already scheduled', |
||||
); |
||||
}); |
||||
|
||||
it('prevent non-proposer from commiting', async function () { |
||||
await expectRevert( |
||||
this.timelock.scheduleBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: other }, |
||||
), |
||||
'TimelockController: sender requires permission', |
||||
); |
||||
}); |
||||
|
||||
it('enforce minimum delay', async function () { |
||||
await expectRevert( |
||||
this.timelock.scheduleBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY - 1, |
||||
{ from: proposer }, |
||||
), |
||||
'TimelockController: insufficient delay', |
||||
); |
||||
}); |
||||
}); |
||||
|
||||
describe('execute', function () { |
||||
beforeEach(async function () { |
||||
this.operation = genOperationBatch( |
||||
Array(8).fill('0x76E53CcEb05131Ef5248553bEBDb8F70536830b1'), |
||||
Array(8).fill(0), |
||||
Array(8).fill('0x58a60f63'), |
||||
ZERO_BYTES32, |
||||
'0x9545eeabc7a7586689191f78a5532443698538e54211b5bd4d7dc0fc0102b5c7', |
||||
); |
||||
}); |
||||
|
||||
it('revert if operation is not scheduled', async function () { |
||||
await expectRevert( |
||||
this.timelock.executeBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: operation is not ready', |
||||
); |
||||
}); |
||||
|
||||
describe('with scheduled operation', function () { |
||||
beforeEach(async function () { |
||||
({ receipt: this.receipt, logs: this.logs } = await this.timelock.scheduleBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
)); |
||||
}); |
||||
|
||||
it('revert if execution comes too early 1/2', async function () { |
||||
await expectRevert( |
||||
this.timelock.executeBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: operation is not ready', |
||||
); |
||||
}); |
||||
|
||||
it('revert if execution comes too early 2/2', async function () { |
||||
const timestamp = await this.timelock.getTimestamp(this.operation.id); |
||||
await time.increaseTo(timestamp - 5); // -1 is to tight, test sometime fails
|
||||
|
||||
await expectRevert( |
||||
this.timelock.executeBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: operation is not ready', |
||||
); |
||||
}); |
||||
|
||||
describe('on time', function () { |
||||
beforeEach(async function () { |
||||
const timestamp = await this.timelock.getTimestamp(this.operation.id); |
||||
await time.increaseTo(timestamp); |
||||
}); |
||||
|
||||
it('executor can reveal', async function () { |
||||
const receipt = await this.timelock.executeBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
); |
||||
for (const i in this.operation.targets) { |
||||
expectEvent(receipt, 'CallExecuted', { |
||||
id: this.operation.id, |
||||
index: web3.utils.toBN(i), |
||||
target: this.operation.targets[i], |
||||
value: web3.utils.toBN(this.operation.values[i]), |
||||
data: this.operation.datas[i], |
||||
}); |
||||
} |
||||
}); |
||||
|
||||
it('prevent non-executor from revealing', async function () { |
||||
await expectRevert( |
||||
this.timelock.executeBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: other }, |
||||
), |
||||
'TimelockController: sender requires permission', |
||||
); |
||||
}); |
||||
|
||||
it('length mismatch #1', async function () { |
||||
await expectRevert( |
||||
this.timelock.executeBatch( |
||||
[], |
||||
this.operation.values, |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: length mismatch', |
||||
); |
||||
}); |
||||
|
||||
it('length mismatch #2', async function () { |
||||
await expectRevert( |
||||
this.timelock.executeBatch( |
||||
this.operation.targets, |
||||
[], |
||||
this.operation.datas, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: length mismatch', |
||||
); |
||||
}); |
||||
|
||||
it('length mismatch #3', async function () { |
||||
await expectRevert( |
||||
this.timelock.executeBatch( |
||||
this.operation.targets, |
||||
this.operation.values, |
||||
[], |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: length mismatch', |
||||
); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
it('partial execution', async function () { |
||||
const operation = genOperationBatch( |
||||
[ |
||||
this.callreceivermock.address, |
||||
this.callreceivermock.address, |
||||
this.callreceivermock.address, |
||||
], |
||||
[ |
||||
0, |
||||
0, |
||||
0, |
||||
], |
||||
[ |
||||
this.callreceivermock.contract.methods.mockFunction().encodeABI(), |
||||
this.callreceivermock.contract.methods.mockFunctionThrows().encodeABI(), |
||||
this.callreceivermock.contract.methods.mockFunction().encodeABI(), |
||||
], |
||||
ZERO_BYTES32, |
||||
'0x8ac04aa0d6d66b8812fb41d39638d37af0a9ab11da507afd65c509f8ed079d3e', |
||||
); |
||||
|
||||
await this.timelock.scheduleBatch( |
||||
operation.targets, |
||||
operation.values, |
||||
operation.datas, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await time.increase(MINDELAY); |
||||
await expectRevert( |
||||
this.timelock.executeBatch( |
||||
operation.targets, |
||||
operation.values, |
||||
operation.datas, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: underlying transaction reverted', |
||||
); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('cancel', function () { |
||||
beforeEach(async function () { |
||||
this.operation = genOperation( |
||||
'0xC6837c44AA376dbe1d2709F13879E040CAb653ca', |
||||
0, |
||||
'0x296e58dd', |
||||
ZERO_BYTES32, |
||||
'0xa2485763600634800df9fc9646fb2c112cf98649c55f63dd1d9c7d13a64399d9', |
||||
); |
||||
({ receipt: this.receipt, logs: this.logs } = await this.timelock.schedule( |
||||
this.operation.target, |
||||
this.operation.value, |
||||
this.operation.data, |
||||
this.operation.predecessor, |
||||
this.operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
)); |
||||
}); |
||||
|
||||
it('proposer can cancel', async function () { |
||||
const receipt = await this.timelock.cancel(this.operation.id, { from: proposer }); |
||||
expectEvent(receipt, 'Cancelled', { id: this.operation.id }); |
||||
}); |
||||
|
||||
it('prevent non-proposer from canceling', async function () { |
||||
await expectRevert( |
||||
this.timelock.cancel(this.operation.id, { from: other }), |
||||
'TimelockController: sender requires permission', |
||||
); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('maintenance', function () { |
||||
it('prevent unauthorized maintenance', async function () { |
||||
await expectRevert( |
||||
this.timelock.updateDelay(0, { from: other }), |
||||
'TimelockController: caller must be timelock', |
||||
); |
||||
}); |
||||
|
||||
it('timelock scheduled maintenance', async function () { |
||||
const newDelay = time.duration.hours(6); |
||||
const operation = genOperation( |
||||
this.timelock.address, |
||||
0, |
||||
this.timelock.contract.methods.updateDelay(newDelay.toString()).encodeABI(), |
||||
ZERO_BYTES32, |
||||
'0xf8e775b2c5f4d66fb5c7fa800f35ef518c262b6014b3c0aee6ea21bff157f108', |
||||
); |
||||
|
||||
await this.timelock.schedule( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await time.increase(MINDELAY); |
||||
const receipt = await this.timelock.execute( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
{ from: executor }, |
||||
); |
||||
expectEvent(receipt, 'MinDelayChange', { newDuration: newDelay.toString(), oldDuration: MINDELAY }); |
||||
|
||||
expect(await this.timelock.getMinDelay()).to.be.bignumber.equal(newDelay); |
||||
}); |
||||
}); |
||||
|
||||
describe('dependency', function () { |
||||
beforeEach(async function () { |
||||
this.operation1 = genOperation( |
||||
'0xdE66bD4c97304200A95aE0AadA32d6d01A867E39', |
||||
0, |
||||
'0x01dc731a', |
||||
ZERO_BYTES32, |
||||
'0x64e932133c7677402ead2926f86205e2ca4686aebecf5a8077627092b9bb2feb', |
||||
); |
||||
this.operation2 = genOperation( |
||||
'0x3c7944a3F1ee7fc8c5A5134ba7c79D11c3A1FCa3', |
||||
0, |
||||
'0x8f531849', |
||||
this.operation1.id, |
||||
'0x036e1311cac523f9548e6461e29fb1f8f9196b91910a41711ea22f5de48df07d', |
||||
); |
||||
await this.timelock.schedule( |
||||
this.operation1.target, |
||||
this.operation1.value, |
||||
this.operation1.data, |
||||
this.operation1.predecessor, |
||||
this.operation1.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await this.timelock.schedule( |
||||
this.operation2.target, |
||||
this.operation2.value, |
||||
this.operation2.data, |
||||
this.operation2.predecessor, |
||||
this.operation2.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await time.increase(MINDELAY); |
||||
}); |
||||
|
||||
it('cannot execute before dependency', async function () { |
||||
await expectRevert( |
||||
this.timelock.execute( |
||||
this.operation2.target, |
||||
this.operation2.value, |
||||
this.operation2.data, |
||||
this.operation2.predecessor, |
||||
this.operation2.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: missing dependency', |
||||
); |
||||
}); |
||||
|
||||
it('can execute after dependency', async function () { |
||||
await this.timelock.execute( |
||||
this.operation1.target, |
||||
this.operation1.value, |
||||
this.operation1.data, |
||||
this.operation1.predecessor, |
||||
this.operation1.salt, |
||||
{ from: executor }, |
||||
); |
||||
await this.timelock.execute( |
||||
this.operation2.target, |
||||
this.operation2.value, |
||||
this.operation2.data, |
||||
this.operation2.predecessor, |
||||
this.operation2.salt, |
||||
{ from: executor }, |
||||
); |
||||
}); |
||||
}); |
||||
|
||||
describe('usage scenario', function () { |
||||
this.timeout(10000); |
||||
|
||||
it('call', async function () { |
||||
const operation = genOperation( |
||||
this.implementation2.address, |
||||
0, |
||||
this.implementation2.contract.methods.setValue(42).encodeABI(), |
||||
ZERO_BYTES32, |
||||
'0x8043596363daefc89977b25f9d9b4d06c3910959ef0c4d213557a903e1b555e2', |
||||
); |
||||
|
||||
await this.timelock.schedule( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await time.increase(MINDELAY); |
||||
await this.timelock.execute( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
{ from: executor }, |
||||
); |
||||
|
||||
expect(await this.implementation2.getValue()).to.be.bignumber.equal(web3.utils.toBN(42)); |
||||
}); |
||||
|
||||
it('call reverting', async function () { |
||||
const operation = genOperation( |
||||
this.callreceivermock.address, |
||||
0, |
||||
this.callreceivermock.contract.methods.mockFunctionRevertsNoReason().encodeABI(), |
||||
ZERO_BYTES32, |
||||
'0xb1b1b276fdf1a28d1e00537ea73b04d56639128b08063c1a2f70a52e38cba693', |
||||
); |
||||
|
||||
await this.timelock.schedule( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await time.increase(MINDELAY); |
||||
await expectRevert( |
||||
this.timelock.execute( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: underlying transaction reverted', |
||||
); |
||||
}); |
||||
|
||||
it('call throw', async function () { |
||||
const operation = genOperation( |
||||
this.callreceivermock.address, |
||||
0, |
||||
this.callreceivermock.contract.methods.mockFunctionThrows().encodeABI(), |
||||
ZERO_BYTES32, |
||||
'0xe5ca79f295fc8327ee8a765fe19afb58f4a0cbc5053642bfdd7e73bc68e0fc67', |
||||
); |
||||
|
||||
await this.timelock.schedule( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await time.increase(MINDELAY); |
||||
await expectRevert( |
||||
this.timelock.execute( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: underlying transaction reverted', |
||||
); |
||||
}); |
||||
|
||||
it('call out of gas', async function () { |
||||
const operation = genOperation( |
||||
this.callreceivermock.address, |
||||
0, |
||||
this.callreceivermock.contract.methods.mockFunctionOutOfGas().encodeABI(), |
||||
ZERO_BYTES32, |
||||
'0xf3274ce7c394c5b629d5215723563a744b817e1730cca5587c567099a14578fd', |
||||
); |
||||
|
||||
await this.timelock.schedule( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await time.increase(MINDELAY); |
||||
await expectRevert( |
||||
this.timelock.execute( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
{ from: executor, gas: '70000' }, |
||||
), |
||||
'TimelockController: underlying transaction reverted', |
||||
); |
||||
}); |
||||
|
||||
it('call payable with eth', async function () { |
||||
const operation = genOperation( |
||||
this.callreceivermock.address, |
||||
1, |
||||
this.callreceivermock.contract.methods.mockFunction().encodeABI(), |
||||
ZERO_BYTES32, |
||||
'0x5ab73cd33477dcd36c1e05e28362719d0ed59a7b9ff14939de63a43073dc1f44', |
||||
); |
||||
|
||||
await this.timelock.schedule( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await time.increase(MINDELAY); |
||||
|
||||
expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
|
||||
await this.timelock.execute( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
{ from: executor, value: 1 }, |
||||
); |
||||
|
||||
expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(1)); |
||||
}); |
||||
|
||||
it('call nonpayable with eth', async function () { |
||||
const operation = genOperation( |
||||
this.callreceivermock.address, |
||||
1, |
||||
this.callreceivermock.contract.methods.mockFunctionNonPayable().encodeABI(), |
||||
ZERO_BYTES32, |
||||
'0xb78edbd920c7867f187e5aa6294ae5a656cfbf0dea1ccdca3751b740d0f2bdf8', |
||||
); |
||||
|
||||
await this.timelock.schedule( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await time.increase(MINDELAY); |
||||
|
||||
expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
|
||||
await expectRevert( |
||||
this.timelock.execute( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: underlying transaction reverted', |
||||
); |
||||
|
||||
expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
}); |
||||
|
||||
it('call reverting with eth', async function () { |
||||
const operation = genOperation( |
||||
this.callreceivermock.address, |
||||
1, |
||||
this.callreceivermock.contract.methods.mockFunctionRevertsNoReason().encodeABI(), |
||||
ZERO_BYTES32, |
||||
'0xdedb4563ef0095db01d81d3f2decf57cf83e4a72aa792af14c43a792b56f4de6', |
||||
); |
||||
|
||||
await this.timelock.schedule( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
MINDELAY, |
||||
{ from: proposer }, |
||||
); |
||||
await time.increase(MINDELAY); |
||||
|
||||
expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
|
||||
await expectRevert( |
||||
this.timelock.execute( |
||||
operation.target, |
||||
operation.value, |
||||
operation.data, |
||||
operation.predecessor, |
||||
operation.salt, |
||||
{ from: executor }, |
||||
), |
||||
'TimelockController: underlying transaction reverted', |
||||
); |
||||
|
||||
expect(await web3.eth.getBalance(this.timelock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
expect(await web3.eth.getBalance(this.callreceivermock.address)).to.be.bignumber.equal(web3.utils.toBN(0)); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,15 @@ |
||||
const { GSNDevProvider } = require('@openzeppelin/gsn-provider'); |
||||
|
||||
function setGSNProvider (Contract, accounts) { |
||||
const baseProvider = Contract.currentProvider; |
||||
Contract.setProvider( |
||||
new GSNDevProvider(baseProvider, { |
||||
txfee: 70, |
||||
useGSN: false, |
||||
ownerAddress: accounts[8], |
||||
relayerAddress: accounts[9], |
||||
}), |
||||
); |
||||
}; |
||||
|
||||
module.exports = { setGSNProvider }; |
@ -1,6 +1,6 @@ |
||||
const { defaultSender, web3 } = require('@openzeppelin/test-environment'); |
||||
const { deployRelayHub } = require('@openzeppelin/gsn-helpers'); |
||||
|
||||
before('deploy GSN RelayHub', async function () { |
||||
const [defaultSender] = await web3.eth.getAccounts(); |
||||
await deployRelayHub(web3, { from: defaultSender }); |
||||
}); |
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue