Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #43 from keep-network/vendor
Browse files Browse the repository at this point in the history
Update design to Factory/Vendor

Updated architecture to reflect proposal from RFC 12.

Introduced vendor contract to hold references to the factory.
Updated keep registry to hold references to vendors for given keep types.
  • Loading branch information
pdyraga authored Jul 25, 2019
2 parents 2e4a515 + 5b7ca78 commit 4f923ea
Show file tree
Hide file tree
Showing 16 changed files with 4,296 additions and 202 deletions.
2 changes: 1 addition & 1 deletion configs/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Connection details of ethereum blockchain.
[ethereum]
URL = "ws://127.0.0.1:8545"
PrivateKey = "bd03a0aa0b96c5cff1accafdc806aa7f655b6a9a13aeb79f4669c9cfad1eb265"
PrivateKey = "0789df7d07e6947a93576b9ef60b97aed9adb944fb3ff6bae5215fd3ab0ad0dd" # ethereum address: 0x1C25f178599d00b3887BF6D9084cf0C6d49a3097

# Addresses of contracts deployed on ethereum blockchain.
[ethereum.ContractAddresses]
Expand Down
16 changes: 7 additions & 9 deletions solidity/contracts/ECDSAKeepFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ contract ECDSAKeepFactory {
address keepAddress
);

