-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
3 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
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,39 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import { LibString } from "@solady/utils/LibString.sol"; | ||
|
||
/// @title Semver | ||
/// @notice Semver is a simple contract for ensuring that contracts are | ||
/// versioned using semantic versioning. | ||
abstract contract Semver { | ||
/// @notice Spacing constant for version components. Used to convert the MAJOR, MINOR, and PATCH components to a | ||
/// uint64. | ||
uint64 private constant SPACING = 1_000_000; | ||
|
||
struct Versions { | ||
uint16 major; | ||
uint16 minor; | ||
uint16 patch; | ||
} | ||
|
||
/// @notice Getter for the semantic version of the contract. This must be implemented on each contract. | ||
function _version() internal pure virtual returns (Versions memory); | ||
|
||
/// @notice Getter for the semantic version of the contract. This is not | ||
/// meant to be used onchain but instead meant to be used by offchain | ||
/// tooling. | ||
/// @return Semver contract version as a string. | ||
function version() external view returns (string memory) { | ||
Versions memory v = _version(); | ||
return string.concat( | ||
LibString.toString(v.major), ".", LibString.toString(v.minor), ".", LibString.toString(v.patch) | ||
); | ||
} | ||
|
||
/// @notice Converts a contact's semver to a uint64 with padding for use with the reinitializer modifier. | ||
function reinitializerValue() public pure returns (uint64) { | ||
Versions memory v = _version(); | ||
return (v.major * SPACING ** 2) + (v.minor * SPACING) + v.patch; | ||
} | ||
} |
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,34 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.15; | ||
|
||
import { Test } from "forge-std/Test.sol"; | ||
import { Semver } from "src/universal/Semver.sol"; | ||
|
||
/// @notice A mock contract that implements Semver | ||
contract MockSemver is Semver { | ||
function _version() internal pure override returns (Semver.Versions memory) { | ||
return Semver.Versions(1, 2, 3); | ||
} | ||
} | ||
|
||
/// @title SemverTest | ||
/// @notice Tests for the Semver interface | ||
contract SemverTest is Test { | ||
/// @notice The mock Semver contract | ||
MockSemver semver; | ||
|
||
/// @notice Deploy the mock contract | ||
function setUp() public { | ||
semver = new MockSemver(); | ||
} | ||
|
||
/// @notice Test that the version is returned correctly | ||
function test_version() public { | ||
assertEq(semver.version(), "1.2.3"); | ||
} | ||
|
||
/// @notice Test that the reinitializerValue function returns the correct uint64 | ||
function test_reinitializerValue() public { | ||
assertEq(uint256(semver.reinitializerValue()), 1000002000003); | ||
} | ||
} |