Add transient storage slot support in StorageSlot.sol (#4980)

Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
Hadrien Croubois
2024-04-04 01:15:30 +02:00
committed by GitHub
parent 2d259ac346
commit d6ad9db0a0
17 changed files with 1829 additions and 1134 deletions

View File

@ -375,7 +375,7 @@ describe('GovernorTimelockAccess', function () {
if (delay > 0) {
await this.helper.waitForEta();
}
expect(await this.helper.execute())
await expect(this.helper.execute())
.to.emit(this.mock, 'ProposalExecuted')
.withArgs(this.proposal.id)
.to.emit(this.receiver, 'CalledUnrestricted');

5
test/helpers/strings.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = {
// Capitalize the first char of a string
// Example: capitalize('uint256') → 'Uint256'
capitalize: str => str.charAt(0).toUpperCase() + str.slice(1),
};

View File

@ -6,10 +6,18 @@ const { generators } = require('../helpers/random');
const slot = ethers.id('some.storage.slot');
const otherSlot = ethers.id('some.other.storage.slot');
const TYPES = [
{ name: 'Boolean', type: 'bool', value: true, isValueType: true, zero: false },
{ name: 'Address', type: 'address', value: generators.address(), isValueType: true, zero: generators.address.zero },
{ name: 'Bytes32', type: 'bytes32', value: generators.bytes32(), isValueType: true, zero: generators.bytes32.zero },
{ name: 'Uint256', type: 'uint256', value: generators.uint256(), isValueType: true, zero: generators.uint256.zero },
{ name: 'Int256', type: 'int256', value: generators.int256(), isValueType: true, zero: generators.int256.zero },
{ name: 'Bytes', type: 'bytes', value: generators.hexBytes(128), isValueType: false, zero: generators.hexBytes.zero },
{ name: 'String', type: 'string', value: 'lorem ipsum', isValueType: false, zero: '' },
];
async function fixture() {
const [account] = await ethers.getSigners();
const mock = await ethers.deployContract('StorageSlotMock');
return { mock, account };
return { mock: await ethers.deployContract('StorageSlotMock') };
}
describe('StorageSlot', function () {
@ -17,60 +25,82 @@ describe('StorageSlot', function () {
Object.assign(this, await loadFixture(fixture));
});
for (const { type, value, zero } of [
{ type: 'Boolean', value: true, zero: false },
{ type: 'Address', value: generators.address(), zero: generators.address.zero },
{ type: 'Bytes32', value: generators.bytes32(), zero: generators.bytes32.zero },
{ type: 'Uint256', value: generators.uint256(), zero: generators.uint256.zero },
{ type: 'Int256', value: generators.int256(), zero: generators.int256.zero },
{ type: 'Bytes', value: generators.hexBytes(128), zero: generators.hexBytes.zero },
{ type: 'String', value: 'lorem ipsum', zero: '' },
]) {
for (const { name, type, value, zero } of TYPES) {
describe(`${type} storage slot`, function () {
it('set', async function () {
await this.mock.getFunction(`set${type}Slot`)(slot, value);
await this.mock.getFunction(`set${name}Slot`)(slot, value);
});
describe('get', function () {
beforeEach(async function () {
await this.mock.getFunction(`set${type}Slot`)(slot, value);
await this.mock.getFunction(`set${name}Slot`)(slot, value);
});
it('from right slot', async function () {
expect(await this.mock.getFunction(`get${type}Slot`)(slot)).to.equal(value);
expect(await this.mock.getFunction(`get${name}Slot`)(slot)).to.equal(value);
});
it('from other slot', async function () {
expect(await this.mock.getFunction(`get${type}Slot`)(otherSlot)).to.equal(zero);
expect(await this.mock.getFunction(`get${name}Slot`)(otherSlot)).to.equal(zero);
});
});
});
}
for (const { type, value, zero } of [
{ type: 'String', value: 'lorem ipsum', zero: '' },
{ type: 'Bytes', value: generators.hexBytes(128), zero: '0x' },
]) {
for (const { name, type, value, zero } of TYPES.filter(type => !type.isValueType)) {
describe(`${type} storage pointer`, function () {
it('set', async function () {
await this.mock.getFunction(`set${type}Storage`)(slot, value);
await this.mock.getFunction(`set${name}Storage`)(slot, value);
});
describe('get', function () {
beforeEach(async function () {
await this.mock.getFunction(`set${type}Storage`)(slot, value);
await this.mock.getFunction(`set${name}Storage`)(slot, value);
});
it('from right slot', async function () {
expect(await this.mock.getFunction(`${type.toLowerCase()}Map`)(slot)).to.equal(value);
expect(await this.mock.getFunction(`get${type}Storage`)(slot)).to.equal(value);
expect(await this.mock.getFunction(`${type}Map`)(slot)).to.equal(value);
expect(await this.mock.getFunction(`get${name}Storage`)(slot)).to.equal(value);
});
it('from other slot', async function () {
expect(await this.mock.getFunction(`${type.toLowerCase()}Map`)(otherSlot)).to.equal(zero);
expect(await this.mock.getFunction(`get${type}Storage`)(otherSlot)).to.equal(zero);
expect(await this.mock.getFunction(`${type}Map`)(otherSlot)).to.equal(zero);
expect(await this.mock.getFunction(`get${name}Storage`)(otherSlot)).to.equal(zero);
});
});
});
}
for (const { name, type, value, zero } of TYPES.filter(type => type.isValueType)) {
describe(`${type} transient slot`, function () {
const load = `tload${name}(bytes32)`;
const store = `tstore(bytes32,${type})`;
const event = `${name}Value`;
it('load', async function () {
await expect(this.mock[load](slot)).to.emit(this.mock, event).withArgs(slot, zero);
});
it('store and load (2 txs)', async function () {
await this.mock[store](slot, value);
await expect(this.mock[load](slot)).to.emit(this.mock, event).withArgs(slot, zero);
});
it('store and load (batched)', async function () {
await expect(
this.mock.multicall([
this.mock.interface.encodeFunctionData(store, [slot, value]),
this.mock.interface.encodeFunctionData(load, [slot]),
this.mock.interface.encodeFunctionData(load, [otherSlot]),
]),
)
.to.emit(this.mock, event)
.withArgs(slot, value)
.to.emit(this.mock, event)
.withArgs(otherSlot, zero);
await expect(this.mock[load](slot)).to.emit(this.mock, event).withArgs(slot, zero);
});
});
}
});