Store contract names in registry as bytes32 hashes

This allows use to call the methods with a string but still store the
name as bytes32.
I also had some issues when calling the methods from ethers.js and a
string parameter so this is easier and safer.
This commit is contained in:
2018-04-03 20:23:36 +02:00
parent 13ce6b1a94
commit 4d2ba2a4bf
10 changed files with 43 additions and 36 deletions

View File

@@ -101,7 +101,7 @@ contract Contributors is Upgradeable {
return contributors[_id].account;
}
function getContributorByAddress(address _address) internal returns (Contributor) {
function getContributorByAddress(address _address) internal view returns (Contributor) {
uint id = contributorIds[_address];
return contributors[id];
}

View File

@@ -23,7 +23,6 @@ contract Operator is Upgradeable {
Proposal[] public proposals;
event ProposalCreated(uint256 id, address creator, uint recipient, uint256 amount);
event ProposalVoted(uint256 id, address voter);
event ProposalVoted(uint256 id, address voter, uint256 totalVotes);
event ProposalExecuted(uint256 id, uint recipient, uint256 amount);
@@ -40,10 +39,10 @@ contract Operator is Upgradeable {
_;
}
function contributorsContract() constant public returns (Contributors) {
function contributorsContract() view public returns (Contributors) {
return Contributors(registry.getProxyFor('Contributors'));
}
function tokenContract() constant public returns (Token) {
function tokenContract() view public returns (Token) {
return Token(registry.getProxyFor('Token'));
}

View File

@@ -10,7 +10,7 @@ interface IRegistry {
* @param name of the contract, as specified in the registry
* @param proxy representing the address of the proxy created
*/
event ProxyCreated(bytes32 name, address proxy);
event ProxyCreated(string name, address proxy);
/**
* @dev This event will be emitted every time a new implementation is registered
@@ -18,21 +18,21 @@ interface IRegistry {
* @param version representing the version name of the registered implementation
* @param implementation representing the address of the registered implementation
*/
event VersionAdded(bytes32 name, uint version, address implementation);
event VersionAdded(string name, uint version, address implementation);
/**
* @dev This event will be emitted every time a proxy is upgraded to a new version
* @param name of the contract, as specified in the registry
* @param version representing the version name of the registered implementation
*/
event ProxyImplementationUpgraded(bytes32 name, uint version);
event ProxyImplementationUpgraded(string name, uint version);
/**
* @dev Registers a new version with its implementation address
* @param name of the contract, as specified in the registry
* @param implementation representing the address of the new implementation to be registered
*/
function addVersion(bytes32 name, address implementation) public;
function addVersion(string name, address implementation) public;
/**
* @dev Tells the address of the implementation for a given version
@@ -40,14 +40,14 @@ interface IRegistry {
* @param version to query the implementation of
* @return address of the implementation registered for the given version
*/
function getVersion(bytes32 name, uint version) public view returns (address);
function getVersion(string name, uint version) public view returns (address);
/**
* @dev Tells the latest address of the implementation
* @param name of the contract, as specified in the registry
* @return address of the implementation registered for the latest version
*/
function getLatestVersion(bytes32 name) public view returns (address);
function getLatestVersion(string name) public view returns (address);
function getProxyFor(bytes32 name) public view returns (address);
function getProxyFor(string name) public view returns (address);
}

View File

@@ -24,11 +24,12 @@ contract Registry is IRegistry {
* @param name of the contract
* @param implementation representing the address of the new implementation to be registered
*/
function addVersion(bytes32 name, address implementation) public {
currentVersions[name] = currentVersions[name] + 1;
uint version = currentVersions[name];
require(versions[name][version] == 0x0);
versions[name][version] = implementation;
function addVersion(string name, address implementation) public {
bytes32 key = keccak256(name);
currentVersions[key] = currentVersions[key] + 1;
uint version = currentVersions[key];
require(versions[key][version] == 0x0);
versions[key][version] = implementation;
VersionAdded(name, version, implementation);
}
@@ -38,26 +39,31 @@ contract Registry is IRegistry {
* @param version to query the implementation of
* @return address of the implementation registered for the given version
*/
function getVersion(bytes32 name, uint version) public view returns (address) {
return versions[name][version];
function getVersion(string name, uint version) public view returns (address) {
bytes32 key = keccak256(name);
return versions[key][version];
}
function getLatestVersion(bytes32 name) public view returns (address) {
uint current = currentVersions[name];
function getLatestVersion(string name) public view returns (address) {
bytes32 key = keccak256(name);
uint current = currentVersions[key];
return getVersion(name, current);
}
function getProxyFor(bytes32 name) public view returns (address) {
return proxies[name];
function getProxyFor(string name) public view returns (address) {
bytes32 key = keccak256(name);
return proxies[key];
}
function upgrade(bytes32 name, uint version) public {
UpgradeabilityProxy(proxies[name]).upgradeTo(version);
function upgrade(string name, uint version) public {
bytes32 key = keccak256(name);
UpgradeabilityProxy(proxies[key]).upgradeTo(version);
ProxyImplementationUpgraded(name, version);
}
function upgradeToLatest(bytes32 name) public {
uint current = currentVersions[name];
function upgradeToLatest(string name) public {
bytes32 key = keccak256(name);
uint current = currentVersions[key];
upgrade(name, current);
}
@@ -67,12 +73,14 @@ contract Registry is IRegistry {
* @param version representing the first version to be set for the proxy
* @return address of the new proxy created
*/
function createProxy(bytes32 name, uint version) public payable returns (UpgradeabilityProxy) {
require(proxies[name] == 0x0);
function createProxy(string name, uint version) public payable returns (UpgradeabilityProxy) {
bytes32 key = keccak256(name);
require(proxies[key] == 0x0);
UpgradeabilityProxy proxy = new UpgradeabilityProxy(name, version);
proxies[name] = address(proxy);
proxies[key] = address(proxy);
Upgradeable(proxy).initialize.value(msg.value)(msg.sender);
ProxyCreated(name, proxy);
return proxy;
}
}

View File

@@ -10,7 +10,7 @@ import './UpgradeabilityStorage.sol';
*/
contract UpgradeabilityProxy is Proxy, UpgradeabilityStorage {
function UpgradeabilityProxy(bytes32 _name, uint _version) public {
function UpgradeabilityProxy(string _name, uint _version) public {
_proxiedContractName = _name;
registry = IRegistry(msg.sender);
upgradeTo(_version);

View File

@@ -14,14 +14,14 @@ contract UpgradeabilityStorage {
address internal _implementation;
// contract name
bytes32 public _proxiedContractName;
string public _proxiedContractName;
modifier requireRegistry() {
require(address(registry) != 0x0);
_;
}
modifier onlyRegistryContractFor(bytes32 name) {
modifier onlyRegistryContractFor(string name) {
require(address(registry) != 0x0);
require(msg.sender == registry.getProxyFor(name));
_;