-
Notifications
You must be signed in to change notification settings - Fork 0
Add delegated storage contracts #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| pragma solidity ^0.4.18; | ||
|
|
||
| import '../DelegatedStorage/DelegatedStorage.sol'; | ||
| import '../DelegatedStorage/KeyManagerInterface.sol'; | ||
| import '../Proxy/DelegateManagerInterface.sol'; | ||
| import 'zeppelin-solidity/contracts/ownership/Ownable.sol'; | ||
| import './CoordinatorStorageDelegate.sol'; | ||
|
|
||
| contract Coordinator is KeyManagerInterface, DelegateManagerInterface, Ownable { | ||
|
|
||
| CoordinatorStorageDelegate public _storage; | ||
| CoordinatorStorageDelegate public delegate; | ||
|
|
||
| function Coordinator(address coordinatorStorageDelegate) public { | ||
| _storage = CoordinatorStorageDelegate(new DelegatedStorage(this, this)); | ||
| delegate = CoordinatorStorageDelegate(coordinatorStorageDelegate); | ||
| } | ||
|
|
||
| function setContract(string _key, address _contract) public onlyOwner { | ||
| _storage.setRegisteredContract(keccak256(_key), _contract); | ||
| _storage.setRegisteredContractKey(_contract, keccak256(_key)); | ||
| } | ||
|
|
||
| function removeContract(string _key) public onlyOwner { | ||
| _storage.deleteRegisteredContract(keccak256(_key)); | ||
| } | ||
|
|
||
| function setStorageDelegate(string _key, address _delegate) public onlyOwner { | ||
| _storage.setRegisteredStorageDelegate(keccak256(_key), _delegate); | ||
| } | ||
|
|
||
| function setDelegate(string _key, address _delegate) public onlyOwner { | ||
| _storage.setRegisteredDelegate(keccak256(_key), _delegate); | ||
| } | ||
|
|
||
| // KeyManagerInterface | ||
| function keyForContract(address _contract) public view returns (bytes32) { | ||
| if (_contract == address(this)) | ||
| return keccak256("Coordinator"); | ||
| return _storage.getRegisteredContractKey(_contract); | ||
| } | ||
|
|
||
| // DelegateManagerInterface | ||
| function delegateForContract(address _contract) public view returns (address) { | ||
| if (msg.sender == address(_storage)) { | ||
| return getRegisteredStorageDelegate(_contract); | ||
| } else { | ||
| return getRegisteredDelegate(_contract); | ||
| } | ||
| } | ||
|
|
||
| /* Private functions */ | ||
|
|
||
| function getRegisteredStorageDelegate(address _contract) private view returns (address) { | ||
| if (_contract == address(this)) | ||
| return delegate; | ||
| return _storage.getRegisteredStorageDelegate(keyForContract(_contract)); | ||
| } | ||
|
|
||
| function getRegisteredDelegate(address _contract) private view returns (address) { | ||
| return _storage.getRegisteredDelegate(keyForContract(msg.sender)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| pragma solidity ^0.4.18; | ||
|
|
||
| import '../Storage/BaseStorage.sol'; | ||
|
|
||
| contract CoordinatorStorageDelegate is BaseStorage { | ||
|
|
||
| /**** Get Methods ***********/ | ||
|
|
||
| /// @param key The key for the record | ||
| function getRegisteredContract(bytes32 key) public view returns (address) { | ||
| return getAddress(keccak256("registeredContracts", key)); | ||
| } | ||
|
|
||
| /// @param _contract The address of the contract | ||
| function getRegisteredContractKey(address _contract) public view returns (bytes32) { | ||
| return getBytes32(keccak256("registeredContractKeys", _contract)); | ||
| } | ||
|
|
||
| /// @param key The key for the record | ||
| function getRegisteredStorageDelegate(bytes32 key) public view returns (address) { | ||
| return getAddress(keccak256("registeredStorageDelegates", key)); | ||
| } | ||
|
|
||
| /// @param key The key for the record | ||
| function getRegisteredDelegate(bytes32 key) public view returns (address) { | ||
| return getAddress(keccak256("registeredDelegates", key)); | ||
| } | ||
|
|
||
| /**** Set Methods ***********/ | ||
|
|
||
| /// @param key The key for the record | ||
| /// @param _contract The address of the contract | ||
| function setRegisteredContract(bytes32 key, address _contract) public { | ||
| setAddress(keccak256("registeredContracts", key), _contract); | ||
| } | ||
|
|
||
| /// @param _contract The address of the contract | ||
| /// @param key The key for the record | ||
| function setRegisteredContractKey(address _contract, bytes32 key) public { | ||
| setBytes32(keccak256("registeredContractKeys", _contract), key); | ||
| } | ||
|
|
||
| /// @param key The key for the record | ||
| /// @param delegate The delegate for the contract key | ||
| function setRegisteredStorageDelegate(bytes32 key, address delegate) public { | ||
| setAddress(keccak256("registeredStorageDelegates", key), delegate); | ||
| } | ||
|
|
||
| /// @param key The key for the record | ||
| /// @param delegate The delegate for the contract key | ||
| function setRegisteredDelegate(bytes32 key, address delegate) public { | ||
| setAddress(keccak256("registeredDelegates", key), delegate); | ||
| } | ||
|
|
||
| /**** Delete Methods ***********/ | ||
|
|
||
| /// @param key The key for the record | ||
| function deleteRegisteredContract(bytes32 key) public { | ||
| deleteAddress(keccak256("registeredContracts", key)); | ||
| } | ||
|
|
||
| /// @param _contract The address of the contract | ||
| function deleteRegisteredContractKey(address _contract) public { | ||
| deleteBytes32(keccak256("registeredContractKeys", _contract)); | ||
| } | ||
|
|
||
| /// @param key The key for the record | ||
| function deleteRegisteredStorageDelegate(bytes32 key) public { | ||
| deleteAddress(keccak256("registeredStorageDelegates", key)); | ||
| } | ||
|
|
||
| /// @param key The key for the record | ||
| function deleteRegisteredDelegate(bytes32 key) public { | ||
| deleteAddress(keccak256("registeredDelegates", key)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| pragma solidity ^0.4.18; | ||
|
|
||
| import '../Storage/BaseStorage.sol'; | ||
| import '../Proxy/DelegateManagerInterface.sol'; | ||
| import '../Proxy/DelegateManagable.sol'; | ||
| import './KeyManagerInterface.sol'; | ||
|
|
||
| contract DelegatedStorage is BaseStorage, DelegateManagable { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thinking we should move everything that extends |
||
|
|
||
| KeyManagerInterface keyManager; | ||
|
|
||
| function DelegatedStorage(KeyManagerInterface _keyManager, DelegateManagerInterface _delegateManager) public | ||
| DelegateManagable(_delegateManager) | ||
| { | ||
| keyManager = _keyManager; | ||
| } | ||
|
|
||
| function scopedKey(bytes32 _key) internal view returns(bytes32) { | ||
| return keccak256(keyManager.keyForContract(msg.sender), _key); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pragma solidity ^0.4.18; | ||
|
|
||
| contract KeyManagerInterface { | ||
| function keyForContract(address _contract) public view returns (bytes32 key); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| pragma solidity ^0.4.18; | ||
|
|
||
| import '../Proxy/DelegateManagable.sol'; | ||
| import '../Proxy/DelegateManagerInterface.sol'; | ||
| import '../StorageConsumer/BasicStorage.sol'; | ||
|
|
||
| contract UpgradableContract is BasicStorage, DelegateManagable { | ||
|
|
||
| function UpgradableContract(address storageAddress, DelegateManagerInterface delegateManager) public | ||
| BasicStorage(storageAddress) | ||
| DelegateManagable(delegateManager) | ||
| { } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| pragma solidity ^0.4.18; | ||
|
|
||
| import './BaseProxy.sol'; | ||
| import './DelegateManagerInterface.sol'; | ||
|
|
||
| contract DelegateManagable is BaseProxy { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thinking about having everything that extends Proxy be named with "Proxy" .. could call this |
||
| DelegateManagerInterface delegateManager; | ||
|
|
||
| function DelegateManagable(DelegateManagerInterface _delegateManager) public { | ||
| delegateManager = _delegateManager; | ||
| } | ||
|
|
||
| // Overrides implementation() in BaseProxy.sol | ||
| function implementation() public view returns (address) { | ||
| return delegateManager.delegateForContract(msg.sender); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pragma solidity ^0.4.18; | ||
|
|
||
| contract DelegateManagerInterface { | ||
| function delegateForContract(address _contract) public view returns (address delegate); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| pragma solidity ^0.4.18; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made a change right before you put this PR up that removes the struct/library thing from If you rebase the latest from master, you won't need to add this |
||
|
|
||
| contract BasicStorage { | ||
| address public _storage; | ||
|
|
||
| function BasicStorage(address storageAddress) public { | ||
| _storage = storageAddress; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick alert ... external imports should be declared before locals