/// @notice Create a new ECDSA keep.
/// @notice Open a new ECDSA keep.
/// @dev Selects a list of members for the keep based on provided parameters.
/// @param _groupSize Number of members in the keep.
/// @param _honestThreshold Minimum number of honest keep members.
/// @param _owner Owner of the keep.
/// @return Created keep.
function createNewKeep(
/// @param _owner Address of the keep owner.
/// @return Created keep address.
function openKeep(
uint256 _groupSize,
uint256 _honestThreshold,
address _owner
Expand All @@ -30,7 +30,7 @@ contract ECDSAKeepFactory {
ECDSAKeep keep = new ECDSAKeep(
_owner,
_members,
_honestThreshold
_honestThreshold
);
keeps.push(keep);

Expand All @@ -44,14 +44,12 @@ contract ECDSAKeepFactory {
/// @param _groupSize Number of members to be selected.
/// @return List of selected members addresses.
function selectECDSAKeepMembers(
uint256 _groupSize
uint256 _groupSize
) internal pure returns (address[] memory members){
// TODO: Implement
_groupSize;

members = new address[](1);
members[0] = 0xE1d6c440DC87476242F313aA1179196fAE89B93e;

// TODO: Currently it assumes members are identified by ID, we should
// consider changing it to an account address or other unique identfier.
}
}
58 changes: 58 additions & 0 deletions solidity/contracts/ECDSAKeepVendor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
pragma solidity ^0.5.4;

import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
import "./utils/AddressArrayUtils.sol";
import "./ECDSAKeepFactory.sol";

/// @title ECDSA Keep Vendor
/// @notice The contract can be used to obtain a new ECDSA keep.
/// @dev Interacts with ECDSA keep factory to obtain a new instance of the ECDSA
/// keep. Several versions of ECDSA keep factories can be registered for the vendor.
/// TODO: This is a stub contract - needs to be implemented.
/// TODO: When more keep types are added consider extracting registration and
/// selection to a separate inheritable contract.
contract ECDSAKeepVendor is Ownable {
using AddressArrayUtils for address[];

// List of ECDSA keep factories.
address[] public factories;

/// @notice Register new ECDSA keep factory.
/// @dev Adds a factory address to the list of registered factories. Address
/// cannot be zero and cannot be already registered.
/// @param _factory ECDSA keep factory address.
function registerFactory(address _factory) public onlyOwner {
require(!factories.contains(_factory), "Factory address already registered");

factories.push(_factory);
}

/// @notice Select a recommended ECDSA keep factory from all registered
/// ECDSA keep factories.
/// @dev This is a stub implementation returning first factory on the list.
/// @return Selected ECDSA keep factory address.
function selectFactory() internal view returns (address) {
// TODO: Implement factory selection mechanism.
return factories[factories.length - 1];
}

/// @notice Open a new ECDSA keep.
/// @dev Calls a recommended ECDSA keep factory to open a new keep.
/// @param _groupSize Number of members in the keep.
/// @param _honestThreshold Minimum number of honest keep members.
/// @param _owner Address of the keep owner.
/// @return Opened keep address.
function openKeep(
uint256 _groupSize,
uint256 _honestThreshold,
address _owner
) public payable returns (address keepAddress) {
address factory = selectFactory();

return ECDSAKeepFactory(factory).openKeep(
_groupSize,
_honestThreshold,
_owner
);
}
}
62 changes: 18 additions & 44 deletions solidity/contracts/KeepRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,55 +1,29 @@
pragma solidity ^0.5.4;

import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
// TODO: For simplification we use import for the other contracts. `ECDSAKeepFactory.sol`
// and `KeepRegistry.sol` will be kept in different repos in the future so a better
// way of calling another contract from this contract should be introduced.
import "./ECDSAKeepFactory.sol";

/// @title Keep Registry
/// @notice Contract handling keeps registry.
/// @dev TODO: This is a stub contract - needs to be implemented.
/// @dev The keep registry serves the role of the master list and tracks sanctioned
/// vendors. It ensures that only approved contracts are used.
/// TODO: This is a stub contract - needs to be implemented.
contract KeepRegistry is Ownable {
// Enumeration of supported keeps types.
enum KeepTypes {ECDSA, BondedECDSA}

// Structure holding keep details.
struct Keep {
address owner; // owner of the keep
address keepAddress; // address of the keep contract
KeepTypes keepType; // type of the keep
}

// Factory handling ECDSA keeps.
address internal ecdsaKeepFactory;

// List of created keeps.
Keep[] keeps;

constructor(address _ecdsaKeepFactory) public {
require(_ecdsaKeepFactory != address(0), "Implementation address can't be zero.");
setECDSAKeepFactory(_ecdsaKeepFactory);
}

function setECDSAKeepFactory(address _ecdsaKeepFactory) public onlyOwner {
ecdsaKeepFactory = _ecdsaKeepFactory;
// Registered keep vendors. Mapping of a keep type to a keep vendor address.
mapping (string => address) internal keepVendors;

/// @notice Set a keep vendor contract address for a keep type.
/// @dev Only contract owner can call this function.
/// @param _keepType Keep type.
/// @param _vendorAddress Keep Vendor contract address.
function setVendor(string memory _keepType, address _vendorAddress) public onlyOwner {
keepVendors[_keepType] = _vendorAddress;
}

/// @notice Create a new ECDSA keep.
/// @dev Calls ECDSA Keep Factory to create a keep.
/// @param _groupSize Number of members in the keep.
/// @param _honestThreshold Minimum number of honest keep members.
/// @return Created keep address.
function createECDSAKeep(
uint256 _groupSize,
uint256 _honestThreshold
) public payable returns (address keep) {
keep = ECDSAKeepFactory(ecdsaKeepFactory).createNewKeep(
_groupSize,
_honestThreshold,
msg.sender
);

keeps.push(Keep(msg.sender, keep, KeepTypes.ECDSA));
/// @notice Get a keep vendor contract address for a keep type.
/// @param _keepType Keep type.
/// @return Keep vendor contract address.
function getVendor(string memory _keepType) public view returns (address) {
// TODO: We should probably refer to vendor via proxy - https://github.com/keep-network/keep-tecdsa/pull/43#discussion_r306207111
return keepVendors[_keepType];
}
}
16 changes: 16 additions & 0 deletions solidity/contracts/utils/AddressArrayUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pragma solidity ^0.5.4;

library AddressArrayUtils {
function contains(address[] memory self, address _address)
internal
pure
returns (bool)
{
for (uint i = 0; i < self.length; i++) {
if (_address == self[i]) {
return true;
}
}
return false;
}
}
13 changes: 10 additions & 3 deletions solidity/migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
const ECDSAKeepFactory = artifacts.require("./ECDSAKeepFactory.sol");
const ECDSAKeepVendor = artifacts.require("./ECDSAKeepVendor.sol");
const KeepRegistry = artifacts.require("./KeepRegistry.sol");

module.exports = async function (deployer) {
await deployer.deploy(ECDSAKeepFactory);
await deployer.deploy(KeepRegistry, ECDSAKeepFactory.address);
};
await deployer.deploy(ECDSAKeepFactory)
const ecdsaKeepFactory = await ECDSAKeepFactory.deployed()

const ecdsaKeepVendor = await deployer.deploy(ECDSAKeepVendor)
await ecdsaKeepVendor.registerFactory(ecdsaKeepFactory.address)

const keepRegistry = await deployer.deploy(KeepRegistry)
await keepRegistry.setVendor('ECDSAKeep', ecdsaKeepVendor.address)
}
Loading

0 comments on commit 4f923ea

Please sign in to comment.