Skip to content

Commit

Permalink
Merge pull request #139 from livepeer/yf/versioning
Browse files Browse the repository at this point in the history
Add git commit hash as version identifier to contract info in Controller
  • Loading branch information
yondonfu authored Jan 10, 2018
2 parents a313fa6 + d38eed2 commit b9f08fd
Show file tree
Hide file tree
Showing 9 changed files with 4,162 additions and 3,092 deletions.
2 changes: 2 additions & 0 deletions Dockerfile-test
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FROM node:8.5.0

WORKDIR /app/
# Need .git so we can get the git head commit hash
COPY .git /app/.git
COPY .babelrc /app/.babelrc
COPY .soliumrc.json /app/.soliumrc.json
COPY .soliumignore /app/.soliumignore
Expand Down
29 changes: 22 additions & 7 deletions contracts/Controller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import "zeppelin-solidity/contracts/lifecycle/Pausable.sol";


contract Controller is Pausable, IController {
// Track contract ids and their mapped addresses
mapping (bytes32 => address) registry;
// Track information about a registered contract
struct ContractInfo {
address contractAddress; // Address of contract
bytes20 gitCommitHash; // SHA1 hash of head Git commit during registration of this contract
}

// Track contract ids and contract info
mapping (bytes32 => ContractInfo) registry;

function Controller() public {
// Start system as paused
Expand All @@ -20,10 +26,11 @@ contract Controller is Pausable, IController {
* @param _id Contract id (keccak256 hash of contract name)
* @param _contract Contract address
*/
function setContract(bytes32 _id, address _contract) external onlyOwner {
registry[_id] = _contract;
function setContractInfo(bytes32 _id, address _contractAddress, bytes20 _gitCommitHash) external onlyOwner {
registry[_id].contractAddress = _contractAddress;
registry[_id].gitCommitHash = _gitCommitHash;

SetContract(_id, _contract);
SetContractInfo(_id, _contractAddress, _gitCommitHash);
}

/*
Expand All @@ -32,14 +39,22 @@ contract Controller is Pausable, IController {
* @param _controller Controller address
*/
function updateController(bytes32 _id, address _controller) external onlyOwner {
return IManager(registry[_id]).setController(_controller);
return IManager(registry[_id].contractAddress).setController(_controller);
}

/*
* @dev Return contract info for a given contract id
* @param _id Contract id (keccak256 hash of contract name)
*/
function getContractInfo(bytes32 _id) public view returns (address, bytes20) {
return (registry[_id].contractAddress, registry[_id].gitCommitHash);
}

/*
* @dev Get contract address for an id
* @param _id Contract id
*/
function getContract(bytes32 _id) public view returns (address) {
return registry[_id];
return registry[_id].contractAddress;
}
}
4 changes: 2 additions & 2 deletions contracts/IController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import "zeppelin-solidity/contracts/lifecycle/Pausable.sol";


contract IController is Pausable {
event SetContract(bytes32 id, address contractAddr);
event SetContractInfo(bytes32 id, address contractAddress, bytes20 gitCommitHash);

function setContract(bytes32 _id, address _contract) external;
function setContractInfo(bytes32 _id, address _contractAddress, bytes20 _gitCommitHash) external;
function updateController(bytes32 _id, address _controller) external;
function getContract(bytes32 _id) public view returns (address);
}
15 changes: 13 additions & 2 deletions migrations/3_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const config = require("./migrations.config.js")
const path = require("path")
const {Repository} = require("nodegit")
const BigNumber = require("bignumber.js")
const {contractId} = require("../utils/helpers")

Expand All @@ -15,14 +17,22 @@ const LivepeerToken = artifacts.require("LivepeerToken")
const LivepeerTokenFaucet = artifacts.require("LivepeerTokenFaucet")
const ManagerProxy = artifacts.require("ManagerProxy")

const getGitHeadCommitHash = async () => {
const repoRootPath = path.resolve(__dirname, "..")
const repo = await Repository.open(repoRootPath)
const headCommit = await repo.getHeadCommit()
return "0x" + headCommit.sha()
}

const deploy = async (deployer, artifact, ...args) => {
await deployer.deploy(artifact, ...args)
return await artifact.deployed()
}

const deployAndRegister = async (deployer, controller, artifact, name, ...args) => {
const contract = await deploy(deployer, artifact, ...args)
await controller.setContract(contractId(name), contract.address)
const commitHash = await getGitHeadCommitHash()
await controller.setContractInfo(contractId(name), contract.address, commitHash)
return contract
}

Expand All @@ -37,7 +47,8 @@ const deployProxyAndRegister = async (deployer, controller, targetArtifact, name
const proxy = await ManagerProxy.new(controller.address, contractId(targetName))
deployer.logger.log("Proxy contract for " + name + ": " + proxy.address)

await controller.setContract(contractId(name), proxy.address)
const commitHash = await getGitHeadCommitHash()
await controller.setContractInfo(contractId(name), proxy.address, commitHash)

return await targetArtifact.at(proxy.address)
}
Expand Down
Loading

0 comments on commit b9f08fd

Please sign in to comment.