Skip to content

Commit

Permalink
better comments
Browse files Browse the repository at this point in the history
  • Loading branch information
josojo committed Jan 4, 2024
1 parent de53ab2 commit 37b215d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
15 changes: 10 additions & 5 deletions contracts/ChainIdManager.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.20;

// The ChainIdManager contract provides a list of new usable chainIds via the getNextUsableChainId function.
// If a project uses one chainId, it can be added to the deny list and then no other project will use it.
// The contract does not come with an owner, but anyone can add chainIds to the deny list.
// It costs just gasBurnAmount to deny list a chainId.
// The burnGas function introduces this cost to use up chainIds.
// There are uint64(2**63=9.223372e+18) chainIds minus the publicly used chainIds available.
// Using all of the chainIds would cost 9.223372e+18 * gasBurnAmount = 9.223372e+24 gas = 6.1489147e+17 blocks = 237226647377 years

contract ChainIdManager {
// Counter for the number of Chain IDs
uint64 public chainIdCounter = 0;
// contains a list of denied Chain IDs that should not be used as chainIds
// they are governed by a owner of the contract
// they are anyone who is willing to pay the gas to use them.
mapping(uint64 => bool) public deniedChainIds;
// Fee to use up a chain ID
uint256 public immutable gasBurnAmount = 1000000;
Expand All @@ -27,7 +35,7 @@ contract ChainIdManager {
* @dev Adds multiple Chain IDs to the deny list
* @param chainIds The Chain IDs to add
*/
function denyListChainIds(uint64[] memory chainIds) public {
function denyListChainIds(uint64[] memory chainIds) public {
for (uint256 i = 0; i < chainIds.length; i++) {
denyListChainId(chainIds[i]);
}
Expand All @@ -38,9 +46,6 @@ contract ChainIdManager {
* @return chainId The next usable Chain ID
*/
function getNextUsableChainId() public returns (uint64 chainId) {
// The burnGas function introduces a cost to use up chainIds.
// There are uint64(2**63=9.223372e+18) chainIds minus the publicly used chainIds available.
// Using all of the chainIds would cost 9.223372e+18 * gasBurnAmount = 9.223372e+24 gas = 6.1489147e+17 blocks = 237226647377 years
burnGas();
while (deniedChainIds[chainIdCounter]) {
chainIdCounter++;
Expand Down
15 changes: 7 additions & 8 deletions test/ChainIdManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ contract ChainIdManagerTest is Test {
uint256 finalGas = gasleft();
assert(currentGas - finalGas >= chainIdManager.gasBurnAmount());


uint256 currentGas2 = gasleft();
uint64[] memory newChainIds = new uint64[](6);
newChainIds[0]=3;
newChainIds[1]=4;
newChainIds[2]=5;
newChainIds[3]=6;
newChainIds[4]=7;
newChainIds[5]=8;
uint64[] memory newChainIds = new uint64[](6);
newChainIds[0] = 3;
newChainIds[1] = 4;
newChainIds[2] = 5;
newChainIds[3] = 6;
newChainIds[4] = 7;
newChainIds[5] = 8;
chainIdManager.denyListChainIds(newChainIds);
uint256 finalGas2 = gasleft();
assert(currentGas2 - finalGas2 >= chainIdManager.gasBurnAmount() * 6);
Expand Down

0 comments on commit 37b215d

Please sign in to comment.