Split StorageSlot into TransientSlot (#5239)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Signed-off-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
committed by
Hadrien Croubois
parent
f8432e82d6
commit
1bcd1c65db
@ -39,9 +39,11 @@ for (const [file, template] of Object.entries({
|
||||
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap.js',
|
||||
'utils/SlotDerivation.sol': './templates/SlotDerivation.js',
|
||||
'utils/StorageSlot.sol': './templates/StorageSlot.js',
|
||||
'utils/TransientSlot.sol': './templates/TransientSlot.js',
|
||||
'utils/Arrays.sol': './templates/Arrays.js',
|
||||
'utils/Packing.sol': './templates/Packing.js',
|
||||
'mocks/StorageSlotMock.sol': './templates/StorageSlotMock.js',
|
||||
'mocks/TransientSlotMock.sol': './templates/TransientSlotMock.js',
|
||||
})) {
|
||||
generateFromTemplate(file, template, './contracts/');
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ const format = require('../format-lines');
|
||||
const { TYPES } = require('./Slot.opts');
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.24;
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
/**
|
||||
* @dev Library for reading and writing primitive types to specific storage slots.
|
||||
@ -29,24 +29,6 @@ pragma solidity ^0.8.24;
|
||||
* }
|
||||
* \`\`\`
|
||||
*
|
||||
* Since version 5.1, this library also support writing and reading value types to and from transient storage.
|
||||
*
|
||||
* * Example using transient storage:
|
||||
* \`\`\`solidity
|
||||
* contract Lock {
|
||||
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
|
||||
* bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
|
||||
*
|
||||
* modifier locked() {
|
||||
* require(!_LOCK_SLOT.asBoolean().tload());
|
||||
*
|
||||
* _LOCK_SLOT.asBoolean().tstore(true);
|
||||
* _;
|
||||
* _LOCK_SLOT.asBoolean().tstore(false);
|
||||
* }
|
||||
* }
|
||||
* \`\`\`
|
||||
*
|
||||
* TIP: Consider using this library along with {SlotDerivation}.
|
||||
*/
|
||||
`;
|
||||
@ -81,40 +63,6 @@ function get${name}Slot(${type} storage store) internal pure returns (${name}Slo
|
||||
}
|
||||
`;
|
||||
|
||||
const udvt = ({ type, name }) => `\
|
||||
/**
|
||||
* @dev UDVT that represent a slot holding a ${type}.
|
||||
*/
|
||||
type ${name}SlotType is bytes32;
|
||||
|
||||
/**
|
||||
* @dev Cast an arbitrary slot to a ${name}SlotType.
|
||||
*/
|
||||
function as${name}(bytes32 slot) internal pure returns (${name}SlotType) {
|
||||
return ${name}SlotType.wrap(slot);
|
||||
}
|
||||
`;
|
||||
|
||||
const transient = ({ type, name }) => `\
|
||||
/**
|
||||
* @dev Load the value held at location \`slot\` in transient storage.
|
||||
*/
|
||||
function tload(${name}SlotType slot) internal view returns (${type} value) {
|
||||
assembly ("memory-safe") {
|
||||
value := tload(slot)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Store \`value\` at location \`slot\` in transient storage.
|
||||
*/
|
||||
function tstore(${name}SlotType slot, ${type} value) internal {
|
||||
assembly ("memory-safe") {
|
||||
tstore(slot, value)
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header.trimEnd(),
|
||||
@ -123,8 +71,6 @@ module.exports = format(
|
||||
[].concat(
|
||||
TYPES.map(type => struct(type)),
|
||||
TYPES.flatMap(type => [get(type), !type.isValueType && getStorage(type)].filter(Boolean)),
|
||||
TYPES.filter(type => type.isValueType).map(type => udvt(type)),
|
||||
TYPES.filter(type => type.isValueType).map(type => transient(type)),
|
||||
),
|
||||
).trimEnd(),
|
||||
'}',
|
||||
|
||||
@ -2,7 +2,7 @@ const format = require('../format-lines');
|
||||
const { TYPES } = require('./Slot.opts');
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.24;
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {Multicall} from "../utils/Multicall.sol";
|
||||
import {StorageSlot} from "../utils/StorageSlot.sol";
|
||||
@ -40,18 +40,6 @@ function get${name}Storage(uint256 key) public view returns (${type} memory) {
|
||||
}
|
||||
`;
|
||||
|
||||
const transient = ({ type, name }) => `\
|
||||
event ${name}Value(bytes32 slot, ${type} value);
|
||||
|
||||
function tload${name}(bytes32 slot) public {
|
||||
emit ${name}Value(slot, slot.as${name}().tload());
|
||||
}
|
||||
|
||||
function tstore(bytes32 slot, ${type} value) public {
|
||||
slot.as${name}().tstore(value);
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header,
|
||||
@ -63,7 +51,6 @@ module.exports = format(
|
||||
TYPES.filter(type => type.isValueType).map(type => storageSetValueType(type)),
|
||||
TYPES.filter(type => type.isValueType).map(type => storageGetValueType(type)),
|
||||
TYPES.filter(type => !type.isValueType).map(type => storageSetNonValueType(type)),
|
||||
TYPES.filter(type => type.isValueType).map(type => transient(type)),
|
||||
),
|
||||
).trimEnd(),
|
||||
'}',
|
||||
|
||||
80
scripts/generate/templates/TransientSlot.js
Normal file
80
scripts/generate/templates/TransientSlot.js
Normal file
@ -0,0 +1,80 @@
|
||||
const format = require('../format-lines');
|
||||
const { TYPES } = require('./Slot.opts');
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.24;
|
||||
|
||||
/**
|
||||
* @dev Library for reading and writing value-types to specific transient storage slots.
|
||||
*
|
||||
* Transient slots are often used to store temporary values that are removed after the current transaction.
|
||||
* This library helps with reading and writing to such slots without the need for inline assembly.
|
||||
*
|
||||
* * Example reading and writing values using transient storage:
|
||||
* \`\`\`solidity
|
||||
* contract Lock {
|
||||
* using TransientSlot for *;
|
||||
*
|
||||
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
|
||||
* bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
|
||||
*
|
||||
* modifier locked() {
|
||||
* require(!_LOCK_SLOT.asBoolean().tload());
|
||||
*
|
||||
* _LOCK_SLOT.asBoolean().tstore(true);
|
||||
* _;
|
||||
* _LOCK_SLOT.asBoolean().tstore(false);
|
||||
* }
|
||||
* }
|
||||
* \`\`\`
|
||||
*
|
||||
* TIP: Consider using this library along with {SlotDerivation}.
|
||||
*/
|
||||
`;
|
||||
|
||||
const udvt = ({ type, name }) => `\
|
||||
/**
|
||||
* @dev UDVT that represent a slot holding a ${type}.
|
||||
*/
|
||||
type ${name}Slot is bytes32;
|
||||
|
||||
/**
|
||||
* @dev Cast an arbitrary slot to a ${name}Slot.
|
||||
*/
|
||||
function as${name}(bytes32 slot) internal pure returns (${name}Slot) {
|
||||
return ${name}Slot.wrap(slot);
|
||||
}
|
||||
`;
|
||||
|
||||
const transient = ({ type, name }) => `\
|
||||
/**
|
||||
* @dev Load the value held at location \`slot\` in transient storage.
|
||||
*/
|
||||
function tload(${name}Slot slot) internal view returns (${type} value) {
|
||||
assembly ("memory-safe") {
|
||||
value := tload(slot)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Store \`value\` at location \`slot\` in transient storage.
|
||||
*/
|
||||
function tstore(${name}Slot slot, ${type} value) internal {
|
||||
assembly ("memory-safe") {
|
||||
tstore(slot, value)
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header.trimEnd(),
|
||||
'library TransientSlot {',
|
||||
format(
|
||||
[].concat(
|
||||
TYPES.filter(type => type.isValueType).map(type => udvt(type)),
|
||||
TYPES.filter(type => type.isValueType).map(type => transient(type)),
|
||||
),
|
||||
).trimEnd(),
|
||||
'}',
|
||||
);
|
||||
35
scripts/generate/templates/TransientSlotMock.js
Normal file
35
scripts/generate/templates/TransientSlotMock.js
Normal file
@ -0,0 +1,35 @@
|
||||
const format = require('../format-lines');
|
||||
const { TYPES } = require('./Slot.opts');
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.24;
|
||||
|
||||
import {Multicall} from "../utils/Multicall.sol";
|
||||
import {TransientSlot} from "../utils/TransientSlot.sol";
|
||||
`;
|
||||
|
||||
const transient = ({ type, name }) => `\
|
||||
event ${name}Value(bytes32 slot, ${type} value);
|
||||
|
||||
function tload${name}(bytes32 slot) public {
|
||||
emit ${name}Value(slot, slot.as${name}().tload());
|
||||
}
|
||||
|
||||
function tstore(bytes32 slot, ${type} value) public {
|
||||
slot.as${name}().tstore(value);
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header,
|
||||
'contract TransientSlotMock is Multicall {',
|
||||
format(
|
||||
[].concat(
|
||||
'using TransientSlot for *;',
|
||||
'',
|
||||
TYPES.filter(type => type.isValueType).map(type => transient(type)),
|
||||
),
|
||||
).trimEnd(),
|
||||
'}',
|
||||
);
|
||||
Reference in New Issue
Block a user