pragma solidity ^0.8.0; // SPDX-License-Identifier: MIT import "../guards/AdapterGuard.sol"; import "../guards/MemberGuard.sol"; import "../extensions/IExtension.sol"; import "../helpers/DaoHelper.sol"; /** MIT License Copyright (c) 2020 Openlaw Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ contract DaoRegistry is MemberGuard, AdapterGuard { /** * EVENTS */ event SubmittedProposal(bytes32 proposalId, uint256 flags); event SponsoredProposal( bytes32 proposalId, uint256 flags, address votingAdapter ); event ProcessedProposal(bytes32 proposalId, uint256 flags); event AdapterAdded( bytes32 adapterId, address adapterAddress, uint256 flags ); event AdapterRemoved(bytes32 adapterId); event ExtensionAdded(bytes32 extensionId, address extensionAddress); event ExtensionRemoved(bytes32 extensionId); event UpdateDelegateKey(address memberAddress, address newDelegateKey); event ConfigurationUpdated(bytes32 key, uint256 value); event AddressConfigurationUpdated(bytes32 key, address value); enum DaoState { CREATION, READY } enum MemberFlag { EXISTS, JAILED } enum ProposalFlag { EXISTS, SPONSORED, PROCESSED } enum AclFlag { REPLACE_ADAPTER, SUBMIT_PROPOSAL, UPDATE_DELEGATE_KEY, SET_CONFIGURATION, ADD_EXTENSION, REMOVE_EXTENSION, NEW_MEMBER, JAIL_MEMBER } /** * STRUCTURES */ struct Proposal { /// the structure to track all the proposals in the DAO address adapterAddress; /// the adapter address that called the functions to change the DAO state uint256 flags; /// flags to track the state of the proposal: exist, sponsored, processed, canceled, etc. } struct Member { /// the structure to track all the members in the DAO uint256 flags; /// flags to track the state of the member: exists, etc } struct Checkpoint { /// A checkpoint for marking number of votes from a given block uint96 fromBlock; uint160 amount; } struct DelegateCheckpoint { /// A checkpoint for marking the delegate key for a member from a given block uint96 fromBlock; address delegateKey; } struct AdapterEntry { bytes32 id; uint256 acl; } struct ExtensionEntry { bytes32 id; mapping(address => uint256) acl; bool deleted; } /** * PUBLIC VARIABLES */ /// @notice internally tracks deployment under eip-1167 proxy pattern bool public initialized = false; /// @notice The dao state starts as CREATION and is changed to READY after the finalizeDao call DaoState public state; /// @notice The map to track all members of the DAO with their existing flags mapping(address => Member) public members; /// @notice The list of members address[] private _members; /// @notice delegate key => member address mapping mapping(address => address) public memberAddressesByDelegatedKey; /// @notice The map that keeps track of all proposasls submitted to the DAO mapping(bytes32 => Proposal) public proposals; /// @notice The map that tracks the voting adapter address per proposalId: proposalId => adapterAddress mapping(bytes32 => address) public votingAdapter; /// @notice The map that keeps track of all adapters registered in the DAO: sha3(adapterId) => adapterAddress mapping(bytes32 => address) public adapters; /// @notice The inverse map to get the adapter id based on its address mapping(address => AdapterEntry) public inverseAdapters; /// @notice The map that keeps track of all extensions registered in the DAO: sha3(extId) => extAddress mapping(bytes32 => address) public extensions; /// @notice The inverse map to get the extension id based on its address mapping(address => ExtensionEntry) public inverseExtensions; /// @notice The map that keeps track of configuration parameters for the DAO and adapters: sha3(configId) => numericValue mapping(bytes32 => uint256) public mainConfiguration; /// @notice The map to track all the configuration of type Address: sha3(configId) => addressValue mapping(bytes32 => address) public addressConfiguration; /// @notice controls the lock mechanism using the block.number uint256 public lockedAt; /** * INTERNAL VARIABLES */ /// @notice memberAddress => checkpointNum => DelegateCheckpoint mapping(address => mapping(uint32 => DelegateCheckpoint)) _checkpoints; /// @notice memberAddress => numDelegateCheckpoints mapping(address => uint32) _numCheckpoints; /// @notice Clonable contract must have an empty constructor constructor() {} /** * @notice Initialises the DAO * @dev Involves initialising available tokens, checkpoints, and membership of creator * @dev Can only be called once * @param creator The DAO's creator, who will be an initial member * @param payer The account which paid for the transaction to create the DAO, who will be an initial member */ //slither-disable-next-line reentrancy-no-eth function initialize(address creator, address payer) external { require(!initialized, "dao already initialized"); initialized = true; potentialNewMember(msg.sender); potentialNewMember(creator); potentialNewMember(payer); } /** * ACCESS CONTROL */ /** * @dev Sets the state of the dao to READY */ function finalizeDao() external { require( isActiveMember(this, msg.sender) || isAdapter(msg.sender), "not allowed to finalize" ); state = DaoState.READY; } /** * @notice Contract lock strategy to lock only the caller is an adapter or extension. */ function lockSession() external { if (isAdapter(msg.sender) || isExtension(msg.sender)) { lockedAt = block.number; } } /** * @notice Contract lock strategy to release the lock only the caller is an adapter or extension. */ function unlockSession() external { if (isAdapter(msg.sender) || isExtension(msg.sender)) { lockedAt = 0; } } /** * CONFIGURATIONS */ /** * @notice Sets a configuration value * @dev Changes the value of a key in the configuration mapping * @param key The configuration key for which the value will be set * @param value The value to set the key */ function setConfiguration( bytes32 key, uint256 value ) external hasAccess(this, AclFlag.SET_CONFIGURATION) { mainConfiguration[key] = value; emit ConfigurationUpdated(key, value); } /** * @notice Sets an configuration value * @dev Changes the value of a key in the configuration mapping * @param key The configuration key for which the value will be set * @param value The value to set the key */ function setAddressConfiguration( bytes32 key, address value ) external hasAccess(this, AclFlag.SET_CONFIGURATION) { addressConfiguration[key] = value; emit AddressConfigurationUpdated(key, value); } /** * @return The configuration value of a particular key * @param key The key to look up in the configuration mapping */ function getConfiguration(bytes32 key) external view returns (uint256) { return mainConfiguration[key]; } /** * @return The configuration value of a particular key * @param key The key to look up in the configuration mapping */ function getAddressConfiguration( bytes32 key ) external view returns (address) { return addressConfiguration[key]; } /** * ADAPTERS */ /** * @notice Replaces an adapter in the registry in a single step. * @notice It handles addition and removal of adapters as special cases. * @dev It removes the current adapter if the adapterId maps to an existing adapter address. * @dev It adds an adapter if the adapterAddress parameter is not zeroed. * @param adapterId The unique identifier of the adapter * @param adapterAddress The address of the new adapter or zero if it is a removal operation * @param acl The flags indicating the access control layer or permissions of the new adapter * @param keys The keys indicating the adapter configuration names. * @param values The values indicating the adapter configuration values. */ function replaceAdapter( bytes32 adapterId, address adapterAddress, uint128 acl, bytes32[] calldata keys, uint256[] calldata values ) external hasAccess(this, AclFlag.REPLACE_ADAPTER) { require(adapterId != bytes32(0), "adapterId must not be empty"); address currentAdapterAddr = adapters[adapterId]; if (currentAdapterAddr != address(0x0)) { delete inverseAdapters[currentAdapterAddr]; delete adapters[adapterId]; emit AdapterRemoved(adapterId); } for (uint256 i = 0; i < keys.length; i++) { bytes32 key = keys[i]; uint256 value = values[i]; mainConfiguration[key] = value; emit ConfigurationUpdated(key, value); } if (adapterAddress != address(0x0)) { require( inverseAdapters[adapterAddress].id == bytes32(0), "adapterAddress already in use" ); adapters[adapterId] = adapterAddress; inverseAdapters[adapterAddress].id = adapterId; inverseAdapters[adapterAddress].acl = acl; emit AdapterAdded(adapterId, adapterAddress, acl); } } /** * @notice Looks up if there is an adapter of a given address * @return Whether or not the address is an adapter * @param adapterAddress The address to look up */ function isAdapter(address adapterAddress) public view returns (bool) { return inverseAdapters[adapterAddress].id != bytes32(0); } /** * @notice Checks if an adapter has a given ACL flag * @return Whether or not the given adapter has the given flag set * @param adapterAddress The address to look up * @param flag The ACL flag to check against the given address */ function hasAdapterAccess( address adapterAddress, AclFlag flag ) external view returns (bool) { return DaoHelper.getFlag(inverseAdapters[adapterAddress].acl, uint8(flag)); } /** * @return The address of a given adapter ID * @param adapterId The ID to look up */ function getAdapterAddress( bytes32 adapterId ) external view returns (address) { require(adapters[adapterId] != address(0), "adapter not found"); return adapters[adapterId]; } /** * EXTENSIONS */ /** * @notice Adds a new extension to the registry * @param extensionId The unique identifier of the new extension * @param extension The address of the extension */ // slither-disable-next-line reentrancy-events function addExtension( bytes32 extensionId, IExtension extension ) external hasAccess(this, AclFlag.ADD_EXTENSION) { require(extensionId != bytes32(0), "extension id must not be empty"); require( extensions[extensionId] == address(0x0), "extensionId already in use" ); require( !inverseExtensions[address(extension)].deleted, "extension can not be re-added" ); extensions[extensionId] = address(extension); inverseExtensions[address(extension)].id = extensionId; emit ExtensionAdded(extensionId, address(extension)); } // v1.0.6 signature function addExtension(bytes32, IExtension, address) external { revert("not implemented"); } /** * @notice Removes an adapter from the registry * @param extensionId The unique identifier of the extension */ function removeExtension( bytes32 extensionId ) external hasAccess(this, AclFlag.REMOVE_EXTENSION) { require(extensionId != bytes32(0), "extensionId must not be empty"); address extensionAddress = extensions[extensionId]; require(extensionAddress != address(0x0), "extensionId not registered"); ExtensionEntry storage extEntry = inverseExtensions[extensionAddress]; extEntry.deleted = true; //slither-disable-next-line mapping-deletion delete extensions[extensionId]; emit ExtensionRemoved(extensionId); } /** * @notice Looks up if there is an extension of a given address * @return Whether or not the address is an extension * @param extensionAddr The address to look up */ function isExtension(address extensionAddr) public view returns (bool) { return inverseExtensions[extensionAddr].id != bytes32(0); } /** * @notice It sets the ACL flags to an Adapter to make it possible to access specific functions of an Extension. */ function setAclToExtensionForAdapter( address extensionAddress, address adapterAddress, uint256 acl ) external hasAccess(this, AclFlag.ADD_EXTENSION) { require(isAdapter(adapterAddress), "not an adapter"); require(isExtension(extensionAddress), "not an extension"); inverseExtensions[extensionAddress].acl[adapterAddress] = acl; } /** * @notice Checks if an adapter has a given ACL flag * @return Whether or not the given adapter has the given flag set * @param adapterAddress The address to look up * @param flag The ACL flag to check against the given address */ function hasAdapterAccessToExtension( address adapterAddress, address extensionAddress, uint8 flag ) external view returns (bool) { return isAdapter(adapterAddress) && DaoHelper.getFlag( inverseExtensions[extensionAddress].acl[adapterAddress], uint8(flag) ); } /** * @return The address of a given extension Id * @param extensionId The ID to look up */ function getExtensionAddress( bytes32 extensionId ) external view returns (address) { require(extensions[extensionId] != address(0), "extension not found"); return extensions[extensionId]; } /** * PROPOSALS */ /** * @notice Submit proposals to the DAO registry */ function submitProposal( bytes32 proposalId ) external hasAccess(this, AclFlag.SUBMIT_PROPOSAL) { require(proposalId != bytes32(0), "invalid proposalId"); require( !getProposalFlag(proposalId, ProposalFlag.EXISTS), "proposalId must be unique" ); proposals[proposalId] = Proposal(msg.sender, 1); // 1 means that only the first flag is being set i.e. EXISTS emit SubmittedProposal(proposalId, 1); } /** * @notice Sponsor proposals that were submitted to the DAO registry * @dev adds SPONSORED to the proposal flag * @param proposalId The ID of the proposal to sponsor * @param sponsoringMember The member who is sponsoring the proposal */ function sponsorProposal( bytes32 proposalId, address sponsoringMember, address votingAdapterAddr ) external onlyMember2(this, sponsoringMember) { // also checks if the flag was already set Proposal storage proposal = _setProposalFlag( proposalId, ProposalFlag.SPONSORED ); uint256 flags = proposal.flags; require( proposal.adapterAddress == msg.sender, "only the adapter that submitted the proposal can sponsor it" ); require( !DaoHelper.getFlag(flags, uint8(ProposalFlag.PROCESSED)), "proposal already processed" ); votingAdapter[proposalId] = votingAdapterAddr; emit SponsoredProposal(proposalId, flags, votingAdapterAddr); } /** * @notice Mark a proposal as processed in the DAO registry * @param proposalId The ID of the proposal that is being processed */ function processProposal(bytes32 proposalId) external { Proposal storage proposal = _setProposalFlag( proposalId, ProposalFlag.PROCESSED ); require(proposal.adapterAddress == msg.sender, "err::adapter mismatch"); uint256 flags = proposal.flags; emit ProcessedProposal(proposalId, flags); } /** * @notice Sets a flag of a proposal * @dev Reverts if the proposal is already processed * @param proposalId The ID of the proposal to be changed * @param flag The flag that will be set on the proposal */ function _setProposalFlag( bytes32 proposalId, ProposalFlag flag ) internal returns (Proposal storage) { Proposal storage proposal = proposals[proposalId]; uint256 flags = proposal.flags; require( DaoHelper.getFlag(flags, uint8(ProposalFlag.EXISTS)), "proposal does not exist for this dao" ); require( proposal.adapterAddress == msg.sender, "invalid adapter try to set flag" ); require(!DaoHelper.getFlag(flags, uint8(flag)), "flag already set"); flags = DaoHelper.setFlag(flags, uint8(flag), true); proposals[proposalId].flags = flags; return proposals[proposalId]; } /** * @return Whether or not a flag is set for a given proposal * @param proposalId The proposal to check against flag * @param flag The flag to check in the proposal */ function getProposalFlag( bytes32 proposalId, ProposalFlag flag ) public view returns (bool) { return DaoHelper.getFlag(proposals[proposalId].flags, uint8(flag)); } /** * MEMBERS */ /** * @notice Sets true for the JAILED flag. * @param memberAddress The address of the member to update the flag. */ function jailMember( address memberAddress ) external hasAccess(this, AclFlag.JAIL_MEMBER) { require(memberAddress != address(0x0), "invalid member address"); Member storage member = members[memberAddress]; require( DaoHelper.getFlag(member.flags, uint8(MemberFlag.EXISTS)), "member does not exist" ); member.flags = DaoHelper.setFlag( member.flags, uint8(MemberFlag.JAILED), true ); } /** * @notice Sets false for the JAILED flag. * @param memberAddress The address of the member to update the flag. */ function unjailMember( address memberAddress ) external hasAccess(this, AclFlag.JAIL_MEMBER) { require(memberAddress != address(0x0), "invalid member address"); Member storage member = members[memberAddress]; require( DaoHelper.getFlag(member.flags, uint8(MemberFlag.EXISTS)), "member does not exist" ); member.flags = DaoHelper.setFlag( member.flags, uint8(MemberFlag.JAILED), false ); } /** * @notice Checks if a given member address is not jailed. * @param memberAddress The address of the member to check the flag. */ function notJailed(address memberAddress) external view returns (bool) { return !DaoHelper.getFlag( members[memberAddress].flags, uint8(MemberFlag.JAILED) ); } /** * @notice Registers a member address in the DAO if it is not registered or invalid. * @notice A potential new member is a member that holds no shares, and its registration still needs to be voted on. */ function potentialNewMember( address memberAddress ) public hasAccess(this, AclFlag.NEW_MEMBER) { require(memberAddress != address(0x0), "invalid member address"); Member storage member = members[memberAddress]; if (!DaoHelper.getFlag(member.flags, uint8(MemberFlag.EXISTS))) { require( memberAddressesByDelegatedKey[memberAddress] == address(0x0), "member address already taken as delegated key" ); member.flags = DaoHelper.setFlag( member.flags, uint8(MemberFlag.EXISTS), true ); memberAddressesByDelegatedKey[memberAddress] = memberAddress; _members.push(memberAddress); } address bankAddress = extensions[DaoHelper.BANK]; if (bankAddress != address(0x0)) { BankExtension bank = BankExtension(bankAddress); if (bank.balanceOf(memberAddress, DaoHelper.MEMBER_COUNT) == 0) { bank.addToBalance( this, memberAddress, DaoHelper.MEMBER_COUNT, 1 ); } } } /** * @return Whether or not a given address is a member of the DAO. * @dev it will resolve by delegate key, not member address. * @param addr The address to look up */ function isMember(address addr) external view returns (bool) { address memberAddress = memberAddressesByDelegatedKey[addr]; return getMemberFlag(memberAddress, MemberFlag.EXISTS); } /** * @return Whether or not a flag is set for a given member * @param memberAddress The member to check against flag * @param flag The flag to check in the member */ function getMemberFlag( address memberAddress, MemberFlag flag ) public view returns (bool) { return DaoHelper.getFlag(members[memberAddress].flags, uint8(flag)); } /** * @notice Returns the number of members in the registry. */ function getNbMembers() external view returns (uint256) { return _members.length; } /** * @notice Returns the member address for the given index. */ function getMemberAddress(uint256 index) external view returns (address) { return _members[index]; } /** * DELEGATE */ /** * @notice Updates the delegate key of a member * @param memberAddr The member doing the delegation * @param newDelegateKey The member who is being delegated to */ function updateDelegateKey( address memberAddr, address newDelegateKey ) external hasAccess(this, AclFlag.UPDATE_DELEGATE_KEY) { require(newDelegateKey != address(0x0), "newDelegateKey cannot be 0"); // skip checks if member is setting the delegate key to their member address if (newDelegateKey != memberAddr) { require( // newDelegate must not be delegated to memberAddressesByDelegatedKey[newDelegateKey] == address(0x0), "cannot overwrite existing delegated keys" ); } else { require( memberAddressesByDelegatedKey[memberAddr] == address(0x0), "address already taken as delegated key" ); } Member storage member = members[memberAddr]; require( DaoHelper.getFlag(member.flags, uint8(MemberFlag.EXISTS)), "member does not exist" ); // Reset the delegation of the previous delegate memberAddressesByDelegatedKey[ getCurrentDelegateKey(memberAddr) ] = address(0x0); memberAddressesByDelegatedKey[newDelegateKey] = memberAddr; _createNewDelegateCheckpoint(memberAddr, newDelegateKey); emit UpdateDelegateKey(memberAddr, newDelegateKey); } /** * @param checkAddr The address to check for a delegate * @return the delegated address or the checked address if it is not a delegate */ function getAddressIfDelegated( address checkAddr ) external view returns (address) { address delegatedKey = memberAddressesByDelegatedKey[checkAddr]; return delegatedKey == address(0x0) ? checkAddr : delegatedKey; } /** * @param memberAddr The member whose delegate will be returned * @return the delegate key at the current time for a member */ function getCurrentDelegateKey( address memberAddr ) public view returns (address) { uint32 nCheckpoints = _numCheckpoints[memberAddr]; return nCheckpoints > 0 ? _checkpoints[memberAddr][nCheckpoints - 1].delegateKey : memberAddr; } /** * @param memberAddr The member address to look up * @return The delegate key address for memberAddr at the second last checkpoint number */ function getPreviousDelegateKey( address memberAddr ) external view returns (address) { uint32 nCheckpoints = _numCheckpoints[memberAddr]; return nCheckpoints > 1 ? _checkpoints[memberAddr][nCheckpoints - 2].delegateKey : memberAddr; } /** * @notice Determine the prior number of votes for an account as of a block number * @dev Block number must be a finalized block or else this function will revert to prevent misinformation. * @param memberAddr The address of the account to check * @param blockNumber The block number to get the vote balance at * @return The delegate key of the member */ function getPriorDelegateKey( address memberAddr, uint256 blockNumber ) external view returns (address) { require(blockNumber < block.number, "getPriorDelegateKey: NYD"); uint32 nCheckpoints = _numCheckpoints[memberAddr]; if (nCheckpoints == 0) { return memberAddr; } // First check most recent balance if ( _checkpoints[memberAddr][nCheckpoints - 1].fromBlock <= blockNumber ) { return _checkpoints[memberAddr][nCheckpoints - 1].delegateKey; } // Next check implicit zero balance if (_checkpoints[memberAddr][0].fromBlock > blockNumber) { return memberAddr; } uint32 lower = 0; uint32 upper = nCheckpoints - 1; while (upper > lower) { uint32 center = upper - (upper - lower) / 2; // ceil, avoiding overflow DelegateCheckpoint memory cp = _checkpoints[memberAddr][center]; if (cp.fromBlock == blockNumber) { return cp.delegateKey; } else if (cp.fromBlock < blockNumber) { lower = center; } else { upper = center - 1; } } return _checkpoints[memberAddr][lower].delegateKey; } /** * @notice Creates a new delegate checkpoint of a certain member * @param member The member whose delegate checkpoints will be added to * @param newDelegateKey The delegate key that will be written into the new checkpoint */ function _createNewDelegateCheckpoint( address member, address newDelegateKey ) internal { uint32 nCheckpoints = _numCheckpoints[member]; // The only condition that we should allow the deletegaKey upgrade // is when the block.number exactly matches the fromBlock value. // Anything different from that should generate a new checkpoint. if ( //slither-disable-next-line incorrect-equality nCheckpoints > 0 && _checkpoints[member][nCheckpoints - 1].fromBlock == block.number ) { _checkpoints[member][nCheckpoints - 1].delegateKey = newDelegateKey; } else { _checkpoints[member][nCheckpoints] = DelegateCheckpoint( uint96(block.number), newDelegateKey ); _numCheckpoints[member] = nCheckpoints + 1; } } }