Skip to content

Commit

Permalink
Merge branch 'master' into interfaces/erc/5313
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx authored Feb 3, 2023
2 parents 7f21b79 + 740ce2d commit d036a21
Show file tree
Hide file tree
Showing 16 changed files with 553 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-games-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`StorageSlot`: Add support for `string` and `bytes`.
55 changes: 55 additions & 0 deletions .github/actions/storage-layout/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Compare storage layouts
inputs:
token:
description: github token
required: true
buildinfo:
description: compilation artifacts
required: false
default: artifacts/build-info/*.json
layout:
description: extracted storage layout
required: false
default: HEAD.layout.json
out_layout:
description: storage layout to upload
required: false
default: ${{ github.ref_name }}.layout.json
ref_layout:
description: storage layout for the reference branch
required: false
default: ${{ github.base_ref }}.layout.json

runs:
using: composite
steps:
- name: Extract layout
run: |
node scripts/checks/extract-layout.js ${{ inputs.buildinfo }} > ${{ inputs.layout }}
shell: bash
- name: Download reference
if: github.event_name == 'pull_request'
run: |
RUN_ID=`gh run list --repo ${{ github.repository }} --branch ${{ github.base_ref }} --workflow ${{ github.workflow }} --limit 100 --json 'conclusion,databaseId,event' --jq 'map(select(.conclusion=="success" and .event!="pull_request"))[0].databaseId'`
gh run download ${RUN_ID} --repo ${{ github.repository }} -n layout
env:
GITHUB_TOKEN: ${{ inputs.token }}
shell: bash
continue-on-error: true
id: reference
- name: Compare layouts
if: steps.reference.outcome == 'success' && github.event_name == 'pull_request'
run: |
node scripts/checks/compare-layout.js --head ${{ inputs.layout }} --ref ${{ inputs.ref_layout }} >> $GITHUB_STEP_SUMMARY
shell: bash
- name: Rename artifacts for upload
if: github.event_name != 'pull_request'
run: |
mv ${{ inputs.layout }} ${{ inputs.out_layout }}
shell: bash
- name: Save artifacts
if: github.event_name != 'pull_request'
uses: actions/upload-artifact@v3
with:
name: layout
path: ${{ inputs.out_layout }}
4 changes: 4 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ jobs:
uses: ./.github/actions/gas-compare
with:
token: ${{ github.token }}
- name: Check storage layout
uses: ./.github/actions/storage-layout
with:
token: ${{ github.token }}

foundry-tests:
if: github.repository != 'OpenZeppelin/openzeppelin-contracts-upgradeable'
Expand Down
4 changes: 2 additions & 2 deletions contracts/governance/IGovernor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract contract IGovernor is IERC165 {
* @dev Emitted when a vote is cast with params.
*
* Note: `support` values should be seen as buckets. Their interpretation depends on the voting module used.
* `params` are additional encoded parameters. Their intepepretation also depends on the voting module used.
* `params` are additional encoded parameters. Their interpepretation also depends on the voting module used.
*/
event VoteCastWithParams(
address indexed voter,
Expand Down Expand Up @@ -140,7 +140,7 @@ abstract contract IGovernor is IERC165 {

/**
* @notice module:user-config
* @dev Delay, in number of block, between the proposal is created and the vote starts. This can be increassed to
* @dev Delay, in number of block, between the proposal is created and the vote starts. This can be increased to
* leave time for users to buy voting power, or delegate it, before the voting of a proposal starts.
*/
function votingDelay() public view virtual returns (uint256);
Expand Down
38 changes: 37 additions & 1 deletion contracts/mocks/StorageSlotMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pragma solidity ^0.8.0;
import "../utils/StorageSlot.sol";

contract StorageSlotMock {
using StorageSlot for bytes32;
using StorageSlot for *;

function setBoolean(bytes32 slot, bool value) public {
slot.getBooleanSlot().value = value;
Expand Down Expand Up @@ -38,4 +38,40 @@ contract StorageSlotMock {
function getUint256(bytes32 slot) public view returns (uint256) {
return slot.getUint256Slot().value;
}

mapping(uint256 => string) public stringMap;

function setString(bytes32 slot, string calldata value) public {
slot.getStringSlot().value = value;
}

function setStringStorage(uint256 key, string calldata value) public {
stringMap[key].getStringSlot().value = value;
}

function getString(bytes32 slot) public view returns (string memory) {
return slot.getStringSlot().value;
}

function getStringStorage(uint256 key) public view returns (string memory) {
return stringMap[key].getStringSlot().value;
}

mapping(uint256 => bytes) public bytesMap;

function setBytes(bytes32 slot, bytes calldata value) public {
slot.getBytesSlot().value = value;
}

function setBytesStorage(uint256 key, bytes calldata value) public {
bytesMap[key].getBytesSlot().value = value;
}

function getBytes(bytes32 slot) public view returns (bytes memory) {
return slot.getBytesSlot().value;
}

function getBytesStorage(uint256 key) public view returns (bytes memory) {
return bytesMap[key].getBytesSlot().value;
}
}
52 changes: 51 additions & 1 deletion contracts/utils/StorageSlot.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/StorageSlot.sol)
// This file was procedurally generated from scripts/generate/templates/StorageSlot.js.

pragma solidity ^0.8.0;

Expand Down Expand Up @@ -27,7 +28,8 @@ pragma solidity ^0.8.0;
* }
* ```
*
* _Available since v4.1 for `address`, `bool`, `bytes32`, and `uint256`._
* _Available since v4.1 for `address`, `bool`, `bytes32`, `uint256`._
* _Available since v4.9 for `string`, `bytes`._
*/
library StorageSlot {
struct AddressSlot {
Expand All @@ -46,6 +48,14 @@ library StorageSlot {
uint256 value;
}

struct StringSlot {
string value;
}

struct BytesSlot {
bytes value;
}

/**
* @dev Returns an `AddressSlot` with member `value` located at `slot`.
*/
Expand Down Expand Up @@ -85,4 +95,44 @@ library StorageSlot {
r.slot := slot
}
}

/**
* @dev Returns an `StringSlot` with member `value` located at `slot`.
*/
function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}

/**
* @dev Returns an `StringSlot` representation of the string storage pointer `store`.
*/
function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := store.slot
}
}

/**
* @dev Returns an `BytesSlot` with member `value` located at `slot`.
*/
function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}

/**
* @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
*/
function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := store.slot
}
}
}
2 changes: 0 additions & 2 deletions docs/modules/ROOT/pages/extending-contracts.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ contract ModifiedAccessControl is AccessControl {

The `super.revokeRole` statement at the end will invoke ``AccessControl``'s original version of `revokeRole`, the same code that would've run if there were no overrides in place.

NOTE: As of v3.0.0, `view` functions are not `virtual` in OpenZeppelin, and therefore cannot be overridden. We're considering https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2154[lifting this restriction] in an upcoming release. Let us know if this is something you care about!

[[using-hooks]]
== Using Hooks

Expand Down
Loading

0 comments on commit d036a21

Please sign in to comment.