Skip to content

Commit

Permalink
Merge pull request #138 from CirclesUBI/20240412-operate-flow-reentre…
Browse files Browse the repository at this point in the history
…ncy-guard

20240412 operate flow reentrency guard
  • Loading branch information
jaensen authored May 3, 2024
2 parents 9461fb2 + 9467030 commit f17bb8f
Show file tree
Hide file tree
Showing 28 changed files with 65 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/circles/Circles.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "../lib/Math64x64.sol";
import "./ERC1155.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/circles/Demurrage.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "../errors/Errors.sol";
import "../lib/Math64x64.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/circles/DiscountedBalances.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "../lib/Math64x64.sol";
import "./Demurrage.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/circles/ICircles.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "./IDemurrage.sol";

Expand Down
2 changes: 1 addition & 1 deletion src/circles/IDemurrage.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

interface IDemurrage {
function inflationDayZero() external view returns (uint256);
Expand Down
4 changes: 3 additions & 1 deletion src/errors/Errors.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

interface IHubErrors {
error CirclesHubOnlyDuringBootstrap(uint8 code);
Expand Down Expand Up @@ -69,6 +69,8 @@ interface ICirclesErrors {
error CirclesLogicAssertion(uint8 code);

error CirclesIdMustBeDerivedFromAddress(uint256 providedId, uint8 code);

error CirclesReentrancyGuard(uint8 code);
}

interface IStandardTreasuryErrors {
Expand Down
2 changes: 1 addition & 1 deletion src/groups/BaseMintPolicy.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "./IMintPolicy.sol";
import "./Definitions.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/groups/Definitions.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

contract BaseMintPolicyDefinitions {
// Type declarations
Expand Down
2 changes: 1 addition & 1 deletion src/groups/IMintPolicy.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

interface IMintPolicy {
function beforeMintPolicy(
Expand Down
38 changes: 36 additions & 2 deletions src/hub/Hub.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "../circles/Circles.sol";
import "../errors/Errors.sol";
Expand Down Expand Up @@ -131,6 +131,14 @@ contract Hub is Circles, MetadataDefinitions, IHubErrors, ICirclesErrors {
*/
mapping(address => mapping(address => TrustMarker)) public trustMarkers;

/**
* @dev Normal storage slot for the reentrancy guard.
* todo: the original task was to use a transient storage slot,
* but solc v0.8.24 still didn't recognize the tload/tstore assembly.
* see
*/
bool private _reentrancyGuard;

// Events

event RegisterHuman(address indexed avatar);
Expand Down Expand Up @@ -166,6 +174,32 @@ contract Hub is Circles, MetadataDefinitions, IHubErrors, ICirclesErrors {
_;
}

/**
* @dev Reentrancy guard for nonReentrant functions.
* see https://soliditylang.org/blog/2024/01/26/transient-storage/
*/
modifier nonReentrant(uint8 _code) {
// todo: this should use transient storage slot
// but didn't compile; investigate
// assembly {
// if tload(0) { revert(0, 0) }
// tstore(0, 1)
// }
// _;
// assembly {
// tstore(0, 0)
// }

// for now, default to normal storage slot
// replace this later with transient storage slot
if (_reentrancyGuard) {
revert CirclesReentrancyGuard(_code);
}
_reentrancyGuard = true;
_;
_reentrancyGuard = false;
}

// Constructor

/**
Expand Down Expand Up @@ -527,7 +561,7 @@ contract Hub is Circles, MetadataDefinitions, IHubErrors, ICirclesErrors {
FlowEdge[] calldata _flow,
Stream[] calldata _streams,
bytes calldata _packedCoordinates
) external {
) external nonReentrant(0) {
// first unpack the coordinates to array of uint16
uint16[] memory coordinates = _unpackCoordinates(_packedCoordinates, _flow.length);

Expand Down
2 changes: 1 addition & 1 deletion src/hub/IHub.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "../circles/ICircles.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/hub/MetadataDefinitions.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

contract MetadataDefinitions {
// Type declarations
Expand Down
2 changes: 1 addition & 1 deletion src/lift/DemurrageCircles.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/lift/ERC20DiscountedBalances.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../circles/Demurrage.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/lift/ERC20InflationaryBalances.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../circles/Demurrage.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/lift/ERC20Lift.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "@openzeppelin/contracts/utils/Create2.sol";
import "../errors/Errors.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/lift/ERC20Permit.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/lift/IERC20Lift.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

enum CirclesType {
Demurrage,
Expand Down
2 changes: 1 addition & 1 deletion src/lift/InflationaryCircles.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/migration/IHub.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

/**
* @title IHub v1
Expand Down
2 changes: 1 addition & 1 deletion src/migration/IToken.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

Expand Down
2 changes: 1 addition & 1 deletion src/migration/Migration.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "../errors/Errors.sol";
import "../hub/IHub.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/names/Base58Converter.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

contract Base58Converter {
string internal constant ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
Expand Down
2 changes: 1 addition & 1 deletion src/names/INameRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

interface INameRegistry {
function updateCidV0Digest(address avatar, bytes32 cidVoDigest) external;
Expand Down
2 changes: 1 addition & 1 deletion src/names/NameRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "../errors/Errors.sol";
import "../hub/IHub.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/treasury/IStandardVault.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

interface IStandardVault {
function returnCollateral(address receiver, uint256[] calldata ids, uint256[] calldata values, bytes calldata data)
Expand Down
2 changes: 1 addition & 1 deletion src/treasury/StandardTreasury.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/treasury/StandardVault.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.13;
pragma solidity >=0.8.24;

import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import "../errors/Errors.sol";
Expand Down

0 comments on commit f17bb8f

Please sign in to comment.