Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions contracts/Coordinator/Coordinator.sol
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';
Copy link
Contributor

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

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));
}
}
76 changes: 76 additions & 0 deletions contracts/Coordinator/CoordinatorStorageDelegate.sol
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));
}
}
21 changes: 21 additions & 0 deletions contracts/DelegatedStorage/DelegatedStorage.sol
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking we should move everything that extends BaseStorage into the /Storage dir.


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);
}
}
5 changes: 5 additions & 0 deletions contracts/DelegatedStorage/KeyManagerInterface.sol
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);
}
13 changes: 13 additions & 0 deletions contracts/DelegatedStorage/UpgradableContract.sol
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)
{ }
}
17 changes: 17 additions & 0 deletions contracts/Proxy/DelegateManagable.sol
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ManagedProxy or something like that

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);
}
}
5 changes: 5 additions & 0 deletions contracts/Proxy/DelegateManagerInterface.sol
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);
}
9 changes: 9 additions & 0 deletions contracts/StorageConsumer/BasicStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pragma solidity ^0.4.18;
Copy link
Contributor

Choose a reason for hiding this comment

The 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 StorageConsumer and StorageStateful. I think this change might be adding it back in?

If you rebase the latest from master, you won't need to add this BasicStorage contract since StorageConsumer is now simplified to only have a _storage address like this one has.


contract BasicStorage {
address public _storage;

function BasicStorage(address storageAddress) public {
_storage = storageAddress;
}
}