Migrate AccessManager tests to ethers (#4710)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
Ernesto García
2023-11-09 15:56:54 +00:00
committed by GitHub
parent cb1ef861e5
commit cf6ff90b6d
10 changed files with 1240 additions and 1447 deletions

View File

@ -1,23 +1,23 @@
const { time } = require('@openzeppelin/test-helpers');
const { MAX_UINT64 } = require('./constants');
const { namespaceSlot } = require('./namespaced-storage');
const {
time: { setNextBlockTimestamp },
} = require('@nomicfoundation/hardhat-network-helpers');
bigint: { MAX_UINT64 },
} = require('./constants');
const { namespaceSlot } = require('./namespaced-storage');
const { bigint: time } = require('./time');
const { keccak256, AbiCoder } = require('ethers');
function buildBaseRoles() {
const roles = {
ADMIN: {
id: web3.utils.toBN(0),
id: 0n,
},
SOME_ADMIN: {
id: web3.utils.toBN(17),
id: 17n,
},
SOME_GUARDIAN: {
id: web3.utils.toBN(35),
id: 35n,
},
SOME: {
id: web3.utils.toBN(42),
id: 42n,
},
PUBLIC: {
id: MAX_UINT64,
@ -53,23 +53,27 @@ const CONSUMING_SCHEDULE_STORAGE_SLOT = namespaceSlot('AccessManaged', 0n);
/**
* @requires this.{manager, caller, target, calldata}
*/
async function scheduleOperation(manager, { caller, target, calldata, delay }) {
const timestamp = await time.latest();
const scheduledAt = timestamp.addn(1);
await setNextBlockTimestamp(scheduledAt); // Fix next block timestamp for predictability
const { receipt } = await manager.schedule(target, calldata, scheduledAt.add(delay), {
from: caller,
});
async function prepareOperation(manager, { caller, target, calldata, delay }) {
const timestamp = await time.clock.timestamp();
const scheduledAt = timestamp + 1n;
await time.forward.timestamp(scheduledAt, false); // Fix next block timestamp for predictability
return {
receipt,
schedule: () => manager.connect(caller).schedule(target, calldata, scheduledAt + delay),
scheduledAt,
operationId: hashOperation(caller, target, calldata),
};
}
const lazyGetAddress = addressable => addressable.address ?? addressable.target ?? addressable;
const hashOperation = (caller, target, data) =>
web3.utils.keccak256(web3.eth.abi.encodeParameters(['address', 'address', 'bytes'], [caller, target, data]));
keccak256(
AbiCoder.defaultAbiCoder().encode(
['address', 'address', 'bytes'],
[lazyGetAddress(caller), lazyGetAddress(target), data],
),
);
module.exports = {
buildBaseRoles,
@ -78,6 +82,6 @@ module.exports = {
EXPIRATION,
EXECUTION_ID_STORAGE_SLOT,
CONSUMING_SCHEDULE_STORAGE_SLOT,
scheduleOperation,
prepareOperation,
hashOperation,
};

View File

@ -1,5 +1,12 @@
// TODO: deprecate the old version in favor of this one
const bigint = {
MAX_UINT48: 2n ** 48n - 1n,
MAX_UINT64: 2n ** 64n - 1n,
};
// TODO: remove toString() when bigint are supported
module.exports = {
MAX_UINT48: (2n ** 48n - 1n).toString(),
MAX_UINT64: (2n ** 64n - 1n).toString(),
MAX_UINT48: bigint.MAX_UINT48.toString(),
MAX_UINT64: bigint.MAX_UINT64.toString(),
bigint,
};

View File

@ -1,16 +1,14 @@
const { keccak256, id, toBeHex, MaxUint256 } = require('ethers');
const { artifacts } = require('hardhat');
function namespaceId(contractName) {
return `openzeppelin.storage.${contractName}`;
}
function namespaceLocation(id) {
const hashIdBN = web3.utils.toBN(web3.utils.keccak256(id)).subn(1); // keccak256(id) - 1
const hashIdHex = web3.utils.padLeft(web3.utils.numberToHex(hashIdBN), 64);
const mask = BigInt(web3.utils.padLeft('0x00', 64, 'f')); // ~0xff
return BigInt(web3.utils.keccak256(hashIdHex)) & mask;
function namespaceLocation(value) {
const hashIdBN = BigInt(id(value)) - 1n; // keccak256(id) - 1
const mask = MaxUint256 - 0xffn; // ~0xff
return BigInt(keccak256(toBeHex(hashIdBN, 32))) & mask;
}
function namespaceSlot(contractName, offset) {