From e9cb195a346de1c7c635696a95247f811244d5e3 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 26 Jul 2024 17:15:25 +0200 Subject: [PATCH] first experimental cleanup of the registry --- README.md | 2 - contracts/Registry.sol | 243 --------------------- contracts/ValidatorSetHbbft.sol | 7 +- contracts/interfaces/IMetadataRegistry.sol | 21 -- contracts/interfaces/IOwnerRegistry.sol | 14 -- contracts/interfaces/IReverseRegistry.sol | 15 -- docs/docs/index.md | 2 - initial-contracts.json | 6 +- tasks/make_spec.ts | 20 +- 9 files changed, 16 insertions(+), 314 deletions(-) delete mode 100644 contracts/Registry.sol delete mode 100644 contracts/interfaces/IMetadataRegistry.sol delete mode 100644 contracts/interfaces/IOwnerRegistry.sol delete mode 100644 contracts/interfaces/IReverseRegistry.sol diff --git a/README.md b/README.md index dee3fe76..ab72d6e5 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,6 @@ Check also the [BlockRewardHbbft call graph](/docs/BlockRewardHbbft-call-graph.p - `setCurrentSeed`. This is a setter that can only be called by the HBBFT engine in order to set the newly generated random number that is gonna be used for the new validator set selection. - `currentSeed`. This public getter is used by the `ValidatorSetHbbft` contract at a predefined block of each staking epoch to get the accumulated random seed for randomly choosing new validators among active pools. It can also be used by anyone who wants to use the network's random seed. -- `Registry`: stores human-readable keys associated with addresses, like DNS information (see [Parity Wiki](https://wiki.parity.io/Parity-name-registry.html)). This contract is needed primarily to store the address of the `Certifier` contract (see [Parity Wiki](https://wiki.parity.io/Permissioning.html#gas-price) for details). - - `StakingHbbft`: contains staking logic including: - creating, storing, and removing pools by candidates and validators; - staking coins by participants (delegators, candidates, or validators) into the pools; diff --git a/contracts/Registry.sol b/contracts/Registry.sol deleted file mode 100644 index 439b65af..00000000 --- a/contracts/Registry.sol +++ /dev/null @@ -1,243 +0,0 @@ -// SPDX-License-Identifier: Apache 2.0 -pragma solidity =0.8.25; - -import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; - -import { IMetadataRegistry } from "./interfaces/IMetadataRegistry.sol"; -import { IOwnerRegistry } from "./interfaces/IOwnerRegistry.sol"; -import { IReverseRegistry } from "./interfaces/IReverseRegistry.sol"; -import { ZeroAddress } from "./lib/Errors.sol"; - -/// @dev Stores human-readable keys associated with addresses, like DNS information -/// (see https://wiki.parity.io/Parity-name-registry.html). Needed primarily to store the address -/// of the `TxPermission` contract (see https://wiki.parity.io/Permissioning.html#transaction-type for details). -contract Registry is Ownable, IMetadataRegistry, IOwnerRegistry, IReverseRegistry { - struct Entry { - address owner; - address reverse; - bool deleted; - mapping(string => bytes32) data; - } - - mapping(bytes32 => Entry) internal entries; - mapping(address => string) internal reverses; - - uint256 public fee = 1 ether; - - event Drained(uint256 amount); - event FeeChanged(uint256 amount); - event ReverseProposed(string name, address indexed reverse); - - error InsufficientValue(); - error NotAnOwner(); - error NameAlreadyReserved(bytes32 _name); - error NameNotExist(bytes32 _name); - error ReverseNotProposed(); - - modifier whenUnreserved(bytes32 _name) { - if (entries[_name].deleted || entries[_name].owner != address(0)) { - revert NameAlreadyReserved(_name); - } - _; - } - - modifier onlyOwnerOf(bytes32 _name) { - if (entries[_name].owner != msg.sender) { - revert NotAnOwner(); - } - _; - } - - modifier whenProposed(string memory _name) { - if (entries[keccak256(bytes(_name))].reverse != msg.sender) { - revert ReverseNotProposed(); - } - _; - } - - modifier whenEntry(string memory _name) { - bytes32 nameHash = keccak256(bytes(_name)); - - if (entries[nameHash].deleted || entries[nameHash].owner == address(0)) { - revert NameNotExist(nameHash); - } - _; - } - - modifier whenEntryRaw(bytes32 _name) { - if (entries[_name].deleted || entries[_name].owner == address(0)) { - revert NameNotExist(_name); - } - _; - } - - modifier whenFeePaid() { - if (msg.value < fee) { - revert InsufficientValue(); - } - _; - } - - constructor(address _certifierContract, address _contractOwner) Ownable(_contractOwner) { - if (_certifierContract == address(0) || _contractOwner == address(0)) { - revert ZeroAddress(); - } - - bytes32 serviceTransactionChecker = keccak256("service_transaction_checker"); - - entries[serviceTransactionChecker].owner = _contractOwner; - entries[serviceTransactionChecker].data["A"] = bytes20(_certifierContract); - - emit Reserved(serviceTransactionChecker, _contractOwner); - emit DataChanged(serviceTransactionChecker, "A", "A"); - } - - // Reservation functions - function reserve(bytes32 _name) external payable whenUnreserved(_name) whenFeePaid returns (bool success) { - entries[_name].owner = msg.sender; - emit Reserved(_name, msg.sender); - return true; - } - - function transfer( - bytes32 _name, - address _to - ) external whenEntryRaw(_name) onlyOwnerOf(_name) returns (bool success) { - entries[_name].owner = _to; - emit Transferred(_name, msg.sender, _to); - return true; - } - - function drop(bytes32 _name) external whenEntryRaw(_name) onlyOwnerOf(_name) returns (bool success) { - if (keccak256(bytes(reverses[entries[_name].reverse])) == _name) { - emit ReverseRemoved(reverses[entries[_name].reverse], entries[_name].reverse); - delete reverses[entries[_name].reverse]; - } - entries[_name].deleted = true; - emit Dropped(_name, msg.sender); - return true; - } - - // Data admin functions - function setData( - bytes32 _name, - string calldata _key, - bytes32 _value - ) external whenEntryRaw(_name) onlyOwnerOf(_name) returns (bool success) { - entries[_name].data[_key] = _value; - emit DataChanged(_name, _key, _key); - return true; - } - - function setAddress( - bytes32 _name, - string calldata _key, - address _value - ) external whenEntryRaw(_name) onlyOwnerOf(_name) returns (bool success) { - entries[_name].data[_key] = bytes20(_value); - emit DataChanged(_name, _key, _key); - return true; - } - - function setUint( - bytes32 _name, - string calldata _key, - uint256 _value - ) external whenEntryRaw(_name) onlyOwnerOf(_name) returns (bool success) { - entries[_name].data[_key] = bytes32(_value); - emit DataChanged(_name, _key, _key); - return true; - } - - // Reverse registration functions - function proposeReverse( - string calldata _name, - address _who - ) external whenEntry(_name) onlyOwnerOf(keccak256(bytes(_name))) returns (bool success) { - bytes32 sha3Name = keccak256(bytes(_name)); - if ( - entries[sha3Name].reverse != address(0) && keccak256(bytes(reverses[entries[sha3Name].reverse])) == sha3Name - ) { - delete reverses[entries[sha3Name].reverse]; - emit ReverseRemoved(_name, entries[sha3Name].reverse); - } - entries[sha3Name].reverse = _who; - emit ReverseProposed(_name, _who); - return true; - } - - function confirmReverse( - string calldata _name - ) external whenEntry(_name) whenProposed(_name) returns (bool success) { - reverses[msg.sender] = _name; - emit ReverseConfirmed(_name, msg.sender); - return true; - } - - function confirmReverseAs( - string calldata _name, - address _who - ) external whenEntry(_name) onlyOwner returns (bool success) { - reverses[_who] = _name; - emit ReverseConfirmed(_name, _who); - return true; - } - - function removeReverse() external whenEntry(reverses[msg.sender]) { - emit ReverseRemoved(reverses[msg.sender], msg.sender); - delete entries[keccak256(bytes(reverses[msg.sender]))].reverse; - delete reverses[msg.sender]; - } - - // Admin functions for the owner - function setFee(uint256 _amount) external onlyOwner returns (bool) { - fee = _amount; - emit FeeChanged(_amount); - return true; - } - - function drain() external onlyOwner returns (bool) { - emit Drained(address(this).balance); - payable(msg.sender).transfer(address(this).balance); - return true; - } - - // MetadataRegistry views - function getData(bytes32 _name, string calldata _key) external view whenEntryRaw(_name) returns (bytes32) { - return entries[_name].data[_key]; - } - - function getAddress(bytes32 _name, string calldata _key) external view whenEntryRaw(_name) returns (address) { - return address(bytes20(entries[_name].data[_key])); - } - - function getUint(bytes32 _name, string calldata _key) external view whenEntryRaw(_name) returns (uint256) { - return uint256(entries[_name].data[_key]); - } - - // OwnerRegistry views - function getOwner(bytes32 _name) external view whenEntryRaw(_name) returns (address) { - return entries[_name].owner; - } - - // ReversibleRegistry views - function hasReverse(bytes32 _name) external view whenEntryRaw(_name) returns (bool) { - return entries[_name].reverse != address(0); - } - - function getReverse(bytes32 _name) external view whenEntryRaw(_name) returns (address) { - return entries[_name].reverse; - } - - function canReverse(address _data) external view returns (bool) { - return bytes(reverses[_data]).length != 0; - } - - function reverse(address _data) external view returns (string memory) { - return reverses[_data]; - } - - function reserved(bytes32 _name) external view whenEntryRaw(_name) returns (bool) { - return entries[_name].owner != address(0); - } -} diff --git a/contracts/ValidatorSetHbbft.sol b/contracts/ValidatorSetHbbft.sol index d2036147..eb07e6ba 100644 --- a/contracts/ValidatorSetHbbft.sol +++ b/contracts/ValidatorSetHbbft.sol @@ -244,9 +244,8 @@ contract ValidatorSetHbbft is Initializable, OwnableUpgradeable, IValidatorSetHb } /// @dev Called by the system when a pending validator set is ready to be activated. - /// Only valid when msg.sender == SUPER_USER (EIP96, 2**160 - 2). /// After this function is called, the `getValidators` getter returns the new validator set. - /// If this function finalizes a new validator set formed by the `newValidatorSet` function, + /// If this function finalizes, a new validator set is created by the `newValidatorSet` function. /// an old validator set is also stored and can be read by the `getPreviousValidators` getter. function finalizeChange() external onlyBlockRewardContract { if (_pendingValidators.length != 0) { @@ -270,7 +269,11 @@ contract ValidatorSetHbbft is Initializable, OwnableUpgradeable, IValidatorSetHb _newValidatorSet(new address[](0)); } + /// @dev Inactive validators and their stakers loose there stake after a certain period of time. + /// This function defines the lenght of this time window. + /// @param _seconds new value in seconds. function setValidatorInactivityThreshold(uint256 _seconds) external onlyOwner { + // chosen abritary minimum value of a week. // if you want smaller values for tests, // the contract can be deployed with a smaller value diff --git a/contracts/interfaces/IMetadataRegistry.sol b/contracts/interfaces/IMetadataRegistry.sol deleted file mode 100644 index ae283791..00000000 --- a/contracts/interfaces/IMetadataRegistry.sol +++ /dev/null @@ -1,21 +0,0 @@ -// SPDX-License-Identifier: Apache 2.0 -pragma solidity =0.8.25; - -interface IMetadataRegistry { - event DataChanged(bytes32 indexed name, string key, string plainKey); - - function getData(bytes32 _name, string calldata _key) - external - view - returns (bytes32); - - function getAddress(bytes32 _name, string calldata _key) - external - view - returns (address); - - function getUint(bytes32 _name, string calldata _key) - external - view - returns (uint256); -} diff --git a/contracts/interfaces/IOwnerRegistry.sol b/contracts/interfaces/IOwnerRegistry.sol deleted file mode 100644 index c870f75f..00000000 --- a/contracts/interfaces/IOwnerRegistry.sol +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-License-Identifier: Apache 2.0 -pragma solidity =0.8.25; - -interface IOwnerRegistry { - event Reserved(bytes32 indexed name, address indexed owner); - event Transferred( - bytes32 indexed name, - address indexed oldOwner, - address indexed newOwner - ); - event Dropped(bytes32 indexed name, address indexed owner); - - function getOwner(bytes32 _name) external view returns (address); -} diff --git a/contracts/interfaces/IReverseRegistry.sol b/contracts/interfaces/IReverseRegistry.sol deleted file mode 100644 index 4656eb91..00000000 --- a/contracts/interfaces/IReverseRegistry.sol +++ /dev/null @@ -1,15 +0,0 @@ -// SPDX-License-Identifier: Apache 2.0 -pragma solidity =0.8.25; - -interface IReverseRegistry { - event ReverseConfirmed(string name, address indexed reverse); - event ReverseRemoved(string name, address indexed reverse); - - function hasReverse(bytes32 _name) external view returns (bool); - - function getReverse(bytes32 _name) external view returns (address); - - function canReverse(address _data) external view returns (bool); - - function reverse(address _data) external view returns (string memory); -} diff --git a/docs/docs/index.md b/docs/docs/index.md index f3933532..fb5cd4cf 100644 --- a/docs/docs/index.md +++ b/docs/docs/index.md @@ -21,8 +21,6 @@ POSDAO smart contracts are located in the contracts directory, which contains th - **RandomHbbft:** stores random numbers that the engine generates and sends, used for the validator selection. -- **Registry:** stores human-readable keys associated with addresses (see [Parity Wiki](https://wiki.parity.io/Parity-name-registry.html)). Used primarily to store the address of the `Certifier` contract (see [Parity Wiki](https://wiki.parity.io/Permissioning.html#gas-price) for details). - - **StakingHbbft:** contains the contract staking logic for candidates, delegators and validators. - **TxPermission:** controls the use of a zero gas price by validators in service transactions, protecting the network against "transaction spamming" by malicious validators. diff --git a/initial-contracts.json b/initial-contracts.json index 17ea3380..c68c7a9c 100644 --- a/initial-contracts.json +++ b/initial-contracts.json @@ -40,9 +40,5 @@ "proxyAddress": "0x1200000000000000000000000000000000000001", "implementationAddress": "0x1200000000000000000000000000000000000000" } - ], - "registry": { - "name": "Registry", - "address": "0x6000000000000000000000000000000000000000" - } + ] } \ No newline at end of file diff --git a/tasks/make_spec.ts b/tasks/make_spec.ts index 536216d0..1d2c0408 100644 --- a/tasks/make_spec.ts +++ b/tasks/make_spec.ts @@ -80,20 +80,20 @@ task("make_spec_hbbft", "used to make a spec file") spec.engine.hbbft.params.blockRewardContractAddress = initialContracts.getAddress("BlockRewardHbbft"); spec.params.transactionPermissionContract = initialContracts.getAddress("TxPermissionHbbft"); spec.params.transactionPermissionContractTransition = '0x0'; - spec.params.registrar = initialContracts.registry?.address; + + // spec.params.registrar = initialContracts.registry?.address; - await initialContracts.registry!.compileContract( - hre, - [ - initialContracts.getAddress("CertifierHbbft"), - networkConfig.owner - ] - ); + // await initialContracts.registry!.compileContract( + // hre, + // [ + // initialContracts.getAddress("CertifierHbbft"), + // networkConfig.owner + // ] + // ); spec.accounts = { - ...spec.accounts, - ...initialContracts.registry?.toSpecAccount(0) + ...spec.accounts }; console.log('Using the following initial validators: ' + networkConfig.initialMiningAddresses);