forked from lidofinance/lido-l2
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
101 additions
and
38 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-FileCopyrightText: 2022 Lido <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
pragma solidity 0.8.10; | ||
|
||
import {UnstructuredStorage} from "../lib//UnstructuredStorage.sol"; | ||
|
||
contract Versioned { | ||
using UnstructuredStorage for bytes32; | ||
|
||
event ContractVersionSet(uint256 version); | ||
|
||
error NonZeroContractVersionOnInit(); | ||
error InvalidContractVersionIncrement(); | ||
error UnexpectedContractVersion(uint256 expected, uint256 received); | ||
|
||
/// @dev Storage slot: uint256 version | ||
/// Version of the initialized contract storage. | ||
/// The version stored in CONTRACT_VERSION_POSITION equals to: | ||
/// - 0 right after the deployment, before an initializer is invoked (and only at that moment); | ||
/// - N after calling initialize(), where N is the initially deployed contract version; | ||
/// - N after upgrading contract by calling finalizeUpgrade_vN(). | ||
bytes32 internal constant CONTRACT_VERSION_POSITION = keccak256("lido.Versioned.contractVersion"); | ||
|
||
uint256 internal constant PETRIFIED_VERSION_MARK = type(uint256).max; | ||
|
||
constructor() { | ||
// lock version in the implementation's storage to prevent initialization | ||
CONTRACT_VERSION_POSITION.setStorageUint256(PETRIFIED_VERSION_MARK); | ||
} | ||
|
||
/// @notice Returns the current contract version. | ||
function getContractVersion() public view returns (uint256) { | ||
return CONTRACT_VERSION_POSITION.getStorageUint256(); | ||
} | ||
|
||
function _checkContractVersion(uint256 version) internal view { | ||
uint256 expectedVersion = getContractVersion(); | ||
if (version != expectedVersion) { | ||
revert UnexpectedContractVersion(expectedVersion, version); | ||
} | ||
} | ||
|
||
/// @dev Sets the contract version to N. Should be called from the initialize() function. | ||
function _initializeContractVersionTo(uint256 version) internal { | ||
if (getContractVersion() != 0) revert NonZeroContractVersionOnInit(); | ||
_setContractVersion(version); | ||
} | ||
|
||
/// @dev Updates the contract version. Should be called from a finalizeUpgrade_vN() function. | ||
function _updateContractVersion(uint256 newVersion) internal { | ||
if (newVersion != getContractVersion() + 1) revert InvalidContractVersionIncrement(); | ||
_setContractVersion(newVersion); | ||
} | ||
|
||
function _setContractVersion(uint256 version) private { | ||
CONTRACT_VERSION_POSITION.setStorageUint256(version); | ||
emit ContractVersionSet(version); | ||
} | ||
} |